Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\CallableType;
use PHPStan\Type\ClassConstantAccessType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\ClosureType;
use PHPStan\Type\ConditionalType;
Expand Down Expand Up @@ -1188,9 +1189,13 @@ private function resolveConstTypeNode(ConstTypeNode $typeNode, NameScope $nameSc
throw new ShouldNotHappenException(); // global constant should get parsed as class name in IdentifierTypeNode
}

$isStaticConst = false;
if ($nameScope->getClassName() !== null) {
switch (strtolower($constExpr->className)) {
case 'static':
$className = $nameScope->getClassName();
$isStaticConst = true;
break;
case 'self':
$className = $nameScope->getClassName();
break;
Expand Down Expand Up @@ -1220,6 +1225,15 @@ private function resolveConstTypeNode(ConstTypeNode $typeNode, NameScope $nameSc
$classReflection = $this->getReflectionProvider()->getClass($className);

$constantName = $constExpr->name;

if ($isStaticConst && !$classReflection->isFinal() && !Strings::contains($constantName, '*')) {
if (!$classReflection->hasConstant($constantName)) {
return new ErrorType();
}

return new ClassConstantAccessType(new StaticType($classReflection), $constantName);
}

if (Strings::contains($constantName, '*')) {
// convert * into .*? and escape everything else so the constants can be matched against the pattern
$pattern = '{^' . str_replace('\\*', '.*?', preg_quote($constantName)) . '$}D';
Expand Down
112 changes: 112 additions & 0 deletions src/Type/ClassConstantAccessType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type;

use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\LateResolvableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use function sprintf;

final class ClassConstantAccessType implements CompoundType, LateResolvableType
{

use LateResolvableTypeTrait;
use NonGeneralizableTypeTrait;

public function __construct(
private Type $classType,
private string $constantName,
)
{
}

public function getReferencedClasses(): array
{
return $this->classType->getReferencedClasses();
}

public function getObjectClassNames(): array
{
return [];
}

public function getObjectClassReflections(): array
{
return [];
}

public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
{
return $this->classType->getReferencedTemplateTypes($positionVariance);
}

public function equals(Type $type): bool
{
return $type instanceof self
&& $this->classType->equals($type->classType)
&& $this->constantName === $type->constantName;
}

public function describe(VerbosityLevel $level): string
{
return sprintf('%s::%s', $this->classType->describe($level), $this->constantName);
}

public function isResolvable(): bool
{
return !TypeUtils::containsTemplateType($this->classType)
&& !($this->classType instanceof StaticType);
}

protected function getResult(): Type
{
if ($this->classType->hasConstant($this->constantName)->yes()) {

Check warning on line 66 in src/Type/ClassConstantAccessType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ protected function getResult(): Type { - if ($this->classType->hasConstant($this->constantName)->yes()) { + if (!$this->classType->hasConstant($this->constantName)->no()) { return $this->classType->getConstant($this->constantName)->getValueType(); }

Check warning on line 66 in src/Type/ClassConstantAccessType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ protected function getResult(): Type { - if ($this->classType->hasConstant($this->constantName)->yes()) { + if (!$this->classType->hasConstant($this->constantName)->no()) { return $this->classType->getConstant($this->constantName)->getValueType(); }
return $this->classType->getConstant($this->constantName)->getValueType();
}

return new ErrorType();
}

/**
* @param callable(Type): Type $cb
*/
public function traverse(callable $cb): Type
{
$classType = $cb($this->classType);

if ($this->classType === $classType) {
return $this;
}

return new self($classType, $this->constantName);
}

public function traverseSimultaneously(Type $right, callable $cb): Type
{
if (!$right instanceof self) {
return $this;
}

$classType = $cb($this->classType, $right->classType);

if ($this->classType === $classType) {
return $this;
}

return new self($classType, $this->constantName);
}

public function toPhpDocNode(): TypeNode
{
return new ConstTypeNode(
new ConstFetchNode(
$this->classType->describe(VerbosityLevel::typeOnly()),
$this->constantName,
),
);
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13828.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types = 1);

namespace Bug13828;

use function PHPStan\Testing\assertType;

abstract class FooBar
{
const FOO_BAR = 'foo';

/** @return static::FOO_BAR */
public function test(): string
{
return static::FOO_BAR;
}
}

class Foo extends FooBar
{
const FOO_BAR = 'foo';
}

class Bar extends FooBar
{
const FOO_BAR = 'bar';
}

function test(): void
{
assertType("'foo'", (new Foo())->test());
assertType("'bar'", (new Bar())->test());
}
Loading