Skip to content

Commit

Permalink
Option usePathConstantsAsConstantString
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMystikJonas committed Dec 5, 2022
1 parent ec31e41 commit aa7bb92
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
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
11 changes: 9 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,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());
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

0 comments on commit aa7bb92

Please sign in to comment.