Skip to content

Commit 1c6bb6b

Browse files
Seldaekondrejmirtes
authored andcommitted
Add support for multiple wildcards in const type annotations, fixes phpstan/phpstan#5534
1 parent 1513b3b commit 1c6bb6b

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"ondram/ci-detector": "^3.4.0",
2626
"ondrejmirtes/better-reflection": "4.3.67",
2727
"phpstan/php-8-stubs": "^0.1.23",
28-
"phpstan/phpdoc-parser": "^0.5.6",
28+
"phpstan/phpdoc-parser": "^0.5.7",
2929
"react/child-process": "^0.6.1",
3030
"react/event-loop": "^1.1",
3131
"react/http": "^1.1",

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PhpDoc/TypeNodeResolver.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,11 +702,12 @@ private function resolveConstTypeNode(ConstTypeNode $typeNode, NameScope $nameSc
702702
$classReflection = $this->getReflectionProvider()->getClass($className);
703703

704704
$constantName = $constExpr->name;
705-
if (Strings::endsWith($constantName, '*')) {
706-
$constantNameStartsWith = Strings::substring($constantName, 0, Strings::length($constantName) - 1);
705+
if (Strings::contains($constantName, '*')) {
706+
// convert * into .*? and escape everything else so the constants can be matched against the pattern
707+
$pattern = '{^' . str_replace('\\*', '.*?', preg_quote($constantName)) . '$}D';
707708
$constantTypes = [];
708709
foreach ($classReflection->getNativeReflection()->getConstants() as $classConstantName => $constantValue) {
709-
if (!Strings::startsWith($classConstantName, $constantNameStartsWith)) {
710+
if (Strings::match($classConstantName, $pattern) === null) {
710711
continue;
711712
}
712713

tests/PHPStan/Analyser/data/const-expr-phpdoc-type.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class Foo
1010

1111
public const SOME_CONSTANT = 1;
1212
public const SOME_OTHER_CONSTANT = 2;
13+
public const YET_ONE_CONST = 3;
14+
public const YET_ANOTHER_CONST = 4;
15+
public const DUMMY_SOME_CONSTANT = 99; // should only match the *
1316

1417
/**
1518
* @param 'foo'|'bar' $one
@@ -21,6 +24,9 @@ class Foo
2124
* @param 234 $seven
2225
* @param self::SOME_OTHER_* $eight
2326
* @param self::* $nine
27+
* @param self::*_CONST $ten
28+
* @param self::YET_*_CONST $eleven
29+
* @param self::*OTHER* $twelve
2430
*/
2531
public function doFoo(
2632
$one,
@@ -31,7 +37,10 @@ public function doFoo(
3137
$six,
3238
$seven,
3339
$eight,
34-
$nine
40+
$nine,
41+
$ten,
42+
$eleven,
43+
$twelve
3544
)
3645
{
3746
assertType("'bar'|'foo'", $one);
@@ -42,7 +51,10 @@ public function doFoo(
4251
assertType('1.0', $six);
4352
assertType('234', $seven);
4453
assertType('2', $eight);
45-
assertType('1|2', $nine);
54+
assertType('1|2|3|4|99', $nine);
55+
assertType('3|4', $ten);
56+
assertType('3|4', $eleven);
57+
assertType('2|4', $twelve);
4658
}
4759

4860
}

0 commit comments

Comments
 (0)