class ValidFunctionNameSniff
Same name in this branch
- 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php \Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff
- 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\NamingConventions\ValidFunctionNameSniff
Hierarchy
- class \PHP_CodeSniffer\Sniffs\AbstractScopeSniff implements \PHP_CodeSniffer\Sniffs\Sniff
- class \PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions\ValidFunctionNameSniff extends \PHP_CodeSniffer\Sniffs\AbstractScopeSniff
Expanded class hierarchy of ValidFunctionNameSniff
1 file declares its use of ValidFunctionNameSniff
- ValidFunctionNameSniff.php in vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Squiz/ Sniffs/ NamingConventions/ ValidFunctionNameSniff.php
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ PEAR/ Sniffs/ NamingConventions/ ValidFunctionNameSniff.php, line 17
Namespace
PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventionsView source
class ValidFunctionNameSniff extends AbstractScopeSniff {
/**
* A list of all PHP magic methods.
*
* @var array
*/
protected $magicMethods = [
'construct' => true,
'destruct' => true,
'call' => true,
'callstatic' => true,
'get' => true,
'set' => true,
'isset' => true,
'unset' => true,
'sleep' => true,
'wakeup' => true,
'serialize' => true,
'unserialize' => true,
'tostring' => true,
'invoke' => true,
'set_state' => true,
'clone' => true,
'debuginfo' => true,
];
/**
* A list of all PHP magic functions.
*
* @var array
*/
protected $magicFunctions = [
'autoload' => true,
];
/**
* Constructs a PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
*/
public function __construct() {
parent::__construct(Tokens::$ooScopeTokens, [
T_FUNCTION,
], true);
}
//end __construct()
/**
* 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) {
$tokens = $phpcsFile->getTokens();
// Determine if this is a function which needs to be examined.
$conditions = $tokens[$stackPtr]['conditions'];
end($conditions);
$deepestScope = key($conditions);
if ($deepestScope !== $currScope) {
return;
}
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$className = $phpcsFile->getDeclarationName($currScope);
if (isset($className) === false) {
$className = '[Anonymous Class]';
}
$errorData = [
$className . '::' . $methodName,
];
$methodNameLc = strtolower($methodName);
$classNameLc = strtolower($className);
// Is this a magic method. i.e., is prefixed with "__" ?
if (preg_match('|^__[^_]|', $methodName) !== 0) {
$magicPart = substr($methodNameLc, 2);
if (isset($this->magicMethods[$magicPart]) === true) {
return;
}
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
}
// PHP4 constructors are allowed to break our rules.
if ($methodNameLc === $classNameLc) {
return;
}
// PHP4 destructors are allowed to break our rules.
if ($methodNameLc === '_' . $classNameLc) {
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
$scope = $methodProps['scope'];
$scopeSpecified = $methodProps['scope_specified'];
if ($methodProps['scope'] === 'private') {
$isPublic = false;
}
else {
$isPublic = true;
}
// If it's a private method, it must have an underscore on the front.
if ($isPublic === false) {
if ($methodName[0] !== '_') {
$error = 'Private method name "%s" must be prefixed with an underscore';
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
$phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'no');
}
else {
$phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'yes');
}
}
// If it's not a private method, it must not have an underscore on the front.
if ($isPublic === true && $scopeSpecified === true && $methodName[0] === '_') {
$error = '%s method name "%s" must not be prefixed with an underscore';
$data = [
ucfirst($scope),
$errorData[0],
];
$phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
}
$testMethodName = ltrim($methodName, '_');
if (Common::isCamelCaps($testMethodName, false, true, false) === false) {
if ($scopeSpecified === true) {
$error = '%s method name "%s" is not in camel caps format';
$data = [
ucfirst($scope),
$errorData[0],
];
$phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data);
}
else {
$error = 'Method name "%s" is not in camel caps format';
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
}
}
}
//end processTokenWithinScope()
/**
* Processes the tokens outside the scope.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
*
* @return void
*/
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);
}
}
//end processTokenOutsideScope()
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
AbstractScopeSniff::$listenOutside | private | property | True if this test should fire on tokens outside of the scope. | ||
AbstractScopeSniff::$scopeTokens | private | property | The type of scope opener tokens that this test wishes to listen to. | ||
AbstractScopeSniff::$tokens | private | property | The token types that this test wishes to listen to within the scope. | ||
AbstractScopeSniff::process | final public | function | Processes the tokens that this test is listening for. | Overrides Sniff::process | |
AbstractScopeSniff::register | final public | function | The method that is called to register the tokens this test wishes to listen to. |
Overrides Sniff::register | |
ValidFunctionNameSniff::$magicFunctions | protected | property | A list of all PHP magic functions. | ||
ValidFunctionNameSniff::$magicMethods | protected | property | A list of all PHP magic methods. | ||
ValidFunctionNameSniff::processTokenOutsideScope | protected | function | Processes the tokens outside the scope. | Overrides AbstractScopeSniff::processTokenOutsideScope | 1 |
ValidFunctionNameSniff::processTokenWithinScope | protected | function | Processes the tokens within the scope. | Overrides AbstractScopeSniff::processTokenWithinScope | |
ValidFunctionNameSniff::__construct | public | function | Constructs a PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff. | Overrides AbstractScopeSniff::__construct |