From c09dd52a6e6f04e4a7a9ab15d203231050fb16d5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 24 Feb 2025 18:04:33 +0700 Subject: [PATCH] [PhpParser] Re-add back get value from __DIR__ and __FILE__ on ValueResolver (#6756) * [PhpParser] Re-add back get value from __DIR__ and __FILE__ on ValueResolver * Fix * add more use case for inside array --- src/PhpParser/Node/Value/ValueResolver.php | 36 +++++++++++++- .../GetValueMagicDir/Fixture/fixture.php.inc | 33 +++++++++++++ .../GetValueMagicDir/GetValueMagicDirTest.php | 28 +++++++++++ .../Source/GetValueMagicDirRector.php | 49 +++++++++++++++++++ .../config/configured_rule.php | 9 ++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 tests/Issues/GetValueMagicDir/Fixture/fixture.php.inc create mode 100644 tests/Issues/GetValueMagicDir/GetValueMagicDirTest.php create mode 100644 tests/Issues/GetValueMagicDir/Source/GetValueMagicDirRector.php create mode 100644 tests/Issues/GetValueMagicDir/config/configured_rule.php diff --git a/src/PhpParser/Node/Value/ValueResolver.php b/src/PhpParser/Node/Value/ValueResolver.php index c70d676fc40..300d10874ff 100644 --- a/src/PhpParser/Node/Value/ValueResolver.php +++ b/src/PhpParser/Node/Value/ValueResolver.php @@ -15,6 +15,8 @@ use PhpParser\Node\InterpolatedStringPart; use PhpParser\Node\Name; use PhpParser\Node\Scalar\MagicConst\Class_; +use PhpParser\Node\Scalar\MagicConst\Dir; +use PhpParser\Node\Scalar\MagicConst\File; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; @@ -22,6 +24,7 @@ use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\ConstantScalarType; use PHPStan\Type\Type; +use Rector\Application\Provider\CurrentFileProvider; use Rector\Enum\ObjectReference; use Rector\Exception\ShouldNotHappenException; use Rector\NodeAnalyzer\ConstFetchAnalyzer; @@ -47,7 +50,8 @@ public function __construct( private readonly ConstFetchAnalyzer $constFetchAnalyzer, private readonly ReflectionProvider $reflectionProvider, private readonly ReflectionResolver $reflectionResolver, - private readonly ClassReflectionAnalyzer $classReflectionAnalyzer + private readonly ClassReflectionAnalyzer $classReflectionAnalyzer, + private readonly CurrentFileProvider $currentFileProvider, ) { } @@ -186,6 +190,16 @@ private function getConstExprEvaluator(): ConstExprEvaluator } $this->constExprEvaluator = new ConstExprEvaluator(function (Expr $expr) { + if ($expr instanceof Dir) { + // __DIR__ + return $this->resolveDirConstant(); + } + + if ($expr instanceof File) { + // __FILE__ + return $this->resolveFileConstant($expr); + } + // resolve "SomeClass::SOME_CONST" if ($expr instanceof ClassConstFetch && $expr->class instanceof Name) { return $this->resolveClassConstFetch($expr); @@ -200,6 +214,26 @@ private function getConstExprEvaluator(): ConstExprEvaluator return $this->constExprEvaluator; } + private function resolveDirConstant(): string + { + $file = $this->currentFileProvider->getFile(); + if (! $file instanceof \Rector\ValueObject\Application\File) { + throw new ShouldNotHappenException(); + } + + return dirname($file->getFilePath()); + } + + private function resolveFileConstant(File $file): string + { + $file = $this->currentFileProvider->getFile(); + if (! $file instanceof \Rector\ValueObject\Application\File) { + throw new ShouldNotHappenException(); + } + + return $file->getFilePath(); + } + /** * @return mixed[]|null */ diff --git a/tests/Issues/GetValueMagicDir/Fixture/fixture.php.inc b/tests/Issues/GetValueMagicDir/Fixture/fixture.php.inc new file mode 100644 index 00000000000..1e189224851 --- /dev/null +++ b/tests/Issues/GetValueMagicDir/Fixture/fixture.php.inc @@ -0,0 +1,33 @@ + ['foo' => __DIR__ . '/bar']]); + } +} + +?> +----- + ['foo' => 'tests/Issues/GetValueMagicDir/Fixture/bar']]; + } +} + +?> diff --git a/tests/Issues/GetValueMagicDir/GetValueMagicDirTest.php b/tests/Issues/GetValueMagicDir/GetValueMagicDirTest.php new file mode 100644 index 00000000000..adcb498718b --- /dev/null +++ b/tests/Issues/GetValueMagicDir/GetValueMagicDirTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/GetValueMagicDir/Source/GetValueMagicDirRector.php b/tests/Issues/GetValueMagicDir/Source/GetValueMagicDirRector.php new file mode 100644 index 00000000000..2827fe448ed --- /dev/null +++ b/tests/Issues/GetValueMagicDir/Source/GetValueMagicDirRector.php @@ -0,0 +1,49 @@ +valueResolver->getValue($node->args[0]->value); + + if ($node instanceof FuncCall) { + return new String_($this->filePathHelper->relativePath($value)); + } + + $value['bar']['foo'] = $this->filePathHelper->relativePath($value['bar']['foo']); + return $this->nodeFactory->createArray($value); + } +} diff --git a/tests/Issues/GetValueMagicDir/config/configured_rule.php b/tests/Issues/GetValueMagicDir/config/configured_rule.php new file mode 100644 index 00000000000..70c2ddb94b8 --- /dev/null +++ b/tests/Issues/GetValueMagicDir/config/configured_rule.php @@ -0,0 +1,9 @@ +withRules([GetValueMagicDirRector::class]);