1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of PHP CS Fixer. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* Dariusz Rumiński <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This source file is subject to the MIT license that is bundled |
10
|
|
|
* with this source code in the file LICENSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Addiks\MorePhpCsFixers\Whitespace; |
14
|
|
|
|
15
|
|
|
use PhpCsFixer\AbstractFixer; |
16
|
|
|
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface; |
17
|
|
|
use PhpCsFixer\FixerDefinition\CodeSample; |
18
|
|
|
use PhpCsFixer\FixerDefinition\FixerDefinition; |
19
|
|
|
use PhpCsFixer\Tokenizer\Token; |
20
|
|
|
use PhpCsFixer\Tokenizer\Tokens; |
21
|
|
|
use PhpCsFixer\WhitespacesFixerConfig; |
22
|
|
|
|
23
|
|
View Code Duplication |
final class BlankLineBeforeCatchBlockFixer extends AbstractFixer implements WhitespacesAwareFixerInterface |
|
|
|
|
24
|
|
|
{ |
25
|
7 |
|
public function __construct() |
26
|
|
|
{ |
27
|
7 |
|
parent::__construct(); |
28
|
|
|
|
29
|
|
|
# Fixes psalm: PropertyNotSetInConstructor |
30
|
7 |
|
$this->whitespacesConfig = new WhitespacesFixerConfig(); |
31
|
7 |
|
} |
32
|
|
|
|
33
|
|
|
public function getName() |
34
|
|
|
{ |
35
|
|
|
return 'Addiks/blank_line_before_catch_block'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
1 |
|
public function getDefinition() |
42
|
|
|
{ |
43
|
1 |
|
return new FixerDefinition( |
44
|
1 |
|
'An empty line feed must precede any catch or finally codeblock.', |
45
|
|
|
[ |
46
|
1 |
|
new CodeSample( |
47
|
1 |
|
'<?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
|
1 |
|
public function getPriority() |
67
|
|
|
{ |
68
|
|
|
// should run after no_blank_lines_after_phpdoc |
69
|
1 |
|
return -26; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
5 |
|
public function isCandidate(Tokens $tokens) |
76
|
|
|
{ |
77
|
5 |
|
return $tokens->isTokenKindFound(T_CATCH) || $tokens->isTokenKindFound(T_FINALLY); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
5 |
|
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void |
84
|
|
|
{ |
85
|
|
|
/** @var string $lineEnding */ |
86
|
5 |
|
$lineEnding = $this->whitespacesConfig->getLineEnding(); |
87
|
|
|
|
88
|
5 |
|
foreach ($tokens as $index => $token) { |
89
|
|
|
/** @var Token $token */ |
90
|
5 |
|
if ($token->isGivenKind([T_CATCH, T_FINALLY])) { |
91
|
5 |
|
$index = (int)$tokens->getPrevMeaningfulToken($index); |
92
|
|
|
|
93
|
5 |
|
if ($tokens[$index]->equals('}')) { |
94
|
5 |
|
--$index; |
95
|
|
|
|
96
|
5 |
|
while ($tokens[$index]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) { |
97
|
1 |
|
--$index; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** @var Token $whitespace */ |
101
|
5 |
|
$whitespace = $tokens[$index]; |
102
|
|
|
|
103
|
|
|
/** @var int $presentNewlines */ |
104
|
5 |
|
$presentNewlines = substr_count($whitespace->getContent(), $lineEnding); |
105
|
|
|
|
106
|
5 |
|
if ($whitespace->isWhitespace() && $presentNewlines < 2) { |
107
|
5 |
|
$tokens[$index] = $this->convertWhitespaceToken($whitespace); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
5 |
|
} |
113
|
|
|
|
114
|
3 |
|
private function convertWhitespaceToken(Token $whitespace): Token |
115
|
|
|
{ |
116
|
|
|
/** @var string $lineEnding */ |
117
|
3 |
|
$lineEnding = $this->whitespacesConfig->getLineEnding(); |
118
|
|
|
|
119
|
3 |
|
return new Token([ |
120
|
3 |
|
T_WHITESPACE, |
121
|
3 |
|
substr_replace( |
122
|
3 |
|
$whitespace->getContent(), |
123
|
3 |
|
$lineEnding.$lineEnding, |
124
|
3 |
|
strpos($whitespace->getContent(), $lineEnding), |
125
|
3 |
|
1 |
126
|
|
|
), |
127
|
|
|
]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.