Beispiel #1
0
 /**
  * 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(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);
     if ($nameEnd === false) {
         $name = $tokens[$nameStart]['content'];
     } else {
         $name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart));
     }
     // Check for camel caps format.
     $valid = Common::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);
         $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'no');
     } else {
         $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'yes');
     }
 }
 /**
  * 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(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 ($testName !== '' && Common::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 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(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 (Common::isCamelCaps($functionName, false, true, false) === false) {
         $error = 'Function name "%s" is not in camel caps format';
         $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(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 (Common::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);
     }
 }
 /**
  * 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(File $phpcsFile, $stackPtr)
 {
     $functionName = $phpcsFile->getDeclarationName($stackPtr);
     if ($functionName === null) {
         // Ignore closures.
         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;
     }
     // Ignore first underscore in functions prefixed with "_".
     $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');
     }
 }
 /**
  * Processes the variable found within a double quoted string.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  * @param int                  $stackPtr  The position of the double quoted
  *                                        string.
  *
  * @return void
  */
 protected function processVariableInString(File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $phpReservedVars = array('_SERVER', '_GET', '_POST', '_REQUEST', '_SESSION', '_ENV', '_COOKIE', '_FILES', 'GLOBALS', 'http_response_header', 'HTTP_RAW_POST_DATA', 'php_errormsg');
     if (preg_match_all('|[^\\\\]\\$([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
         foreach ($matches[1] as $varName) {
             // If it's a php reserved var, then its ok.
             if (in_array($varName, $phpReservedVars) === true) {
                 continue;
             }
             if (Common::isCamelCaps($varName, false, true, false) === false) {
                 $error = 'Variable "%s" is not in valid camel caps format';
                 $data = array($varName);
                 $phpcsFile->addError($error, $stackPtr, 'StringVarNotCamelCaps', $data);
             } else {
                 if (preg_match('|\\d|', $varName) === 1) {
                     $warning = 'Variable "%s" contains numbers but this is discouraged';
                     $data = array($varName);
                     $phpcsFile->addWarning($warning, $stackPtr, 'StringVarContainsNumbers', $data);
                 }
             }
         }
         //end foreach
     }
     //end if
 }