From e4290155fa631e73e5d4028bf6edf6e68acd663f Mon Sep 17 00:00:00 2001 From: Martin Jonas Date: Mon, 5 Dec 2022 17:02:08 +0100 Subject: [PATCH] Option usePathConstantsAsConstantString --- conf/config.neon | 4 +++ .../InitializerExprTypeResolver.php | 13 +++++-- src/Testing/PHPStanTestCase.php | 2 +- tests/PHPStan/Analyser/PathConstantsTest.php | 35 +++++++++++++++++++ tests/PHPStan/Analyser/data/pathConstants.php | 6 ++++ .../usePathConstantsAsConstantString.neon | 2 ++ 6 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 tests/PHPStan/Analyser/PathConstantsTest.php create mode 100644 tests/PHPStan/Analyser/data/pathConstants.php create mode 100644 tests/PHPStan/Analyser/usePathConstantsAsConstantString.neon diff --git a/conf/config.neon b/conf/config.neon index 56b7965763..0c8996fd77 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 b3d49df16e..17ffbed9a9 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,19 @@ 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(); + } + $stringType = new ConstantStringType($file); + return $this->usePathConstantsAsConstantString ? $stringType : $stringType->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(); + } + $stringType = new ConstantStringType(dirname($file)); + return $this->usePathConstantsAsConstantString ? $stringType : $stringType->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 3177fef05e..2d3b73db09 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 0000000000..c22864f698 --- /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 0000000000..3bf091958f --- /dev/null +++ b/tests/PHPStan/Analyser/data/pathConstants.php @@ -0,0 +1,6 @@ +