/**
  * Processes this test, 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();
     if (isset($tokens[$stackPtr]['scope_opener']) === false) {
         //$error  = 'Possible parse error: ';
         //$error .= $tokens[$stackPtr]['content'];
         //$error .= ' missing opening or closing brace';
         //$phpcsFile->addWarning($error, $stackPtr);
         $phpcsFile->addEvent('XP_CLASS_DECLARATION_MISSING_BRACE', array(), $stackPtr);
         return;
     }
     $curlyBrace = $tokens[$stackPtr]['scope_opener'];
     $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $curlyBrace - 1, $stackPtr, true);
     $classLine = $tokens[$lastContent]['line'];
     $braceLine = $tokens[$curlyBrace]['line'];
     /* Marius - useless check
        if ($braceLine > ($classLine + 1)) {
            $difference  = ($braceLine - $classLine - 1);
            $difference .= ($difference === 1) ? ' line' : ' lines';
            $error       = 'Opening brace of a ';
            $error      .= $tokens[$stackPtr]['content'];
            $error      .= ' must be on the same line as the ';
            $error      .= $tokens[$stackPtr]['content'];
            $error      .= ' declaration; found '.$difference;
            $phpcsFile->addError($error, $curlyBrace);
            return;
        }
        */
     if ($tokens[$curlyBrace + 1]['content'] !== $phpcsFile->eolChar) {
         //$type  = strtolower($tokens[$stackPtr]['content']);
         //$error = "Opening $type brace must be on a line by itself";
         //$phpcsFile->addError($error, $curlyBrace);
         $phpcsFile->addEvent('XP_CLASS_DECLARATION_OPENING_BRACE_WHITESPACE', array(), $curlyBrace);
     }
     if ($tokens[$curlyBrace - 1]['code'] === T_WHITESPACE) {
         $prevContent = $tokens[$curlyBrace - 1]['content'];
         if ($prevContent !== $phpcsFile->eolChar) {
             $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
             $spaces = strlen($blankSpace);
             if ($spaces !== 1) {
                 //$error = "Expected 1 space before opening brace; $spaces found";
                 //$phpcsFile->addError($error, $curlyBrace);
                 $phpcsFile->addEvent('XP_CLASS_DECLARATION_ONE_SPACE_BEFORE_OPENING_BRACE', array(), $curlyBrace);
             }
         }
     } else {
         //$error = "Expected 1 space before opening brace; 0 found";
         //$phpcsFile->addError($error, $curlyBrace);
         $phpcsFile->addEvent('XP_CLASS_DECLARATION_ONE_SPACE_BEFORE_OPENING_BRACE', array(), $curlyBrace);
     }
 }
 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @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
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $decName = $phpcsFile->findNext(T_STRING, $stackPtr);
     $fileName = dirname($phpcsFile->getFilename());
     $GnPosition = strrpos($fileName, DIRECTORY_SEPARATOR . 'GN');
     if (false === $GnPosition) {
         return;
     }
     $fileName = substr($fileName, $GnPosition + 1);
     $fileName .= DIRECTORY_SEPARATOR . basename($phpcsFile->getFilename());
     $fileName = substr($fileName, 0, strrpos($fileName, '.'));
     $className = $fileName;
     $className = substr($className, strpos($className, '_'));
     $className = substr($className, strpos($className, DIRECTORY_SEPARATOR) + 1);
     $fileName = str_replace(DIRECTORY_SEPARATOR, '_', $fileName);
     $className = str_replace(DIRECTORY_SEPARATOR, '_', $className);
     if (strpos($fileName, '__') === false) {
         $className = $fileName;
     }
     if ($tokens[$decName]['content'] !== $fileName and $tokens[$decName]['content'] !== $className) {
         $name = ucfirst($tokens[$stackPtr]['content']);
         $file .= '"' . $tokens[$stackPtr]['content'] . ' ' . $className . '".';
         $phpcsFile->addEvent('MATCH_CLASS_NAME', array('name' => $name, 'file' => $file), $stackPtr);
     }
 }
 /**
  * Lance le sniff, lorsqu'un des types de segments est d๏ฟฝtect๏ฟฝ
  *
  * @param PHP_CodeSniffer_File $phpcsFile Le fichier actuellement analys๏ฟฝ.
  * @param int                  $stackPtr  La position du segment actuel dans la pile $tokens.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $operator = $tokens[$stackPtr]['content'];
     if ($tokens[$stackPtr - 1]['code'] === T_WHITESPACE) {
         $phpcsFile->addEvent('EXPECTED_SPACE_DECREMENTSPACING', array('operator' => $operator), $stackPtr);
     }
 }
 /**
  * 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();
     $className = $phpcsFile->findNext(T_STRING, $stackPtr);
     $name = trim($tokens[$className]['content']);
     // Interface names ends with Interface suffix
     if (strrpos($name, 'Interface') !== strlen($name) - strlen('Interface')) {
         $phpcsFile->addEvent('NAME_END_VALID_INTERFACE', array('name' => $name), $stackPtr);
     }
 }
 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @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
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     // Check that this is the only class or interface in the file
     $stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), $stackPtr + 1);
     if ($stackPtr !== false) {
         // We have another, so an error is thrown
         //$error = 'Only one interface or class is allowed in a file';
         //$phpcsFile->addError($error, $nextClass);
         $phpcsFile->addEvent('MULTIPLE_CLASS_OR_INTERFACE_IN_SINGLE_FILE', array(), $stackPtr);
     }
 }
Example #6
0
 /**
  * Processes this test, 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)
 {
     if (0 !== $stackPtr) {
         //$phpcsFile->addError('Extra content before open tag', $stackPtr);
         $phpcsFile->addEvent('XP_OPEN_TAG_EXTRA_CONTENT_BEFORE_OPEN_TAG', array(), $stackPtr);
     }
     $tokens = $phpcsFile->getTokens();
     $openTag = $tokens[$stackPtr];
     if ($openTag['content'] === '<?') {
         $error = 'Short PHP opening tag used. Found "' . $openTag['content'] . '" Expected "<?php".';
         //$phpcsFile->addError($error, $stackPtr);
         $phpcsFile->addEvent('XP_OPEN_TAG_SHORT_PHP_OPENING_TAG', array('message' => $error), $stackPtr);
     }
     if ($openTag['code'] === T_OPEN_TAG_WITH_ECHO) {
         $nextVar = $tokens[$phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true)];
         $error = 'Short PHP opening tag used with echo. Found "';
         $error .= $openTag['content'] . ' ' . $nextVar['content'] . ' ..." but expected "<?php echo ' . $nextVar['content'] . ' ...".';
         //$phpcsFile->addError($error, $stackPtr);
         $phpcsFile->addEvent('XP_OPEN_TAG_SHORT_PHP_OPENING_TAG_WITH_ECHO', array('message' => $error), $stackPtr);
     }
 }
 /**
  * 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();
     $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr, NULL, FALSE, NULL, TRUE);
     if ($modifier === false) {
         // || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) {
         $var = $tokens[$stackPtr]['content'];
         $error = 'Scope modifier not specified for member variable "' . $var . '"';
         //$phpcsFile->addError($error, $stackPtr);
         $phpcsFile->addEvent('XP_MEMBER_VAR_SCOPE_NOT_SPECIFIED', array('message' => $error), $stackPtr);
     }
 }
 /**
  * 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();
     $className = $phpcsFile->findNext(T_STRING, $stackPtr);
     $name = trim($tokens[$className]['content']);
     // Make sure the first letter is a capital.
     if (preg_match('|^[A-Z]|', $name) === 0) {
         $phpcsFile->addEvent('NAME_START_LETTER_VALID_CLASS', array('name' => ucfirst($tokens[$stackPtr]['content'])), $stackPtr);
     }
     // Check that each new word starts with a capital as well, but don't
     // check the first word, as it is checked above.
     $validName = true;
     $nameBits = explode('_', $name);
     $firstBit = array_shift($nameBits);
     foreach ($nameBits as $bit) {
         if ($bit === '' || $bit[0] !== strtoupper($bit[0])) {
             $validName = false;
             break;
         }
     }
     if ($validName !== true) {
         // Strip underscores because they cause the suggested name
         // to be incorrect.
         $nameBits = explode('_', trim($name, '_'));
         $firstBit = array_shift($nameBits);
         if ($firstBit === '') {
             $phpcsFile->addEvent('NAME_NOT_VALID_VALID_CLASS', array('name' => ucfirst($tokens[$stackPtr]['content'])), $stackPtr);
         } else {
             $newName = strtoupper($firstBit[0]) . substr($firstBit, 1) . '_';
             foreach ($nameBits as $bit) {
                 if ($bit !== '') {
                     $newName .= strtoupper($bit[0]) . substr($bit, 1) . '_';
                 }
             }
             $newName = rtrim($newName, '_');
             $phpcsFile->addEvent('NAME_NOT_VALID_NEWNAME_VALID_CLASS', array('name' => ucfirst($tokens[$stackPtr]['content']), 'newname' => $newName), $stackPtr);
         }
     }
     //end if
 }
 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
  * @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 this is an inline condition (ie. there is no scope opener), then
     // return, as this is not a new scope.
     if (isset($tokens[$stackPtr]['scope_closer']) === false) {
         return;
     }
     // We need to actually find the first piece of content on this line,
     // as if this is a method with tokens before it (public, static etc)
     // or an if with an else before it, then we need to start the scope
     // checking from there, rather than the current token.
     $lineStart = $stackPtr - 1;
     for ($lineStart; $lineStart > 0; $lineStart--) {
         if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
             break;
         }
     }
     // We found a new line, now go forward and find the first non-whitespace
     // token.
     $lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart + 1, null, true);
     $startColumn = $tokens[$lineStart]['column'];
     $scopeStart = $tokens[$stackPtr]['scope_opener'];
     $scopeEnd = $tokens[$stackPtr]['scope_closer'];
     // Check that the closing brace is on it's own line.
     $lastContent = $phpcsFile->findPrevious(array(T_WHITESPACE), $scopeEnd - 1, $scopeStart, true);
     if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
         $error = 'Closing brace must be on a line by itself';
         //$phpcsFile->addError($error, $scopeEnd);
         $phpcsFile->addEvent('XP_SCOPE_CLOSING_BRACE_CLOSING_BRACE_MUST_BE_ON_A_LINE_BY_ITSELF', array('message' => $error), $scopeEnd);
         return;
     }
     // Check now that the closing brace is lined up correctly.
     $braceIndent = $tokens[$scopeEnd]['column'];
     $isBreakCloser = $tokens[$scopeEnd]['code'] === T_BREAK;
     if (in_array($tokens[$stackPtr]['code'], array(T_CASE, T_DEFAULT)) === true && $isBreakCloser === true) {
         // BREAK statements should be indented 4 spaces from the
         // CASE or DEFAULT statement.
         if ($braceIndent !== $startColumn + 2) {
             $error = 'Break statement indented incorrectly; expected ' . ($startColumn + 1) . ' spaces, found ' . ($braceIndent - 1);
             //$phpcsFile->addError($error, $scopeEnd);
             $phpcsFile->addEvent('XP_SCOPE_CLOSING_BRACE_BREAK_STATEMENT_INDENTED_INCORRECTLY', array('message' => $error), $scopeEnd);
         }
     } else {
         if ($braceIndent !== $startColumn) {
             $error = 'Closing brace indented incorrectly; expected ' . ($startColumn - 1) . ' spaces, found ' . ($braceIndent - 1);
             //$phpcsFile->addError($error, $scopeEnd);
             $phpcsFile->addEvent('XP_SCOPE_CLOSING_BRACE_CLOSING_BRACE_INDENTED_INCORRECTLY', array('message' => $error), $scopeEnd);
         }
     }
 }
 /**
  * Processes this test, when one of its tokens is encountered
  *
  * @param  PHP_CodeSniffer_File $phpcsFile All the tokens found in the document
  * @param  integer              $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 this is an inline condition (ie. there is no scope opener), then
     // return, as this is not a new scope.
     if (isset($tokens[$stackPtr]['scope_closer']) === false) {
         return;
     }
     // We need to actually find the first piece of content on this line,
     // as if this is a method with tokens before it (public, static etc)
     // or an if with an else before it, then we need to start the scope
     // checking from there, rather than the current token.
     $lineStart = $stackPtr - 1;
     for ($lineStart; $lineStart > 0; $lineStart--) {
         if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
             break;
         }
     }
     // We found a new line, now go forward and find the first non-whitespace
     // token.
     $lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart + 1, null, true);
     $startColumn = $tokens[$lineStart]['column'];
     $scopeStart = $tokens[$stackPtr]['scope_opener'];
     $scopeEnd = $tokens[$stackPtr]['scope_closer'];
     // Check that the closing brace is on it's own line.
     $lastContent = $phpcsFile->findPrevious(array(T_WHITESPACE), $scopeEnd - 1, $scopeStart, true);
     if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
         $phpcsFile->addEvent('ONELINE_SCOPE_CLOSINGBRACE', array('name' => ucfirst($tokens[$stackPtr]['content'])), $scopeEnd);
         return;
     }
     // SQLI ce qui suit ne nous intรฉresse pas dans ce contexte, de plus un nombre d'espaces est codรฉ en dur
     //        // Check now that the closing brace is lined up correctly.
     //        $braceIndent   = $tokens[$scopeEnd]['column'];
     //        $isBreakCloser = ($tokens[$scopeEnd]['code'] === T_BREAK);
     //        if (in_array($tokens[$stackPtr]['code'], array(T_CASE, T_DEFAULT)) === true and $isBreakCloser === true) {
     //            // BREAK statements should be indented 4 spaces from the
     //            // CASE or DEFAULT statement.
     //            if ($braceIndent !== ($startColumn + 4)) {
     //                $error = 'Break statement indented incorrectly; expected ' . ($startColumn + 3)
     //                       . ' spaces, found ' . ($braceIndent - 1);
     //                $phpcsFile->addError($error, $scopeEnd);
     //            }
     //        } else {
     //            if ($braceIndent !== $startColumn) {
     //                $error = 'Closing brace indented incorrectly; expected ' . ($startColumn - 1)
     //                       . ' spaces, found ' . ($braceIndent - 1);
     //                $phpcsFile->addError($error, $scopeEnd);
     //            }
     //        }
 }
 /**
  * Processes this test, 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();
     // Check if there is a scope opening after a closing bracket
     if ($tokens[$stackPtr]['code'] == T_CLOSE_PARENTHESIS) {
         $check = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, NULL, TRUE);
         if ($tokens[$check]['code'] == T_OPEN_CURLY_BRACKET) {
             // var_dump($tokens[$check]);
         }
         return;
     }
     $nextToken = $tokens[$stackPtr + 1];
     if ($nextToken['code'] === T_WHITESPACE) {
     } else {
         $error = sprintf('Not enough whitespace after Keyword: %s', $tokens[$stackPtr]['content']);
         //$phpcsFile->addError($error, $stackPtr);
         $phpcsFile->addEvent('XP_WHITESPACE_AFTER_KEYWORD_MISSING', array('message' => $error), $stackPtr);
     }
 }
 /**
  * Processes this test, 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);
     $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
     $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
     $multiLine = $tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line'];
     $nextParam = $openBracket;
     $params = array();
     while (($nextParam = $phpcsFile->findNext(T_VARIABLE, $nextParam + 1, $closeBracket)) !== false) {
         $nextToken = $phpcsFile->findNext(T_WHITESPACE, $nextParam + 1, $closeBracket + 1, true);
         if ($nextToken === false) {
             break;
         }
         $nextCode = $tokens[$nextToken]['code'];
         if ($nextCode === T_EQUAL) {
             // Check parameter default spacing.
             if ($nextToken - $nextParam > 1) {
                 //$gap   = strlen($tokens[($nextParam + 1)]['content']);
                 //$arg   = $tokens[$nextParam]['content'];
                 //$error = "Expected 0 spaces between argument \"$arg\" and equals sign; $gap found";
                 //$phpcsFile->addError($error, $nextToken);
                 $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_SPACING_NOPSACE_BEFORE_EQUALS', array(), $nextToken);
             }
             if ($tokens[$nextToken + 1]['code'] === T_WHITESPACE && $tokens[$nextToken + 1]['content'] !== ' ') {
                 //$gap   = strlen($tokens[($nextToken + 1)]['content']);
                 //$arg   = $tokens[$nextParam]['content'];
                 //$error = "Expected 1 space between default value and equals sign for argument \"$arg\"; $gap found";
                 //$phpcsFile->addError($error, $nextToken);
                 $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_SPACING_ONESPACE_AFTER_EQUALS', array(), $nextToken);
             }
             if ($tokens[$nextToken + 1]['code'] !== T_WHITESPACE) {
                 //$arg   = $tokens[$nextParam]['content'];
                 //$error = "Expected 1 space between default value and equals sign for argument \"$arg\"; 0 found";
                 //$phpcsFile->addError($error, $nextToken);
                 $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_SPACING_ONESPACE_AFTER_EQUALS', array(), $nextToken);
             }
         }
         // Find and check the comma (if there is one).
         $nextComma = $phpcsFile->findNext(T_COMMA, $nextParam + 1, $closeBracket);
         if ($nextComma !== false) {
             // Comma found.
             if ($tokens[$nextComma - 1]['code'] === T_WHITESPACE) {
                 //$space = strlen($tokens[($nextComma - 1)]['content']);
                 //$arg   = $tokens[$nextParam]['content'];
                 //$error = "Expected 0 spaces between argument \"$arg\" and comma; $space found";
                 //$phpcsFile->addError($error, $nextToken);
                 $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_SPACING_NOSPACE_BEFORE_COMMA', array(), $nextToken);
             }
         }
         // Take references into account when expecting the
         // location of whitespace.
         if ($phpcsFile->isReference($nextParam - 1) === true) {
             $whitespace = $tokens[$nextParam - 2];
         } else {
             $whitespace = $tokens[$nextParam - 1];
         }
         if (empty($params) === false) {
             // This is not the first argument in the function declaration.
             $arg = $tokens[$nextParam]['content'];
             if ($whitespace['code'] === T_WHITESPACE) {
                 $gap = strlen($whitespace['content']);
                 // Before we throw an error, make sure there is no type hint.
                 $comma = $phpcsFile->findPrevious(T_COMMA, $nextParam - 1);
                 $nextToken = $phpcsFile->findNext(T_WHITESPACE, $comma + 1, null, true);
                 if ($phpcsFile->isReference($nextToken) === true) {
                     $nextToken++;
                 }
                 if ($nextToken !== $nextParam) {
                     // There was a type hint, so check the spacing between
                     // the hint and the variable as well.
                     $hint = $tokens[$nextToken]['content'];
                     if ($gap !== 1) {
                         //$error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
                         //$phpcsFile->addError($error, $nextToken);
                         $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_ONESPACE_AFTER_HINT', array(), $nextToken);
                     }
                     if ($multiLine === false) {
                         if ($tokens[$comma + 1]['code'] !== T_WHITESPACE) {
                             //$error = "Expected 1 space between comma and type hint \"$hint\"; 0 found";
                             //$phpcsFile->addError($error, $nextToken);
                             $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_ONESPACE_BETWEEN_COMMA_AND_HINT', array(), $nextToken);
                         } else {
                             $gap = strlen($tokens[$comma + 1]['content']);
                             if ($gap !== 1) {
                                 //$error = "Expected 1 space between comma and type hint \"$hint\"; $gap found";
                                 //$phpcsFile->addError($error, $nextToken);
                                 $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_ONESPACE_BETWEEN_COMMA_AND_HINT', array(), $nextToken);
                             }
                         }
                     }
                 } else {
                     if ($multiLine === false && $gap !== 1) {
                         //$error = "Expected 1 space between comma and argument \"$arg\"; $gap found";
                         //$phpcsFile->addError($error, $nextToken);
                         $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_ONESPACE_BETWEEN_COMMA_AND_ARGUMENT', array(), $nextToken);
                     }
                 }
                 //end if
             } else {
                 //$error = "Expected 1 space between comma and argument \"$arg\"; 0 found";
                 //$phpcsFile->addError($error, $nextToken);
                 $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_ONESPACE_BETWEEN_COMMA_AND_ARGUMENT', array(), $nextToken);
             }
             //end if
         } else {
             // First argument in function declaration.
             if ($whitespace['code'] === T_WHITESPACE) {
                 $gap = strlen($whitespace['content']);
                 $arg = $tokens[$nextParam]['content'];
                 // Before we throw an error, make sure there is no type hint.
                 $bracket = $phpcsFile->findPrevious(T_OPEN_PARENTHESIS, $nextParam - 1);
                 $nextToken = $phpcsFile->findNext(T_WHITESPACE, $bracket + 1, null, true);
                 if ($phpcsFile->isReference($nextToken) === true) {
                     $nextToken++;
                 }
                 if ($nextToken !== $nextParam) {
                     // There was a type hint, so check the spacing between
                     // the hint and the variable as well.
                     $hint = $tokens[$nextToken]['content'];
                     if ($gap !== 1) {
                         //$error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
                         //$phpcsFile->addError($error, $nextToken);
                         $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_ONESPACE_AFTER_HINT', array(), $nextToken);
                     }
                     if ($multiLine === false && $tokens[$bracket + 1]['code'] === T_WHITESPACE) {
                         //$gap   = strlen($tokens[($bracket + 1)]['content']);
                         //$error = "Expected 0 spaces between opening bracket and type hint \"$hint\"; $gap found";
                         //$phpcsFile->addError($error, $nextToken);
                         $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_ONESPACE_BETWEEN_BRACKET_AND_HINT', array(), $nextToken);
                     }
                 } else {
                     if ($multiLine === false) {
                         //$error = "Expected 0 spaces between opening bracket and argument \"$arg\"; $gap found";
                         //$phpcsFile->addError($error, $nextToken);
                         $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_ONESPACE_BETWEEN_BRACKET_AND_ARGUMENT', array(), $nextToken);
                     }
                 }
             }
             //end if
         }
         //end if
         $params[] = $nextParam;
     }
     //end while
     if (empty($params) === true) {
         // There are no parameters for this function.
         if ($closeBracket - $openBracket !== 1) {
             //$space = strlen($tokens[($closeBracket - 1)]['content']);
             //$error = "Expected 0 spaces between brackets of function declaration; $space found";
             //$phpcsFile->addError($error, $stackPtr);
             $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_ONESPACE_BETWEEN_BRACKETS', array(), $stackPtr);
         }
     } else {
         if ($multiLine === false && $tokens[$closeBracket - 1]['code'] === T_WHITESPACE) {
             //$lastParam = array_pop($params);
             //$arg       = $tokens[$lastParam]['content'];
             //$gap       = strlen($tokens[($closeBracket - 1)]['content']);
             //$error     = "Expected 0 spaces between argument \"$arg\" and closing bracket; $gap found";
             //$phpcsFile->addError($error, $closeBracket);
             $phpcsFile->addEvent('XP_FUNCTION_DECLARATION_ARGUMENT_NOSPACE_BETWEEN_ARGUMENT_AND_CLOSING_BRACKET', array(), $closeBracket);
         }
     }
 }
 /**
  * Processes the variable found within a double quoted string
  *
  * @param  PHP_CodeSniffer_File $phpcsFile The file being scanned
  * @param  integer              $stackPtr  The position of the double quoted string
  * @return void
  */
 public function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $phpReservedVars = array('_SERVER', '_GET', '_POST', '_REQUEST', '_SESSION', '_ENV', '_COOKIE', '_FILES', 'GLOBALS');
     if (preg_match_all('|[^\\\\]\\$([a-zA-Z0-9_]+)|', $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;
             }
             // There is no way for us to know if the var is public or private, so we have to ignore a
             // leading underscore if there is one and just check the main part of the variable name
             $originalVarName = $varName;
             // SQLI : les premier caractรจre ne doit pas รชtre un underscore
             //                if (substr($varName, 0, 1) === '_') {
             //                    if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
             //                        $varName = substr($varName, 1);
             //                    }
             //                }
             if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
                 $varName = $matches[0];
                 $phpcsFile->addEvent('NOT_VALID_CAMEL_VALID_VARIABLE', array('varname' => $varName), $stackPtr);
             } else {
                 if (preg_match('|\\d|', $varName)) {
                     $phpcsFile->addEvent('CONTAINS_NUMBER_VALID_VARIABLE', array('varname' => $varName), $stackPtr);
                 }
             }
         }
     }
 }
 /**
  * Process the function parameter comments.
  *
  * @param int $commentStart The position in the stack where
  *                          the comment started.
  *
  * @return void
  */
 protected function processParams($commentStart)
 {
     $realParams = $this->currentFile->getMethodParameters($this->_functionToken);
     $params = $this->commentParser->getParams();
     $foundParams = array();
     if (empty($params) === false) {
         $lastParm = count($params) - 1;
         /* if (substr_count($params[$lastParm]->getWhitespaceAfter(), $this->currentFile->eolChar) !== 2) {
                $error    = 'Last parameter comment requires a blank newline after it';
                $errorPos = ($params[$lastParm]->getLine() + $commentStart);
                $this->currentFile->addError($error, $errorPos);
            } */
         // Parameters must appear immediately after the comment.
         /*if ($params[0]->getOrder() !== 2) {
               $error    = 'Parameters must appear immediately after the comment';
               $errorPos = ($params[0]->getLine() + $commentStart);
               $this->currentFile->addError($error, $errorPos);
           } */
         $previousParam = null;
         $spaceBeforeVar = 10000;
         $spaceBeforeComment = 10000;
         $longestType = 0;
         $longestVar = 0;
         foreach ($params as $param) {
             $paramComment = trim($param->getComment());
             $errorPos = $param->getLine() + $commentStart;
             // Make sure that there is only one space before the var type.
             /* if ($param->getWhitespaceBeforeType() !== ' ') {
                    $error = 'Expected 1 space before variable type';
                    $this->currentFile->addError($error, $errorPos);
                } */
             $spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
             if ($spaceCount < $spaceBeforeVar) {
                 $spaceBeforeVar = $spaceCount;
                 $longestType = $errorPos;
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeComment(), ' ');
             if ($spaceCount < $spaceBeforeComment && $paramComment !== '') {
                 $spaceBeforeComment = $spaceCount;
                 $longestVar = $errorPos;
             }
             // Make sure they are in the correct order,
             // and have the correct name.
             $pos = $param->getPosition();
             $paramName = $param->getVarName() !== '' ? $param->getVarName() : '[ UNKNOWN ]';
             if ($previousParam !== null) {
                 $previousName = $previousParam->getVarName() !== '' ? $previousParam->getVarName() : 'UNKNOWN';
                 // Check to see if the parameters align properly.
                 /*if ($param->alignsVariableWith($previousParam) === false) {
                       $error = 'The variable names for parameters '.$previousName.' ('.($pos - 1).') and '.$paramName.' ('.$pos.') do not align';
                       $this->currentFile->addError($error, $errorPos);
                   }*/
                 /*if ($param->alignsCommentWith($previousParam) === false) {
                       $error = 'The comments for parameters '.$previousName.' ('.($pos - 1).') and '.$paramName.' ('.$pos.') do not align';
                       $this->currentFile->addError($error, $errorPos);
                   }*/
             }
             //end if
             // Make sure the names of the parameter comment matches the
             // actual parameter.
             if (isset($realParams[$pos - 1]) === true) {
                 $realName = substr($realParams[$pos - 1]['name'], 1);
                 $foundParams[] = $realName;
                 // Append ampersand to name if passing by reference.
                 /*if ($realParams[($pos - 1)]['pass_by_reference'] === true) {
                       $realName = '&'.$realName;
                   }*/
                 if ($realName !== $param->getVarName()) {
                     $error = 'Doc comment var "' . $paramName;
                     $error .= '" does not match actual variable name "' . $realName;
                     $error .= '" at position ' . $pos;
                     //$this->currentFile->addError($error, $errorPos);
                     $this->currentFile->addEvent('XP_FUNCTION_COMMENT_PARAM_NOMATCH', array('message' => $error), $errorPos);
                 }
             } else {
                 // We must have an extra parameter comment.
                 $error = 'Superfluous doc comment at position ' . $pos;
                 //$this->currentFile->addError($error, $errorPos);
                 $this->currentFile->addEvent('XP_FUNCTION_COMMENT_PARAM_SUPERFLUOUS', array('message' => $error), $errorPos);
             }
             if ($param->getVarName() === '') {
                 $error = 'Missing parameter name at position ' . $pos;
                 //$this->currentFile->addError($error, $errorPos);
                 $this->currentFile->addEvent('XP_FUNCTION_COMMENT_PARAM_MISSING_NAME', array('message' => $error), $errorPos);
             }
             // var_dump($realParams[($pos - 1)]['pass_by_reference']);
             if ($param->getType() === '') {
                 $error = 'Missing type at position ' . $pos;
                 //$this->currentFile->addError($error, $errorPos);
                 $this->currentFile->addEvent('XP_FUNCTION_COMMENT_PARAM_MISSING_TYPE', array('message' => $error), $errorPos);
             } else {
                 if ($realParams[$pos - 1]['pass_by_reference'] === FALSE && '&' === substr($param->getType(), 0, 1)) {
                     $error = 'Apidoc states parameter is passed by reference but it is not at position ' . $pos;
                     //$this->currentFile->addError($error, $errorPos);
                 } else {
                     if ($realParams[$pos - 1]['pass_by_reference'] === TRUE && '&' !== substr($param->getType(), 0, 1)) {
                         $error = 'Apidoc states parameter is not passed by reference but it is at position ' . $pos;
                         //$this->currentFile->addError($error, $errorPos);
                     }
                 }
             }
             /* if ($paramComment === '') {
                    $error = 'Missing comment for param "'.$paramName.'" at position '.$pos;
                    $this->currentFile->addError($error, $errorPos);
                } */
             $previousParam = $param;
         }
         //end foreach
         if ($spaceBeforeVar !== 1 && $spaceBeforeVar !== 10000 && $spaceBeforeComment !== 10000) {
             $error = 'Expected 1 space after the longest type';
             //$this->currentFile->addError($error, $longestType);
             $this->currentFile->addEvent('XP_FUNCTION_COMMENT_SPACE_AFTER_LONGEST_TYPE', array(), $longestType);
         }
         if ($spaceBeforeComment !== 1 && $spaceBeforeComment !== 10000) {
             $error = 'Expected 1 space after the longest variable name';
             //$this->currentFile->addError($error, $longestVar);
             $this->currentFile->addEvent('XP_FUNCTION_COMMENT_SPACE_AFTER_LONGEST_VARIABLE', array(), $longestType);
         }
     }
     //end if
     $realNames = array();
     foreach ($realParams as $realParam) {
         $realNames[] = substr($realParam['name'], 1);
     }
     // Report and missing comments.
     $diff = array_diff($realNames, $foundParams);
     foreach ($diff as $neededParam) {
         if (count($params) !== 0) {
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
         } else {
             $errorPos = $commentStart;
         }
         $error = 'Doc comment for "' . $neededParam . '" missing';
         //$this->currentFile->addError($error, $errorPos);
         $this->currentFile->addEvent('XP_FUNCTION_COMMENT_PARAM_MISSING', array('message' => $error), $errorPos);
     }
 }
 /**
  * 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);
     // Is this a magic function. IE. is prefixed with "__".
     if (preg_match('|^__|', $functionName) !== 0) {
         $magicPart = substr($functionName, 2);
         if (in_array($magicPart, $this->_magicFunctions) === false) {
             $phpcsFile->addEvent('NAME_INVALID_MAGIC_METHOD_VALID_FUNCTION', array('name' => $functionName), $stackPtr);
         }
         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] === '_') {
         $phpcsFile->addEvent('NAME_INVALID_PRIVATE_METHOD_VALID_FUNCTION', array('name' => $functionName), $stackPtr);
         return;
     }
     /*
                 if ($functionName{0} !== strtoupper($functionName{0})) {
         $phpcsFile->addEvent(
             'NAME_PREFIXED_START_CAPITAL_LETTER_VALID_FUNCTION', 
             array('name' => $functionName),
             $stackPtr
         );        	
                     return;
                 }
     */
     //}
     // If it doesn't have a camel caps part, it's not valid.
     if (trim($camelCapsPart) === '') {
         $phpcsFile->addEvent('NAME_NOT_VALID_INCOMPLETE_VALID_FUNCTION', array('name' => $functionName), $stackPtr);
         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;
         }
         $phpcsFile->addEvent('NAME_INVALID_CONSIDER_NEWNAME_VALID_FUNCTION', array('name' => $functionName, 'newname' => $newName), $stackPtr);
     }
 }
 /**
  * Processes this test, 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();
     if ($this->nestedInCondition($tokens, $stackPtr)) {
         return;
     }
     /*
         By this stage, it is known that there is an assignment on this line.
         We only want to process the block once we reach the last assignment,
         so we need to determine if there are more to follow.
     */
     // The assignment may span over multiple lines, so look for the
     // end of the assignment so we can check assignment blocks correctly.
     $lineEnd = $phpcsFile->findNext(array(T_SEMICOLON, T_COMMA), $stackPtr + 1);
     $nextNonAllowed = $phpcsFile->findNext(array(T_PRIVATE, T_PUBLIC, T_PROTECTED, T_WHITESPACE, T_CONST, T_STATIC, T_VAR), $lineEnd + 1, NULL, TRUE);
     $lineNumber = $nextNonAllowed ? $tokens[$nextNonAllowed]['line'] : $tokens[$lineEnd]['line'] + 1;
     $nextAssign = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$assignmentTokens, $lineEnd + 1);
     if ($nextAssign !== false) {
         $isAssign = true;
         if ($tokens[$nextAssign]['line'] === $lineNumber) {
             if ($this->nestedInCondition($tokens, $nextAssign)) {
                 $isAssign = false;
             }
         }
         if ($isAssign === true) {
             return;
         }
     }
     // Getting here means that this is the last in a block of statements.
     $assignments = array();
     $assignments[] = $stackPtr;
     $prevAssignment = $stackPtr;
     $lastLine = $tokens[$stackPtr]['line'];
     while (($prevAssignment = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$assignmentTokens, $prevAssignment - 1)) !== false) {
         // The assignment's end token must be on the line directly
         // above the current one to be in the same assignment block.
         $lineEnd = $phpcsFile->findNext(array(T_SEMICOLON, T_COMMA), $prevAssignment + 1);
         $nextNonAllowed = $phpcsFile->findNext(array(T_PRIVATE, T_PUBLIC, T_PROTECTED, T_WHITESPACE, T_CONST, T_STATIC, T_VAR), $lineEnd + 1, NULL, TRUE);
         $lineNumber = $nextNonAllowed ? $tokens[$nextNonAllowed]['line'] : $tokens[$lineEnd]['line'] + 1;
         // And the end token must actually belong to this assignment.
         $nextOpener = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$scopeOpeners, $prevAssignment + 1);
         if ($nextOpener !== false && $nextOpener < $lineEnd) {
             break;
         }
         if ($lineNumber !== $lastLine) {
             break;
         }
         // Make sure it is not assigned inside a condition (eg. IF, FOR).
         if (isset($tokens[$prevAssignment]['nested_parenthesis']) === true) {
             foreach ($tokens[$prevAssignment]['nested_parenthesis'] as $start => $end) {
                 if (isset($tokens[$start]['parenthesis_owner']) === true) {
                     break 2;
                 }
             }
         }
         $assignments[] = $prevAssignment;
         $lastLine = $tokens[$prevAssignment]['line'];
     }
     //end while
     $assignmentData = array();
     $maxAssignmentLength = 0;
     $maxVariableLength = 0;
     foreach ($assignments as $assignment) {
         $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $assignment - 1, null, true);
         $endColumn = $tokens[$prev + 1]['column'];
         if ($maxVariableLength < $endColumn) {
             $maxVariableLength = $endColumn;
         }
         if ($maxAssignmentLength < strlen($tokens[$assignment]['content'])) {
             $maxAssignmentLength = strlen($tokens[$assignment]['content']);
         }
         $assignmentData[$assignment] = array('variable_length' => $endColumn, 'assignment_length' => strlen($tokens[$assignment]['content']));
     }
     //end foreach
     // If we have multiple assignments align them and make sure they are
     // aligned to the common indenting of 2 spaces
     // if only one assignment is made skip left side space
     $multiAssignmentsModifier = count($assignmentData) > 1 ? 1 + $maxVariableLength % 2 : 0;
     foreach ($assignmentData as $assignment => $data) {
         if ($data['assignment_length'] === $maxAssignmentLength) {
             if ($data['variable_length'] === $maxVariableLength) {
                 // The assignment is the longest possible, so the column that
                 // everything has to align to is based on it.
                 $column = $maxVariableLength + $multiAssignmentsModifier;
                 break;
             } else {
                 // The assignment token is the longest out of all of the
                 // assignments, but the variable name is not, so the column
                 // the start at can go back more to cover the space
                 // between the variable name and the assigment operator.
                 $column = $maxVariableLength - ($maxAssignmentLength - 1) + 1;
             }
         }
     }
     // Determine the actual position that each equals sign should be in.
     foreach ($assignments as $assignment) {
         // Actual column takes into account the length of the assignment operator.
         $actualColumn = $column + $maxAssignmentLength - strlen($tokens[$assignment]['content']);
         if ($tokens[$assignment]['column'] !== $actualColumn) {
             $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $assignment - 1, null, true);
             $expected = $actualColumn - $tokens[$prev + 1]['column'];
             if ($tokens[$assignment]['line'] !== $tokens[$prev]['line']) {
                 // Instead of working out how many spaces there are
                 // across new lines, the error message becomes more
                 // generic below.
                 $found = null;
             } else {
                 $found = $tokens[$assignment]['column'] - $tokens[$prev + 1]['column'];
             }
             // If the expected number of spaces for alignment exceeds the
             // maxPadding rule, we can ignore this assignment.
             if ($expected > $this->maxPadding) {
                 continue;
             }
             // Skip multi-line assignments if required.
             if ($found === null && $this->ignoreMultiLine === true) {
                 continue;
             }
             $expected .= $expected === 1 ? ' space' : ' spaces';
             if ($found === null) {
                 $found = 'a new line';
             } else {
                 $found .= $found === 1 ? ' space' : ' spaces';
             }
             if (count($assignments) === 1) {
                 $error = "Equals sign not aligned correctly; expected {$expected} but found {$found}";
             } else {
                 $error = "Equals sign not aligned with surrounding assignments; expected {$expected} but found {$found}";
             }
             if ($this->error === true) {
                 //$phpcsFile->addError($error, $assignment);
                 $phpcsFile->addEvent('XP_MULTIPLE_STATEMENT_ALIGNMENT_EQUALS_SIGN_NOT_ALIGNED_CORRECTLY', array('message' => $error), $stackPtr);
             } else {
                 //$phpcsFile->addWarning($error, $assignment);
                 $phpcsFile->addEvent('XP_MULTIPLE_STATEMENT_ALIGNMENT_EQUALS_SIGN_NOT_ALIGNED_WITH_SURROUNDING_ASSIGNMENTS', array('message' => $error), $stackPtr);
             }
         }
         //end if
     }
     //end foreach
 }
 /**
  * Called to process class 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
  */
 public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $this->currentFile = $phpcsFile;
     $tokens = $phpcsFile->getTokens();
     $commentToken = array(T_COMMENT, T_DOC_COMMENT);
     // Extract the var comment docblock
     $commentEnd = $phpcsFile->findPrevious($commentToken, $stackPtr - 3);
     if ($commentEnd !== false and $tokens[$commentEnd]['code'] === T_COMMENT) {
         $phpcsFile->addEvent('STYLE_VARIABLE_COMMENT', array(), $stackPtr);
         return;
     } else {
         if ($commentEnd === false or $tokens[$commentEnd]['code'] !== T_DOC_COMMENT) {
             $phpcsFile->addEvent('MISSING_VARIABLE_COMMENT', array(), $stackPtr);
             return;
         } else {
             // Make sure the comment we have found belongs to us
             $commentFor = $phpcsFile->findNext(array(T_VARIABLE, T_CLASS, T_INTERFACE), $commentEnd + 1);
             if ($commentFor !== $stackPtr) {
                 $phpcsFile->addEvent('MISSING_VARIABLE_COMMENT', array(), $stackPtr);
                 return;
             }
         }
     }
     $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1;
     $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1);
     // Parse the header comment docblock
     try {
         $this->commentParser = new PHP_CodeSniffer_CommentParser_MemberCommentParser($comment, $phpcsFile);
         $this->commentParser->parse();
     } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
         $line = $e->getLineWithinComment() + $commentStart;
         $phpcsFile->addEvent('ERROR_PARSING_VARIABLE_COMMENT', array(), $line);
         return;
     }
     $comment = $this->commentParser->getComment();
     if (is_null($comment) === true) {
         $phpcsFile->addEvent('VARIABLE_COMMENT_EMPTY', array(), $commentStart);
         return;
     }
     // Check for a comment description
     $short = $comment->getShortComment();
     if (trim($short) === '') {
         $phpcsFile->addEvent('MISSING_SHORT_DESC_VARIABLE_COMMENT', array(), $commentStart);
         return;
     } else {
         // No extra newline before short description
         $newlineCount = 0;
         $newlineSpan = strspn($short, $phpcsFile->eolChar);
         if ($short !== '' and $newlineSpan > 0) {
             $phpcsFile->addEvent('EXTRA_LINE_VARIABLE_COMMENT', array('line' => $line), $commentStart + 1);
         }
         $newlineCount = substr_count($short, $phpcsFile->eolChar) + 1;
         // Exactly one blank line between short and long description
         $long = $comment->getLongComment();
         if (empty($long) === false) {
             $between = $comment->getWhiteSpaceBetween();
             $newlineBetween = substr_count($between, $phpcsFile->eolChar);
             if ($newlineBetween !== 2) {
                 $phpcsFile->addEvent('BLANK_LINE_BETWEEN_VARIABLE_COMMENT', array(), $commentStart + $newlineCount + 1);
             }
         }
         // Short description must be single line and end with a full stop
         $testShort = trim($short);
         $lastChar = $testShort[strlen($testShort) - 1];
         if (substr_count($testShort, $phpcsFile->eolChar) !== 0) {
             $phpcsFile->addEvent('SHORT_DESC_VARIABLE_COMMENT', array(), $commentStart + 1);
         }
     }
     // Exactly one blank line before tags
     $tags = $this->commentParser->getTagOrders();
     if (count($tags) > 1) {
         $newlineSpan = $comment->getNewlineAfter();
         if ($newlineSpan !== 2) {
             if (isset($long) and $long !== '') {
                 $newlineCount += substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1;
             }
             if (isset($newlineCount) === false) {
                 $phpcsFile->addEvent('BLANK_LINE_BEFORE_VARIABLE_COMMENT', array(), $commentStart);
             } else {
                 $phpcsFile->addEvent('BLANK_LINE_BEFORE_VARIABLE_COMMENT', array(), $commentStart + $newlineCount);
             }
             $short = rtrim($short, $phpcsFile->eolChar . ' ');
         }
     }
     // Check for unknown/deprecated tags
     $unknownTags = $this->commentParser->getUnknown();
     foreach ($unknownTags as $errorTag) {
         // Unknown tags are not parsed, do not process further
         $phpcsFile->addWarning('BLANK_LINE_BEFORE_VARIABLE_COMMENT', array('tagname' => $errorTag[tag]), $commentStart + $errorTag['line']);
     }
     // Check each tag
     $this->processVar($commentStart, $commentEnd);
 }
 /**
  * Process the function parameter comments
  *
  * @param  integer $commentStart The position in the stack where the comment started
  * @param  integer $commentEnd   The position in the stack where the comment ended
  * @return void
  */
 protected function _processParams($commentStart, $commentEnd)
 {
     $realParams = $this->_currentFile->getMethodParameters($this->_functionToken);
     $params = $this->_commentParser->getParams();
     $foundParams = array();
     if (empty($params) === false) {
         $isSpecialMethod = ($this->_methodName === '__construct' or $this->_methodName === '__destruct');
         if (substr_count($params[count($params) - 1]->getWhitespaceAfter(), $this->_currentFile->eolChar) !== 1 and $isSpecialMethod === false) {
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
             $this->_currentFile->addEvent('EMPTY_LINE_LAST_PARAMETER_FUNCTION_COMMENT', array(), $errorPos + 1);
         }
         // Parameters must appear immediately after the comment
         if ($params[0]->getOrder() !== 2) {
             $errorPos = $params[0]->getLine() + $commentStart;
             $this->_currentFile->addEvent('PARAMETER_AFTER_COMMENT_FUNCTION_COMMENT', array(), $errorPos);
         }
         $previousParam = null;
         $spaceBeforeVar = 10000;
         $spaceBeforeComment = 10000;
         $longestType = 0;
         $longestVar = 0;
         if (count($this->_commentParser->getThrows()) !== 0) {
             $isSpecialMethod = false;
         }
         foreach ($params as $param) {
             $paramComment = trim($param->getComment());
             $errorPos = $param->getLine() + $commentStart;
             if ($isSpecialMethod === true and $param->getWhitespaceBeforeType() !== ' ') {
                 $this->_currentFile->addEvent('ONE_SPACE_VARIABLE_FUNCTION_COMMENT', array(), $errorPos);
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
             if ($spaceCount < $spaceBeforeVar) {
                 $spaceBeforeVar = $spaceCount;
                 $longestType = $errorPos;
             }
             $spaceCount = substr_count($param->getWhitespaceBeforeComment(), ' ');
             if ($spaceCount < $spaceBeforeComment and $paramComment !== '') {
                 $spaceBeforeComment = $spaceCount;
                 $longestVar = $errorPos;
             }
             // Make sure they are in the correct order, and have the correct name
             $pos = $param->getPosition();
             $paramName = $param->getVarName() !== '' ? $param->getVarName() : '[ UNKNOWN ]';
             if ($previousParam !== null) {
                 $previousName = $previousParam->getVarName() !== '' ? $previousParam->getVarName() : 'UNKNOWN';
                 // Check to see if the parameters align properly
                 if ($param->alignsVariableWith($previousParam) === false) {
                     $this->_currentFile->addEvent('VARIABLES_NAMES_NOT_ALIGN_FUNCTION_COMMENT', array('previousname' => $previousName, 'previousnamepos' => $pos - 1, 'paramname' => $paramName, 'paramnamepos' => $pos), $errorPos);
                 }
                 if ($param->alignsCommentWith($previousParam) === false) {
                     $this->_currentFile->addEvent('COMMENTS_NOT_ALIGN_FUNCTION_COMMENT', array('previousname' => $previousName, 'previousnamepos' => $pos - 1, 'paramname' => $paramName, 'paramnamepos' => $pos), $errorPos);
                 }
             }
             // Variable must be one of the supported standard type
             $typeNames = explode('|', $param->getType());
             foreach ($typeNames as $typeName) {
                 $suggestedName = PHP_CodeSniffer::suggestType($typeName);
                 if ($typeName !== $suggestedName) {
                     $this->_currentFile->addEvent('EXPECTED_FOUND_FUNCTION_COMMENT', array('suggestedname' => $suggestedName, 'paramname' => $paramName, 'typename' => $paramName, 'paramnamepos' => $pos), $errorPos);
                     continue;
                 }
                 if (count($typeNames) !== 1) {
                     continue;
                 }
                 // Check type hint for array and custom type
                 $suggestedTypeHint = '';
                 if (strpos($suggestedName, 'array') !== false) {
                     $suggestedTypeHint = 'array';
                 } else {
                     if (in_array($typeName, PHP_CodeSniffer::$allowedTypes) === false) {
                         $suggestedTypeHint = $suggestedName;
                     }
                 }
                 if ($suggestedTypeHint !== '' and isset($realParams[$pos - 1]) === true) {
                     $typeHint = $realParams[$pos - 1]['type_hint'];
                     if ($typeHint === '') {
                         $this->_currentFile->addEvent('TYPEHINT_MISSING_FUNCTION_COMMENT', array('suggestedtypehint' => $suggestedTypeHint, 'paramname' => $paramName, 'paramnamepos' => $pos), $commentEnd + 2);
                     } else {
                         if ($typeHint !== $suggestedTypeHint) {
                             $this->_currentFile->addEvent('EXPECTED_TYPEHINT_FOUND_FUNCTION_COMMENT', array('suggestedtypehint' => $suggestedTypeHint, 'typehint' => $typeHint, 'paramname' => $paramName, 'paramnamepos' => $pos), $commentEnd + 2);
                         }
                     }
                 } else {
                     if ($suggestedTypeHint === '' and isset($realParams[$pos - 1]) === true) {
                         $typeHint = $realParams[$pos - 1]['type_hint'];
                         if ($typeHint !== '') {
                             $this->_currentFile->addEvent('UNKNOW_TYPEHINT_FOUND_FUNCTION_COMMENT', array('typehint' => $typeHint, 'paramname' => $paramName, 'paramnamepos' => $pos), $commentEnd + 2);
                         }
                     }
                 }
             }
             // Make sure the names of the parameter comment matches the
             // actual parameter
             if (isset($realParams[$pos - 1]) === true) {
                 $realName = $realParams[$pos - 1]['name'];
                 $foundParams[] = $realName;
                 // Append ampersand to name if passing by reference
                 if ($realParams[$pos - 1]['pass_by_reference'] === true) {
                     $realName = '&' . $realName;
                 }
                 if ($realName !== $param->getVarName()) {
                     $this->_currentFile->addEvent('DOCCOMMENT_NOT_MATCH_FUNCTION_COMMENT', array('paramname' => $paramName, 'realname' => $realName, 'paramnamepos' => $pos), $errorPos);
                 }
             } else {
                 // We must have an extra parameter comment
                 $this->_currentFile->addEvent('SUPERFLUOUS_DOCCOMMENT_FUNCTION_COMMENT', array('doccommentpos' => $pos), $errorPos);
             }
             if ($param->getVarName() === '') {
                 $this->_currentFile->addEvent('MISSING_PARAMETER_FUNCTION_COMMENT', array('parameterpos' => $pos), $errorPos);
             }
             if ($param->getType() === '') {
                 $this->_currentFile->addEvent('MISSING_TYPE_FUNCTION_COMMENT', array('typepos' => $pos), $errorPos);
             }
             if ($paramComment === '') {
                 $this->_currentFile->addEvent('MISSING_COMMENT_PARAM_FUNCTION_COMMENT', array('paramname' => $paramName, 'paramnamepos' => $pos), $errorPos);
             } else {
                 // Check if optional params include (Optional) within their description
                 $functionBegin = $this->_currentFile->findNext(array(T_FUNCTION), $commentStart);
                 $functionName = $this->_currentFile->findNext(array(T_STRING), $functionBegin);
                 $openBracket = $this->_tokens[$functionBegin]['parenthesis_opener'];
                 $closeBracket = $this->_tokens[$functionBegin]['parenthesis_closer'];
                 $nextParam = $this->_currentFile->findNext(T_VARIABLE, $openBracket + 1, $closeBracket);
                 while ($nextParam !== false) {
                     $nextToken = $this->_currentFile->findNext(T_WHITESPACE, $nextParam + 1, $closeBracket + 1, true);
                     if ($nextToken === false and $this->_tokens[$nextParam + 1]['code'] === T_CLOSE_PARENTHESIS) {
                         break;
                     }
                     $nextCode = $this->_tokens[$nextToken]['code'];
                     $arg = $this->_tokens[$nextParam]['content'];
                     if ($nextCode === T_EQUAL and $paramName === $arg) {
                         if (substr($paramComment, 0, 11) !== '(Optional) ') {
                             $this->_currentFile->addEvent('OPTIONAL_PARAM_START_FUNCTION_COMMENT', array('paramname' => $paramName), $errorPos);
                         }
                     }
                     $nextParam = $this->_currentFile->findNext(T_VARIABLE, $nextParam + 1, $closeBracket);
                 }
             }
             $previousParam = $param;
         }
         if ($spaceBeforeVar !== 1 and $spaceBeforeVar !== 10000 and $spaceBeforeComment !== 10000) {
             $this->_currentFile->addEvent('ONE_SPACE_LONGEST_TYPE_FUNCTION_COMMENT', array(), $longestType);
         }
         if ($spaceBeforeComment !== 1 and $spaceBeforeComment !== 10000) {
             $this->_currentFile->addEvent('ONE_SPACE_LONGEST_VARIABLE_FUNCTION_COMMENT', array(), $longestVar);
         }
     }
     $realNames = array();
     foreach ($realParams as $realParam) {
         $realNames[] = $realParam['name'];
     }
     // Report missing comments
     $diff = array_diff($realNames, $foundParams);
     foreach ($diff as $neededParam) {
         if (count($params) !== 0) {
             $errorPos = $params[count($params) - 1]->getLine() + $commentStart;
         } else {
             $errorPos = $commentStart;
         }
         $this->_currentFile->addEvent('DOCCOMMENT_MISSING_FUNCTION_COMMENT', array('param' => $neededParam), $errorPos);
     }
 }
Example #19
0
 /**
  * Checks if a line is too long.
  *
  * @param PHP_CodeSniffer_File $phpcsFile   The file being scanned.
  * @param int                  $stackPtr    The token at the end of the line.
  * @param string               $lineContent The content of the line.
  *
  * @return void
  */
 protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent)
 {
     // If the content is a CVS or SVN id in a version tag, or it is
     // a license tag with a name and URL, there is nothing the
     // developer can do to shorten the line, so don't throw errors.
     if (preg_match('|@version[^\\$]+\\$Id|', $lineContent) === 0 && preg_match('|@license|', $lineContent) === 0) {
         $lineLength = strlen($lineContent);
         if ($this->absoluteLineLimit > 0 && $lineLength > $this->absoluteLineLimit) {
             $error = 'Line exceeds maximum limit of ' . $this->absoluteLineLimit . " characters; contains {$lineLength} characters";
             //$phpcsFile->addError($error, $stackPtr);
             $phpcsFile->addEvent('XP_LINE_LENGTH_EXCEEDS_MAXIMUM_LIMIT', array('message' => $error), $stackPtr);
         } else {
             if ($lineLength > $this->lineLimit) {
                 $warning = 'Line exceeds ' . $this->lineLimit . " characters; contains {$lineLength} characters";
                 //$phpcsFile->addWarning($warning, $stackPtr);
                 $phpcsFile->addEvent('XP_LINE_LENGTH_EXCEEDS_LIMIT', array('message' => $warning), $stackPtr);
             }
         }
     }
 }
 /**
  * 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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $phpReservedVars = array('_SERVER', '_GET', '_POST', '_REQUEST', '_SESSION', '_ENV', '_COOKIE', '_FILES', 'GLOBALS');
     if (preg_match_all('|[^\\\\]\\$([a-zA-Z0-9_]+)|', $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;
             }
             // There is no way for us to know if the var is public or private,
             // so we have to ignore a leading underscore if there is one and just
             // check the main part of the variable name.
             $originalVarName = $varName;
             if (substr($varName, 0, 1) === '_') {
                 if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
                     $varName = substr($varName, 1);
                 }
             }
             if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
                 $varName = $matches[0];
                 //$error   = "Variable \"$originalVarName\" is not in valid camel caps format";
                 //$phpcsFile->addError($error, $stackPtr);
                 $error = 'Variable ' . $originalVarName . ' is not in valid camel caps format';
                 $phpcsFile->addEvent('XP_VALID_VARIABLE_NAME_NOT_IN_CAMEL_CAPS_FORMAT', array('message' => $error), $stackPtr);
             } else {
                 if (preg_match('|\\d|', $varName)) {
                     //$warning = "Variable \"$originalVarName\" contains numbers but this is discouraged";
                     //$phpcsFile->addWarning($warning, $stackPtr);
                     $error = 'Variable ' . $originalVarName . ' contains numbers but this is discouraged';
                     $phpcsFile->addEvent('XP_VALID_VARIABLE_NAME_CONTAINS_NUMBERS', array('message' => $error), $stackPtr);
                 }
             }
         }
     }
     //end if
 }
Example #21
0
 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
  * @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 this is an inline condition (ie. there is no scope opener), then
     // return, as this is not a new scope.
     if (isset($tokens[$stackPtr]['scope_opener']) === false) {
         return;
     }
     if ($tokens[$stackPtr]['code'] === T_ELSE) {
         $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true);
         // We will handle the T_IF token in another call to process.
         if ($tokens[$next]['code'] === T_IF) {
             return;
         }
     }
     // Find the first token on this line.
     $firstToken = $stackPtr;
     for ($i = $stackPtr; $i >= 0; $i--) {
         // Record the first code token on the line.
         if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
             $firstToken = $i;
         }
         // It's the start of the line, so we've found our first php token.
         if ($tokens[$i]['column'] === 1) {
             break;
         }
     }
     // Based on the conditions that surround this token, determine the
     // indent that we expect this current content to be.
     $expectedIndent = $this->calculateExpectedIndent($tokens, $firstToken);
     if ($tokens[$firstToken]['column'] !== $expectedIndent) {
         $error = 'Line indented incorrectly; expected ';
         $error .= $expectedIndent - 1 . ' spaces, found ';
         $error .= $tokens[$firstToken]['column'] - 1;
         //$phpcsFile->addWarning($error, $stackPtr);
         $phpcsFile->addEvent('XP_SCOPE_INDENT_LINE_INDENTED_INCORRECTLY', array('message' => $error), $stackPtr);
     }
     $scopeOpener = $tokens[$stackPtr]['scope_opener'];
     $scopeCloser = $tokens[$stackPtr]['scope_closer'];
     // Some scopes are expected not to have indents.
     if (in_array($tokens[$firstToken]['code'], $this->nonIndentingScopes) === false) {
         $indent = $expectedIndent + $this->indent;
     } else {
         $indent = $expectedIndent;
     }
     $newline = false;
     $commentOpen = false;
     $inHereDoc = false;
     // Only loop over the content beween the opening and closing brace, not
     // the braces themselves.
     for ($i = $scopeOpener + 1; $i < $scopeCloser; $i++) {
         // If this token is another scope, skip it as it will be handled by
         // another call to this sniff.
         if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$scopeOpeners) === true) {
             if (isset($tokens[$i]['scope_opener']) === true) {
                 $i = $tokens[$i]['scope_closer'];
             } else {
                 // If this token does not have a scope_opener indice, then
                 // it's probably an inline scope, so let's skip to the next
                 // semicolon. Inline scopes include inline if's, abstract
                 // methods etc.
                 $nextToken = $phpcsFile->findNext(T_SEMICOLON, $i, $scopeCloser);
                 if ($nextToken !== false) {
                     $i = $nextToken;
                 }
             }
             continue;
         }
         // If this is a HEREDOC then we need to ignore it as the
         // whitespace before the contents within the HEREDOC are
         // considered part of the content.
         if ($tokens[$i]['code'] === T_START_HEREDOC) {
             $inHereDoc = true;
             continue;
         } else {
             if ($inHereDoc === true) {
                 if ($tokens[$i]['code'] === T_END_HEREDOC) {
                     $inHereDoc = false;
                 }
                 continue;
             }
         }
         if ($tokens[$i]['column'] === 1) {
             // We started a newline.
             $newline = true;
         }
         if ($newline === true && $tokens[$i]['code'] !== T_WHITESPACE) {
             // If we started a newline and we find a token that is not
             // whitespace, then this must be the first token on the line that
             // must be indented.
             $newline = false;
             $firstToken = $i;
             $column = $tokens[$firstToken]['column'];
             // Special case for non-PHP code.
             if ($tokens[$firstToken]['code'] === T_INLINE_HTML) {
                 $trimmedContentLength = strlen(ltrim($tokens[$firstToken]['content']));
                 if ($trimmedContentLength === 0) {
                     continue;
                 }
                 $contentLength = strlen($tokens[$firstToken]['content']);
                 $column = $contentLength - $trimmedContentLength + 1;
             }
             // Check to see if this constant string spans multiple lines.
             // If so, then make sure that the strings on lines other than the
             // first line are indented appropriately, based on their whitespace.
             if (in_array($tokens[$firstToken]['code'], PHP_CodeSniffer_Tokens::$stringTokens) === true) {
                 if (in_array($tokens[$firstToken - 1]['code'], PHP_CodeSniffer_Tokens::$stringTokens) === true) {
                     // If we find a string that directly follows another string
                     // then its just a string that spans multiple lines, so we
                     // don't need to check for indenting.
                     continue;
                 }
             }
             // This is a special condition for T_DOC_COMMENT and C-style
             // comments, which contain whitespace between each line.
             $comments = array(T_COMMENT, T_DOC_COMMENT);
             if (in_array($tokens[$firstToken]['code'], $comments) === true) {
                 $content = trim($tokens[$firstToken]['content']);
                 if (preg_match('|^/\\*|', $content) !== 0) {
                     // Check to see if the end of the comment is on the same line
                     // as the start of the comment. If it is, then we don't
                     // have to worry about opening a comment.
                     if (preg_match('|\\*/$|', $content) === 0) {
                         // We don't have to calculate the column for the
                         // start of the comment as there is a whitespace
                         // token before it.
                         $commentOpen = true;
                     }
                 } else {
                     if ($commentOpen === true) {
                         if ($content === '') {
                             // We are in a comment, but this line has nothing on it
                             // so let's skip it.
                             continue;
                         }
                         $contentLength = strlen($tokens[$firstToken]['content']);
                         $trimmedContentLength = strlen(ltrim($tokens[$firstToken]['content']));
                         $column = $contentLength - $trimmedContentLength + 1;
                         if (preg_match('|\\*/$|', $content) !== 0) {
                             $commentOpen = false;
                         }
                     }
                 }
                 //end if
             }
             //end if
             // The token at the start of the line, needs to have its' column
             // greater than the relative indent we set above. If it is less,
             // an error should be shown.
             if ($column !== $indent) {
                 if ($this->exact === true || $column < $indent) {
                     $error = 'Line indented incorrectly; expected ';
                     if ($this->exact === false) {
                         $error .= 'at least ';
                     }
                     $error .= $indent - 1 . ' spaces, found ';
                     $error .= $column - 1;
                     //$phpcsFile->addWarning($error, $firstToken);
                     $phpcsFile->addEvent('XP_SCOPE_INDENT_LINE_INDENTED_INCORRECTLY', array('message' => $error), $stackPtr);
                 }
             }
         }
         //end if
     }
     //end for
 }
Example #22
0
 /**
  * Processes this test, 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)
 {
     $this->currentFile = $phpcsFile;
     // We are only interested if this is the first open tag.
     if ($stackPtr !== 0) {
         if ($phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false) {
             return;
         }
     }
     $tokens = $phpcsFile->getTokens();
     // Find the next non whitespace token.
     $commentStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
     if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) {
         // We are only interested if this is the first open tag.
         return;
     } else {
         if ($tokens[$commentStart]['code'] !== T_COMMENT) {
             //$error = 'You must use "/*" style comments for a file comment';
             //$phpcsFile->addError($error, $commentStart);
             $phpcsFile->addEvent('XP_CLASS_HEADER_INVALID', $commentStart);
             return;
         } else {
             if ($commentStart === false || $tokens[$commentStart]['code'] !== T_COMMENT) {
                 //$phpcsFile->addError('Missing file doc comment', $errorToken);
                 $phpcsFile->addEvent('XP_CLASS_HEADER_MISSING', $commentStart);
                 return;
             } else {
                 // File header must directly follow opening tag
                 if ($tokens[$commentStart]['line'] !== $tokens[$stackPtr]['line'] + 1) {
                     //$error = 'File header not directly following open tag';
                     //$phpcsFile->addError($error, $commentStart);
                     $phpcsFile->addEvent('XP_CLASS_HEADER_NOT_AFTER_OPEN_TAG', $commentStart);
                 }
                 // Extract the header comment docblock.
                 $commentEnd = $phpcsFile->findNext(T_COMMENT, $commentStart + 1, null, true) - 1;
                 $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1);
                 // Parse the header comment docblock.
                 try {
                     $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
                     $this->commentParser->parse();
                 } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
                     $line = $e->getLineWithinComment() + $commentStart;
                     //$phpcsFile->addError($e->getMessage(), $line);
                     $phpcsFile->addEvent('XP_CLASS_HEADER_EXCEPTION', array('message' => $e->getMessage()), $line);
                     return;
                 }
                 $comment = $this->commentParser->getComment();
                 // No extra newline before short description.
                 $short = $comment->getShortComment();
                 $newlineCount = 0;
                 $newlineSpan = strspn($short, $phpcsFile->eolChar);
                 if ($short !== '' && $newlineSpan > 0) {
                     $line = $newlineSpan > 1 ? 'newlines' : 'newline';
                     //$error = "Extra $line found before file comment short description";
                     //$phpcsFile->addError($error, ($commentStart + 1));
                     $phpcsFile->addEvent('XP_CLASS_HEADER_NEWLINE_BEFORE_SHORT_DESCRIPTION', array(), $commentStart + 1);
                 }
                 $found = FALSE;
                 if (strstr(trim($short), '$Id')) {
                     //$error = "There must be an empty line between SVN ID tag and the description";
                     //$phpcsFile->addError($error, ($commentStart));
                     $phpcsFile->addEvent('XP_CLASS_HEADER_EMPTYLINE_BETWEEN_SHORT_DESCRIPTION_AND_SVNID', array(), $commentStart + 1);
                     $found = TRUE;
                 }
                 $newlineCount = substr_count($short, $phpcsFile->eolChar) + 1;
                 // Exactly one blank line between short and long description.
                 $long = $comment->getLongComment();
                 if (empty($long) === false) {
                     $between = $comment->getWhiteSpaceBetween();
                     $newlineBetween = substr_count($between, $phpcsFile->eolChar);
                     if ($newlineBetween !== 2) {
                         //$error = 'There must be exactly one blank line between description and the ID Tag';
                         //$phpcsFile->addError($error, ($commentStart + $newlineCount + 1));
                         $phpcsFile->addEvent('XP_CLASS_HEADER_EMPTYLINE_BETWEEN_DESCRIPTION_AND_ID', array(), $commentStart + $newlineCount + 1);
                     }
                     if (!strstr(trim($long), '$Id')) {
                         //$error = 'SVN Id tag is corrupt';
                         //$phpcsFile->addError($error, ($commentStart + $newlineCount + 1));
                         $phpcsFile->addEvent('XP_CLASS_HEADER_SVNID_CORRUPT', array(), $commentStart + $newlineCount + 1);
                     }
                     $newlineCount += $newlineBetween;
                 } elseif (!$found) {
                     //$error = 'SVN Id tag is missing';
                     //$phpcsFile->addError($error, ($commentStart + $newlineCount + 1));
                     $phpcsFile->addEvent('XP_CLASS_HEADER_SVNID_MISSING', array(), $commentStart + $newlineCount + 1);
                 }
                 if (2 < $comment->getNewlineAfter()) {
                     //$error = 'Extra content after SVN ID Tag';
                     //$phpcsFile->addError($error, ($commentStart + $newlineCount));
                     $phpcsFile->addEvent('XP_CLASS_HEADER_CONTENT_AFTER_SVNID', array(), $commentStart + $newlineCount);
                 }
             }
         }
     }
     //end if
 }
 /**
  * Processes this test, 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)
 {
     $this->currentFile = $phpcsFile;
     $tokens = $phpcsFile->getTokens();
     $find = array(T_ABSTRACT, T_WHITESPACE, T_FINAL);
     // Extract the class comment docblock.
     $commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true);
     if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) {
         $phpcsFile->addEvent('STYLE_CLASS_COMMENT', array(), $stackPtr);
         return;
     } else {
         if ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT) {
             $phpcsFile->addEvent('MISSING_CLASS_COMMENT', array(), $stackPtr);
             return;
         }
     }
     $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1;
     $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, $commentEnd + 1, $stackPtr, false, $phpcsFile->eolChar);
     // Distinguish file and class comment.
     $prevClassToken = $phpcsFile->findPrevious(T_CLASS, $stackPtr - 1);
     if ($prevClassToken === false) {
         // This is the first class token in this file, need extra checks.
         $prevNonComment = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentStart - 1, null, true);
         if ($prevNonComment !== false) {
             $prevComment = $phpcsFile->findPrevious(T_DOC_COMMENT, $prevNonComment - 1);
             if ($prevComment === false) {
                 // There is only 1 doc comment between open tag and class token.
                 $newlineToken = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, $stackPtr, false, $phpcsFile->eolChar);
                 if ($newlineToken !== false) {
                     $newlineToken = $phpcsFile->findNext(T_WHITESPACE, $newlineToken + 1, $stackPtr, false, $phpcsFile->eolChar);
                     if ($newlineToken !== false) {
                         // Blank line between the class and the doc block.
                         // The doc block is most likely a file comment.
                         $phpcsFile->addEvent('MISSING_CLASS_COMMENT', array(), $stackPtr + 1);
                         return;
                     }
                 }
                 //end if
             }
             //end if
             // Exactly one blank line before the class comment.
             $prevTokenEnd = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true);
             if ($prevTokenEnd !== false) {
                 $blankLineBefore = 0;
                 for ($i = $prevTokenEnd + 1; $i < $commentStart; $i++) {
                     if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === $phpcsFile->eolChar) {
                         $blankLineBefore++;
                     }
                 }
                 if ($blankLineBefore !== 2) {
                     $phpcsFile->addEvent('BLANK_LINE_CLASS_COMMENT', array(), $commentStart - 1);
                 }
             }
         }
         //end if
     }
     //end if
     $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1);
     // Parse the class comment docblock.
     try {
         $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
         $this->commentParser->parse();
     } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
         $line = $e->getLineWithinComment() + $commentStart;
         $phpcsFile->addEvent('ERROR_PARSING_CLASS_COMMENT', array(), $line);
         return;
     }
     $comment = $this->commentParser->getComment();
     if (is_null($comment) === true) {
         $phpcsFile->addEvent('CLASS_COMMENT_EMPTY', array(), $commentStart);
         return;
     }
     // Check for a comment description.
     $short = rtrim($comment->getShortComment(), $phpcsFile->eolChar);
     if (trim($short) === '') {
         $phpcsFile->addEvent('MISSING_SHORT_DESC_CLASS_COMMENT', array(), $commentStart);
         return;
     }
     // No extra newline before short description.
     $newlineCount = 0;
     $newlineSpan = strspn($short, $phpcsFile->eolChar);
     if ($short !== '' && $newlineSpan > 0) {
         $line = $newlineSpan > 1 ? 'newlines' : 'newline';
         $phpcsFile->addEvent('EXTRA_LINE_CLASS_COMMENT', array('line' => $line), $commentStart + 1);
     }
     $newlineCount = substr_count($short, $phpcsFile->eolChar) + 1;
     // Exactly one blank line between short and long description.
     $long = $comment->getLongComment();
     if (empty($long) === false) {
         $between = $comment->getWhiteSpaceBetween();
         $newlineBetween = substr_count($between, $phpcsFile->eolChar);
         if ($newlineBetween !== 2) {
             $phpcsFile->addEvent('BLANK_LINE_BETWEEN_CLASS_COMMENT', array(), $commentStart + $newlineCount + 1);
         }
     }
     // Exactly one blank line before tags.
     $tags = $this->commentParser->getTagOrders();
     if (count($tags) > 1) {
         $newlineSpan = $comment->getNewlineAfter();
         if ($newlineSpan !== 2) {
             if ($long !== '') {
                 $newlineCount += substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1;
             }
             $phpcsFile->addEvent('BLANK_LINE_BEFORE_PARAM_CLASS_COMMENT', array(), $commentStart + $newlineCount);
             $short = rtrim($short, $phpcsFile->eolChar . ' ');
         }
     }
     // Short description must be single line and end with a full stop.
     $testShort = trim($short);
     $lastChar = $testShort[strlen($testShort) - 1];
     if (substr_count($testShort, $phpcsFile->eolChar) !== 0) {
         $phpcsFile->addEvent('SHORT_DESC_CLASS_COMMENT', array(), $commentStart + 1);
     }
     // Check for unknown/deprecated tags.
     $unknownTags = $this->commentParser->getUnknown();
     foreach ($unknownTags as $errorTag) {
         $tagname = $errorTag[tag];
         $phpcsFile->addEvent('TAG_NOTALLOWED_CLASS_COMMENT', array('tagname' => $tagname), $commentStart + $errorTag['line']);
         return;
     }
     // Check each tag.
     $this->processTags($commentStart, $commentEnd);
 }