Skip to content

Commit 6c65b13

Browse files
committed
refactor(ValidFunctionName): Simplify method name checking, underscores are already handled by PSR2 sniff (#3552621)
1 parent 0ae707a commit 6c65b13

File tree

2 files changed

+3
-61
lines changed

2 files changed

+3
-61
lines changed

coder_sniffer/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff;
14-
use PHP_CodeSniffer\Util\Common;
1514

1615
/**
1716
* \Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff.
1817
*
1918
* Extends
2019
* \PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff
21-
* to also check global function names outside the scope of classes and to not
22-
* allow methods beginning with an underscore.
20+
* to also check global function names outside the scope of classes.
2321
*
2422
* @category PHP
2523
* @package PHP_CodeSniffer
@@ -39,62 +37,6 @@ class ValidFunctionNameSniff extends CamelCapsFunctionNameSniff
3937
];
4038

4139

42-
/**
43-
* Processes the tokens within the scope.
44-
*
45-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed.
46-
* @param int $stackPtr The position where this token was
47-
* found.
48-
* @param int $currScope The position of the current scope.
49-
*
50-
* @return void
51-
*/
52-
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
53-
{
54-
$methodName = $phpcsFile->getDeclarationName($stackPtr);
55-
if ($methodName === '') {
56-
// Ignore closures.
57-
return;
58-
}
59-
60-
$className = $phpcsFile->getDeclarationName($currScope);
61-
$errorData = [$className . '::' . $methodName];
62-
63-
// Is this a magic method. i.e., is prefixed with "__" ?
64-
if (preg_match('|^__|', $methodName) !== 0) {
65-
$magicPart = strtolower(substr($methodName, 2));
66-
if (isset($this->magicMethods[$magicPart]) === false
67-
&& isset($this->methodsDoubleUnderscore[$magicPart]) === false
68-
) {
69-
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
70-
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
71-
}
72-
73-
return;
74-
}
75-
76-
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
77-
if (Common::isCamelCaps($methodName, false, true, $this->strict) === false) {
78-
if ($methodProps['scope_specified'] === true) {
79-
$error = '%s method name "%s" is not in lowerCamel format';
80-
$data = [
81-
ucfirst($methodProps['scope']),
82-
$errorData[0],
83-
];
84-
$phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data);
85-
} else {
86-
$error = 'Method name "%s" is not in lowerCamel format';
87-
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
88-
}
89-
90-
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no');
91-
return;
92-
} else {
93-
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
94-
}
95-
}
96-
97-
9840
/**
9941
* Processes the tokens outside the scope.
10042
*

tests/Drupal/bad/BadUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ protected function getErrorList(string $testFile): array
267267
401 => 1,
268268
403 => 1,
269269
406 => 2,
270-
407 => 3,
271-
411 => 2,
270+
407 => 2,
271+
411 => 1,
272272
417 => 1,
273273
418 => 2,
274274
421 => 1,

0 commit comments

Comments
 (0)