Skip to content

Commit 5010ef4

Browse files
committed
self::CONSTANT can be precise even with PHPDoc type
1 parent 8bf2fe0 commit 5010ef4

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

src/Analyser/MutatingScope.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,18 @@ private function resolveType(Expr $node): Type
19921992
return new MixedType();
19931993
}
19941994

1995-
$constantType = $constantReflection->getValueType();
1995+
if (
1996+
$isObject
1997+
&& (
1998+
!$constantReflection instanceof ClassConstantReflection
1999+
|| !$constantClassReflection->isFinal()
2000+
)
2001+
) {
2002+
$constantType = $constantReflection->getValueType();
2003+
} else {
2004+
$constantType = ConstantTypeHelper::getTypeFromValue($constantReflection->getValue());
2005+
}
2006+
19962007
if (
19972008
$constantType instanceof ConstantType
19982009
&& in_array(sprintf('%s::%s', $constantClassReflection->getName(), $constantName), $this->dynamicConstantNames, true)

tests/PHPStan/Analyser/data/bug-5293.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function getAllSupportedCurrencies(): void
4949
{
5050
array_map(
5151
function (string $currencyCode): int {
52-
assertType('non-empty-string', $currencyCode);
52+
assertType('\'AUD\'|\'BGN\'|\'BRL\'|\'CAD\'|\'CHF\'|\'CZK\'|\'DKK\'|\'EUR\'|\'GBP\'|\'HUF\'|\'NOK\'|\'NZD\'|\'PLN\'|\'RON\'|\'SEK\'|\'SGD\'|\'USD\'', $currencyCode);
5353
return 1;
5454
},
5555
self::SUPPORTED_CURRENCIES

tests/PHPStan/Analyser/data/class-constant-stub-files.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class Foo
1515
}
1616

1717
function (): void {
18-
assertType('int', Foo::BAR);
19-
assertType('int', Foo::BAZ);
18+
assertType('1', Foo::BAR);
19+
assertType('1', Foo::BAZ);
20+
21+
$foo = new Foo();
22+
assertType('int', $foo::BAR);
23+
assertType('int', $foo::BAZ);
2024
};

tests/PHPStan/Analyser/data/class-constant-types.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public function doFoo()
2121
assertType('mixed', static::NO_TYPE);
2222
assertType('mixed', $this::NO_TYPE);
2323

24-
assertType('string', self::TYPE);
24+
assertType('\'foo\'', self::TYPE);
2525
assertType('string', static::TYPE);
2626
assertType('string', $this::TYPE);
2727

28-
assertType('string', self::PRIVATE_TYPE);
28+
assertType('\'foo\'', self::PRIVATE_TYPE);
2929
assertType('string', static::PRIVATE_TYPE);
3030
assertType('string', $this::PRIVATE_TYPE);
3131
}
@@ -41,7 +41,7 @@ class Bar extends Foo
4141

4242
public function doFoo()
4343
{
44-
assertType('string', self::TYPE);
44+
assertType('\'bar\'', self::TYPE);
4545
assertType('string', static::TYPE);
4646
assertType('string', $this::TYPE);
4747

@@ -60,7 +60,7 @@ class Baz extends Foo
6060

6161
public function doFoo()
6262
{
63-
assertType('int', self::TYPE);
63+
assertType('1', self::TYPE);
6464
assertType('int', static::TYPE);
6565
assertType('int', $this::TYPE);
6666
}
@@ -75,9 +75,37 @@ class Lorem extends Foo
7575

7676
public function doFoo()
7777
{
78-
assertType('string', self::TYPE);
78+
assertType('1', self::TYPE);
7979
assertType('string', static::TYPE);
8080
assertType('string', $this::TYPE);
8181
}
8282

8383
}
84+
85+
final class FinalFoo
86+
{
87+
88+
const NO_TYPE = 1;
89+
90+
/** @var string */
91+
const TYPE = 'foo';
92+
93+
/** @var string */
94+
private const PRIVATE_TYPE = 'foo';
95+
96+
public function doFoo()
97+
{
98+
assertType('1', self::NO_TYPE);
99+
assertType('1', static::NO_TYPE);
100+
assertType('1', $this::NO_TYPE);
101+
102+
assertType('\'foo\'', self::TYPE);
103+
assertType('\'foo\'', static::TYPE);
104+
assertType('\'foo\'', $this::TYPE);
105+
106+
assertType('\'foo\'', self::PRIVATE_TYPE);
107+
assertType('\'foo\'', static::PRIVATE_TYPE);
108+
assertType('\'foo\'', $this::PRIVATE_TYPE);
109+
}
110+
111+
}

0 commit comments

Comments
 (0)