|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace PhpCsFixerCustomFixers\Fixer; |
| 6 | + |
| 7 | +use PhpCsFixer\FixerDefinition\CodeSample; |
| 8 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 9 | +use PhpCsFixer\Tokenizer\Token; |
| 10 | +use PhpCsFixer\Tokenizer\Tokens; |
| 11 | + |
| 12 | +final class NoPhpStormGeneratedCommentFixer extends AbstractFixer |
| 13 | +{ |
| 14 | + public function getDefinition() : FixerDefinition |
| 15 | + { |
| 16 | + return new FixerDefinition( |
| 17 | + 'There should be no comment generated by PhpStorm.', |
| 18 | + [new CodeSample('<?php |
| 19 | +/** |
| 20 | + * Created by PhpStorm. |
| 21 | + * User: root |
| 22 | + * Date: 01.01.70 |
| 23 | + * Time: 12:00 |
| 24 | + */ |
| 25 | +namespace Foo; |
| 26 | +')] |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + public function isCandidate(Tokens $tokens) : bool |
| 31 | + { |
| 32 | + return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]); |
| 33 | + } |
| 34 | + |
| 35 | + public function fix(\SplFileInfo $file, Tokens $tokens) : void |
| 36 | + { |
| 37 | + foreach ($tokens as $index => $token) { |
| 38 | + if (!$tokens[$index]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + if (\preg_match('/\*\h+Created by PhpStorm/i', $token->getContent(), $matches) !== 1) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + $this->removeTrailingHorizontalWhitespaces($tokens, $index - 1); |
| 47 | + |
| 48 | + $this->removeLeadingNewline($tokens, $index + 1); |
| 49 | + |
| 50 | + $tokens->clearTokenAndMergeSurroundingWhitespace($index); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public function getPriority() : int |
| 55 | + { |
| 56 | + return 0; |
| 57 | + } |
| 58 | + |
| 59 | + private function removeTrailingHorizontalWhitespaces(Tokens $tokens, int $index) : void |
| 60 | + { |
| 61 | + if (!$tokens[$index]->isGivenKind(T_WHITESPACE)) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $newContent = \preg_replace('/\h+$/', '', $tokens[$index]->getContent()); |
| 66 | + |
| 67 | + if (empty($newContent)) { |
| 68 | + $tokens->clearAt($index); |
| 69 | + |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if ($newContent === $tokens[$index]->getContent()) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $tokens[$index] = new Token([T_WHITESPACE, $newContent]); |
| 78 | + } |
| 79 | + |
| 80 | + private function removeLeadingNewline(Tokens $tokens, int $index) : void |
| 81 | + { |
| 82 | + if (!$tokens[$index]->isGivenKind(T_WHITESPACE)) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + $newContent = \preg_replace('/^\h*\R/', '', $tokens[$index]->getContent()); |
| 87 | + |
| 88 | + if ($newContent === $tokens[$index]->getContent()) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (empty($newContent)) { |
| 93 | + $tokens->clearAt($index); |
| 94 | + |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + $tokens[$index] = new Token([T_WHITESPACE, $newContent]); |
| 99 | + } |
| 100 | +} |
0 commit comments