annotate vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidVariableNameSniff.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@17 3 * \Drupal\Sniffs\NamingConventions\ValidVariableNameSniff.
Chris@0 4 *
Chris@0 5 * @category PHP
Chris@0 6 * @package PHP_CodeSniffer
Chris@0 7 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 8 */
Chris@0 9
Chris@17 10 namespace Drupal\Sniffs\NamingConventions;
Chris@17 11
Chris@17 12 use PHP_CodeSniffer\Files\File;
Chris@17 13 use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
Chris@17 14
Chris@0 15 /**
Chris@17 16 * \Drupal\Sniffs\NamingConventions\ValidVariableNameSniff.
Chris@0 17 *
Chris@0 18 * Checks the naming of member variables.
Chris@0 19 *
Chris@0 20 * @category PHP
Chris@0 21 * @package PHP_CodeSniffer
Chris@0 22 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 23 */
Chris@17 24 class ValidVariableNameSniff extends AbstractVariableSniff
Chris@0 25 {
Chris@0 26
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Processes class member variables.
Chris@0 30 *
Chris@17 31 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
Chris@17 32 * @param int $stackPtr The position of the current token
Chris@17 33 * in the stack passed in $tokens.
Chris@0 34 *
Chris@0 35 * @return void
Chris@0 36 */
Chris@17 37 protected function processMemberVar(File $phpcsFile, $stackPtr)
Chris@0 38 {
Chris@0 39 $tokens = $phpcsFile->getTokens();
Chris@0 40
Chris@0 41 $memberProps = $phpcsFile->getMemberProperties($stackPtr);
Chris@0 42 if (empty($memberProps) === true) {
Chris@0 43 return;
Chris@0 44 }
Chris@0 45
Chris@0 46 $memberName = ltrim($tokens[$stackPtr]['content'], '$');
Chris@0 47
Chris@0 48 if (strpos($memberName, '_') === false) {
Chris@0 49 return;
Chris@0 50 }
Chris@0 51
Chris@0 52 // Check if the class extends another class and get the name of the class
Chris@0 53 // that is extended.
Chris@0 54 if (empty($tokens[$stackPtr]['conditions']) === false) {
Chris@0 55 $classPtr = key($tokens[$stackPtr]['conditions']);
Chris@0 56 $extendsName = $phpcsFile->findExtendedClassName($classPtr);
Chris@0 57
Chris@0 58 // Special case config entities: those are allowed to have underscores in
Chris@0 59 // their class property names. If a class extends something like
Chris@0 60 // ConfigEntityBase then we consider it a config entity class and allow
Chris@0 61 // underscores.
Chris@0 62 if ($extendsName !== false && strpos($extendsName, 'ConfigEntity') !== false) {
Chris@0 63 return;
Chris@0 64 }
Chris@0 65 }
Chris@0 66
Chris@0 67 $error = 'Class property %s should use lowerCamel naming without underscores';
Chris@0 68 $data = array($tokens[$stackPtr]['content']);
Chris@0 69 $phpcsFile->addError($error, $stackPtr, 'LowerCamelName', $data);
Chris@0 70
Chris@0 71 }//end processMemberVar()
Chris@0 72
Chris@0 73
Chris@0 74 /**
Chris@0 75 * Processes normal variables.
Chris@0 76 *
Chris@17 77 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
Chris@17 78 * @param int $stackPtr The position where the token was found.
Chris@0 79 *
Chris@0 80 * @return void
Chris@0 81 */
Chris@17 82 protected function processVariable(File $phpcsFile, $stackPtr)
Chris@0 83 {
Chris@0 84 $tokens = $phpcsFile->getTokens();
Chris@0 85
Chris@0 86 $varName = ltrim($tokens[$stackPtr]['content'], '$');
Chris@0 87
Chris@0 88 $phpReservedVars = array(
Chris@0 89 '_SERVER',
Chris@0 90 '_GET',
Chris@0 91 '_POST',
Chris@0 92 '_REQUEST',
Chris@0 93 '_SESSION',
Chris@0 94 '_ENV',
Chris@0 95 '_COOKIE',
Chris@0 96 '_FILES',
Chris@0 97 'GLOBALS',
Chris@0 98 );
Chris@0 99
Chris@0 100 // If it's a php reserved var, then its ok.
Chris@0 101 if (in_array($varName, $phpReservedVars) === true) {
Chris@0 102 return;
Chris@0 103 }
Chris@0 104
Chris@0 105 // If it is a static public variable of a class, then its ok.
Chris@0 106 if ($tokens[($stackPtr - 1)]['code'] === T_DOUBLE_COLON) {
Chris@0 107 return;
Chris@0 108 }
Chris@0 109
Chris@0 110 if (preg_match('/^[A-Z]/', $varName) === 1) {
Chris@0 111 $error = "Variable \"$varName\" starts with a capital letter, but only \$lowerCamelCase or \$snake_case is allowed";
Chris@0 112 $phpcsFile->addError($error, $stackPtr, 'LowerStart');
Chris@0 113 }
Chris@0 114
Chris@0 115 }//end processVariable()
Chris@0 116
Chris@0 117
Chris@0 118 /**
Chris@0 119 * Processes variables in double quoted strings.
Chris@0 120 *
Chris@17 121 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
Chris@17 122 * @param int $stackPtr The position where the token was found.
Chris@0 123 *
Chris@0 124 * @return void
Chris@0 125 */
Chris@17 126 protected function processVariableInString(File $phpcsFile, $stackPtr)
Chris@0 127 {
Chris@0 128 // We don't care about variables in strings.
Chris@0 129 return;
Chris@0 130
Chris@0 131 }//end processVariableInString()
Chris@0 132
Chris@0 133
Chris@0 134 }//end class