Skip to content

Commit 0ae707a

Browse files
authored
refactor(MethodScope): Update sniff implementation from upstream (#3552621)
1 parent 92210d5 commit 0ae707a

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

coder_sniffer/Drupal/Sniffs/Scope/MethodScopeSniff.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* Verifies that class/interface/trait methods have scope modifiers.
1818
*
1919
* Largely copied from
20-
* \PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope\MethodScopeSniff to work on
21-
* traits and have a fixer.
20+
* \PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope\MethodScopeSniff to have a
21+
* fixer.
2222
*
2323
* @category PHP
2424
* @package PHP_CodeSniffer
@@ -29,12 +29,11 @@ class MethodScopeSniff extends AbstractScopeSniff
2929

3030

3131
/**
32-
* Constructs a
33-
* \PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope\MethodScopeSniff.
32+
* Constructor.
3433
*/
3534
public function __construct()
3635
{
37-
parent::__construct([T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM], [T_FUNCTION]);
36+
parent::__construct(Tokens::OO_SCOPE_TOKENS, [T_FUNCTION]);
3837
}
3938

4039

@@ -47,32 +46,26 @@ public function __construct()
4746
*
4847
* @return void
4948
*/
50-
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
49+
protected function processTokenWithinScope(File $phpcsFile, int $stackPtr, int $currScope)
5150
{
5251
$tokens = $phpcsFile->getTokens();
5352

54-
$methodName = $phpcsFile->getDeclarationName($stackPtr);
55-
if ($methodName === '') {
56-
// Ignore closures.
53+
// Determine if this is a function which needs to be examined.
54+
$conditions = $tokens[$stackPtr]['conditions'];
55+
end($conditions);
56+
$deepestScope = key($conditions);
57+
if ($deepestScope !== $currScope) {
5758
return;
5859
}
5960

60-
if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true) {
61-
// Ignore nested functions.
61+
$methodName = $phpcsFile->getDeclarationName($stackPtr);
62+
if ($methodName === '') {
63+
// Ignore live coding.
6264
return;
6365
}
6466

65-
$modifier = null;
66-
for ($i = ($stackPtr - 1); $i > 0; $i--) {
67-
if ($tokens[$i]['line'] < $tokens[$stackPtr]['line']) {
68-
break;
69-
} elseif (isset(Tokens::SCOPE_MODIFIERS[$tokens[$i]['code']]) === true) {
70-
$modifier = $i;
71-
break;
72-
}
73-
}
74-
75-
if ($modifier === null) {
67+
$properties = $phpcsFile->getMethodProperties($stackPtr);
68+
if ($properties['scope_specified'] === false) {
7669
$error = 'Visibility must be declared on method "%s"';
7770
$data = [$methodName];
7871
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Missing', $data);
@@ -96,7 +89,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
9689
*
9790
* @return void
9891
*/
99-
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
92+
protected function processTokenOutsideScope(File $phpcsFile, int $stackPtr)
10093
{
10194
}
10295
}

0 commit comments

Comments
 (0)