Skip to content

Commit 8b61482

Browse files
authored
Merge pull request #150 from PHPCSStandards/feature/squiz-scopekeywordspacing-defensive-coding
Squiz/ScopeKeywordSpacing: add more defensive coding + extra tests
2 parents 88d3cec + 1a046ac commit 8b61482

7 files changed

+62
-30
lines changed

src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ public function process(File $phpcsFile, $stackPtr)
127127

128128
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
129129
$spacing = 0;
130+
} else if (isset($tokens[($stackPtr + 2)]) === false) {
131+
// Parse error/live coding. Bow out.
132+
return;
130133
} else {
131134
if ($tokens[($stackPtr + 2)]['line'] !== $tokens[$stackPtr]['line']) {
132135
$spacing = 'newline';

src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc renamed to src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ public static function fCreate($attributes = []): ?static
9595
}
9696

9797
// Also account for static used within union types.
98-
public function fCreate($attributes = []): object|static
99-
{
100-
}
98+
public function staticLast($attributes = []): object|static {}
99+
public function staticMiddle(): string|static|object {}
100+
public function staticFirst(): static|object {}
101101

102102
// Ensure that static as a scope keyword when preceeded by a colon which is not for a type declaration is still handled.
103103
$callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };

src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc.fixed renamed to src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ public static function fCreate($attributes = []): ?static
9090
}
9191

9292
// Also account for static used within union types.
93-
public function fCreate($attributes = []): object|static
94-
{
95-
}
93+
public function staticLast($attributes = []): object|static {}
94+
public function staticMiddle(): string|static|object {}
95+
public function staticFirst(): static|object {}
9696

9797
// Ensure that static as a scope keyword when preceeded by a colon which is not for a type declaration is still handled.
9898
$callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Intentional parse error.
4+
// Testing that the sniff does not throw an "Undefined array key" notice during live coding.
5+
// This must be the only test in this file.
6+
readonly
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Intentional parse error.
4+
// Testing that the sniff still adds a space when there is only a comment after the keyword during live coding.
5+
// This must be the only test in this file.
6+
readonly/*comment*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Intentional parse error.
4+
// Testing that the sniff still adds a space when there is only a comment after the keyword during live coding.
5+
// This must be the only test in this file.
6+
readonly /*comment*/

src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php

+35-24
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,44 @@ class ScopeKeywordSpacingUnitTest extends AbstractSniffUnitTest
2626
* The key of the array should represent the line number and the value
2727
* should represent the number of errors that should occur on that line.
2828
*
29+
* @param string $testFile The name of the file being tested.
30+
*
2931
* @return array<int, int>
3032
*/
31-
public function getErrorList()
33+
public function getErrorList($testFile='')
3234
{
33-
return [
34-
7 => 2,
35-
8 => 1,
36-
13 => 1,
37-
14 => 1,
38-
15 => 1,
39-
17 => 2,
40-
26 => 1,
41-
28 => 1,
42-
29 => 1,
43-
64 => 1,
44-
67 => 1,
45-
71 => 1,
46-
103 => 1,
47-
106 => 1,
48-
111 => 1,
49-
119 => 1,
50-
121 => 1,
51-
127 => 2,
52-
134 => 2,
53-
138 => 2,
54-
140 => 3,
55-
];
35+
switch ($testFile) {
36+
case 'ScopeKeywordSpacingUnitTest.1.inc':
37+
return [
38+
7 => 2,
39+
8 => 1,
40+
13 => 1,
41+
14 => 1,
42+
15 => 1,
43+
17 => 2,
44+
26 => 1,
45+
28 => 1,
46+
29 => 1,
47+
64 => 1,
48+
67 => 1,
49+
71 => 1,
50+
103 => 1,
51+
106 => 1,
52+
111 => 1,
53+
119 => 1,
54+
121 => 1,
55+
127 => 2,
56+
134 => 2,
57+
138 => 2,
58+
140 => 3,
59+
];
60+
61+
case 'ScopeKeywordSpacingUnitTest.3.inc':
62+
return [6 => 1];
63+
64+
default:
65+
return [];
66+
}//end switch
5667

5768
}//end getErrorList()
5869

0 commit comments

Comments
 (0)