Skip to content

Commit 17543ce

Browse files
Merge pull request #94 from VincentLanglet/refacto
Refacto
2 parents 7b42157 + 51dbba4 commit 17543ce

38 files changed

+118
-96
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/
22
composer.phar
3+
coverage/
34
vendor/

SymfonyCustom/Sniffs/FixerHelper.php renamed to SymfonyCustom/Helpers/FixerHelper.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace SymfonyCustom\Sniffs;
5+
namespace SymfonyCustom\Helpers;
66

77
use PHP_CodeSniffer\Files\File;
88

@@ -58,6 +58,37 @@ public static function removeLines(File $phpcsFile, int $fromPtr, int $fromLine,
5858
$phpcsFile->fixer->endChangeset();
5959
}
6060

61+
/**
62+
* @param File $phpcsFile
63+
* @param int $stackPtr
64+
* @param int $expected
65+
* @param int|string $found
66+
*/
67+
public static function fixWhitespaceAfter(
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, str_repeat(' ', $expected));
77+
} else {
78+
if ('newline' === $found) {
79+
$next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr, null, true);
80+
81+
for ($i = $stackPtr + 1; $i < $next; $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+
6192
/**
6293
* @param File $phpcsFile
6394
* @param int $stackPtr

SymfonyCustom/Sniffs/SniffHelper.php renamed to SymfonyCustom/Helpers/SniffHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace SymfonyCustom\Sniffs;
5+
namespace SymfonyCustom\Helpers;
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Util\Tokens;

SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +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;
10+
use SymfonyCustom\Helpers\FixerHelper;
1111

1212
/**
1313
* A test to ensure that arrays conform to the array coding standard.
@@ -158,56 +158,40 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star
158158
$nextArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $start + 1, $end);
159159
while (false !== $nextArrow) {
160160
if (T_WHITESPACE !== $tokens[$nextArrow - 1]['code']) {
161+
$spaceBefore = 0;
162+
} else {
163+
$spaceBefore = $tokens[$nextArrow - 1]['length'];
164+
}
165+
166+
if (1 !== $spaceBefore) {
161167
$fix = $phpcsFile->addFixableError(
162-
'Expected 1 space between "%s" and double arrow; 0 found',
168+
'Expected 1 space between "%s" and double arrow; %s found',
163169
$nextArrow,
164-
'NoSpaceBeforeDoubleArrow',
165-
[$tokens[$nextArrow - 1]['content']]
170+
'SpaceAfterDoubleArrow',
171+
[$tokens[$nextArrow - 1]['content'], $spaceBefore]
166172
);
167173

168174
if ($fix) {
169-
$phpcsFile->fixer->addContentBefore($nextArrow, ' ');
170-
}
171-
} else {
172-
$spaceLength = $tokens[$nextArrow - 1]['length'];
173-
if (1 !== $spaceLength) {
174-
$fix = $phpcsFile->addFixableError(
175-
'Expected 1 space between "%s" and double arrow; %s found',
176-
$nextArrow,
177-
'SpaceBeforeDoubleArrow',
178-
[$tokens[$nextArrow - 2]['content'], $spaceLength]
179-
);
180-
181-
if ($fix) {
182-
$phpcsFile->fixer->replaceToken($nextArrow - 1, ' ');
183-
}
175+
FixerHelper::fixWhitespaceBefore($phpcsFile, $nextArrow, 1, $spaceBefore);
184176
}
185177
}
186178

187179
if (T_WHITESPACE !== $tokens[$nextArrow + 1]['code']) {
180+
$spaceAfter = 0;
181+
} else {
182+
$spaceAfter = $tokens[$nextArrow + 1]['length'];
183+
}
184+
185+
if (1 !== $spaceAfter) {
188186
$fix = $phpcsFile->addFixableError(
189-
'Expected 1 space between double arrow and "%s"; 0 found',
187+
'Expected 1 space between double arrow and "%s"; %s found',
190188
$nextArrow,
191-
'NoSpaceAfterDoubleArrow',
192-
[$tokens[$nextArrow + 1]['content']]
189+
'SpaceAfterDoubleArrow',
190+
[$tokens[$nextArrow + 1]['content'], $spaceAfter]
193191
);
194192

195193
if ($fix) {
196-
$phpcsFile->fixer->addContent($nextArrow, ' ');
197-
}
198-
} else {
199-
$spaceLength = $tokens[$nextArrow + 1]['length'];
200-
if (1 !== $spaceLength) {
201-
$fix = $phpcsFile->addFixableError(
202-
'Expected 1 space between double arrow and "%s"; %s found',
203-
$nextArrow,
204-
'SpaceAfterDoubleArrow',
205-
[$tokens[$nextArrow + 2]['content'], $spaceLength]
206-
);
207-
208-
if ($fix) {
209-
$phpcsFile->fixer->replaceToken($nextArrow + 1, ' ');
210-
}
194+
FixerHelper::fixWhitespaceAfter($phpcsFile, $nextArrow, 1, $spaceAfter);
211195
}
212196
}
213197

@@ -218,29 +202,21 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star
218202
// We have a multiple value array
219203
foreach ($commas as $comma) {
220204
if (T_WHITESPACE !== $tokens[$comma + 1]['code']) {
205+
$spaceAfter = 0;
206+
} else {
207+
$spaceAfter = $tokens[$comma + 1]['length'];
208+
}
209+
210+
if (1 !== $spaceAfter) {
221211
$fix = $phpcsFile->addFixableError(
222-
'Expected 1 space between comma and "%s"; 0 found',
212+
'Expected 1 space between comma and "%s"; %s found',
223213
$comma,
224-
'NoSpaceAfterComma',
225-
[$tokens[$comma + 1]['content']]
214+
'SpaceAfterComma',
215+
[$tokens[$comma + 1]['content'], $spaceAfter]
226216
);
227217

228218
if ($fix) {
229-
$phpcsFile->fixer->addContent($comma, ' ');
230-
}
231-
} else {
232-
$spaceLength = $tokens[$comma + 1]['length'];
233-
if (1 !== $spaceLength) {
234-
$fix = $phpcsFile->addFixableError(
235-
'Expected 1 space between comma and "%s"; %s found',
236-
$comma,
237-
'SpaceAfterComma',
238-
[$tokens[$comma + 2]['content'], $spaceLength]
239-
);
240-
241-
if ($fix) {
242-
$phpcsFile->fixer->replaceToken($comma + 1, ' ');
243-
}
219+
FixerHelper::fixWhitespaceAfter($phpcsFile, $comma, 1, $spaceAfter);
244220
}
245221
}
246222

SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
9-
use SymfonyCustom\Sniffs\FixerHelper;
10-
use SymfonyCustom\Sniffs\SniffHelper;
9+
use SymfonyCustom\Helpers\FixerHelper;
10+
use SymfonyCustom\Helpers\SniffHelper;
1111

1212
/**
1313
* Throws errors if comments are not grouped by type with one blank line between them.

SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
9-
use SymfonyCustom\Sniffs\FixerHelper;
9+
use SymfonyCustom\Helpers\FixerHelper;
1010

1111
/**
1212
* Ensures doc blocks follow basic formatting.

SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use PHP_CodeSniffer\Util\Tokens;
10-
use SymfonyCustom\Sniffs\SniffHelper;
10+
use SymfonyCustom\Helpers\SniffHelper;
1111

1212
/**
1313
* Class AlphabeticallySortedUseSniff

SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use PHP_CodeSniffer\Util\Tokens;
10-
use SymfonyCustom\Sniffs\SniffHelper;
10+
use SymfonyCustom\Helpers\SniffHelper;
1111

1212
/**
1313
* Checks for "use" statements that are not needed in a file.

SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
9-
use SymfonyCustom\Sniffs\SniffHelper;
9+
use SymfonyCustom\Helpers\SniffHelper;
1010

1111
/**
1212
* Checks that there are not 2 empty lines following each other.

SymfonyCustom/Tests/Arrays/ArrayDeclarationUnitTest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,5 @@ return [
147147
1 =>
148148
2,
149149
];
150+
151+
[1 => 2 , 2=>3,4 => 5];

0 commit comments

Comments
 (0)