Source for file ValidFunctionNameSniff.php
Documentation is available at ValidFunctionNameSniff.php
* Ensures method and function names are correct.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\Tokens;
use PHP_CodeSniffer\Files\File;
class ValidFunctionNameSniff extends AbstractScopeSniff
* A list of all PHP magic methods.
protected $magicMethods = array (
* A list of all PHP magic functions.
protected $magicFunctions = array ('autoload' => true );
* Constructs a PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
public function __construct ()
parent ::__construct (Tokens ::$ooScopeTokens, array (T_FUNCTION ), true );
* 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
* @param int $currScope The position of the current scope.
protected function processTokenWithinScope (File $phpcsFile, $stackPtr, $currScope)
$methodName = $phpcsFile->getDeclarationName ($stackPtr);
if ($methodName === null ) {
$className = $phpcsFile->getDeclarationName ($currScope);
$errorData = array ($className. '::'. $methodName);
// Is this a magic method. i.e., is prefixed with "__" ?
if (isset ($this->magicMethods[$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);
// PHP4 constructors are allowed to break our rules.
if ($methodName === $className) {
// PHP4 destructors are allowed to break our rules.
if ($methodName === '_'. $className) {
$methodProps = $phpcsFile->getMethodProperties ($stackPtr);
$scope = $methodProps['scope'];
$scopeSpecified = $methodProps['scope_specified'];
if ($methodProps['scope'] === 'private') {
// 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');
$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';
$phpcsFile->addError ($error, $stackPtr, 'PublicUnderscore', $data);
// If the scope was specified on the method, then the method must be
// camel caps and an underscore should be checked for. If it wasn't
// specified, treat it like a public method and remove the underscore
// prefix if there is one because we cant determine if it is private or
$testMethodName = $methodName;
if ($scopeSpecified === false && $methodName{0 } === '_') {
$testMethodName = substr($methodName, 1 );
if (Common ::isCamelCaps ($testMethodName, false , $isPublic, false ) === false ) {
if ($scopeSpecified === true ) {
$error = '%s method name "%s" is not in camel caps format';
$phpcsFile->addError ($error, $stackPtr, 'ScopeNotCamelCaps', $data);
$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
protected function processTokenOutsideScope (File $phpcsFile, $stackPtr)
$functionName = $phpcsFile->getDeclarationName ($stackPtr);
if ($functionName === null ) {
if (ltrim($functionName, '_') === '') {
// Ignore special functions.
$errorData = array ($functionName);
// Is this a magic function. i.e., it is prefixed with "__".
if (preg_match('|^__[^_]|', $functionName) !== 0 ) {
if (isset ($this->magicFunctions[$magicPart]) === false ) {
$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
$underscorePos = strrpos($functionName, '_');
if ($underscorePos === false ) {
$camelCapsPart = $functionName;
$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);
$newPackagePart = $packagePart;
$newCamelCapsPart = $camelCapsPart;
// Every function must have a camel caps part, so check that first.
if (Common ::isCamelCaps ($camelCapsPart, false , true , false ) === false ) {
if ($packagePart !== '') {
// Check that each new word starts with a capital.
$nameBits = explode('_', $packagePart);
foreach ($nameBits as $bit) {
foreach ($nameBits as $bit) {
if ($validName === false ) {
$newName = rtrim($newPackagePart, '_'). '_'. $newCamelCapsPart;
if ($newPackagePart === '') {
$newName = $newCamelCapsPart;
$newName = rtrim($newPackagePart, '_'). '_'. $newCamelCapsPart;
$error = 'Function name "%s" is invalid; consider "%s" instead';
$phpcsFile->addError ($error, $stackPtr, 'FunctionNameInvalid', $data);
}//end processTokenOutsideScope()
Documentation generated on Mon, 11 Mar 2019 15:27:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|