Skip to content

Commit 4aafcb3

Browse files
authored
Merge pull request #46 from PHPCSStandards/feature/generic-incrementdecrementspacing-handle-more-cases
Generic/IncrementDecrementSpacing: handle more situations
2 parents ac6abf6 + ad5421c commit 4aafcb3

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ The file documents changes to the PHP_CodeSniffer project.
8080
- Generic.PHP.RequireStrictTypes has a new warning for when there is a declare statement, but the strict_types directive is set to 0
8181
- The warning can be turned off by excluding the Generic.PHP.RequireStrictTypes.Disabled error code
8282
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
83+
- Generic.WhiteSpace.IncrementDecrementSpacing now detects more spacing issues
84+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
8385
- PSR2.Classes.PropertyDeclaration now enforces that the readonly modifier comes after the visibility modifier
8486
- PSR2 and PSR12 do not have documented rules for this as they pre-date the readonly modifier
8587
- PSR-PER has been used to confirm the order of this keyword so it can be applied to PSR2 and PSR12 correctly

src/Standards/Generic/Sniffs/WhiteSpace/IncrementDecrementSpacingSniff.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function process(File $phpcsFile, $stackPtr)
6363
// Is this a pre-increment/decrement ?
6464
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
6565
if ($nextNonEmpty !== false
66-
&& (($phpcsFile->tokenizerType === 'PHP' && $tokens[$nextNonEmpty]['code'] === T_VARIABLE)
66+
&& (($phpcsFile->tokenizerType === 'PHP'
67+
&& ($tokens[$nextNonEmpty]['code'] === T_VARIABLE || $tokens[$nextNonEmpty]['code'] === T_STRING))
6768
|| ($phpcsFile->tokenizerType === 'JS' && $tokens[$nextNonEmpty]['code'] === T_STRING))
6869
) {
6970
if ($nextNonEmpty === ($stackPtr + 1)) {
@@ -116,7 +117,10 @@ public function process(File $phpcsFile, $stackPtr)
116117
// Is this a post-increment/decrement ?
117118
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
118119
if ($prevNonEmpty !== false
119-
&& (($phpcsFile->tokenizerType === 'PHP' && $tokens[$prevNonEmpty]['code'] === T_VARIABLE)
120+
&& (($phpcsFile->tokenizerType === 'PHP'
121+
&& ($tokens[$prevNonEmpty]['code'] === T_VARIABLE
122+
|| $tokens[$prevNonEmpty]['code'] === T_STRING
123+
|| $tokens[$prevNonEmpty]['code'] === T_CLOSE_SQUARE_BRACKET))
120124
|| ($phpcsFile->tokenizerType === 'JS' && $tokens[$prevNonEmpty]['code'] === T_STRING))
121125
) {
122126
if ($prevNonEmpty === ($stackPtr - 1)) {

src/Standards/Generic/Tests/WhiteSpace/IncrementDecrementSpacingUnitTest.inc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,23 @@ $i /*comment*/ --;
1515
$i++;
1616
$i ++;
1717
$i /*comment*/ ++;
18+
19+
// Handle properties and array access too.
20+
$i['key']++;
21+
$i['key'] ++;
22+
$i['key']['id']++;
23+
$i['key']['id'] ++;
24+
25+
$obj->prop++;
26+
$obj->prop ++;
27+
$obj?->prop ++;
28+
29+
$obj->obj->prop++;
30+
$obj->obj->prop ++;
31+
$obj?->obj->prop ++;
32+
33+
$obj->prop['key']++;
34+
$obj->prop['key'] ++;
35+
36+
--ClassName::$prop;
37+
-- ClassName::$prop;

src/Standards/Generic/Tests/WhiteSpace/IncrementDecrementSpacingUnitTest.inc.fixed

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,23 @@ $i /*comment*/ --;
1414
$i++;
1515
$i++;
1616
$i /*comment*/ ++;
17+
18+
// Handle properties and array access too.
19+
$i['key']++;
20+
$i['key']++;
21+
$i['key']['id']++;
22+
$i['key']['id']++;
23+
24+
$obj->prop++;
25+
$obj->prop++;
26+
$obj?->prop++;
27+
28+
$obj->obj->prop++;
29+
$obj->obj->prop++;
30+
$obj?->obj->prop++;
31+
32+
$obj->prop['key']++;
33+
$obj->prop['key']++;
34+
35+
--ClassName::$prop;
36+
--ClassName::$prop;

src/Standards/Generic/Tests/WhiteSpace/IncrementDecrementSpacingUnitTest.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,32 @@ class IncrementDecrementSpacingUnitTest extends AbstractSniffUnitTest
2727
*/
2828
public function getErrorList($testFile='IncrementDecrementSpacingUnitTest.inc')
2929
{
30+
$errors = [
31+
5 => 1,
32+
6 => 1,
33+
8 => 1,
34+
10 => 1,
35+
13 => 1,
36+
14 => 1,
37+
16 => 1,
38+
17 => 1,
39+
];
40+
3041
switch ($testFile) {
3142
case 'IncrementDecrementSpacingUnitTest.inc':
43+
$errors[21] = 1;
44+
$errors[23] = 1;
45+
$errors[26] = 1;
46+
$errors[27] = 1;
47+
$errors[30] = 1;
48+
$errors[31] = 1;
49+
$errors[34] = 1;
50+
$errors[37] = 1;
51+
52+
return $errors;
53+
3254
case 'IncrementDecrementSpacingUnitTest.js':
33-
return [
34-
5 => 1,
35-
6 => 1,
36-
8 => 1,
37-
10 => 1,
38-
13 => 1,
39-
14 => 1,
40-
16 => 1,
41-
17 => 1,
42-
];
55+
return $errors;
4356

4457
default:
4558
return [];

0 commit comments

Comments
 (0)