Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option usePathConstantsAsConstantString #2050

Merged
merged 1 commit into from Dec 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions conf/config.neon
Expand Up @@ -107,6 +107,7 @@ parameters:
propertyAlwaysReadTags: []
additionalConstructors: []
treatPhpDocTypesAsCertain: true
usePathConstantsAsConstantString: false
rememberPossiblyImpureFunctionValues: true
tipsOfTheDay: true
reportMagicMethods: false
Expand Down Expand Up @@ -324,6 +325,7 @@ parametersSchema:
propertyAlwaysReadTags: listOf(string())
additionalConstructors: listOf(string())
treatPhpDocTypesAsCertain: bool()
usePathConstantsAsConstantString: bool()
rememberPossiblyImpureFunctionValues: bool()
reportMagicMethods: bool()
reportMagicProperties: bool()
Expand Down Expand Up @@ -800,6 +802,8 @@ services:

-
class: PHPStan\Reflection\InitializerExprTypeResolver
arguments:
usePathConstantsAsConstantString: %usePathConstantsAsConstantString%

-
class: PHPStan\Reflection\Annotations\AnnotationsMethodsClassReflectionExtension
Expand Down
13 changes: 11 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -81,6 +81,7 @@ public function __construct(
private ReflectionProviderProvider $reflectionProviderProvider,
private PhpVersion $phpVersion,
private OperatorTypeSpecifyingExtensionRegistryProvider $operatorTypeSpecifyingExtensionRegistryProvider,
private bool $usePathConstantsAsConstantString = false,
)
{
}
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/PHPStanTestCase.php
Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Analyser/PathConstantsTest.php
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Testing\TypeInferenceTestCase;

class PathConstantsTest extends TypeInferenceTestCase
{

public function dataFileAsserts(): iterable
{
yield from $this->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',
];
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/data/pathConstants.php
@@ -0,0 +1,6 @@
<?php

namespace PathConstantsTest;

\PHPStan\Testing\assertType('\'Analyser/data\'', substr(__DIR__, -13));
\PHPStan\Testing\assertType('\'pathConstants.php\'', substr(__FILE__, -17));
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/usePathConstantsAsConstantString.neon
@@ -0,0 +1,2 @@
parameters:
usePathConstantsAsConstantString: true