Skip to content

Commit f09b288

Browse files
committed
Generalize file and dir magic constants
1 parent 14592dd commit f09b288

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ public function getType(Expr $expr, InitializerExprContext $context): Type
118118
}
119119
if ($expr instanceof File) {
120120
$file = $context->getFile();
121-
return $file !== null ? new ConstantStringType($file) : new StringType();
121+
return $file !== null ? (new ConstantStringType($file))->generalize(GeneralizePrecision::moreSpecific()) : new StringType();
122122
}
123123
if ($expr instanceof Dir) {
124124
$file = $context->getFile();
125-
return $file !== null ? new ConstantStringType(dirname($file)) : new StringType();
125+
return $file !== null ? (new ConstantStringType(dirname($file)))->generalize(GeneralizePrecision::moreSpecific()) : new StringType();
126126
}
127127
if ($expr instanceof Line) {
128128
return new ConstantIntegerType($expr->getLine());

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,11 +2308,11 @@ public function dataBinaryOperations(): array
23082308
'$line',
23092309
],
23102310
[
2311-
(new ConstantStringType(__DIR__ . '/data'))->describe(VerbosityLevel::precise()),
2311+
'literal-string&non-falsy-string',
23122312
'$dir',
23132313
],
23142314
[
2315-
(new ConstantStringType(__DIR__ . '/data/binary.php'))->describe(VerbosityLevel::precise()),
2315+
'literal-string&non-falsy-string',
23162316
'$file',
23172317
],
23182318
[

tests/PHPStan/Reflection/BetterReflection/SourceLocator/OptimizedSingleFileSourceLocatorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Type\VerbosityLevel;
1111
use SingleFileSourceLocatorTestClass;
1212
use TestSingleFileSourceLocator\AFoo;
13-
use function str_replace;
1413
use const PHP_VERSION_ID;
1514

1615
class OptimizedSingleFileSourceLocatorTest extends PHPStanTestCase
@@ -119,7 +118,7 @@ public function dataConst(): array
119118
],
120119
[
121120
'const_with_dir_const',
122-
"'" . str_replace('\\', '/', __DIR__ . '/data') . "'",
121+
'literal-string&non-falsy-string',
123122
],
124123
[
125124
'OPTIMIZED_SFSL_OBJECT_CONSTANT',

0 commit comments

Comments
 (0)