function ValidFunctionNameSniff::processTokenOutsideScope
Same name in this branch
- 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\NamingConventions\ValidFunctionNameSniff::processTokenOutsideScope()
- 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php \PHP_CodeSniffer\Standards\PEAR\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 CamelCapsFunctionNameSniff::processTokenOutsideScope
File
-
vendor/
drupal/ coder/ coder_sniffer/ Drupal/ Sniffs/ NamingConventions/ ValidFunctionNameSniff.php, line 108
Class
Namespace
Drupal\Sniffs\NamingConventionsCode
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) {
$functionName = $phpcsFile->getDeclarationName($stackPtr);
if ($functionName === null) {
// Ignore closures.
return;
}
$isApiFile = substr($phpcsFile->getFilename(), -8) === '.api.php';
$isHookExample = substr($functionName, 0, 5) === 'hook_';
if ($isApiFile === true && $isHookExample === true) {
// Ignore for example hook_ENTITY_TYPE_insert() functions in .api.php
// files.
return;
}
if ($functionName !== strtolower($functionName)) {
$expected = strtolower(preg_replace('/([^_])([A-Z])/', '$1_$2', $functionName));
$error = 'Invalid function name, expected %s but found %s';
$data = [
$expected,
$functionName,
];
$phpcsFile->addError($error, $stackPtr, 'InvalidName', $data);
}
// Validate function names only in *.module files.
$isModuleFile = substr($phpcsFile->getFilename(), -7) === '.module';
if ($isModuleFile === true) {
// Check if the function prefix is allowed to not respect standard.
foreach ($this->allowedFunctionPrefixes as $allowedFunctionPrefix) {
if (substr($functionName, 0, strlen($allowedFunctionPrefix)) === $allowedFunctionPrefix) {
return;
}
}
$moduleName = substr(basename($phpcsFile->getFilename()), 0, -7);
if (preg_match("/^_?{$moduleName}\\_.+/", $functionName) === 0) {
$error = 'All functions defined in a module file must be prefixed with the module\'s name, found "%s" but expected "%s"';
$data = [
$functionName,
$moduleName . '_' . $functionName,
];
$phpcsFile->addError($error, $stackPtr, 'InvalidPrefix', $data);
}
}
}