|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\CodeQuality\Rector\Concat; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\BinaryOp\Concat; |
| 9 | +use PhpParser\Node\Expr\FuncCall; |
| 10 | +use PhpParser\Node\Scalar\MagicConst\Dir; |
| 11 | +use PhpParser\Node\Scalar\String_; |
| 12 | +use Rector\Rector\AbstractRector; |
| 13 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 15 | + |
| 16 | +/** |
| 17 | + * @see \Rector\Tests\CodeQuality\Rector\Concat\DirnameDirConcatStringToDirectStringPathRector\DirnameDirConcatStringToDirectStringPathRectorTest |
| 18 | + */ |
| 19 | +final class DirnameDirConcatStringToDirectStringPathRector extends AbstractRector |
| 20 | +{ |
| 21 | + public function getRuleDefinition(): RuleDefinition |
| 22 | + { |
| 23 | + return new RuleDefinition('Change dirname() and string concat, to __DIR__ and direct string path', [ |
| 24 | + new CodeSample( |
| 25 | + <<<'CODE_SAMPLE' |
| 26 | +class SomeClass |
| 27 | +{ |
| 28 | + public function run() |
| 29 | + { |
| 30 | + $path = dirname(__DIR__) . '/vendor/autoload.php'; |
| 31 | + } |
| 32 | +} |
| 33 | +CODE_SAMPLE |
| 34 | + , |
| 35 | + <<<'CODE_SAMPLE' |
| 36 | +class SomeClass |
| 37 | +{ |
| 38 | + public function run() |
| 39 | + { |
| 40 | + $path = __DIR__ . '/../vendor/autoload.php'; |
| 41 | + } |
| 42 | +} |
| 43 | +CODE_SAMPLE |
| 44 | + ), |
| 45 | + ]); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + public function getNodeTypes(): array |
| 50 | + { |
| 51 | + return [Concat::class]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param Concat $node |
| 56 | + */ |
| 57 | + public function refactor(Node $node): ?Concat |
| 58 | + { |
| 59 | + if (! $node->left instanceof FuncCall || ! $this->isName($node->left, 'dirname')) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + if (! $node->right instanceof String_) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + $dirnameFuncCall = $node->left; |
| 68 | + if ($dirnameFuncCall->isFirstClassCallable()) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + // avoid multiple dir nesting for now |
| 73 | + if (count($dirnameFuncCall->getArgs()) !== 1) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + $firstArg = $dirnameFuncCall->getArgs()[0]; |
| 78 | + if (! $firstArg->value instanceof Dir) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + $string = $node->right; |
| 83 | + if (str_contains($string->value, '/')) { |
| 84 | + // linux paths |
| 85 | + $string->value = '/../' . ltrim($string->value, '/'); |
| 86 | + $node->left = new Dir(); |
| 87 | + return $node; |
| 88 | + } |
| 89 | + |
| 90 | + if (str_contains($string->value, '\\')) { |
| 91 | + // windows paths |
| 92 | + $string->value = '\\..\\' . ltrim($string->value, '\\'); |
| 93 | + $node->left = new Dir(); |
| 94 | + return $node; |
| 95 | + } |
| 96 | + |
| 97 | + return null; |
| 98 | + } |
| 99 | +} |
0 commit comments