Skip to content

Commit

Permalink
self::CONSTANT can be precise even with PHPDoc type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 23, 2021
1 parent 8bf2fe0 commit 5010ef4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
13 changes: 12 additions & 1 deletion src/Analyser/MutatingScope.php
Expand Up @@ -1992,7 +1992,18 @@ private function resolveType(Expr $node): Type
return new MixedType();
}

$constantType = $constantReflection->getValueType();
if (
$isObject
&& (
!$constantReflection instanceof ClassConstantReflection
|| !$constantClassReflection->isFinal()
)
) {
$constantType = $constantReflection->getValueType();
} else {
$constantType = ConstantTypeHelper::getTypeFromValue($constantReflection->getValue());
}

if (
$constantType instanceof ConstantType
&& in_array(sprintf('%s::%s', $constantClassReflection->getName(), $constantName), $this->dynamicConstantNames, true)
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/bug-5293.php
Expand Up @@ -49,7 +49,7 @@ public static function getAllSupportedCurrencies(): void
{
array_map(
function (string $currencyCode): int {
assertType('non-empty-string', $currencyCode);
assertType('\'AUD\'|\'BGN\'|\'BRL\'|\'CAD\'|\'CHF\'|\'CZK\'|\'DKK\'|\'EUR\'|\'GBP\'|\'HUF\'|\'NOK\'|\'NZD\'|\'PLN\'|\'RON\'|\'SEK\'|\'SGD\'|\'USD\'', $currencyCode);
return 1;
},
self::SUPPORTED_CURRENCIES
Expand Down
8 changes: 6 additions & 2 deletions tests/PHPStan/Analyser/data/class-constant-stub-files.php
Expand Up @@ -15,6 +15,10 @@ class Foo
}

function (): void {
assertType('int', Foo::BAR);
assertType('int', Foo::BAZ);
assertType('1', Foo::BAR);
assertType('1', Foo::BAZ);

$foo = new Foo();
assertType('int', $foo::BAR);
assertType('int', $foo::BAZ);
};
38 changes: 33 additions & 5 deletions tests/PHPStan/Analyser/data/class-constant-types.php
Expand Up @@ -21,11 +21,11 @@ public function doFoo()
assertType('mixed', static::NO_TYPE);
assertType('mixed', $this::NO_TYPE);

assertType('string', self::TYPE);
assertType('\'foo\'', self::TYPE);
assertType('string', static::TYPE);
assertType('string', $this::TYPE);

assertType('string', self::PRIVATE_TYPE);
assertType('\'foo\'', self::PRIVATE_TYPE);
assertType('string', static::PRIVATE_TYPE);
assertType('string', $this::PRIVATE_TYPE);
}
Expand All @@ -41,7 +41,7 @@ class Bar extends Foo

public function doFoo()
{
assertType('string', self::TYPE);
assertType('\'bar\'', self::TYPE);
assertType('string', static::TYPE);
assertType('string', $this::TYPE);

Expand All @@ -60,7 +60,7 @@ class Baz extends Foo

public function doFoo()
{
assertType('int', self::TYPE);
assertType('1', self::TYPE);
assertType('int', static::TYPE);
assertType('int', $this::TYPE);
}
Expand All @@ -75,9 +75,37 @@ class Lorem extends Foo

public function doFoo()
{
assertType('string', self::TYPE);
assertType('1', self::TYPE);
assertType('string', static::TYPE);
assertType('string', $this::TYPE);
}

}

final class FinalFoo
{

const NO_TYPE = 1;

/** @var string */
const TYPE = 'foo';

/** @var string */
private const PRIVATE_TYPE = 'foo';

public function doFoo()
{
assertType('1', self::NO_TYPE);
assertType('1', static::NO_TYPE);
assertType('1', $this::NO_TYPE);

assertType('\'foo\'', self::TYPE);
assertType('\'foo\'', static::TYPE);
assertType('\'foo\'', $this::TYPE);

assertType('\'foo\'', self::PRIVATE_TYPE);
assertType('\'foo\'', static::PRIVATE_TYPE);
assertType('\'foo\'', $this::PRIVATE_TYPE);
}

}

0 comments on commit 5010ef4

Please sign in to comment.