Skip to content

Commit 7b42157

Browse files
Merge pull request #93 from VincentLanglet/psr12
Psr12
2 parents 7f763c5 + 2b11310 commit 7b42157

20 files changed

+147
-542
lines changed

SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use PHP_CodeSniffer\Util\Tokens;
10+
use SymfonyCustom\Sniffs\FixerHelper;
1011

1112
/**
1213
* A test to ensure that arrays conform to the array coding standard.
@@ -74,12 +75,7 @@ public function process(File $phpcsFile, $stackPtr): void
7475
);
7576

7677
if ($fix) {
77-
$phpcsFile->fixer->beginChangeset();
78-
for ($i = $stackPtr + 1; $i < $arrayStart; $i++) {
79-
$phpcsFile->fixer->replaceToken($i, '');
80-
}
81-
82-
$phpcsFile->fixer->endChangeset();
78+
FixerHelper::removeAll($phpcsFile, $stackPtr + 1, $arrayStart);
8379
}
8480
}
8581
} else {
@@ -100,12 +96,7 @@ public function process(File $phpcsFile, $stackPtr): void
10096
);
10197

10298
if ($fix) {
103-
$phpcsFile->fixer->beginChangeset();
104-
for ($i = $arrayStart + 1; $i < $arrayEnd; $i++) {
105-
$phpcsFile->fixer->replaceToken($i, '');
106-
}
107-
108-
$phpcsFile->fixer->endChangeset();
99+
FixerHelper::removeAll($phpcsFile, $arrayStart + 1, $arrayEnd);
109100
}
110101
}
111102

@@ -310,11 +301,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
310301
[$currentIndent, $tokens[$end]['column'] - 1]
311302
);
312303
if ($fix) {
313-
if (0 === $found) {
314-
$phpcsFile->fixer->addContent($end - 1, str_repeat(' ', $expected));
315-
} else {
316-
$phpcsFile->fixer->replaceToken($end - 1, str_repeat(' ', $expected));
317-
}
304+
FixerHelper::fixWhitespaceBefore($phpcsFile, $end, $expected, $found);
318305
}
319306
}
320307

@@ -542,11 +529,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
542529
);
543530

544531
if ($fix) {
545-
if (0 === $found) {
546-
$phpcsFile->fixer->addContent($value['value'] - 1, str_repeat(' ', $expected));
547-
} else {
548-
$phpcsFile->fixer->replaceToken($value['value'] - 1, str_repeat(' ', $expected));
549-
}
532+
FixerHelper::fixWhitespaceBefore($phpcsFile, $value['value'], $expected, $found);
550533
}
551534
}
552535
}
@@ -651,7 +634,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
651634
);
652635

653636
if ($fix) {
654-
$this->align($phpcsFile, $index['index'], $expected, $found);
637+
FixerHelper::fixWhitespaceBefore($phpcsFile, $index['index'], $expected, $found);
655638
}
656639

657640
continue;
@@ -675,7 +658,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
675658
);
676659

677660
if ($fix) {
678-
$this->align($phpcsFile, $index['arrow'], $expected, $found);
661+
FixerHelper::fixWhitespaceBefore($phpcsFile, $index['arrow'], $expected, $found);
679662
}
680663

681664
continue;
@@ -698,34 +681,9 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
698681
);
699682

700683
if ($fix) {
701-
$this->align($phpcsFile, $index['value'], $expected, $found);
684+
FixerHelper::fixWhitespaceBefore($phpcsFile, $index['value'], $expected, $found);
702685
}
703686
}
704687
}
705688
}
706-
707-
/**
708-
* @param File $phpcsFile
709-
* @param int $elementIndex
710-
* @param int $expected
711-
* @param int|string $found
712-
*/
713-
private function align(File $phpcsFile, int $elementIndex, int $expected, $found): void
714-
{
715-
if ('newline' === $found) {
716-
$phpcsFile->fixer->beginChangeset();
717-
718-
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $elementIndex - 1, null, true);
719-
for ($i = $prev + 1; $i < $elementIndex; $i++) {
720-
$phpcsFile->fixer->replaceToken($i, '');
721-
}
722-
723-
$phpcsFile->fixer->replaceToken($elementIndex - 1, str_repeat(' ', $expected));
724-
$phpcsFile->fixer->endChangeset();
725-
} elseif (0 === $found) {
726-
$phpcsFile->fixer->addContent($elementIndex - 1, str_repeat(' ', $expected));
727-
} else {
728-
$phpcsFile->fixer->replaceToken($elementIndex - 1, str_repeat(' ', $expected));
729-
}
730-
}
731689
}

SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function process(File $phpcsFile, $stackPtr): void
3636
$phpcsFile->addError(
3737
'The %s annotation is forbidden to use',
3838
$stackPtr,
39-
'',
39+
'Invalid',
4040
[$tokens[$stackPtr]['content']]
4141
);
4242
}

SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use SymfonyCustom\Sniffs\FixerHelper;
910
use SymfonyCustom\Sniffs\SniffHelper;
1011

1112
/**
@@ -70,7 +71,12 @@ public function process(File $phpcsFile, $stackPtr): void
7071
);
7172

7273
if ($fix) {
73-
$this->removeLines($phpcsFile, $previousElement, $previousLine + 1, $commentTagLine - 1);
74+
FixerHelper::removeLines(
75+
$phpcsFile,
76+
$previousElement,
77+
$previousLine + 1,
78+
$commentTagLine
79+
);
7480
}
7581
}
7682
} elseif ($currentIsCustom && $previousIsCustom) {
@@ -82,7 +88,12 @@ public function process(File $phpcsFile, $stackPtr): void
8288
);
8389

8490
if ($fix) {
85-
$this->removeLines($phpcsFile, $previousElement, $previousLine + 1, $commentTagLine - 1);
91+
FixerHelper::removeLines(
92+
$phpcsFile,
93+
$previousElement,
94+
$previousLine + 1,
95+
$commentTagLine
96+
);
8697
}
8798
}
8899
} elseif (!$currentIsCustom && !$isNewType) {
@@ -106,7 +117,12 @@ public function process(File $phpcsFile, $stackPtr): void
106117

107118
$phpcsFile->fixer->addContentBefore($firstOnLine, $content.$phpcsFile->eolChar);
108119
} else {
109-
$this->removeLines($phpcsFile, $previousElement, $previousLine + 2, $commentTagLine - 1);
120+
FixerHelper::removeLines(
121+
$phpcsFile,
122+
$previousElement,
123+
$previousLine + 2,
124+
$commentTagLine
125+
);
110126
}
111127
}
112128
}
@@ -119,29 +135,4 @@ public function process(File $phpcsFile, $stackPtr): void
119135
}
120136
}
121137
}
122-
123-
/**
124-
* @param File $phpcsFile
125-
* @param int $fromPtr
126-
* @param int $fromLine
127-
* @param int $toLine
128-
*/
129-
private function removeLines(File $phpcsFile, int $fromPtr, int $fromLine, int $toLine): void
130-
{
131-
$tokens = $phpcsFile->getTokens();
132-
133-
$phpcsFile->fixer->beginChangeset();
134-
135-
for ($i = $fromPtr;; $i++) {
136-
if ($tokens[$i]['line'] > $toLine) {
137-
break;
138-
}
139-
140-
if ($fromLine <= $tokens[$i]['line']) {
141-
$phpcsFile->fixer->replaceToken($i, '');
142-
}
143-
}
144-
145-
$phpcsFile->fixer->endChangeset();
146-
}
147138
}

SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use SymfonyCustom\Sniffs\FixerHelper;
910

1011
/**
1112
* Ensures doc blocks follow basic formatting.
@@ -38,10 +39,7 @@ public function process(File $phpcsFile, $stackPtr): void
3839

3940
$commentEnd = $tokens[$stackPtr]['comment_closer'];
4041

41-
$empty = [
42-
T_DOC_COMMENT_WHITESPACE,
43-
T_DOC_COMMENT_STAR,
44-
];
42+
$empty = [T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR];
4543

4644
$short = $phpcsFile->findNext($empty, $stackPtr + 1, $commentEnd, true);
4745
if (false === $short) {
@@ -114,16 +112,7 @@ public function process(File $phpcsFile, $stackPtr): void
114112
);
115113

116114
if ($fix) {
117-
$phpcsFile->fixer->beginChangeset();
118-
for ($i = $stackPtr + 1; $i < $short; $i++) {
119-
if ($tokens[$i + 1]['line'] === $tokens[$short]['line']) {
120-
break;
121-
}
122-
123-
$phpcsFile->fixer->replaceToken($i, '');
124-
}
125-
126-
$phpcsFile->fixer->endChangeset();
115+
FixerHelper::removeAll($phpcsFile, $stackPtr + 1, $short);
127116
}
128117
}
129118

@@ -163,16 +152,7 @@ public function process(File $phpcsFile, $stackPtr): void
163152
);
164153

165154
if ($fix) {
166-
$phpcsFile->fixer->beginChangeset();
167-
for ($i = $prev + 1; $i < $commentEnd; $i++) {
168-
if ($tokens[$i + 1]['line'] === $tokens[$commentEnd]['line']) {
169-
break;
170-
}
171-
172-
$phpcsFile->fixer->replaceToken($i, '');
173-
}
174-
175-
$phpcsFile->fixer->endChangeset();
155+
FixerHelper::removeAll($phpcsFile, $prev + 1, $commentEnd);
176156
}
177157
}
178158
}

SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ public function process(File $phpcsFile, $stackPtr): void
100100
}
101101
}
102102
}
103-
104-
$this->processWhitespace($phpcsFile, $commentStart);
105103
}
106104

107105
/**
@@ -215,58 +213,6 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart): voi
215213
parent::processParams($phpcsFile, $stackPtr, $commentStart);
216214
}
217215

218-
/**
219-
* @param File $phpcsFile
220-
* @param int $commentStart
221-
* @param bool $hasComment
222-
*/
223-
private function processWhitespace(File $phpcsFile, int $commentStart, bool $hasComment = true): void
224-
{
225-
$tokens = $phpcsFile->getTokens();
226-
$before = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true);
227-
228-
$startLine = $tokens[$commentStart]['line'];
229-
$prevLine = $tokens[$before]['line'];
230-
231-
$found = $startLine - $prevLine - 1;
232-
233-
// Skip for class opening
234-
if ($found < 1 && T_OPEN_CURLY_BRACKET !== $tokens[$before]['code']) {
235-
if ($found < 0) {
236-
$found = 0;
237-
}
238-
239-
if ($hasComment) {
240-
$error = 'Expected 1 blank line before docblock; %s found';
241-
$rule = 'SpacingBeforeDocblock';
242-
} else {
243-
$error = 'Expected 1 blank line before function; %s found';
244-
$rule = 'SpacingBeforeFunction';
245-
}
246-
247-
$fix = $phpcsFile->addFixableError($error, $commentStart, $rule, [$found]);
248-
249-
if ($fix) {
250-
if ($found > 1) {
251-
$phpcsFile->fixer->beginChangeset();
252-
253-
for ($i = $before + 1; $i < $commentStart - 1; $i++) {
254-
$phpcsFile->fixer->replaceToken($i, '');
255-
}
256-
257-
$phpcsFile->fixer->endChangeset();
258-
} else {
259-
// Try and maintain indentation.
260-
if (T_WHITESPACE === $tokens[$commentStart - 1]['code']) {
261-
$phpcsFile->fixer->addNewlineBefore($commentStart - 1);
262-
} else {
263-
$phpcsFile->fixer->addNewlineBefore($commentStart);
264-
}
265-
}
266-
}
267-
}
268-
}
269-
270216
/**
271217
* @param File $phpcsFile
272218
* @param int $stackPtr

SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ public function processMemberVar(File $phpcsFile, $stackPtr): void
110110
$phpcsFile->fixer->replaceToken($string, implode(' ', $newContent));
111111
}
112112
}
113-
114-
$this->processWhitespace($phpcsFile, $commentStart);
115113
}
116114

117115
/**
@@ -129,52 +127,4 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
129127
protected function processVariableInString(File $phpcsFile, $stackPtr): void
130128
{
131129
}
132-
133-
/**
134-
* @param File $phpcsFile
135-
* @param int $commentStart
136-
*/
137-
private function processWhitespace(File $phpcsFile, int $commentStart): void
138-
{
139-
$tokens = $phpcsFile->getTokens();
140-
$before = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true);
141-
142-
$startLine = $tokens[$commentStart]['line'];
143-
$prevLine = $tokens[$before]['line'];
144-
145-
$found = $startLine - $prevLine - 1;
146-
147-
// Skip for class opening
148-
if ($found < 1 && T_OPEN_CURLY_BRACKET !== $tokens[$before]['code']) {
149-
if ($found < 0) {
150-
$found = 0;
151-
}
152-
153-
$fix = $phpcsFile->addFixableError(
154-
'Expected 1 blank line before docblock; %s found',
155-
$commentStart,
156-
'SpacingBeforeDocblock',
157-
[$found]
158-
);
159-
160-
if ($fix) {
161-
if ($found > 1) {
162-
$phpcsFile->fixer->beginChangeset();
163-
164-
for ($i = $before + 1; $i < $commentStart - 1; $i++) {
165-
$phpcsFile->fixer->replaceToken($i, '');
166-
}
167-
168-
$phpcsFile->fixer->endChangeset();
169-
} else {
170-
// Try and maintain indentation.
171-
if (T_WHITESPACE === $tokens[$commentStart - 1]['code']) {
172-
$phpcsFile->fixer->addNewlineBefore($commentStart - 1);
173-
} else {
174-
$phpcsFile->fixer->addNewlineBefore($commentStart);
175-
}
176-
}
177-
}
178-
}
179-
}
180130
}

0 commit comments

Comments
 (0)