From 10c10acece4e22eeb9fcff531f624b46b3bd68da Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 19 Jan 2022 01:36:54 +0100 Subject: [PATCH] Squiz/MethodScope: bugfix for unconventional spacing 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. --- .../Squiz/Sniffs/Scope/MethodScopeSniff.php | 13 ++----------- .../Squiz/Tests/Scope/MethodScopeUnitTest.inc | 7 +++++++ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php b/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php index 8c60208c6c..8a34a4f29f 100644 --- a/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php +++ b/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php @@ -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); diff --git a/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc b/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc index bc77d46d1d..3cc617d75b 100644 --- a/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc +++ b/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc @@ -48,3 +48,10 @@ enum SomeEnum private function func1() {} protected function func1() {} } + +class UnconventionalSpacing { + public + static + function + myFunction() {} +}