Skip to content

Commit

Permalink
handle invalid name given to constant()
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed May 31, 2024
1 parent b24c9f8 commit f7f434d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/Type/Php/ConstantFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
Expand Down Expand Up @@ -39,7 +40,13 @@ public function getTypeFromFunctionCall(
if ($constantName->getValue() === '') {
return null;
}
$results[] = $scope->getType($this->constantHelper->createExprFromConstantName($constantName->getValue()));

$expr = $this->constantHelper->createExprFromConstantName($constantName->getValue());
if ($expr === null) {
return new ErrorType();
}

$results[] = $scope->getType($expr);
}

if (count($results) > 0) {
Expand Down
5 changes: 2 additions & 3 deletions src/Type/Php/ConstantHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\ShouldNotHappenException;
use function count;
use function explode;
use function ltrim;
Expand All @@ -19,13 +18,13 @@ class ConstantHelper
/**
* @param non-empty-string $constantName
*/
public function createExprFromConstantName(string $constantName): Expr
public function createExprFromConstantName(string $constantName): ?Expr
{
$classConstParts = explode('::', $constantName);
if (count($classConstParts) >= 2) {
$fqcn = ltrim($classConstParts[0], '\\');
if ($fqcn === '') {
throw new ShouldNotHappenException();
return null;
}

$classConstName = new FullyQualified($fqcn);
Expand Down
7 changes: 6 additions & 1 deletion src/Type/Php/DefinedConstantTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ public function specifyTypes(
return new SpecifiedTypes([], []);
}

$expr = $this->constantHelper->createExprFromConstantName($constantName->getValue());
if ($expr === null) {
return new SpecifiedTypes([], []);
}

return $this->typeSpecifier->create(
$this->constantHelper->createExprFromConstantName($constantName->getValue()),
$expr,
new MixedType(),
$context,
false,
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/data/constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ function doFoo(string $constantName): void
assertType('Constant\Suit::Hearts', constant('\Constant\Suit::Hearts'));

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

0 comments on commit f7f434d

Please sign in to comment.