class CamelCapsFunctionNameSniff
Hierarchy
- class \PHP_CodeSniffer\Sniffs\AbstractScopeSniff implements \PHP_CodeSniffer\Sniffs\Sniff
- class \PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff extends \PHP_CodeSniffer\Sniffs\AbstractScopeSniff
Expanded class hierarchy of CamelCapsFunctionNameSniff
2 files declare their use of CamelCapsFunctionNameSniff
- CamelCapsMethodNameSniff.php in vendor/
squizlabs/ php_codesniffer/ src/ Standards/ PSR1/ Sniffs/ Methods/ CamelCapsMethodNameSniff.php - ValidFunctionNameSniff.php in vendor/
drupal/ coder/ coder_sniffer/ Drupal/ Sniffs/ NamingConventions/ ValidFunctionNameSniff.php
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Generic/ Sniffs/ NamingConventions/ CamelCapsFunctionNameSniff.php, line 17
Namespace
PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventionsView source
class CamelCapsFunctionNameSniff 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 non-magic methods starting with a double underscore.
*
* These come from PHP modules such as SOAPClient.
*
* @var array
*/
protected $methodsDoubleUnderscore = [
'dorequest' => true,
'getcookies' => true,
'getfunctions' => true,
'getlastrequest' => true,
'getlastrequestheaders' => true,
'getlastresponse' => true,
'getlastresponseheaders' => true,
'gettypes' => true,
'setcookie' => true,
'setlocation' => true,
'setsoapheaders' => true,
'soapcall' => true,
];
/**
* A list of all PHP magic functions.
*
* @var array
*/
protected $magicFunctions = [
'autoload' => true,
];
/**
* If TRUE, the string must not have two capital letters next to each other.
*
* @var boolean
*/
public $strict = true;
/**
* Constructs a Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff.
*/
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) {
// Live coding or parse error. Bow out.
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 || isset($this->methodsDoubleUnderscore[$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;
}
// Ignore leading underscores in the method name.
$methodName = ltrim($methodName, '_');
$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 camel caps format';
$data = [
ucfirst($methodProps['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);
}
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no');
}
else {
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
}
}
//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) {
// Live coding or parse error. Bow out.
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);
}
// Ignore leading underscores in the method name.
$functionName = ltrim($functionName, '_');
if (Common::isCamelCaps($functionName, false, true, $this->strict) === false) {
$error = 'Function name "%s" is not in camel caps format';
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
$phpcsFile->recordMetric($stackPtr, 'CamelCase function name', 'no');
}
else {
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
}
}
//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 | |
CamelCapsFunctionNameSniff::$magicFunctions | protected | property | A list of all PHP magic functions. | ||
CamelCapsFunctionNameSniff::$magicMethods | protected | property | A list of all PHP magic methods. | ||
CamelCapsFunctionNameSniff::$methodsDoubleUnderscore | protected | property | A list of all PHP non-magic methods starting with a double underscore. | ||
CamelCapsFunctionNameSniff::$strict | public | property | If TRUE, the string must not have two capital letters next to each other. | ||
CamelCapsFunctionNameSniff::processTokenOutsideScope | protected | function | Processes the tokens outside the scope. | Overrides AbstractScopeSniff::processTokenOutsideScope | 2 |
CamelCapsFunctionNameSniff::processTokenWithinScope | protected | function | Processes the tokens within the scope. | Overrides AbstractScopeSniff::processTokenWithinScope | 2 |
CamelCapsFunctionNameSniff::__construct | public | function | Constructs a Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff. | Overrides AbstractScopeSniff::__construct |