Returns true if the specified string is in the camel caps format.
public static isCamelCaps ( string $string, boolean $classFormat = false, boolean $public = true, boolean $strict = true ) : boolean | ||
$string | string | The string the verify. |
$classFormat | boolean | If true, check to see if the string is in the class format. Class format strings must start with a capital letter and contain no underscores. |
$public | boolean | If true, the first character in the string must be an a-z character. If false, the character must be an underscore. This argument is only applicable if $classFormat is false. |
$strict | boolean | If true, the string must not have two capital letters next to each other. If false, a relaxed camel caps policy is used to allow for acronyms. |
리턴 | boolean |
/** * Processes the function tokens within the class. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); // Detect multiple properties defined at the same time. Throw an error // for this, but also only process the first property in the list so we don't // repeat errors. $find = PHP_CodeSniffer_Tokens::$scopeModifiers; $find = array_merge($find, array(T_VARIABLE, T_VAR, T_SEMICOLON)); $prev = $phpcsFile->findPrevious($find, $stackPtr - 1); if ($tokens[$prev]['code'] === T_VARIABLE) { return; } if ($tokens[$prev]['code'] === T_VAR) { $error = 'The var keyword must not be used to declare a property'; $phpcsFile->addError($error, $stackPtr, 'VarUsed'); } $next = $phpcsFile->findNext(array(T_VARIABLE, T_SEMICOLON), $stackPtr + 1); if ($tokens[$next]['code'] === T_VARIABLE) { $error = 'There must not be more than one property declared per statement'; $phpcsFile->addError($error, $stackPtr, 'Multiple'); } $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr); if ($modifier === false || $tokens[$modifier]['line'] !== $tokens[$stackPtr]['line']) { $error = 'Visibility must be declared on property "%s"'; $data = array($tokens[$stackPtr]['content']); $phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data); } $propertyName = ltrim($tokens[$stackPtr]['content'], '$'); if (PHP_CodeSniffer::isCamelCaps($propertyName, false, true, false) === false) { $error = 'Property name "%s" is not in camel caps format'; $errorData = array($tokens[$stackPtr]['content']); $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); } }
/** * Processes the tokens within the scope. * * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $methodName = $phpcsFile->getDeclarationName($stackPtr); if ($methodName === null) { // Ignore closures. return; } // Ignore magic methods. if (preg_match('|^__|', $methodName) !== 0) { $magicPart = strtolower(substr($methodName, 2)); if (isset($this->magicMethods[$magicPart]) === true || isset($this->methodsDoubleUnderscore[$magicPart]) === true) { return; } } $testName = ltrim($methodName, '_'); if (PHP_CodeSniffer::isCamelCaps($testName, false, true, false) === false) { $error = 'Method name "%s" is not in camel caps format'; $className = $phpcsFile->getDeclarationName($currScope); $errorData = array($className . '::' . $methodName); $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no'); } else { $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes'); } }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $trait = $phpcsFile->findNext(T_STRING, $stackPtr); $name = $tokens[$trait]['content']; if (preg_match('|^[A-Z]|', $name) === 0) { $error = 'Each %s name must begin with a capital letter'; $errorData = array($tokens[$stackPtr]['content']); $phpcsFile->addError($error, $stackPtr, 'StartWithCapital', $errorData); } if ($this->allowUnderscore === true) { if (preg_match('|_[a-z]|', $name)) { $error = 'Trait "%s" is not in camel caps with underscores format'; $phpcsFile->addError($error, $stackPtr, 'CamelCapsWithUnderscore', array($name)); } $name = lcfirst(str_replace('_', '', $name)); } else { if (strpos($name, '_') !== FALSE) { $error = 'Underscores are not allowed in trait "%s"'; $phpcsFile->addError($error, $stackPtr, 'NoUnderscore', array($name)); } $name = array(lcfirst($name)); } if (PHP_CodeSniffer::isCamelCaps($name, false, true, true) === false) { $error = 'Trait "%s" is not in camel caps format'; $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', array($name)); } }
/** * Processes the tokens within the scope. * * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $methodName = $phpcsFile->getDeclarationName($stackPtr); if ($methodName === null) { // Ignore closures. return; } $className = $phpcsFile->getDeclarationName($currScope); $errorData = array($className . '::' . $methodName); // Is this a magic method. i.e., is prefixed with "__" ? if (preg_match('|^__|', $methodName) !== 0) { $magicPart = strtolower(substr($methodName, 2)); if (isset($this->magicMethods[$magicPart]) === false && isset($this->methodsDoubleUnderscore[$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); } return; } $methodProps = $phpcsFile->getMethodProperties($stackPtr); if (PHP_CodeSniffer::isCamelCaps($methodName, false, true, $this->strict) === false) { if ($methodProps['scope_specified'] === true) { $error = '%s method name "%s" is not in lowerCamel format'; $data = array(ucfirst($methodProps['scope']), $errorData[0]); $phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data); } else { $error = 'Method name "%s" is not in lowerCamel format'; $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); } $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no'); return; } else { $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes'); } }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (isset($tokens[$stackPtr]['scope_opener']) === false) { $error = 'Possible parse error: %s missing opening or closing brace'; $data = array($tokens[$stackPtr]['content']); $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); return; } // Determine the name of the class or interface. Note that we cannot // simply look for the first T_STRING because a class name // starting with the number will be multiple tokens. $opener = $tokens[$stackPtr]['scope_opener']; $nameStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $opener, true); $nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener); $name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart)); // Check for camel caps format. $valid = PHP_CodeSniffer::isCamelCaps($name, true, true, false); if ($valid === false) { $type = ucfirst($tokens[$stackPtr]['content']); $error = '%s name "%s" is not in camel caps format'; $data = array($type, $name); $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data); } }
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { if (false !== strpos($phpcsFile->getFilename(), 'model') || false !== strpos($phpcsFile->getFilename(), 'form') || false !== strpos($phpcsFile->getFilename(), 'filter')) { return null; } $tokens = $phpcsFile->getTokens(); $className = $phpcsFile->findNext(T_STRING, $stackPtr); $name = trim($tokens[$className]['content']); // "op" prefix if (0 !== strpos($name, 'op')) { $error = ucfirst($tokens[$stackPtr]['content']) . ' name must begin with "op" prefix'; $phpcsFile->addError($error, $stackPtr); } // "Interface" suffix if ($tokens[$stackPtr]['code'] === T_INTERFACE && !preg_match('/Interface$/', $name)) { $error = ucfirst($tokens[$stackPtr]['content']) . ' name must end with "Interface"'; $phpcsFile->addError($error, $stackPtr); } // stripped prefix if (0 === strpos($name, 'op')) { $name = substr($name, 2); } if (!PHP_CodeSniffer::isCamelCaps($name, true, true, false)) { $error = ucfirst($tokens[$stackPtr]['content']) . ' name is not in camel caps format'; $phpcsFile->addError($error, $stackPtr); } }
/** * @param $node * @return bool|string */ public function validate($node) { if ($node instanceof Stmt\Function_) { if (!\PHP_CodeSniffer::isCamelCaps($node->name, false, true, true)) { return [Logger::LOGLEVEL_ERROR, "Function name is not in camel caps format"]; } return true; } if ($node instanceof Stmt\ClassMethod) { if ($this->isMagicMethod($node->name)) { return true; } if (!\PHP_CodeSniffer::isCamelCaps($node->name, false, true, true)) { return [Logger::LOGLEVEL_ERROR, "Method name is not in camel caps format"]; } return true; } if ($node instanceof Node\Expr\Variable) { if (!\PHP_CodeSniffer::isCamelCaps($node->name, false, true, true) && !\PHP_CodeSniffer::isCamelCaps($node->name, true, true, true)) { return [Logger::LOGLEVEL_WARNING, "Variable name is not in camel caps format"]; } return true; } if ($node instanceof Node\Stmt\PropertyProperty) { if (!\PHP_CodeSniffer::isCamelCaps($node->name, false, true, true) && !\PHP_CodeSniffer::isCamelCaps($node->name, true, true, true)) { return [Logger::LOGLEVEL_WARNING, "Property name is not in camel caps format"]; } return true; } return true; }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. * @param int $stackPtr The position of the current token in the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); // Determine the name of the class or interface. Note that we cannot // simply look for the first T_STRING because a class name // starting with the number will be multiple tokens. $type = $tokens[$stackPtr]['content']; $opener = $tokens[$stackPtr]['scope_opener']; $nameStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $opener, true); $nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener); $name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart)); // Check that each new word starts with a small latter // and the last one with capital $validName = true; $nameBits = explode('_', $name); $className = $lastBit = array_pop($nameBits); foreach ($nameBits as $bit) { if ($bit === '' || $bit[0] !== strtolower($bit[0])) { $validName = false; break; } } $validName = $validName && PHP_CodeSniffer::isCamelCaps($lastBit, true, true, false); if ($validName === false) { // Strip underscores because they cause the suggested name // to be incorrect. $nameBits = explode('_', trim($name, '_')); $lastBit = array_pop($nameBits); if ($lastBit === '') { $error = '%s name is not valid'; $phpcsFile->addError($error, $stackPtr, 'Invalid', array($name)); } else { $newName = ''; foreach ($nameBits as $bit) { if ($bit !== '') { $newName .= strtolower($bit[0]) . substr($bit, 1) . '_'; } } $newName = $newName . ucfirst($lastBit); $error = '%s name is not valid; consider %s instead'; $data = array($name); $data[] = $newName; $phpcsFile->addError($error, $stackPtr, 'Invalid', $data); } } if ($type === 'interface') { if (substr($className, 0, 1) !== 'I') { $error = 'Interface name %s must start with "I"'; $data = array($name); $phpcsFile->addError($error, $stackPtr, 'BadInterfaceName', $data); } else { if (!PHP_CodeSniffer::isCamelCaps(substr($className, 1), true, true, false)) { $phpcsFile->addError('Interface name %s is not in camel caps format', $stackPtr, 'NotCamelCaps', $className); } } } }
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $className = $phpcsFile->getDeclarationName($currScope); $methodName = $phpcsFile->getDeclarationName($stackPtr); if (!PHP_CodeSniffer::isCamelCaps($methodName, false, true, false)) { $error = $className . '::' . $methodName . '() name is not in camel caps format'; $phpcsFile->addError($error, $stackPtr); } }
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $name = $tokens[$stackPtr]['content']; if (!PHP_CodeSniffer::isCamelCaps(substr($name, 1), false, true, false)) { $error = $name . ' name is not in camel caps format'; $phpcsFile->addError($error, $stackPtr); } }
/** * Processes the tokens within the scope. * * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $className = $phpcsFile->getDeclarationName($currScope); $methodName = $phpcsFile->getDeclarationName($stackPtr); // Is this a magic method. IE. is prefixed with "__". if (preg_match('|^__|', $methodName) !== 0) { $magicPart = substr($methodName, 2); if (in_array($magicPart, $this->_magicMethods) === false) { $error = "Method name \"{$className}::{$methodName}\" is invalid; only PHP magic methods should be prefixed with a double underscore"; $phpcsFile->addError($error, $stackPtr); } return; } // PHP4 constructors are allowed to break our rules. if ($methodName === $className) { return; } // PHP4 destructors are allowed to break our rules. if ($methodName === '_' . $className) { return; } $methodProps = $phpcsFile->getMethodProperties($stackPtr); $isPublic = $methodProps['scope'] === 'public' ? true : false; $scope = $methodProps['scope']; $scopeSpecified = $methodProps['scope_specified']; // If it's a private method, it must have an underscore on the front. if ($isPublic === false && $methodName[0] !== '_') { $error = ucfirst($scope) . " method name \"{$className}::{$methodName}\" must be prefixed with an underscore"; $phpcsFile->addError($error, $stackPtr); return; } // If it's not a private method, it must not have an underscore on the front. if ($isPublic === true && $scopeSpecified === true && $methodName[0] === '_') { $error = "Public method name \"{$className}::{$methodName}\" must not be prefixed with an underscore"; $phpcsFile->addError($error, $stackPtr); return; } // 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 // public. $testMethodName = $methodName; if ($scopeSpecified === false && $methodName[0] === '_') { $testMethodName = substr($methodName, 1); } if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) { if ($scopeSpecified === true) { $error = ucfirst($scope) . " method name \"{$className}::{$methodName}\" is not in camel caps format"; } else { $error = "Method name \"{$className}::{$methodName}\" is not in camel caps format"; } $phpcsFile->addError($error, $stackPtr); return; } }
/** * @param PHP_CodeSniffer_File $phpcsFile * @param integer $stackPtr * @param string $name */ protected function validate(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) { $reserved = ["_SERVER", "_GET", "_POST", "_REQUEST", "_SESSION", "_ENV", "_COOKIE", "_FILES", "GLOBALS"]; if (in_array($name, $reserved)) { return; } if (PHP_CodeSniffer::isCamelCaps($name, false, true, false)) { return; } $phpcsFile->addError("Variable \"{$name}\" is an invalid name", $stackPtr); }
/** * {@inheritdoc} */ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $varName = ltrim($tokens[$stackPtr]['content'], '$'); if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) { $error = 'Variable "%s" is not in valid camel caps format'; $data = array($varName); $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data); } elseif (preg_match('|\\d|', $varName)) { $warning = 'Variable "%s" contains numbers but this is discouraged'; $data = array($varName); $phpcsFile->addWarning($warning, $stackPtr, 'MemberVarContainsNumbers', $data); } }
/** * Override parent function to allow custom code validation * * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $methodName = $phpcsFile->getDeclarationName($stackPtr); if ($methodName === null) { // Ignore closures. return; } $className = $phpcsFile->getDeclarationName($currScope); // Is this a magic method. IE. is prefixed with "__". if (preg_match('|^__|', $methodName) !== 0) { $magicPart = substr($methodName, 2); if (in_array($magicPart, $this->magicMethods) === false) { $error = "Method name \"{$className}::{$methodName}\" is invalid; only PHP magic methods should be prefixed with a double underscore"; $phpcsFile->addError($error, $stackPtr); } return; } // PHP4 constructors are allowed to break our rules. if ($methodName === $className) { return; } // PHP4 destructors are allowed to break our rules. if ($methodName === '_' . $className) { return; } $methodProps = $phpcsFile->getMethodProperties($stackPtr); $isPublic = $methodProps['scope'] === 'private' ? false : true; $scope = $methodProps['scope']; $scopeSpecified = $methodProps['scope_specified']; // If it's a private method, it must have an underscore on the front. if ($isPublic === false && $methodName[0] !== '_') { $error = "Private method name \"{$className}::{$methodName}\" must be prefixed with an underscore"; $phpcsFile->addError($error, $stackPtr); return; } // We want to allow private and protected methods to start with an underscore $testMethodName = $methodName; if (($scopeSpecified === false || $scope == 'protected') && $methodName[0] === '_') { $testMethodName = substr($methodName, 1); } if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) { if ($scopeSpecified === true) { $error = ucfirst($scope) . " method name \"{$className}::{$methodName}\" is not in camel caps format"; } else { $error = "Method name \"{$className}::{$methodName}\" is not in camel caps format"; } $phpcsFile->addError($error, $stackPtr); return; } }
/** * Processes the tokens that this sniff is interested in. * * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. * @param int $stackPtr The position in the stack where * the token was found. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); if ($functionName === null) { return; } $errorData = array($functionName); if (PHP_CodeSniffer::isCamelCaps($functionName, false, true, false) === false) { if (!preg_match('~^get|set|scenario|button|__~', $functionName)) { $error = 'Function name "%s" is not in camel caps format'; $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); } } }
private function checkCamelCase(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $objVarName) { $originalVarName = $objVarName; if (substr($objVarName, 0, 1) === '_') { $error = 'Variable "%s" must not contain a leading underscore'; $data = [$originalVarName]; $phpcsFile->addError($error, $stackPtr, 'VariableHasUnderscore', $data); return; } if (PHP_CodeSniffer::isCamelCaps($objVarName, false, true, false) === false) { $error = 'Variable "%s" is not in valid camel caps format'; $data = [$originalVarName]; $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data); } }
/** * Processes the tokens within the scope. * * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $methodName = $phpcsFile->getDeclarationName($stackPtr); if ($methodName === null) { // Ignore closures. return; } $testName = ltrim($methodName, '_'); if (PHP_CodeSniffer::isCamelCaps($testName, false, true, false) === false) { $error = 'Method name "%s" is not in camel caps format'; $className = $phpcsFile->getDeclarationName($currScope); $errorData = array($className . '::' . $methodName); $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); } }
/** * Processes the tokens outside the scope. * * @param PHP_CodeSniffer_File $phpcsFile The file being processed. * @param int $stackPtr The position where this token was * found. * * @return void */ protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); // Does this function claim to be magical? if (preg_match('|^__|', $functionName) !== 0) { $magicPart = substr($functionName, 2); $error = "Function name \"{$functionName}\" is invalid; only PHP magic methods should be prefixed with a double underscore."; $phpcsFile->addError($error, $stackPtr); return; } if (PHP_CodeSniffer::isCamelCaps($functionName, false, true, false) === false) { $error = "Function name \"{$functionName}\" is not in camel caps format"; $phpcsFile->addError($error, $stackPtr); } }
protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); if ($functionName === null) { // Ignore closures. return; } // Only magic methods should be prefixed with "__" if (preg_match('|^__|', $functionName) !== 0) { $magicPart = substr($methodName, 2); if (in_array($magicPart, $this->magicFunctions) === false) { $error = sprintf("Function name \"%s\" is invalid; only PHP magic methods should be prefixed with a double underscore", $functionName); $phpcsFile->addError($error, $stackPtr); } return; } $underscorePos = strrpos($functionName, '_'); if ($underscorePos === false) { // If no underscores were found, the function name must begin lower-case with camel caps if ($functionName[0] == strtoupper($functionName[0])) { $error = sprintf("Function name \"%s\" is not in lower-cased format", $functionName); $phpcsFile->addError($error, $stackPtr); return; } if (PHP_CodeSniffer::isCamelCaps($functionName, false, true, false) === false) { $error = sprintf("Function name \"%s\" is not in camel caps format", $functionName); $phpcsFile->addError($error, $stackPtr); return; } } else { // Allow underscored function names for symfony <= 1.0 $parts = explode('_', $functionName); $validFunctionName = true; foreach ($parts as $part) { if ($part[0] == strtoupper($part[0])) { $validFunctionName = false; continue; } } if (!$validFunctionName) { $error = sprintf("Function name \"%s\" is not in valid. Try \"%s\" instead", $functionName, strtolower($functionName)); $phpcsFile->addError($error, $stackPtr); return; } } }
/** * Processes the tokens outside the scope. * * @param PHP_CodeSniffer_File $phpcsFile The file being processed. * @param int $stackPtr The position where this token was * found. * * @return void */ protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); if ($functionName === null) { return; } $errorData = array($functionName); // Does this function claim to be magical? if (preg_match('|^__|', $functionName) !== 0) { $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; $phpcsFile->addError($error, $stackPtr, 'DoubleUnderscore', $errorData); return; } if (PHP_CodeSniffer::isCamelCaps($functionName, false, true, false) === false) { $error = 'Function name "%s" is not in camel caps format'; $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); } }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); // Determine the name of the class or interface. Note that we cannot // simply look for the first T_STRING because a class name // starting with the number will be multiple tokens. $opener = $tokens[$stackPtr]['scope_opener']; $nameStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $opener, true); $nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener); $name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart)); // Check for camel caps format. $valid = PHP_CodeSniffer::isCamelCaps($name, false, true, false); if ($valid === false) { $type = ucfirst($tokens[$stackPtr]['content']); $error = "{$type} name \"{$name}\" is not in camel caps format"; $phpcsFile->addError($error, $stackPtr); } }
/** * Processes this sniff, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $functionName = $phpcsFile->findNext(array(T_STRING), $stackPtr); if ($this->isFunctionAMagicFunction($tokens[$functionName]['content']) === true || $this->isFunctionAPiBaseFunction($tokens[$functionName]['content']) === true) { return null; } $hasUnderscores = stripos($tokens[$functionName]['content'], '_'); $isLowerCamelCase = PHP_CodeSniffer::isCamelCaps($tokens[$functionName]['content'], false, true, true); $scope = $this->getCorrectScopeOfToken($tokens, $stackPtr); if ($hasUnderscores !== false) { $error = 'Underscores are not allowed in ' . $scope . ' names "' . $tokens[$functionName]['content'] . '"; '; $error .= 'use lowerCamelCase for ' . $scope . ' names instead'; $phpcsFile->addError($error, $stackPtr); } elseif ($isLowerCamelCase === false) { $error = ucfirst($scope) . ' name "' . $tokens[$functionName]['content'] . '" must use lowerCamelCase'; $phpcsFile->addError($error, $stackPtr); } }
/** * Processes the tokens within the scope. * * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $methodName = $phpcsFile->getDeclarationName($stackPtr); if ($methodName === null) { // Ignore closures. return; } // Ignore magic methods. $magicPart = strtolower(substr($methodName, 2)); if (in_array($magicPart, array_merge($this->magicMethods, $this->methodsDoubleUnderscore)) !== false) { return; } $testName = ltrim($methodName, '_'); if (PHP_CodeSniffer::isCamelCaps($testName, false, true, false) === false) { $error = 'Method name "%s" is not in camel caps format'; $className = $phpcsFile->getDeclarationName($currScope); $errorData = array($className . '::' . $methodName); $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); } }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (isset($tokens[$stackPtr]['scope_opener']) === false) { $error = 'Possible parse error: %s missing opening or closing brace'; $data = array($tokens[$stackPtr]['content']); $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); return; } $name = $phpcsFile->getDeclarationName($stackPtr); // Check for camel caps format. // ... but allow underscores for filepathing $valid = PHP_CodeSniffer::isCamelCaps(preg_replace('/^\\w+_/', '', $name), true, true, false); if ($valid === false) { $type = ucfirst($tokens[$stackPtr]['content']); $error = '%s name "%s" is not in camel caps format'; $data = array($type, $name); $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data); } $this->checkBraceSpacing($phpcsFile, $stackPtr); $this->checkBraceLine($phpcsFile, $stackPtr); }
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { // copied code from parent class $tokens = $phpcsFile->getTokens(); $varName = ltrim($tokens[$stackPtr]['content'], '$'); $memberProps = $phpcsFile->getMemberProperties($stackPtr); if (empty($memberProps) === true) { // Couldn't get any info about this variable, which // generally means it is invalid or possibly has a parse // error. Any errors will be reported by the core, so // we can ignore it. return; } // end of copied code // always validate as if the member variable is public $public = true; $errorData = array($varName); if (PHP_CodeSniffer::isCamelCaps($varName, false, $public, false) === false) { $error = 'Variable "%s" is not in valid camel caps format'; $phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $errorData); } }
/** * Processes class member variables. * * Extends Squiz.NamingConventions.ValidVariableName.processMemberVar to remove the requirement for leading underscores on * private member vars. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param integer $stackPtr The position of the current token in the stack passed in $tokens. * * @return void */ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $varName = ltrim($tokens[$stackPtr]['content'], '$'); $memberProps = $phpcsFile->getMemberProperties($stackPtr); if (empty($memberProps) === true) { // Couldn't get any info about this variable, which generally means it is invalid or possibly has a parse // error. Any errors will be reported by the core, so we can ignore it. return; } $errorData = [$varName]; if (substr($varName, 0, 1) === '_') { $error = '%s member variable "%s" must not contain a leading underscore'; $data = [ucfirst($memberProps['scope']), $errorData[0]]; $phpcsFile->addError($error, $stackPtr, 'ClassVarHasUnderscore', $data); return; } if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) { $error = 'Member variable "%s" is not in valid camel caps format'; $phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $errorData); } }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); // This method will only get the first namespace name. // For subspaces we need to iterate through subsequent // tokens and stop when we reach the first semicolon. // If we find anything else bet T_NAMESPACE or T_NS_SEPARATOR // then it will trigger an error. $nsIndex = $phpcsFile->findNext(T_STRING, $stackPtr); while ($tokens[$nsIndex]['type'] !== 'T_SEMICOLON') { if ($tokens[$nsIndex]['type'] == 'T_STRING') { $nsName = $tokens[$nsIndex]['content']; if (preg_match('|^[A-Z]|', $nsName) === 0) { $error = 'Each %s name must begin with a capital letter'; $errorData = array($tokens[$stackPtr]['content']); $phpcsFile->addError($error, $stackPtr, 'StartWithCaptial', $errorData); } if ($this->allowUnderscore === true) { if (preg_match('|_[a-z]|', $nsName) || preg_match('|[A-Z]{2}|', $nsName)) { $error = 'Namespace "%s" is not in camel caps with underscores format'; $phpcsFile->addError($error, $stackPtr, 'CamelCapsWithUnderscore', array($nsName)); } } else { if (PHP_CodeSniffer::isCamelCaps($nsName, false, true, true) === false) { $error = 'Namespace "%s" is not in camel caps format'; $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', array($nsName)); } } } elseif ($tokens[$nsIndex]['type'] !== 'T_NS_SEPARATOR') { $error = 'Invalid token `%s` found in namespace definition'; $errorData = array($tokens[$nsIndex]['type']); $phpcsFile->addError($error, $stackPtr, 'InvalidToken', $errorData); } $nsIndex++; } // end while }
/** * Processes normal variables. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $varName = ltrim($tokens[$stackPtr]['content'], '$'); $phpReservedVars = array('_SERVER', '_GET', '_POST', '_REQUEST', '_SESSION', '_ENV', '_COOKIE', '_FILES', 'GLOBALS'); // If it's a php reserved var, then its ok. if (in_array($varName, $phpReservedVars) === true) { return; } // If it is a static public variable of a class, then its ok. if ($tokens[$stackPtr - 1]['code'] === T_DOUBLE_COLON) { return; } if (preg_match('/[A-Z]/', $varName) && PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === true) { $error = "Variable \"{$varName}\" is camel caps format. do not use mixed case (camelCase), use lower case and _"; $phpcsFile->addError($error, $stackPtr); } // Strange error with $stackPtr prevents us from using this code. /* $tokens = $phpcsFile->getTokens(); $stst = $stackPtr; $memberProps = $phpcsFile->getMemberProperties($stst); if (empty($memberProps) === true) { return; } $memberName = ltrim($tokens[$stackPtr]['content'], '$'); $isGlobal = ($memberProps['scope'] === 'global') ? false : true; if ($isGlobal && $memberName{0} != '_') { $error = "Global variable \"$memberName\" must be prefixed with an underscore before module name"; $phpcsFile->addError($error, $stackPtr); return; } */ }
/** * Processes this sniff, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $functionName = $phpcsFile->findNext(array(T_STRING), $stackPtr); if ($this->isFunctionAMagicFunction($tokens[$functionName]['content']) === true || $this->isFunctionAPiBaseFunction($tokens[$functionName]['content']) === true || $this->isFunctionUserFunction($tokens[$functionName]['content']) === true) { return; } $hasUnderscores = stripos($tokens[$functionName]['content'], '_'); $isLowerCamelCase = PHP_CodeSniffer::isCamelCaps($tokens[$functionName]['content'], false, true, true); $scope = $this->getCorrectScopeOfToken($tokens, $stackPtr); if ($hasUnderscores !== false) { $error = 'Underscores are not allowed in %s names "%s"; '; $error .= 'use lowerCamelCase for %s names instead'; $error .= ''; $data = array($scope, $tokens[$functionName]['content'], $scope); $phpcsFile->addError($error, $stackPtr, 'UnderscoresInFunctionName', $data); } else { if ($isLowerCamelCase === false) { $error = '%s name "%s" must use lowerCamelCase'; $data = array(ucfirst($scope), $tokens[$functionName]['content']); $phpcsFile->addError($error, $stackPtr, 'FilenameLowerCase', $data); } } }
/** * Processes the tokens outside the scope. * * @param PHP_CodeSniffer_File $phpcsFile The file being processed. * @param int $stackPtr The position where this token was * found. * * @return void */ protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); if ($functionName === null) { // Ignore closures. return; } if (ltrim($functionName, '_') === '') { // Ignore special functions. return; } $errorData = array($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]) === false) { $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; $phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData); } return; } // Function names can be in two parts; the package name and // the function name. $packagePart = ''; $camelCapsPart = ''; $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); return; } 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); return; } } // 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 (PHP_CodeSniffer::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); 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) { $newName = rtrim($newPackagePart, '_') . '_' . $newCamelCapsPart; 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); } }