function ValidFunctionNameSniff::processTokenOutsideScope
Same name in this branch
- 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php \Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff::processTokenOutsideScope()
- 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\NamingConventions\ValidFunctionNameSniff::processTokenOutsideScope()
Processes the tokens outside the scope.
Parameters
\PHP_CodeSniffer\Files\File $phpcsFile The file being processed.:
int $stackPtr The position where this token was: found.
Return value
void
Overrides AbstractScopeSniff::processTokenOutsideScope
1 method overrides ValidFunctionNameSniff::processTokenOutsideScope()
- ValidFunctionNameSniff::processTokenOutsideScope in vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Squiz/ Sniffs/ NamingConventions/ ValidFunctionNameSniff.php - Processes the tokens outside the scope.
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ PEAR/ Sniffs/ NamingConventions/ ValidFunctionNameSniff.php, line 181
Class
Namespace
PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventionsCode
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) {
$functionName = $phpcsFile->getDeclarationName($stackPtr);
if ($functionName === null) {
// Ignore closures.
return;
}
if (ltrim($functionName, '_') === '') {
// Ignore special functions.
return;
}
$errorData = [
$functionName,
];
// Is this a magic function. i.e., it is prefixed with "__".
if (preg_match('|^__[^_]|', $functionName) !== 0) {
$magicPart = strtolower(substr($functionName, 2));
if (isset($this->magicFunctions[$magicPart]) === true) {
return;
}
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
}
// Function names can be in two parts; the package name and
// the function name.
$packagePart = '';
$underscorePos = strrpos($functionName, '_');
if ($underscorePos === false) {
$camelCapsPart = $functionName;
}
else {
$packagePart = substr($functionName, 0, $underscorePos);
$camelCapsPart = substr($functionName, $underscorePos + 1);
// We don't care about _'s on the front.
$packagePart = ltrim($packagePart, '_');
}
// If it has a package part, make sure the first letter is a capital.
if ($packagePart !== '') {
if ($functionName[0] === '_') {
$error = 'Function name "%s" is invalid; only private methods should be prefixed with an underscore';
$phpcsFile->addError($error, $stackPtr, 'FunctionUnderscore', $errorData);
}
if ($functionName[0] !== strtoupper($functionName[0])) {
$error = 'Function name "%s" is prefixed with a package name but does not begin with a capital letter';
$phpcsFile->addError($error, $stackPtr, 'FunctionNoCapital', $errorData);
}
}
// If it doesn't have a camel caps part, it's not valid.
if (trim($camelCapsPart) === '') {
$error = 'Function name "%s" is not valid; name appears incomplete';
$phpcsFile->addError($error, $stackPtr, 'FunctionInvalid', $errorData);
return;
}
$validName = true;
$newPackagePart = $packagePart;
$newCamelCapsPart = $camelCapsPart;
// Every function must have a camel caps part, so check that first.
if (Common::isCamelCaps($camelCapsPart, false, true, false) === false) {
$validName = false;
$newCamelCapsPart = strtolower($camelCapsPart[0]) . substr($camelCapsPart, 1);
}
if ($packagePart !== '') {
// Check that each new word starts with a capital.
$nameBits = explode('_', $packagePart);
$nameBits = array_filter($nameBits);
foreach ($nameBits as $bit) {
if ($bit[0] !== strtoupper($bit[0])) {
$newPackagePart = '';
foreach ($nameBits as $bit) {
$newPackagePart .= strtoupper($bit[0]) . substr($bit, 1) . '_';
}
$validName = false;
break;
}
}
}
if ($validName === false) {
if ($newPackagePart === '') {
$newName = $newCamelCapsPart;
}
else {
$newName = rtrim($newPackagePart, '_') . '_' . $newCamelCapsPart;
}
$error = 'Function name "%s" is invalid; consider "%s" instead';
$data = $errorData;
$data[] = $newName;
$phpcsFile->addError($error, $stackPtr, 'FunctionNameInvalid', $data);
}
}