From d245ff87a0fc981f7308dae3a02770c675dcad69 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 16 Sep 2026 13:07:29 +0800 Subject: [PATCH] feat(resolver): add support for interface constant resolution in traits, Fix GH-112 --- src/Resolver/ClassConstantValueTrait.php | 13 ++++ .../interface-const-property-default.phpt | 70 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/compiler/const/interface-const-property-default.phpt diff --git a/src/Resolver/ClassConstantValueTrait.php b/src/Resolver/ClassConstantValueTrait.php index 70eb88fd..ce5b2252 100644 --- a/src/Resolver/ClassConstantValueTrait.php +++ b/src/Resolver/ClassConstantValueTrait.php @@ -69,6 +69,19 @@ trait ClassConstantValueTrait : $value; } } + if ($this->hasInterface($class)) { + $constant = $this->findCompileTimeInterfaceConstant($class, $name); + if ($constant !== null) { + [$interface, $constantDef] = $constant; + return $this->evaluateCompileTimeClassConstant( + $expr, + $interface, + $constantDef, + $name, + $interface, + ); + } + } [$inheritedFound, $inherited] = $this->resolveInheritedClassConst($class, $name); if ($inheritedFound) { return $inherited; diff --git a/tests/compiler/const/interface-const-property-default.phpt b/tests/compiler/const/interface-const-property-default.phpt new file mode 100644 index 00000000..ea5a0670 --- /dev/null +++ b/tests/compiler/const/interface-const-property-default.phpt @@ -0,0 +1,70 @@ +--TEST-- +Interface constants can initialize properties composed from a trait +--FILE-- + OutputInterface::VERBOSITY_VERBOSE, + 'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE, + 'vvv' => OutputInterface::VERBOSITY_DEBUG, + 'quiet' => OutputInterface::VERBOSITY_QUIET, + 'normal' => OutputInterface::VERBOSITY_NORMAL, + ]; + + public function getVerbosity(): int + { + return $this->verbosity; + } + + public function getVerbosityMap(): array + { + return $this->verbosityMap; + } + } + + class Command + { + use InteractsWithIO; + } +} + +namespace { + function main(): void + { + $command = new Issue112\Command(); + var_dump($command->getVerbosity()); + var_dump($command->getVerbosityMap()); + } +} +?> +--EXPECT-- +int(32) +array(5) { + ["v"]=> + int(64) + ["vv"]=> + int(128) + ["vvv"]=> + int(256) + ["quiet"]=> + int(16) + ["normal"]=> + int(32) +}