Skip to content

Commit f7f434d

Browse files
committed
handle invalid name given to constant()
1 parent b24c9f8 commit f7f434d

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

src/Type/Php/ConstantFunctionReturnTypeExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\FunctionReflection;
88
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
9+
use PHPStan\Type\ErrorType;
910
use PHPStan\Type\Type;
1011
use PHPStan\Type\TypeCombinator;
1112
use function count;
@@ -39,7 +40,13 @@ public function getTypeFromFunctionCall(
3940
if ($constantName->getValue() === '') {
4041
return null;
4142
}
42-
$results[] = $scope->getType($this->constantHelper->createExprFromConstantName($constantName->getValue()));
43+
44+
$expr = $this->constantHelper->createExprFromConstantName($constantName->getValue());
45+
if ($expr === null) {
46+
return new ErrorType();
47+
}
48+
49+
$results[] = $scope->getType($expr);
4350
}
4451

4552
if (count($results) > 0) {

src/Type/Php/ConstantHelper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node\Identifier;
99
use PhpParser\Node\Name;
1010
use PhpParser\Node\Name\FullyQualified;
11-
use PHPStan\ShouldNotHappenException;
1211
use function count;
1312
use function explode;
1413
use function ltrim;
@@ -19,13 +18,13 @@ class ConstantHelper
1918
/**
2019
* @param non-empty-string $constantName
2120
*/
22-
public function createExprFromConstantName(string $constantName): Expr
21+
public function createExprFromConstantName(string $constantName): ?Expr
2322
{
2423
$classConstParts = explode('::', $constantName);
2524
if (count($classConstParts) >= 2) {
2625
$fqcn = ltrim($classConstParts[0], '\\');
2726
if ($fqcn === '') {
28-
throw new ShouldNotHappenException();
27+
return null;
2928
}
3029

3130
$classConstName = new FullyQualified($fqcn);

src/Type/Php/DefinedConstantTypeSpecifyingExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ public function specifyTypes(
5454
return new SpecifiedTypes([], []);
5555
}
5656

57+
$expr = $this->constantHelper->createExprFromConstantName($constantName->getValue());
58+
if ($expr === null) {
59+
return new SpecifiedTypes([], []);
60+
}
61+
5762
return $this->typeSpecifier->create(
58-
$this->constantHelper->createExprFromConstantName($constantName->getValue()),
63+
$expr,
5964
new MixedType(),
6065
$context,
6166
false,

tests/PHPStan/Analyser/data/constant.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ function doFoo(string $constantName): void
3838
assertType('Constant\Suit::Hearts', constant('\Constant\Suit::Hearts'));
3939

4040
assertType('*ERROR*', constant('UNDEFINED'));
41+
assertType('*ERROR*', constant('::aa'));

0 commit comments

Comments
 (0)