Skip to content

Commit

Permalink
Squiz/MethodScope: bugfix for unconventional spacing
Browse files Browse the repository at this point in the history
The `Squiz.Scope.MethodScope` sniff is intended to check whether each method has visibility declared, but would in actual fact also enforce that the visibility should be on the same line as the `function` keyword as it stopped searching for the visibility keyword once it reached the start of the line.

Fixed now by using the `File::getMethodProperties()` method for determining visibility instead of searching with sniff specific logic.

Includes unit test.
  • Loading branch information
jrfnl committed Mar 31, 2022
1 parent 2596a15 commit 10c10ac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
13 changes: 2 additions & 11 deletions src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php
Expand Up @@ -54,17 +54,8 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
return;
}

$modifier = null;
for ($i = ($stackPtr - 1); $i > 0; $i--) {
if ($tokens[$i]['line'] < $tokens[$stackPtr]['line']) {
break;
} else if (isset(Tokens::$scopeModifiers[$tokens[$i]['code']]) === true) {
$modifier = $i;
break;
}
}

if ($modifier === null) {
$properties = $phpcsFile->getMethodProperties($stackPtr);
if ($properties['scope_specified'] === false) {
$error = 'Visibility must be declared on method "%s"';
$data = [$methodName];
$phpcsFile->addError($error, $stackPtr, 'Missing', $data);
Expand Down
7 changes: 7 additions & 0 deletions src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc
Expand Up @@ -48,3 +48,10 @@ enum SomeEnum
private function func1() {}
protected function func1() {}
}

class UnconventionalSpacing {
public
static
function
myFunction() {}
}

0 comments on commit 10c10ac

Please sign in to comment.