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];

SymfonyCustom/Tests/Arrays/ArrayDeclarationUnitTest.inc.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,5 @@ return [
145145
0 => 1,
146146
1 => 2,
147147
];
148+
149+
[1 => 2, 2 => 3, 4 => 5];

SymfonyCustom/Tests/Arrays/ArrayDeclarationUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ protected function getErrorList(): array
5353
107 => 2,
5454
142 => 1,
5555
148 => 1,
56+
151 => 7,
5657
];
5758
}
5859

SymfonyCustom/Tests/NamingConventions/ValidFileNameUnitTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ class ValidFileNameUnitTest extends AbstractSniffUnitTest
2121
protected function getErrorList($filename = '')
2222
{
2323
switch ($filename) {
24-
case 'ValidFileNameUnitTest.inc':
25-
return [];
2624
case 'ValidFileNameUnitTest.Invalid.inc':
27-
return [
28-
1 => 1,
29-
];
25+
return [1 => 1];
3026
default:
3127
return [];
3228
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

build.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
</exec>
4040
</target>
4141

42+
<target name="coverage" depends="symlink-cs" description="Run coverage with PHPUnit">
43+
<exec executable="${phpunit.bin}" failonerror="true" >
44+
<arg value="--verbose"/>
45+
<arg value="--group=SymfonyCustom"/>
46+
<arg value="${phpcs.dir}/tests/AllTests.php"/>
47+
<arg value="--coverage-html=coverage"/>
48+
</exec>
49+
</target>
50+
4251
<target name="phpcs" depends="symlink-cs" description="Find coding standard violations using PHP Code Sniffer">
4352
<exec executable="${phpcs.bin}" failonerror="true">
4453
<arg value="--standard=SymfonyCustom"/>

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828
"phpunit/phpunit": "^7.0"
2929
},
3030
"autoload": {
31-
"psr-4": { "TwigCS\\": "TwigCS/" }
31+
"psr-4": {
32+
"TwigCS\\": "TwigCS/src/"
33+
}
3234
},
3335
"autoload-dev": {
34-
"psr-4": { "TwigCS\\Tests\\": "TwigCS/Tests/" }
36+
"psr-4": {
37+
"TwigCS\\Tests\\": "TwigCS/tests/"
38+
}
3539
}
3640
}

0 commit comments

Comments
 (0)