Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff;
use PHP_CodeSniffer\Util\Common;

/**
* \Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff.
*
* Extends
* \PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff
* to also check global function names outside the scope of classes and to not
* allow methods beginning with an underscore.
* to also check global function names outside the scope of classes.
*
* @category PHP
* @package PHP_CodeSniffer
Expand All @@ -39,62 +37,6 @@ class ValidFunctionNameSniff extends CamelCapsFunctionNameSniff
];


/**
* Processes the tokens within the scope.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === '') {
// Ignore closures.
return;
}

$className = $phpcsFile->getDeclarationName($currScope);
$errorData = [$className . '::' . $methodName];

// Is this a magic method. i.e., is prefixed with "__" ?
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = strtolower(substr($methodName, 2));
if (isset($this->magicMethods[$magicPart]) === false
&& isset($this->methodsDoubleUnderscore[$magicPart]) === false
) {
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
}

return;
}

$methodProps = $phpcsFile->getMethodProperties($stackPtr);
if (Common::isCamelCaps($methodName, false, true, $this->strict) === false) {
if ($methodProps['scope_specified'] === true) {
$error = '%s method name "%s" is not in lowerCamel format';
$data = [
ucfirst($methodProps['scope']),
$errorData[0],
];
$phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data);
} else {
$error = 'Method name "%s" is not in lowerCamel format';
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
}

$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no');
return;
} else {
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
}
}


/**
* Processes the tokens outside the scope.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/Drupal/bad/BadUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ protected function getErrorList(string $testFile): array
401 => 1,
403 => 1,
406 => 2,
407 => 3,
411 => 2,
407 => 2,
411 => 1,
417 => 1,
418 => 2,
421 => 1,
Expand Down