diff --git a/src/Analyser/Generator/ExprHandler/MagicDirHandler.php b/src/Analyser/Generator/ExprHandler/MagicDirHandler.php new file mode 100644 index 0000000000..8b4f1a2538 --- /dev/null +++ b/src/Analyser/Generator/ExprHandler/MagicDirHandler.php @@ -0,0 +1,55 @@ + + */ +#[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/MagicFileHandler.php b/src/Analyser/Generator/ExprHandler/MagicFileHandler.php new file mode 100644 index 0000000000..0c71915c9b --- /dev/null +++ b/src/Analyser/Generator/ExprHandler/MagicFileHandler.php @@ -0,0 +1,55 @@ + + */ +#[AutowiredService] +final class MagicFileHandler implements ExprHandler +{ + + public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) + { + } + + public function supports(Expr $expr): bool + { + return $expr instanceof File; + } + + 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 new file mode 100644 index 0000000000..e4c16e4d25 --- /dev/null +++ b/src/Analyser/Generator/ExprHandler/MagicLineHandler.php @@ -0,0 +1,55 @@ + + */ +#[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 f553e3953d..4934282183 100644 --- a/tests/PHPStan/Analyser/Generator/data/gnsr.php +++ b/tests/PHPStan/Analyser/Generator/data/gnsr.php @@ -462,3 +462,11 @@ public function __construct(private int $i, private int $j, private int $k) { } }; }; + +class MagicConstUser { + function doFoo(): void { + assertType('literal-string&non-falsy-string', __DIR__); + assertType('literal-string&non-falsy-string', __FILE__); + assertType('470', __LINE__); + } +}