Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Analyser/Generator/ExprHandler/MagicDirHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser\Generator\ExprHandler;

use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\MagicConst\Dir;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\InitializerExprContext;
use PHPStan\Reflection\InitializerExprTypeResolver;

/**
* @implements ExprHandler<Dir>
*/
#[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(),
);
}

}
55 changes: 55 additions & 0 deletions src/Analyser/Generator/ExprHandler/MagicFileHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser\Generator\ExprHandler;

use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\MagicConst\File;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\InitializerExprContext;
use PHPStan\Reflection\InitializerExprTypeResolver;

/**
* @implements ExprHandler<File>
*/
#[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(),
);
}

}
55 changes: 55 additions & 0 deletions src/Analyser/Generator/ExprHandler/MagicLineHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser\Generator\ExprHandler;

use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\MagicConst\Line;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\InitializerExprContext;
use PHPStan\Reflection\InitializerExprTypeResolver;

/**
* @implements ExprHandler<Line>
*/
#[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(),
);
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
}
}
Loading