Skip to content

Commit 0f966f8

Browse files
authored
Implement MagicConst File/Dir/Line handler
1 parent b64d77e commit 0f966f8

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\Generator\ExprHandler;
4+
5+
use Generator;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Scalar\MagicConst\Dir;
8+
use PhpParser\Node\Stmt;
9+
use PHPStan\Analyser\ExpressionContext;
10+
use PHPStan\Analyser\Generator\ExprAnalysisResult;
11+
use PHPStan\Analyser\Generator\ExprHandler;
12+
use PHPStan\Analyser\Generator\GeneratorScope;
13+
use PHPStan\Analyser\SpecifiedTypes;
14+
use PHPStan\DependencyInjection\AutowiredService;
15+
use PHPStan\Reflection\InitializerExprContext;
16+
use PHPStan\Reflection\InitializerExprTypeResolver;
17+
18+
/**
19+
* @implements ExprHandler<Dir>
20+
*/
21+
#[AutowiredService]
22+
final class MagicDirHandler implements ExprHandler
23+
{
24+
25+
public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
26+
{
27+
}
28+
29+
public function supports(Expr $expr): bool
30+
{
31+
return $expr instanceof Dir;
32+
}
33+
34+
public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator
35+
{
36+
yield from [];
37+
38+
$initializerContext = InitializerExprContext::fromScope($scope);
39+
$type = $this->initializerExprTypeResolver->getType($expr, $initializerContext);
40+
41+
return new ExprAnalysisResult(
42+
$type,
43+
$type,
44+
$scope,
45+
hasYield: false,
46+
isAlwaysTerminating: false,
47+
throwPoints: [],
48+
impurePoints: [],
49+
specifiedTruthyTypes: new SpecifiedTypes(),
50+
specifiedFalseyTypes: new SpecifiedTypes(),
51+
specifiedNullTypes: new SpecifiedTypes(),
52+
);
53+
}
54+
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\Generator\ExprHandler;
4+
5+
use Generator;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Scalar\MagicConst\File;
8+
use PhpParser\Node\Stmt;
9+
use PHPStan\Analyser\ExpressionContext;
10+
use PHPStan\Analyser\Generator\ExprAnalysisResult;
11+
use PHPStan\Analyser\Generator\ExprHandler;
12+
use PHPStan\Analyser\Generator\GeneratorScope;
13+
use PHPStan\Analyser\SpecifiedTypes;
14+
use PHPStan\DependencyInjection\AutowiredService;
15+
use PHPStan\Reflection\InitializerExprContext;
16+
use PHPStan\Reflection\InitializerExprTypeResolver;
17+
18+
/**
19+
* @implements ExprHandler<File>
20+
*/
21+
#[AutowiredService]
22+
final class MagicFileHandler implements ExprHandler
23+
{
24+
25+
public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
26+
{
27+
}
28+
29+
public function supports(Expr $expr): bool
30+
{
31+
return $expr instanceof File;
32+
}
33+
34+
public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator
35+
{
36+
yield from [];
37+
38+
$initializerContext = InitializerExprContext::fromScope($scope);
39+
$type = $this->initializerExprTypeResolver->getType($expr, $initializerContext);
40+
41+
return new ExprAnalysisResult(
42+
$type,
43+
$type,
44+
$scope,
45+
hasYield: false,
46+
isAlwaysTerminating: false,
47+
throwPoints: [],
48+
impurePoints: [],
49+
specifiedTruthyTypes: new SpecifiedTypes(),
50+
specifiedFalseyTypes: new SpecifiedTypes(),
51+
specifiedNullTypes: new SpecifiedTypes(),
52+
);
53+
}
54+
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\Generator\ExprHandler;
4+
5+
use Generator;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Scalar\MagicConst\Line;
8+
use PhpParser\Node\Stmt;
9+
use PHPStan\Analyser\ExpressionContext;
10+
use PHPStan\Analyser\Generator\ExprAnalysisResult;
11+
use PHPStan\Analyser\Generator\ExprHandler;
12+
use PHPStan\Analyser\Generator\GeneratorScope;
13+
use PHPStan\Analyser\SpecifiedTypes;
14+
use PHPStan\DependencyInjection\AutowiredService;
15+
use PHPStan\Reflection\InitializerExprContext;
16+
use PHPStan\Reflection\InitializerExprTypeResolver;
17+
18+
/**
19+
* @implements ExprHandler<Line>
20+
*/
21+
#[AutowiredService]
22+
final class MagicLineHandler implements ExprHandler
23+
{
24+
25+
public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
26+
{
27+
}
28+
29+
public function supports(Expr $expr): bool
30+
{
31+
return $expr instanceof Line;
32+
}
33+
34+
public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator
35+
{
36+
yield from [];
37+
38+
$initializerContext = InitializerExprContext::fromScope($scope);
39+
$type = $this->initializerExprTypeResolver->getType($expr, $initializerContext);
40+
41+
return new ExprAnalysisResult(
42+
$type,
43+
$type,
44+
$scope,
45+
hasYield: false,
46+
isAlwaysTerminating: false,
47+
throwPoints: [],
48+
impurePoints: [],
49+
specifiedTruthyTypes: new SpecifiedTypes(),
50+
specifiedFalseyTypes: new SpecifiedTypes(),
51+
specifiedNullTypes: new SpecifiedTypes(),
52+
);
53+
}
54+
55+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,11 @@ public function __construct(private int $i, private int $j, private int $k) {
462462
}
463463
};
464464
};
465+
466+
class MagicConstUser {
467+
function doFoo(): void {
468+
assertType('literal-string&non-falsy-string', __DIR__);
469+
assertType('literal-string&non-falsy-string', __FILE__);
470+
assertType('470', __LINE__);
471+
}
472+
}

0 commit comments

Comments
 (0)