- Remove static constant lookup logic from parseConstFetch method - Add support for unqualified constants in namespaces with runtime fallback mechanism - Implement UnqualifiedInNamespace constant lookup strategy for proper PHP behavior - Update test cases to verify runtime constant resolution works correctly - Add comprehensive test coverage for namespace constant fallback scenarios - Remove isValidDefineName validation in favor of direct string checking - Support define() calls with namespaced and non-identifier constant names - Add proper error handling for undefined constants in namespacespull/40/head
parent
0b1a45660e
commit
e9f617eae0
7 changed files with 196 additions and 37 deletions
@ -0,0 +1,17 @@ |
||||
--TEST-- |
||||
define accepts namespaced and non-identifier constant names like PHP |
||||
--FILE-- |
||||
<?php |
||||
|
||||
function main(): void |
||||
{ |
||||
define('Vendor\Package\VALUE', 42); |
||||
define('1 non identifier', 'supported'); |
||||
|
||||
var_dump(constant('Vendor\Package\VALUE')); |
||||
var_dump(constant('1 non identifier')); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(42) |
||||
string(9) "supported" |
||||
@ -0,0 +1,41 @@ |
||||
--TEST-- |
||||
Unqualified runtime constants in a namespace fall back to global constants |
||||
--FILE-- |
||||
<?php |
||||
|
||||
namespace RuntimeConstantFallback { |
||||
function readGlobalOnly() |
||||
{ |
||||
return GLOBAL_ONLY; |
||||
} |
||||
|
||||
function readPreferred() |
||||
{ |
||||
return PREFERRED; |
||||
} |
||||
|
||||
function readInternalOverride() |
||||
{ |
||||
return PHP_VERSION; |
||||
} |
||||
} |
||||
|
||||
namespace { |
||||
function main(): void |
||||
{ |
||||
define('GLOBAL_ONLY', 'global'); |
||||
define('PREFERRED', 'global-preferred'); |
||||
define('RuntimeConstantFallback\Preferred', 'wrong-case-name'); |
||||
define('RuntimeConstantFallback\PREFERRED', 'namespaced'); |
||||
define('RuntimeConstantFallback\PHP_VERSION', 'runtime-override'); |
||||
|
||||
var_dump(\RuntimeConstantFallback\readGlobalOnly()); |
||||
var_dump(\RuntimeConstantFallback\readPreferred()); |
||||
var_dump(\RuntimeConstantFallback\readInternalOverride()); |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
string(6) "global" |
||||
string(10) "namespaced" |
||||
string(16) "runtime-override" |
||||
@ -0,0 +1,45 @@ |
||||
--TEST-- |
||||
Qualified and imported runtime constants use their exact resolved names |
||||
--FILE-- |
||||
<?php |
||||
|
||||
namespace RuntimeConstantConsumer { |
||||
use const RuntimeConstantProvider\IMPORTED_VALUE as IMPORTED; |
||||
use RuntimeConstantProvider\CLASS_ALIAS_COLLISION; |
||||
|
||||
function readImported() |
||||
{ |
||||
return IMPORTED; |
||||
} |
||||
|
||||
function readQualified() |
||||
{ |
||||
return Config\PATH; |
||||
} |
||||
|
||||
function readClassAliasCollision() |
||||
{ |
||||
return CLASS_ALIAS_COLLISION; |
||||
} |
||||
} |
||||
|
||||
namespace { |
||||
function main(): void |
||||
{ |
||||
define('RuntimeConstantProvider\IMPORTED_VALUE', 'imported'); |
||||
define('RuntimeConstantConsumer\Config\PATH', 'qualified'); |
||||
define('CLASS_ALIAS_COLLISION', 'global-constant'); |
||||
define('RuntimeConstantProvider\CLASS_ALIAS_COLLISION', 'wrong-provider'); |
||||
define('IMPORTED', 'wrong-global'); |
||||
define('Config\PATH', 'wrong-qualified'); |
||||
|
||||
var_dump(\RuntimeConstantConsumer\readImported()); |
||||
var_dump(\RuntimeConstantConsumer\readQualified()); |
||||
var_dump(\RuntimeConstantConsumer\readClassAliasCollision()); |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
string(8) "imported" |
||||
string(9) "qualified" |
||||
string(15) "global-constant" |
||||
@ -0,0 +1,25 @@ |
||||
--TEST-- |
||||
Undefined runtime constants in a namespace throw Error after global fallback |
||||
--FILE-- |
||||
<?php |
||||
|
||||
namespace RuntimeConstantUndefined { |
||||
function readMissing() |
||||
{ |
||||
return MISSING_RUNTIME_CONSTANT; |
||||
} |
||||
} |
||||
|
||||
namespace { |
||||
function main(): void |
||||
{ |
||||
try { |
||||
\RuntimeConstantUndefined\readMissing(); |
||||
} catch (\Error $error) { |
||||
echo $error->getMessage(), PHP_EOL; |
||||
} |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
Undefined constant "RuntimeConstantUndefined\MISSING_RUNTIME_CONSTANT" |
||||
Loading…
Reference in new issue