diff --git a/src/Analyser/Generator/ExprHandler/MagicFileHandler.php b/src/Analyser/Generator/ExprHandler/MagicConstHandler.php similarity index 63% rename from src/Analyser/Generator/ExprHandler/MagicFileHandler.php rename to src/Analyser/Generator/ExprHandler/MagicConstHandler.php index 0c71915c9b..e69794698e 100644 --- a/src/Analyser/Generator/ExprHandler/MagicFileHandler.php +++ b/src/Analyser/Generator/ExprHandler/MagicConstHandler.php @@ -4,7 +4,15 @@ use Generator; use PhpParser\Node\Expr; +use PhpParser\Node\Scalar\MagicConst\Class_; +use PhpParser\Node\Scalar\MagicConst\Dir; use PhpParser\Node\Scalar\MagicConst\File; +use PhpParser\Node\Scalar\MagicConst\Function_; +use PhpParser\Node\Scalar\MagicConst\Line; +use PhpParser\Node\Scalar\MagicConst\Method; +use PhpParser\Node\Scalar\MagicConst\Namespace_; +use PhpParser\Node\Scalar\MagicConst\Property; +use PhpParser\Node\Scalar\MagicConst\Trait_; use PhpParser\Node\Stmt; use PHPStan\Analyser\ExpressionContext; use PHPStan\Analyser\Generator\ExprAnalysisResult; @@ -16,10 +24,10 @@ use PHPStan\Reflection\InitializerExprTypeResolver; /** - * @implements ExprHandler + * @implements ExprHandler */ #[AutowiredService] -final class MagicFileHandler implements ExprHandler +final class MagicConstHandler implements ExprHandler { public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) @@ -28,7 +36,15 @@ public function __construct(private InitializerExprTypeResolver $initializerExpr public function supports(Expr $expr): bool { - return $expr instanceof File; + return $expr instanceof Dir + || $expr instanceof File + || $expr instanceof Line + || $expr instanceof Namespace_ + || $expr instanceof Class_ + || $expr instanceof Property + || $expr instanceof Function_ + || $expr instanceof Method + || $expr instanceof Trait_; } public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator diff --git a/src/Analyser/Generator/ExprHandler/MagicDirHandler.php b/src/Analyser/Generator/ExprHandler/MagicDirHandler.php deleted file mode 100644 index 8b4f1a2538..0000000000 --- a/src/Analyser/Generator/ExprHandler/MagicDirHandler.php +++ /dev/null @@ -1,55 +0,0 @@ - - */ -#[AutowiredService] -final class MagicDirHandler implements ExprHandler -{ - - public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) - { - } - - public function supports(Expr $expr): bool - { - return $expr instanceof Dir; - } - - public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator - { - yield from []; - - $initializerContext = InitializerExprContext::fromScope($scope); - $type = $this->initializerExprTypeResolver->getType($expr, $initializerContext); - - return new ExprAnalysisResult( - $type, - $type, - $scope, - hasYield: false, - isAlwaysTerminating: false, - throwPoints: [], - impurePoints: [], - specifiedTruthyTypes: new SpecifiedTypes(), - specifiedFalseyTypes: new SpecifiedTypes(), - specifiedNullTypes: new SpecifiedTypes(), - ); - } - -} diff --git a/src/Analyser/Generator/ExprHandler/MagicLineHandler.php b/src/Analyser/Generator/ExprHandler/MagicLineHandler.php deleted file mode 100644 index e4c16e4d25..0000000000 --- a/src/Analyser/Generator/ExprHandler/MagicLineHandler.php +++ /dev/null @@ -1,55 +0,0 @@ - - */ -#[AutowiredService] -final class MagicLineHandler implements ExprHandler -{ - - public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) - { - } - - public function supports(Expr $expr): bool - { - return $expr instanceof Line; - } - - public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator - { - yield from []; - - $initializerContext = InitializerExprContext::fromScope($scope); - $type = $this->initializerExprTypeResolver->getType($expr, $initializerContext); - - return new ExprAnalysisResult( - $type, - $type, - $scope, - hasYield: false, - isAlwaysTerminating: false, - throwPoints: [], - impurePoints: [], - specifiedTruthyTypes: new SpecifiedTypes(), - specifiedFalseyTypes: new SpecifiedTypes(), - specifiedNullTypes: new SpecifiedTypes(), - ); - } - -} diff --git a/tests/PHPStan/Analyser/Generator/data/gnsr.php b/tests/PHPStan/Analyser/Generator/data/gnsr.php index 42f2192425..f6f31ea313 100644 --- a/tests/PHPStan/Analyser/Generator/data/gnsr.php +++ b/tests/PHPStan/Analyser/Generator/data/gnsr.php @@ -469,6 +469,12 @@ function doFoo(): void { assertType('literal-string&non-falsy-string', __DIR__); assertType('literal-string&non-falsy-string', __FILE__); assertType('471', __LINE__); + assertType("'GeneratorNodeScopeResolverTest'", __NAMESPACE__); + assertType("'GeneratorNodeScopeResolverTest\\\\MagicConstUser'", __CLASS__); + assertType("''", __TRAIT__); + assertType("'doFoo'", __FUNCTION__); + assertType("'GeneratorNodeScopeResolverTest\\\\MagicConstUser::doFoo'", __METHOD__); + assertType("''", __PROPERTY__); } }