diff --git a/conf/config.neon b/conf/config.neon index 56b79657630..0c8996fd770 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -107,6 +107,7 @@ parameters: propertyAlwaysReadTags: [] additionalConstructors: [] treatPhpDocTypesAsCertain: true + usePathConstantsAsConstantString: false rememberPossiblyImpureFunctionValues: true tipsOfTheDay: true reportMagicMethods: false @@ -324,6 +325,7 @@ parametersSchema: propertyAlwaysReadTags: listOf(string()) additionalConstructors: listOf(string()) treatPhpDocTypesAsCertain: bool() + usePathConstantsAsConstantString: bool() rememberPossiblyImpureFunctionValues: bool() reportMagicMethods: bool() reportMagicProperties: bool() @@ -800,6 +802,8 @@ services: - class: PHPStan\Reflection\InitializerExprTypeResolver + arguments: + usePathConstantsAsConstantString: %usePathConstantsAsConstantString% - class: PHPStan\Reflection\Annotations\AnnotationsMethodsClassReflectionExtension diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index b3d49df16e1..c5eb0dd60d9 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -81,6 +81,7 @@ public function __construct( private ReflectionProviderProvider $reflectionProviderProvider, private PhpVersion $phpVersion, private OperatorTypeSpecifyingExtensionRegistryProvider $operatorTypeSpecifyingExtensionRegistryProvider, + private bool $usePathConstantsAsConstantString = false, ) { } @@ -120,11 +121,17 @@ public function getType(Expr $expr, InitializerExprContext $context): Type } if ($expr instanceof File) { $file = $context->getFile(); - return $file !== null ? (new ConstantStringType($file))->generalize(GeneralizePrecision::moreSpecific()) : new StringType(); + if ($file === null) { + return new StringType(); + } + return $this->usePathConstantsAsConstantString ? new ConstantStringType($file) : (new ConstantStringType($file))->generalize(GeneralizePrecision::moreSpecific()); } if ($expr instanceof Dir) { $file = $context->getFile(); - return $file !== null ? (new ConstantStringType(dirname($file)))->generalize(GeneralizePrecision::moreSpecific()) : new StringType(); + if ($file === null) { + return new StringType(); + } + return $this->usePathConstantsAsConstantString ? new ConstantStringType(dirname($file)) : (new ConstantStringType(dirname($file)))->generalize(GeneralizePrecision::moreSpecific()); } if ($expr instanceof Line) { return new ConstantIntegerType($expr->getLine()); diff --git a/src/Testing/PHPStanTestCase.php b/src/Testing/PHPStanTestCase.php index 3177fef05e6..2d3b73db091 100644 --- a/src/Testing/PHPStanTestCase.php +++ b/src/Testing/PHPStanTestCase.php @@ -168,7 +168,7 @@ public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeS new DirectInternalScopeFactory( MutatingScope::class, $reflectionProvider, - new InitializerExprTypeResolver($constantResolver, $reflectionProviderProvider, new PhpVersion(PHP_VERSION_ID), $container->getByType(OperatorTypeSpecifyingExtensionRegistryProvider::class)), + new InitializerExprTypeResolver($constantResolver, $reflectionProviderProvider, new PhpVersion(PHP_VERSION_ID), $container->getByType(OperatorTypeSpecifyingExtensionRegistryProvider::class), $container->getParameter('usePathConstantsAsConstantString')), $container->getByType(DynamicReturnTypeExtensionRegistryProvider::class), $container->getByType(ExprPrinter::class), $typeSpecifier, diff --git a/tests/PHPStan/Analyser/PathConstantsTest.php b/tests/PHPStan/Analyser/PathConstantsTest.php new file mode 100644 index 00000000000..c22864f6988 --- /dev/null +++ b/tests/PHPStan/Analyser/PathConstantsTest.php @@ -0,0 +1,35 @@ +gatherAssertTypes(__DIR__ . '/data/pathConstants.php'); + } + + /** + * @dataProvider dataFileAsserts + * @param mixed ...$args + */ + public function testFileAsserts( + string $assertType, + string $file, + ...$args, + ): void + { + $this->assertFileAsserts($assertType, $file, ...$args); + } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/usePathConstantsAsConstantString.neon', + ]; + } + +} diff --git a/tests/PHPStan/Analyser/data/pathConstants.php b/tests/PHPStan/Analyser/data/pathConstants.php new file mode 100644 index 00000000000..3bf091958f2 --- /dev/null +++ b/tests/PHPStan/Analyser/data/pathConstants.php @@ -0,0 +1,6 @@ +