| @@ 23-129 (lines=107) @@ | ||
| 20 | use PhpCsFixer\Tokenizer\Tokens; |
|
| 21 | use PhpCsFixer\WhitespacesFixerConfig; |
|
| 22 | ||
| 23 | final class BlankLineBeforeCatchBlockFixer extends AbstractFixer implements WhitespacesAwareFixerInterface |
|
| 24 | { |
|
| 25 | public function __construct() |
|
| 26 | { |
|
| 27 | parent::__construct(); |
|
| 28 | ||
| 29 | # Fixes psalm: PropertyNotSetInConstructor |
|
| 30 | $this->whitespacesConfig = new WhitespacesFixerConfig(); |
|
| 31 | } |
|
| 32 | ||
| 33 | public function getName() |
|
| 34 | { |
|
| 35 | return 'Addiks/blank_line_before_catch_block'; |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * {@inheritdoc} |
|
| 40 | */ |
|
| 41 | public function getDefinition() |
|
| 42 | { |
|
| 43 | return new FixerDefinition( |
|
| 44 | 'An empty line feed must precede any catch or finally codeblock.', |
|
| 45 | [ |
|
| 46 | new CodeSample( |
|
| 47 | '<?php |
|
| 48 | try { |
|
| 49 | foo(); |
|
| 50 | ||
| 51 | } catch (\Exception $b) { |
|
| 52 | bar(); |
|
| 53 | ||
| 54 | } finally { |
|
| 55 | baz(); |
|
| 56 | } |
|
| 57 | ' |
|
| 58 | ), |
|
| 59 | ] |
|
| 60 | ); |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * {@inheritdoc} |
|
| 65 | */ |
|
| 66 | public function getPriority() |
|
| 67 | { |
|
| 68 | // should run after no_blank_lines_after_phpdoc |
|
| 69 | return -26; |
|
| 70 | } |
|
| 71 | ||
| 72 | /** |
|
| 73 | * {@inheritdoc} |
|
| 74 | */ |
|
| 75 | public function isCandidate(Tokens $tokens) |
|
| 76 | { |
|
| 77 | return $tokens->isTokenKindFound(T_CATCH) || $tokens->isTokenKindFound(T_FINALLY); |
|
| 78 | } |
|
| 79 | ||
| 80 | /** |
|
| 81 | * {@inheritdoc} |
|
| 82 | */ |
|
| 83 | protected function applyFix(\SplFileInfo $file, Tokens $tokens): void |
|
| 84 | { |
|
| 85 | /** @var string $lineEnding */ |
|
| 86 | $lineEnding = $this->whitespacesConfig->getLineEnding(); |
|
| 87 | ||
| 88 | foreach ($tokens as $index => $token) { |
|
| 89 | /** @var Token $token */ |
|
| 90 | if ($token->isGivenKind([T_CATCH, T_FINALLY])) { |
|
| 91 | $index = (int)$tokens->getPrevMeaningfulToken($index); |
|
| 92 | ||
| 93 | if ($tokens[$index]->equals('}')) { |
|
| 94 | --$index; |
|
| 95 | ||
| 96 | while ($tokens[$index]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) { |
|
| 97 | --$index; |
|
| 98 | } |
|
| 99 | ||
| 100 | /** @var Token $whitespace */ |
|
| 101 | $whitespace = $tokens[$index]; |
|
| 102 | ||
| 103 | /** @var int $presentNewlines */ |
|
| 104 | $presentNewlines = substr_count($whitespace->getContent(), $lineEnding); |
|
| 105 | ||
| 106 | if ($whitespace->isWhitespace() && $presentNewlines < 2) { |
|
| 107 | $tokens[$index] = $this->convertWhitespaceToken($whitespace); |
|
| 108 | } |
|
| 109 | } |
|
| 110 | } |
|
| 111 | } |
|
| 112 | } |
|
| 113 | ||
| 114 | private function convertWhitespaceToken(Token $whitespace): Token |
|
| 115 | { |
|
| 116 | /** @var string $lineEnding */ |
|
| 117 | $lineEnding = $this->whitespacesConfig->getLineEnding(); |
|
| 118 | ||
| 119 | return new Token([ |
|
| 120 | T_WHITESPACE, |
|
| 121 | substr_replace( |
|
| 122 | $whitespace->getContent(), |
|
| 123 | $lineEnding.$lineEnding, |
|
| 124 | strpos($whitespace->getContent(), $lineEnding), |
|
| 125 | 1 |
|
| 126 | ), |
|
| 127 | ]); |
|
| 128 | } |
|
| 129 | } |
|
| 130 | ||
| @@ 23-130 (lines=108) @@ | ||
| 20 | use PhpCsFixer\Tokenizer\Tokens; |
|
| 21 | use PhpCsFixer\WhitespacesFixerConfig; |
|
| 22 | ||
| 23 | final class BlankLineBeforeElseBlockFixer extends AbstractFixer implements WhitespacesAwareFixerInterface |
|
| 24 | { |
|
| 25 | public function __construct() |
|
| 26 | { |
|
| 27 | parent::__construct(); |
|
| 28 | ||
| 29 | # Fixes psalm: PropertyNotSetInConstructor |
|
| 30 | $this->whitespacesConfig = new WhitespacesFixerConfig(); |
|
| 31 | } |
|
| 32 | ||
| 33 | public function getName() |
|
| 34 | { |
|
| 35 | return 'Addiks/blank_line_before_else_block'; |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * {@inheritdoc} |
|
| 40 | */ |
|
| 41 | public function getDefinition() |
|
| 42 | { |
|
| 43 | return new FixerDefinition( |
|
| 44 | 'An empty line feed must precede any else or elseif codeblock.', |
|
| 45 | [ |
|
| 46 | new CodeSample( |
|
| 47 | '<?php |
|
| 48 | if ($a) { |
|
| 49 | foo(); |
|
| 50 | ||
| 51 | } elseif ($b) { |
|
| 52 | bar(); |
|
| 53 | ||
| 54 | } else { |
|
| 55 | baz(); |
|
| 56 | } |
|
| 57 | ' |
|
| 58 | ), |
|
| 59 | ] |
|
| 60 | ); |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * {@inheritdoc} |
|
| 65 | */ |
|
| 66 | public function getPriority() |
|
| 67 | { |
|
| 68 | // should run after no_blank_lines_after_phpdoc |
|
| 69 | return -26; |
|
| 70 | } |
|
| 71 | ||
| 72 | /** |
|
| 73 | * {@inheritdoc} |
|
| 74 | */ |
|
| 75 | public function isCandidate(Tokens $tokens) |
|
| 76 | { |
|
| 77 | return $tokens->isTokenKindFound(T_ELSEIF) || $tokens->isTokenKindFound(T_ELSE); |
|
| 78 | } |
|
| 79 | ||
| 80 | /** |
|
| 81 | * {@inheritdoc} |
|
| 82 | */ |
|
| 83 | protected function applyFix(\SplFileInfo $file, Tokens $tokens): void |
|
| 84 | { |
|
| 85 | /** @var string $lineEnding */ |
|
| 86 | $lineEnding = $this->whitespacesConfig->getLineEnding(); |
|
| 87 | ||
| 88 | foreach ($tokens as $index => $token) { |
|
| 89 | /** @var Token $token */ |
|
| 90 | ||
| 91 | if ($token->isGivenKind([T_ELSE, T_ELSEIF])) { |
|
| 92 | $index = (int)$tokens->getPrevMeaningfulToken($index); |
|
| 93 | ||
| 94 | if ($tokens[$index]->equals('}')) { |
|
| 95 | --$index; |
|
| 96 | ||
| 97 | while ($tokens[$index]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) { |
|
| 98 | --$index; |
|
| 99 | } |
|
| 100 | ||
| 101 | /** @var Token $whitespace */ |
|
| 102 | $whitespace = $tokens[$index]; |
|
| 103 | ||
| 104 | /** @var int $presentNewlines */ |
|
| 105 | $presentNewlines = substr_count($whitespace->getContent(), $lineEnding); |
|
| 106 | ||
| 107 | if ($whitespace->isWhitespace() && $presentNewlines < 2) { |
|
| 108 | $tokens[$index] = $this->convertWhitespaceToken($whitespace); |
|
| 109 | } |
|
| 110 | } |
|
| 111 | } |
|
| 112 | } |
|
| 113 | } |
|
| 114 | ||
| 115 | private function convertWhitespaceToken(Token $whitespace): Token |
|
| 116 | { |
|
| 117 | /** @var string $lineEnding */ |
|
| 118 | $lineEnding = $this->whitespacesConfig->getLineEnding(); |
|
| 119 | ||
| 120 | return new Token([ |
|
| 121 | T_WHITESPACE, |
|
| 122 | substr_replace( |
|
| 123 | $whitespace->getContent(), |
|
| 124 | $lineEnding.$lineEnding, |
|
| 125 | strpos($whitespace->getContent(), $lineEnding), |
|
| 126 | 1 |
|
| 127 | ), |
|
| 128 | ]); |
|
| 129 | } |
|
| 130 | } |
|
| 131 | ||