Skip to content

Commit 5e42581

Browse files
authored
Implement remaining Magic-Const Handlers
1 parent d7482af commit 5e42581

File tree

4 files changed

+25
-113
lines changed

4 files changed

+25
-113
lines changed

src/Analyser/Generator/ExprHandler/MagicFileHandler.php renamed to src/Analyser/Generator/ExprHandler/MagicConstHandler.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44

55
use Generator;
66
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Scalar\MagicConst\Class_;
8+
use PhpParser\Node\Scalar\MagicConst\Dir;
79
use PhpParser\Node\Scalar\MagicConst\File;
10+
use PhpParser\Node\Scalar\MagicConst\Function_;
11+
use PhpParser\Node\Scalar\MagicConst\Line;
12+
use PhpParser\Node\Scalar\MagicConst\Method;
13+
use PhpParser\Node\Scalar\MagicConst\Namespace_;
14+
use PhpParser\Node\Scalar\MagicConst\Property;
15+
use PhpParser\Node\Scalar\MagicConst\Trait_;
816
use PhpParser\Node\Stmt;
917
use PHPStan\Analyser\ExpressionContext;
1018
use PHPStan\Analyser\Generator\ExprAnalysisResult;
@@ -16,10 +24,10 @@
1624
use PHPStan\Reflection\InitializerExprTypeResolver;
1725

1826
/**
19-
* @implements ExprHandler<File>
27+
* @implements ExprHandler<Dir|File|Line|Namespace_|Class_|Property|Function_|Method|Trait_>
2028
*/
2129
#[AutowiredService]
22-
final class MagicFileHandler implements ExprHandler
30+
final class MagicConstHandler implements ExprHandler
2331
{
2432

2533
public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
@@ -28,7 +36,15 @@ public function __construct(private InitializerExprTypeResolver $initializerExpr
2836

2937
public function supports(Expr $expr): bool
3038
{
31-
return $expr instanceof File;
39+
return $expr instanceof Dir
40+
|| $expr instanceof File
41+
|| $expr instanceof Line
42+
|| $expr instanceof Namespace_
43+
|| $expr instanceof Class_
44+
|| $expr instanceof Property
45+
|| $expr instanceof Function_
46+
|| $expr instanceof Method
47+
|| $expr instanceof Trait_;
3248
}
3349

3450
public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator

src/Analyser/Generator/ExprHandler/MagicDirHandler.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Analyser/Generator/ExprHandler/MagicLineHandler.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

tests/PHPStan/Analyser/Generator/data/gnsr.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,12 @@ function doFoo(): void {
469469
assertType('literal-string&non-falsy-string', __DIR__);
470470
assertType('literal-string&non-falsy-string', __FILE__);
471471
assertType('471', __LINE__);
472+
assertType("'GeneratorNodeScopeResolverTest'", __NAMESPACE__);
473+
assertType("'GeneratorNodeScopeResolverTest\\\\MagicConstUser'", __CLASS__);
474+
assertType("''", __TRAIT__);
475+
assertType("'doFoo'", __FUNCTION__);
476+
assertType("'GeneratorNodeScopeResolverTest\\\\MagicConstUser::doFoo'", __METHOD__);
477+
assertType("''", __PROPERTY__);
472478
}
473479
}
474480

0 commit comments

Comments
 (0)