|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of PHP CS Fixer: custom fixers. |
| 5 | + * |
| 6 | + * (c) 2018 Kuba Werłos |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace PhpCsFixerCustomFixers\Fixer; |
| 13 | + |
| 14 | +use PhpCsFixer\FixerDefinition\CodeSample; |
| 15 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 16 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 17 | +use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer; |
| 18 | +use PhpCsFixer\Tokenizer\Token; |
| 19 | +use PhpCsFixer\Tokenizer\Tokens; |
| 20 | + |
| 21 | +final class NoUselessDirnameCallFixer extends AbstractFixer |
| 22 | +{ |
| 23 | + public function getDefinition(): FixerDefinitionInterface |
| 24 | + { |
| 25 | + return new FixerDefinition( |
| 26 | + 'Function `dirname` call must be removed if not needed.', |
| 27 | + [new CodeSample('<?php |
| 28 | +require dirname(__DIR__) . "/vendor/autoload.php"; |
| 29 | +')] |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Must run before ConcatSpaceFixer. |
| 35 | + */ |
| 36 | + public function getPriority(): int |
| 37 | + { |
| 38 | + return 1; |
| 39 | + } |
| 40 | + |
| 41 | + public function isCandidate(Tokens $tokens): bool |
| 42 | + { |
| 43 | + return $tokens->isTokenKindFound(\T_DIR); |
| 44 | + } |
| 45 | + |
| 46 | + public function isRisky(): bool |
| 47 | + { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + public function fix(\SplFileInfo $file, Tokens $tokens): void |
| 52 | + { |
| 53 | + for ($index = $tokens->count() - 1; $index > 0; $index--) { |
| 54 | + if (!$tokens[$index]->isGivenKind(\T_DIR)) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + $prevInserts = $this->getPrevTokensUpdates($tokens, $index); |
| 59 | + if ($prevInserts === null) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + $nextInserts = $this->getNextTokensUpdates($tokens, $index); |
| 64 | + if ($nextInserts === null) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + foreach ($prevInserts + $nextInserts as $i => $content) { |
| 69 | + if ($content === '') { |
| 70 | + $tokens->clearTokenAndMergeSurroundingWhitespace($i); |
| 71 | + } else { |
| 72 | + $tokens[$i] = new Token([\T_CONSTANT_ENCAPSED_STRING, $content]); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return null|array<int, string> |
| 80 | + */ |
| 81 | + private function getPrevTokensUpdates(Tokens $tokens, int $index): ?array |
| 82 | + { |
| 83 | + $updates = []; |
| 84 | + |
| 85 | + /** @var int $openParenthesisIndex */ |
| 86 | + $openParenthesisIndex = $tokens->getPrevMeaningfulToken($index); |
| 87 | + if (!$tokens[$openParenthesisIndex]->equals('(')) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + $updates[$openParenthesisIndex] = ''; |
| 91 | + |
| 92 | + /** @var int $dirnameCallIndex */ |
| 93 | + $dirnameCallIndex = $tokens->getPrevMeaningfulToken($openParenthesisIndex); |
| 94 | + if (!$tokens[$dirnameCallIndex]->equals([\T_STRING, 'dirname'], false)) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + if (!(new FunctionsAnalyzer())->isGlobalFunctionCall($tokens, $dirnameCallIndex)) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + $updates[$dirnameCallIndex] = ''; |
| 101 | + |
| 102 | + /** @var int $namespaceSeparatorIndex */ |
| 103 | + $namespaceSeparatorIndex = $tokens->getPrevMeaningfulToken($dirnameCallIndex); |
| 104 | + if ($tokens[$namespaceSeparatorIndex]->isGivenKind(\T_NS_SEPARATOR)) { |
| 105 | + $updates[$namespaceSeparatorIndex] = ''; |
| 106 | + } |
| 107 | + |
| 108 | + return $updates; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @return null|array<int, string> |
| 113 | + */ |
| 114 | + private function getNextTokensUpdates(Tokens $tokens, int $index): ?array |
| 115 | + { |
| 116 | + $depthLevel = 1; |
| 117 | + $updates = []; |
| 118 | + |
| 119 | + /** @var int $commaOrClosingParenthesisIndex */ |
| 120 | + $commaOrClosingParenthesisIndex = $tokens->getNextMeaningfulToken($index); |
| 121 | + if ($tokens[$commaOrClosingParenthesisIndex]->equals(',')) { |
| 122 | + $updates[$commaOrClosingParenthesisIndex] = ''; |
| 123 | + /** @var int $afterCommaIndex */ |
| 124 | + $afterCommaIndex = $tokens->getNextMeaningfulToken($commaOrClosingParenthesisIndex); |
| 125 | + if ($tokens[$afterCommaIndex]->isGivenKind(\T_LNUMBER)) { |
| 126 | + $depthLevel = (int) $tokens[$afterCommaIndex]->getContent(); |
| 127 | + $updates[$afterCommaIndex] = ''; |
| 128 | + /** @var int $commaOrClosingParenthesisIndex */ |
| 129 | + $commaOrClosingParenthesisIndex = $tokens->getNextMeaningfulToken($afterCommaIndex); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if ($tokens[$commaOrClosingParenthesisIndex]->equals(',')) { |
| 134 | + $updates[$commaOrClosingParenthesisIndex] = ''; |
| 135 | + /** @var int $commaOrClosingParenthesisIndex */ |
| 136 | + $commaOrClosingParenthesisIndex = $tokens->getNextMeaningfulToken($commaOrClosingParenthesisIndex); |
| 137 | + } |
| 138 | + $closingParenthesisIndex = $commaOrClosingParenthesisIndex; |
| 139 | + |
| 140 | + if (!$tokens[$closingParenthesisIndex]->equals(')')) { |
| 141 | + return null; |
| 142 | + } |
| 143 | + $updates[$closingParenthesisIndex] = ''; |
| 144 | + |
| 145 | + /** @var int $concatenationIndex */ |
| 146 | + $concatenationIndex = $tokens->getNextMeaningfulToken($closingParenthesisIndex); |
| 147 | + if (!$tokens[$concatenationIndex]->equals('.')) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + /** @var int $stringIndex */ |
| 152 | + $stringIndex = $tokens->getNextMeaningfulToken($concatenationIndex); |
| 153 | + if (!$tokens[$stringIndex]->isGivenKind(\T_CONSTANT_ENCAPSED_STRING)) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + $stringContent = $tokens[$stringIndex]->getContent(); |
| 158 | + $updates[$stringIndex] = \substr($stringContent, 0, 1) . \str_repeat('/..', $depthLevel) . \substr($stringContent, 1); |
| 159 | + |
| 160 | + return $updates; |
| 161 | + } |
| 162 | +} |
0 commit comments