Skip to content

Commit a8fe499

Browse files
📦 Refactoring
1 parent 10c3d0f commit a8fe499

File tree

4 files changed

+121
-98
lines changed

4 files changed

+121
-98
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/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: 3 additions & 20 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.
@@ -111,16 +112,7 @@ public function process(File $phpcsFile, $stackPtr): void
111112
);
112113

113114
if ($fix) {
114-
$phpcsFile->fixer->beginChangeset();
115-
for ($i = $stackPtr + 1; $i < $short; $i++) {
116-
if ($tokens[$i + 1]['line'] === $tokens[$short]['line']) {
117-
break;
118-
}
119-
120-
$phpcsFile->fixer->replaceToken($i, '');
121-
}
122-
123-
$phpcsFile->fixer->endChangeset();
115+
FixerHelper::removeAll($phpcsFile, $stackPtr + 1, $short);
124116
}
125117
}
126118

@@ -160,16 +152,7 @@ public function process(File $phpcsFile, $stackPtr): void
160152
);
161153

162154
if ($fix) {
163-
$phpcsFile->fixer->beginChangeset();
164-
for ($i = $prev + 1; $i < $commentEnd; $i++) {
165-
if ($tokens[$i + 1]['line'] === $tokens[$commentEnd]['line']) {
166-
break;
167-
}
168-
169-
$phpcsFile->fixer->replaceToken($i, '');
170-
}
171-
172-
$phpcsFile->fixer->endChangeset();
155+
FixerHelper::removeAll($phpcsFile, $prev + 1, $commentEnd);
173156
}
174157
}
175158
}

SymfonyCustom/Sniffs/FixerHelper.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyCustom\Sniffs;
6+
7+
use PHP_CodeSniffer\Files\File;
8+
9+
/**
10+
* class FixerHelper
11+
*
12+
* @internal
13+
*/
14+
class FixerHelper
15+
{
16+
/**
17+
* @param File $phpcsFile
18+
* @param int $fromPtr
19+
* @param int $toPtr
20+
*/
21+
public static function removeAll(File $phpcsFile, int $fromPtr, int $toPtr): void
22+
{
23+
$tokens = $phpcsFile->getTokens();
24+
25+
$phpcsFile->fixer->beginChangeset();
26+
27+
$i = $fromPtr;
28+
while (isset($tokens[$i]) && $i < $toPtr) {
29+
$phpcsFile->fixer->replaceToken($i, '');
30+
31+
$i++;
32+
}
33+
34+
$phpcsFile->fixer->endChangeset();
35+
}
36+
37+
/**
38+
* @param File $phpcsFile
39+
* @param int $fromPtr
40+
* @param int $fromLine
41+
* @param int $toLine
42+
*/
43+
public static function removeLines(File $phpcsFile, int $fromPtr, int $fromLine, int $toLine): void
44+
{
45+
$tokens = $phpcsFile->getTokens();
46+
47+
$phpcsFile->fixer->beginChangeset();
48+
49+
$i = $fromPtr;
50+
while (isset($tokens[$i]) && $tokens[$i]['line'] < $toLine) {
51+
if ($fromLine <= $tokens[$i]['line']) {
52+
$phpcsFile->fixer->replaceToken($i, '');
53+
}
54+
55+
$i++;
56+
}
57+
58+
$phpcsFile->fixer->endChangeset();
59+
}
60+
61+
/**
62+
* @param File $phpcsFile
63+
* @param int $stackPtr
64+
* @param int $expected
65+
* @param int|string $found
66+
*/
67+
public static function fixWhitespaceBefore(
68+
File $phpcsFile,
69+
int $stackPtr,
70+
int $expected,
71+
$found
72+
): void {
73+
$phpcsFile->fixer->beginChangeset();
74+
75+
if (0 === $found) {
76+
$phpcsFile->fixer->addContent($stackPtr - 1, str_repeat(' ', $expected));
77+
} else {
78+
if ('newline' === $found) {
79+
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
80+
81+
for ($i = $prev + 1; $i < $stackPtr; $i++) {
82+
$phpcsFile->fixer->replaceToken($i, '');
83+
}
84+
}
85+
86+
$phpcsFile->fixer->replaceToken($stackPtr - 1, str_repeat(' ', $expected));
87+
}
88+
89+
$phpcsFile->fixer->endChangeset();
90+
}
91+
}

0 commit comments

Comments
 (0)