|
| 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\ConfigurationException\InvalidFixerConfigurationException; |
| 15 | +use PhpCsFixer\Fixer\AbstractPhpUnitFixer; |
| 16 | +use PhpCsFixer\Fixer\ConfigurableFixerInterface; |
| 17 | +use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface; |
| 18 | +use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; |
| 19 | +use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface; |
| 20 | +use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; |
| 21 | +use PhpCsFixer\FixerDefinition\CodeSample; |
| 22 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 23 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 24 | +use PhpCsFixer\Preg; |
| 25 | +use PhpCsFixer\Tokenizer\Token; |
| 26 | +use PhpCsFixer\Tokenizer\Tokens; |
| 27 | +use PhpCsFixer\WhitespacesFixerConfig; |
| 28 | +use Symfony\Component\OptionsResolver\Options; |
| 29 | + |
| 30 | +/** |
| 31 | + * @implements ConfigurableFixerInterface<_InputConfig, _Config> |
| 32 | + * |
| 33 | + * @phpstan-type _InputConfig array{directory?: string, description?: string} |
| 34 | + * @phpstan-type _Config array{directory: string, description: string} |
| 35 | + */ |
| 36 | +final class PhpdocNoNamedArgumentsTagFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface |
| 37 | +{ |
| 38 | + private string $description = ''; |
| 39 | + private string $directory = ''; |
| 40 | + private WhitespacesFixerConfig $whitespacesConfig; |
| 41 | + |
| 42 | + public function setWhitespacesConfig(WhitespacesFixerConfig $config): void |
| 43 | + { |
| 44 | + $this->whitespacesConfig = $config; |
| 45 | + } |
| 46 | + |
| 47 | + public function getDefinition(): FixerDefinitionInterface |
| 48 | + { |
| 49 | + return new FixerDefinition( |
| 50 | + 'There must be `@no-named-arguments` tag in PHPDoc of a class/enum/interface/trait.', |
| 51 | + [new CodeSample(<<<'PHP' |
| 52 | + <?php |
| 53 | + class Foo |
| 54 | + { |
| 55 | + public function bar(string $s) {} |
| 56 | + } |
| 57 | + |
| 58 | + PHP)], |
| 59 | + '', |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function getConfigurationDefinition(): FixerConfigurationResolverInterface |
| 64 | + { |
| 65 | + $fixerName = $this->getName(); |
| 66 | + |
| 67 | + return new FixerConfigurationResolver([ |
| 68 | + (new FixerOptionBuilder('description', 'description of the tag')) |
| 69 | + ->setAllowedTypes(['string']) |
| 70 | + ->setDefault($this->description) |
| 71 | + ->getOption(), |
| 72 | + (new FixerOptionBuilder('directory', 'directory in which apply the changes, empty value will result with current working directory (result of `getcwd` call)')) |
| 73 | + ->setAllowedTypes(['string']) |
| 74 | + ->setDefault($this->directory) |
| 75 | + ->setNormalizer(static function (Options $options, string $value) use ($fixerName): string { |
| 76 | + if ($value === '') { |
| 77 | + $value = \getcwd(); |
| 78 | + \assert(\is_string($value)); |
| 79 | + } |
| 80 | + |
| 81 | + if (!\is_dir($value)) { |
| 82 | + throw new InvalidFixerConfigurationException($fixerName, \sprintf('The directory "%s" does not exists.', $value)); |
| 83 | + } |
| 84 | + |
| 85 | + $value = realpath($value) . \DIRECTORY_SEPARATOR; |
| 86 | + |
| 87 | + return $value; |
| 88 | + }) |
| 89 | + ->getOption(), |
| 90 | + ]); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param _InputConfig $configuration |
| 95 | + */ |
| 96 | + public function configure(array $configuration): void |
| 97 | + { |
| 98 | + /** @var array{directory: string, description: string} $configuration */ |
| 99 | + $configuration = $this->getConfigurationDefinition()->resolve($configuration); |
| 100 | + |
| 101 | + $this->directory = $configuration['directory']; |
| 102 | + $this->description = $configuration['description']; |
| 103 | + } |
| 104 | + |
| 105 | + public function getPriority(): int |
| 106 | + { |
| 107 | + return 0; |
| 108 | + } |
| 109 | + |
| 110 | + public function isCandidate(Tokens $tokens): bool |
| 111 | + { |
| 112 | + return $tokens->isAnyTokenKindsFound(Token::getClassyTokenKinds()); |
| 113 | + } |
| 114 | + |
| 115 | + public function isRisky(): bool |
| 116 | + { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + public function fix(\SplFileInfo $file, Tokens $tokens): void |
| 121 | + { |
| 122 | + if (!\str_starts_with($file->getRealPath(), $this->directory)) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + for ($index = $tokens->count() - 1; $index > 0; $index--) { |
| 127 | + if (!$tokens[$index]->isClassy()) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + $prevIndex = $tokens->getPrevMeaningfulToken($index); |
| 132 | + if ($tokens[$prevIndex]->isGivenKind(\T_NEW)) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + $this->ensureIsDocBlockWithNoNameArgumentsTag($tokens, $index); |
| 137 | + |
| 138 | + $docBlockIndex = $tokens->getPrevTokenOfKind($index + 2, [[\T_DOC_COMMENT]]); |
| 139 | + \assert(\is_int($docBlockIndex)); |
| 140 | + |
| 141 | + $content = $tokens[$docBlockIndex]->getContent(); |
| 142 | + |
| 143 | + $newContent = Preg::replace('/@no-named-arguments.*(\\R)/', \rtrim('@no-named-arguments ' . $this->description) . '$1', $content); |
| 144 | + |
| 145 | + if ($newContent !== $content) { |
| 146 | + $tokens[$docBlockIndex] = new Token([\T_DOC_COMMENT, $newContent]); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private function ensureIsDocBlockWithNoNameArgumentsTag(Tokens $tokens, int $index): void |
| 152 | + { |
| 153 | + /** @var \Closure(Tokens, int, WhitespacesFixerConfig): void $closure */ |
| 154 | + static $closure; |
| 155 | + |
| 156 | + if ($closure === null) { |
| 157 | + $function = function (Tokens $tokens, int $index, WhitespacesFixerConfig $whitespacesConfig): void { |
| 158 | + $object = new class () extends AbstractPhpUnitFixer implements WhitespacesAwareFixerInterface { |
| 159 | + public function ensureIsDocBlockWithNoNameArgumentsTag(Tokens $tokens, int $index, WhitespacesFixerConfig $whitespacesConfig): void |
| 160 | + { |
| 161 | + $this->setWhitespacesConfig($whitespacesConfig); |
| 162 | + $this->ensureIsDocBlockWithAnnotation($tokens, $index, 'no-named-arguments', ['no-named-arguments'], []); |
| 163 | + } |
| 164 | + |
| 165 | + protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $endIndex): void {} |
| 166 | + |
| 167 | + public function getDefinition(): FixerDefinitionInterface |
| 168 | + { |
| 169 | + throw new \BadMethodCallException('Not implemented'); |
| 170 | + } |
| 171 | + }; |
| 172 | + $object->ensureIsDocBlockWithNoNameArgumentsTag($tokens, $index, $whitespacesConfig); |
| 173 | + }; |
| 174 | + $closure = \Closure::bind($function, null, AbstractPhpUnitFixer::class); |
| 175 | + } |
| 176 | + |
| 177 | + $closure($tokens, $index, $this->whitespacesConfig); |
| 178 | + } |
| 179 | +} |
0 commit comments