Skip to content

Commit 94afcf8

Browse files
committed
Fix comma spacing sniff.
1 parent 0e35d92 commit 94afcf8

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

PhpCollective/Sniffs/WhiteSpace/CommaSpacingSniff.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use PHP_CodeSniffer\Files\File;
1111
use PHP_CodeSniffer\Sniffs\Sniff;
12+
use PHP_CodeSniffer\Util\Tokens;
1213

1314
/**
1415
* Ensures no whitespaces before and one whitespace after is placed around each comma.
@@ -50,9 +51,30 @@ public function process(File $phpcsFile, $stackPtr): void
5051
}
5152

5253
$error = 'Space before comma, expected none, though';
54+
55+
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, $previous, null, true);
56+
if (!$prevIndex) {
57+
$phpcsFile->addError($error, $next, 'InvalidCommaBefore');
58+
59+
return;
60+
}
61+
5362
$fix = $phpcsFile->addFixableError($error, $next, 'InvalidCommaBefore');
5463
if ($fix) {
55-
$phpcsFile->fixer->replaceToken($previous + 1, '');
64+
$phpcsFile->fixer->beginChangeset();
65+
66+
$content = $tokens[$prevIndex]['content'];
67+
$phpcsFile->fixer->replaceToken($prevIndex, $content . ',');
68+
$phpcsFile->fixer->replaceToken($stackPtr, '');
69+
70+
$nextIndex = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
71+
if ($nextIndex) {
72+
for ($i = $stackPtr + 1; $i < $nextIndex; $i++) {
73+
$phpcsFile->fixer->replaceToken($i, '');
74+
}
75+
}
76+
77+
$phpcsFile->fixer->endChangeset();
5678
}
5779
}
5880
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* MIT License
5+
* For full license information, please view the LICENSE file that was distributed with this source code.
6+
*/
7+
8+
namespace PhpCollective\Test\PhpCollective\Sniffs\WhiteSpace;
9+
10+
use PhpCollective\Sniffs\WhiteSpace\CommaSpacingSniff;
11+
use PhpCollective\Test\TestCase;
12+
13+
class CommaSpacingSniffTest extends TestCase
14+
{
15+
/**
16+
* @return void
17+
*/
18+
public function testDocBlockConstSniffer(): void
19+
{
20+
$this->assertSnifferFindsErrors(new CommaSpacingSniff(), 2);
21+
}
22+
23+
/**
24+
* @return void
25+
*/
26+
public function testDocBlockConstFixer(): void
27+
{
28+
$this->assertSnifferCanFixErrors(new CommaSpacingSniff());
29+
}
30+
}

tests/_data/CommaSpacing/after.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpCollective;
6+
7+
class FixMe
8+
{
9+
public function example(): array
10+
{
11+
return [
12+
'url' => '/',
13+
//, __('Preferences') => array('url' => '/preferences/index')
14+
'Key' => ['value'],
15+
__('Alerts') => ['url' => '/alerts/index'],
16+
];
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpCollective;
6+
7+
class FixMe
8+
{
9+
public function example(): array
10+
{
11+
return [
12+
'url' => '/'
13+
//, __('Preferences') => array('url' => '/preferences/index')
14+
, 'Key' => ['value']
15+
, __('Alerts') => ['url' => '/alerts/index'],
16+
];
17+
}
18+
}

0 commit comments

Comments
 (0)