-
-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PhpParser] Re-add back get value from __DIR__ and __FILE__ on ValueR…
…esolver (#6756) * [PhpParser] Re-add back get value from __DIR__ and __FILE__ on ValueResolver * Fix * add more use case for inside array
- Loading branch information
1 parent
7231ba8
commit c09dd52
Showing
5 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Issues\GetValueMagicDir\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
strlen(__DIR__); | ||
strlen(__FILE__); | ||
|
||
new \Foo(['bar' => ['foo' => __DIR__ . '/bar']]); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Issues\GetValueMagicDir\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
'tests/Issues/GetValueMagicDir/Fixture'; | ||
'tests/Issues/GetValueMagicDir/Fixture/fixture.php'; | ||
|
||
['bar' => ['foo' => 'tests/Issues/GetValueMagicDir/Fixture/bar']]; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Issues\GetValueMagicDir; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class GetValueMagicDirTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
tests/Issues/GetValueMagicDir/Source/GetValueMagicDirRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Issues\GetValueMagicDir\Source; | ||
|
||
use PhpParser\Builder; | ||
use PhpParser\Node; | ||
use PhpParser\Node\ArrayItem; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\New_; | ||
use PhpParser\Node\Scalar\String_; | ||
use Rector\FileSystem\FilePathHelper; | ||
use Rector\PhpParser\Node\NodeFactory; | ||
use Rector\PhpParser\Node\Value\ValueResolver; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class GetValueMagicDirRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly ValueResolver $valueResolver, | ||
private readonly FilePathHelper $filePathHelper, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('', []); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [FuncCall::class, New_::class]; | ||
} | ||
|
||
public function refactor(Node $node): String_|Array_ | ||
{ | ||
$value = $this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Tests\Issues\GetValueMagicDir\Source\GetValueMagicDirRector; | ||
|
||
return RectorConfig::configure() | ||
->withRules([GetValueMagicDirRector::class]); |