Skip to content
Merged
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
25 changes: 24 additions & 1 deletion src/Type/Constant/ConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Nette\Utils\Strings;
use PhpParser\Node\Name;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\InaccessibleMethod;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
Expand Down Expand Up @@ -47,6 +48,8 @@ class ConstantStringType extends StringType implements ConstantScalarType
use ConstantScalarTypeTrait;
use ConstantScalarToBooleanTrait;

private ?ObjectType $objectType = null;

/** @api */
public function __construct(private string $value, private bool $isClassString = false)
{
Expand Down Expand Up @@ -106,7 +109,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic

// We are transforming constant class-string to ObjectType. But we need to filter out
// an uncertainty originating in possible ObjectType's class subtypes.
$objectType = new ObjectType($this->getValue());
$objectType = $this->getObjectType();

// Do not use TemplateType's isSuperTypeOf handling directly because it takes ObjectType
// uncertainty into account.
Expand Down Expand Up @@ -403,6 +406,26 @@ public function getGreaterOrEqualType(): Type
return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
}

public function canAccessConstants(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->isClassString());
}

public function hasConstant(string $constantName): TrinaryLogic
{
return $this->getObjectType()->hasConstant($constantName);
}

public function getConstant(string $constantName): ConstantReflection
{
return $this->getObjectType()->getConstant($constantName);
}

private function getObjectType(): ObjectType
{
return $this->objectType ??= new ObjectType($this->value);
}

/**
* @param mixed[] $properties
*/
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/conditional-complex-templates.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7374.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/template-constant-bound.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7391.php');
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7391.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace Bug7391;

use function PHPStan\Testing\assertType;

class Foo
{
public const X = '5';
public const Cl = self::class;
}

function () {
$foo = Foo::Cl;
assertType("'Bug7391\\\\Foo'", $foo);
assertType("'5'", $foo::X);
assertType('*ERROR*', $foo::class);
};