Skip to content

Add support for generating namespaced constant #10552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

ranvis
Copy link
Contributor

@ranvis ranvis commented Feb 10, 2023

Add support for generating namespaced constant to gen_stub.php.
The code already has escaping for namespaced constants, but constant instances are not initialized with a residing namespace.

Changes in ext/*.stub.php are to fix namespace of constants which actually are in global.

sample input:

<?php
/** @generate-class-entries */
namespace {
    /** @var null */
    const CONST_NULL = null;
    /** @var bool */
    const CONST_BOOL = true;
    /** @var int */
    const CONST_INT = 1;
    /** @var float */
    const CONST_FLOAT = 1.5;
    /** @var string */
    const CONST_STRING = "str";
    /** @var bool
     *  @cvalue EXTERNAL_BOOL */
    const EXTERN_BOOL = UNKNOWN;
    /** @var int
     *  @cvalue EXTERNAL_INT */
    const EXTERN_INT = UNKNOWN;
    /** @var float
     *  @cvalue EXTERNAL_FLOAT */
    const EXTERN_FLOAT = UNKNOWN;
    /** @var string
     *  @cvalue EXTERNAL_STRING */
    const EXTERN_STRING = UNKNOWN;
}
namespace Not\In\Global {
    /** @var null */
    const CONST_NULL = null;
    /** @var bool */
    const CONST_BOOL = false;
    /** @var int */
    const CONST_INT = -1;
    /** @var float */
    const CONST_FLOAT = -1.5;
    /** @var string */
    const CONST_STRING = "STR";
    /** @var bool
     *  @cvalue EXTERNAL_NS_BOOL */
    const EXTERN_BOOL = UNKNOWN;
    /** @var int
     *  @cvalue EXTERNAL_NS_INT */
    const EXTERN_INT = UNKNOWN;
    /** @var float
     *  @cvalue EXTERNAL_NS_FLOAT */
    const EXTERN_FLOAT = UNKNOWN;
    /** @var string
     *  @cvalue EXTERNAL_NS_STRING */
    const EXTERN_STRING = UNKNOWN;
}

generated code:

static void register_TestConstants_symbols(int module_number)
{
	REGISTER_NULL_CONSTANT("CONST_NULL", CONST_PERSISTENT);
	REGISTER_BOOL_CONSTANT("CONST_BOOL", true, CONST_PERSISTENT);
	REGISTER_LONG_CONSTANT("CONST_INT", 1, CONST_PERSISTENT);
	REGISTER_DOUBLE_CONSTANT("CONST_FLOAT", 1.5, CONST_PERSISTENT);
	REGISTER_STRING_CONSTANT("CONST_STRING", "str", CONST_PERSISTENT);
	REGISTER_BOOL_CONSTANT("EXTERN_BOOL", EXTERNAL_BOOL, CONST_PERSISTENT);
	REGISTER_LONG_CONSTANT("EXTERN_INT", EXTERNAL_INT, CONST_PERSISTENT);
	REGISTER_DOUBLE_CONSTANT("EXTERN_FLOAT", EXTERNAL_FLOAT, CONST_PERSISTENT);
	REGISTER_STRING_CONSTANT("EXTERN_STRING", EXTERNAL_STRING, CONST_PERSISTENT);
	REGISTER_NULL_CONSTANT("Not\\In\\Global\\CONST_NULL", CONST_PERSISTENT);
	REGISTER_BOOL_CONSTANT("Not\\In\\Global\\CONST_BOOL", false, CONST_PERSISTENT);
	REGISTER_LONG_CONSTANT("Not\\In\\Global\\CONST_INT", -1, CONST_PERSISTENT);
	REGISTER_DOUBLE_CONSTANT("Not\\In\\Global\\CONST_FLOAT", -1.5, CONST_PERSISTENT);
	REGISTER_STRING_CONSTANT("Not\\In\\Global\\CONST_STRING", "STR", CONST_PERSISTENT);
	REGISTER_BOOL_CONSTANT("Not\\In\\Global\\EXTERN_BOOL", EXTERNAL_NS_BOOL, CONST_PERSISTENT);
	REGISTER_LONG_CONSTANT("Not\\In\\Global\\EXTERN_INT", EXTERNAL_NS_INT, CONST_PERSISTENT);
	REGISTER_DOUBLE_CONSTANT("Not\\In\\Global\\EXTERN_FLOAT", EXTERNAL_NS_FLOAT, CONST_PERSISTENT);
	REGISTER_STRING_CONSTANT("Not\\In\\Global\\EXTERN_STRING", EXTERNAL_NS_STRING, CONST_PERSISTENT);
}

@ranvis
Copy link
Contributor Author

ranvis commented Feb 16, 2023

@kocsismate Thank you. I've updated the branch.

@kocsismate
Copy link
Member

Thank you! IMO, this could be fixed in PHP 8.2 as well, so I'd appreciate if you could change the target branch to PHP-8.2.

@kocsismate kocsismate changed the base branch from master to PHP-8.2 February 19, 2023 20:32
@kocsismate kocsismate changed the base branch from PHP-8.2 to master February 19, 2023 20:32
@kocsismate
Copy link
Member

As far as I see, in order to make the functionality work on PHP 8.2, eithr a @cvalue is needed to be added for \ZendTestNS2\ZEND_CONSTANT_A, or value resolution has to be fixed.

@ranvis
Copy link
Contributor Author

ranvis commented Feb 21, 2023

Thank you for the clue. I think @cvalue looks nicer here.
Also it required an additional care for unknown test of a const inside a namespace for this branch.

Anyway, I appreciate if it could land in 8.2 :)

@ranvis ranvis changed the base branch from master to PHP-8.2 February 21, 2023 09:23
@kocsismate kocsismate closed this Feb 23, 2023
@kocsismate kocsismate reopened this Feb 23, 2023
@ranvis ranvis deleted the stub-ns-const branch February 24, 2023 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants