Chris@0: getTokens(); Chris@0: Chris@0: $memberProps = $phpcsFile->getMemberProperties($stackPtr); Chris@0: if (empty($memberProps) === true) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $memberName = ltrim($tokens[$stackPtr]['content'], '$'); Chris@0: Chris@0: if (strpos($memberName, '_') === false) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Check if the class extends another class and get the name of the class Chris@0: // that is extended. Chris@0: if (empty($tokens[$stackPtr]['conditions']) === false) { Chris@0: $classPtr = key($tokens[$stackPtr]['conditions']); Chris@0: $extendsName = $phpcsFile->findExtendedClassName($classPtr); Chris@0: Chris@0: // Special case config entities: those are allowed to have underscores in Chris@0: // their class property names. If a class extends something like Chris@0: // ConfigEntityBase then we consider it a config entity class and allow Chris@0: // underscores. Chris@0: if ($extendsName !== false && strpos($extendsName, 'ConfigEntity') !== false) { Chris@0: return; Chris@0: } Chris@0: } Chris@0: Chris@0: $error = 'Class property %s should use lowerCamel naming without underscores'; Chris@0: $data = array($tokens[$stackPtr]['content']); Chris@0: $phpcsFile->addError($error, $stackPtr, 'LowerCamelName', $data); Chris@0: Chris@0: }//end processMemberVar() Chris@0: Chris@0: Chris@0: /** Chris@0: * Processes normal variables. Chris@0: * Chris@17: * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found. Chris@17: * @param int $stackPtr The position where the token was found. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@17: protected function processVariable(File $phpcsFile, $stackPtr) Chris@0: { Chris@0: $tokens = $phpcsFile->getTokens(); Chris@0: Chris@0: $varName = ltrim($tokens[$stackPtr]['content'], '$'); Chris@0: Chris@0: $phpReservedVars = array( Chris@0: '_SERVER', Chris@0: '_GET', Chris@0: '_POST', Chris@0: '_REQUEST', Chris@0: '_SESSION', Chris@0: '_ENV', Chris@0: '_COOKIE', Chris@0: '_FILES', Chris@0: 'GLOBALS', Chris@0: ); Chris@0: Chris@0: // If it's a php reserved var, then its ok. Chris@0: if (in_array($varName, $phpReservedVars) === true) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // If it is a static public variable of a class, then its ok. Chris@0: if ($tokens[($stackPtr - 1)]['code'] === T_DOUBLE_COLON) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if (preg_match('/^[A-Z]/', $varName) === 1) { Chris@0: $error = "Variable \"$varName\" starts with a capital letter, but only \$lowerCamelCase or \$snake_case is allowed"; Chris@0: $phpcsFile->addError($error, $stackPtr, 'LowerStart'); Chris@0: } Chris@0: Chris@0: }//end processVariable() Chris@0: Chris@0: Chris@0: /** Chris@0: * Processes variables in double quoted strings. Chris@0: * Chris@17: * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found. Chris@17: * @param int $stackPtr The position where the token was found. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@17: protected function processVariableInString(File $phpcsFile, $stackPtr) Chris@0: { Chris@0: // We don't care about variables in strings. Chris@0: return; Chris@0: Chris@0: }//end processVariableInString() Chris@0: Chris@0: Chris@0: }//end class