feat(resolver): add support for interface constant resolution in traits, Fix GH-112

master
韩天峰 3 weeks ago
parent 5df39f2b4f
commit d245ff87a0
  1. 13
      src/Resolver/ClassConstantValueTrait.php
  2. 70
      tests/compiler/const/interface-const-property-default.phpt

@ -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;

@ -0,0 +1,70 @@
--TEST--
Interface constants can initialize properties composed from a trait
--FILE--
<?php
namespace Issue112 {
interface BaseOutputInterface
{
public const VERBOSITY_QUIET = 16;
}
interface OutputInterface extends BaseOutputInterface
{
public const VERBOSITY_NORMAL = 32;
public const VERBOSITY_VERBOSE = 64;
public const VERBOSITY_VERY_VERBOSE = 128;
public const VERBOSITY_DEBUG = 256;
}
trait InteractsWithIO
{
protected int $verbosity = OutputInterface::VERBOSITY_NORMAL;
protected array $verbosityMap = [
'v' => 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)
}
Loading…
Cancel
Save