Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squiz/ScopeKeywordSpacing: add more defensive coding + extra tests #150

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public function process(File $phpcsFile, $stackPtr)

if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
$spacing = 0;
} else if (isset($tokens[($stackPtr + 2)]) === false) {
// Parse error/live coding. Bow out.
return;
} else {
if ($tokens[($stackPtr + 2)]['line'] !== $tokens[$stackPtr]['line']) {
$spacing = 'newline';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public static function fCreate($attributes = []): ?static
}

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

// Ensure that static as a scope keyword when preceeded by a colon which is not for a type declaration is still handled.
$callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public static function fCreate($attributes = []): ?static
}

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

// Ensure that static as a scope keyword when preceeded by a colon which is not for a type declaration is still handled.
$callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Intentional parse error.
// Testing that the sniff does not throw an "Undefined array key" notice during live coding.
// This must be the only test in this file.
readonly
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Intentional parse error.
// Testing that the sniff still adds a space when there is only a comment after the keyword during live coding.
// This must be the only test in this file.
readonly/*comment*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Intentional parse error.
// Testing that the sniff still adds a space when there is only a comment after the keyword during live coding.
// This must be the only test in this file.
readonly /*comment*/
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,44 @@ class ScopeKeywordSpacingUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [
7 => 2,
8 => 1,
13 => 1,
14 => 1,
15 => 1,
17 => 2,
26 => 1,
28 => 1,
29 => 1,
64 => 1,
67 => 1,
71 => 1,
103 => 1,
106 => 1,
111 => 1,
119 => 1,
121 => 1,
127 => 2,
134 => 2,
138 => 2,
140 => 3,
];
switch ($testFile) {
case 'ScopeKeywordSpacingUnitTest.1.inc':
return [
7 => 2,
8 => 1,
13 => 1,
14 => 1,
15 => 1,
17 => 2,
26 => 1,
28 => 1,
29 => 1,
64 => 1,
67 => 1,
71 => 1,
103 => 1,
106 => 1,
111 => 1,
119 => 1,
121 => 1,
127 => 2,
134 => 2,
138 => 2,
140 => 3,
];

case 'ScopeKeywordSpacingUnitTest.3.inc':
return [6 => 1];

default:
return [];
}//end switch

}//end getErrorList()

Expand Down