|
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
|
|
|
final class BlankLineBeforeDocCommentFixer extends AbstractFixer implements WhitespacesAwareFixerInterface |
|
24
|
|
|
{ |
|
25
|
5 |
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
5 |
|
parent::__construct(); |
|
28
|
|
|
|
|
29
|
|
|
# Fixes psalm: PropertyNotSetInConstructor |
|
30
|
5 |
|
$this->whitespacesConfig = new WhitespacesFixerConfig(); |
|
31
|
5 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getName() |
|
34
|
|
|
{ |
|
35
|
|
|
return 'Addiks/blank_line_before_doccomment'; |
|
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 doc-comment.', |
|
45
|
|
|
[ |
|
46
|
1 |
|
new CodeSample( |
|
47
|
1 |
|
'<?php |
|
48
|
|
|
|
|
49
|
|
|
/** @var string $foo */ |
|
50
|
|
|
$foo = "Lorem ipsum"; |
|
51
|
|
|
|
|
52
|
|
|
/** @var string $bar */ |
|
53
|
|
|
$bar = "dolor sit amet"; |
|
54
|
|
|
' |
|
55
|
|
|
), |
|
56
|
|
|
] |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function getPriority() |
|
64
|
|
|
{ |
|
65
|
|
|
// should run after no_blank_lines_after_phpdoc |
|
66
|
1 |
|
return -26; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function isCandidate(Tokens $tokens) |
|
73
|
|
|
{ |
|
74
|
3 |
|
return $tokens->isTokenKindFound(T_DOC_COMMENT); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
3 |
|
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var string $lineEnding */ |
|
83
|
3 |
|
$lineEnding = $this->whitespacesConfig->getLineEnding(); |
|
84
|
|
|
|
|
85
|
3 |
|
foreach ($tokens as $index => $token) { |
|
86
|
|
|
/** @var Token $token */ |
|
87
|
3 |
|
if ($token->isGivenKind(T_DOC_COMMENT) && $index > 2) { |
|
88
|
3 |
|
--$index; |
|
89
|
|
|
|
|
90
|
|
|
/** @var Token $whitespace */ |
|
91
|
3 |
|
$whitespace = $tokens[$index]; |
|
92
|
|
|
|
|
93
|
|
|
/** @var int $presentNewlines */ |
|
94
|
3 |
|
$presentNewlines = substr_count($whitespace->getContent(), $lineEnding); |
|
95
|
|
|
|
|
96
|
3 |
|
if ($whitespace->isWhitespace() && $presentNewlines < 2) { |
|
97
|
3 |
|
$tokens[$index] = $this->convertWhitespaceToken($whitespace); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
3 |
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
private function convertWhitespaceToken(Token $whitespace): Token |
|
104
|
|
|
{ |
|
105
|
|
|
/** @var string $lineEnding */ |
|
106
|
2 |
|
$lineEnding = $this->whitespacesConfig->getLineEnding(); |
|
107
|
|
|
|
|
108
|
2 |
|
return new Token([ |
|
109
|
2 |
|
T_WHITESPACE, |
|
110
|
2 |
|
substr_replace( |
|
111
|
2 |
|
$whitespace->getContent(), |
|
112
|
2 |
|
$lineEnding.$lineEnding, |
|
113
|
2 |
|
strpos($whitespace->getContent(), $lineEnding), |
|
114
|
2 |
|
1 |
|
115
|
|
|
), |
|
116
|
|
|
]); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|