Skip to content

Commit

Permalink
Generalize file and dir magic constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 22, 2022
1 parent 14592dd commit f09b288
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -118,11 +118,11 @@ public function getType(Expr $expr, InitializerExprContext $context): Type
}
if ($expr instanceof File) {
$file = $context->getFile();
return $file !== null ? new ConstantStringType($file) : new StringType();
return $file !== null ? (new ConstantStringType($file))->generalize(GeneralizePrecision::moreSpecific()) : new StringType();
}
if ($expr instanceof Dir) {
$file = $context->getFile();
return $file !== null ? new ConstantStringType(dirname($file)) : new StringType();
return $file !== null ? (new ConstantStringType(dirname($file)))->generalize(GeneralizePrecision::moreSpecific()) : new StringType();
}
if ($expr instanceof Line) {
return new ConstantIntegerType($expr->getLine());
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Expand Up @@ -2308,11 +2308,11 @@ public function dataBinaryOperations(): array
'$line',
],
[
(new ConstantStringType(__DIR__ . '/data'))->describe(VerbosityLevel::precise()),
'literal-string&non-falsy-string',
'$dir',
],
[
(new ConstantStringType(__DIR__ . '/data/binary.php'))->describe(VerbosityLevel::precise()),
'literal-string&non-falsy-string',
'$file',
],
[
Expand Down
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Type\VerbosityLevel;
use SingleFileSourceLocatorTestClass;
use TestSingleFileSourceLocator\AFoo;
use function str_replace;
use const PHP_VERSION_ID;

class OptimizedSingleFileSourceLocatorTest extends PHPStanTestCase
Expand Down Expand Up @@ -119,7 +118,7 @@ public function dataConst(): array
],
[
'const_with_dir_const',
"'" . str_replace('\\', '/', __DIR__ . '/data') . "'",
'literal-string&non-falsy-string',
],
[
'OPTIMIZED_SFSL_OBJECT_CONSTANT',
Expand Down

0 comments on commit f09b288

Please sign in to comment.