当前位置: 首页>>代码示例>>PHP>>正文


PHP PHP_CodeSniffer_File::addFixableWarning方法代码示例

本文整理汇总了PHP中PHP_CodeSniffer_File::addFixableWarning方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer_File::addFixableWarning方法的具体用法?PHP PHP_CodeSniffer_File::addFixableWarning怎么用?PHP PHP_CodeSniffer_File::addFixableWarning使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PHP_CodeSniffer_File的用法示例。


在下文中一共展示了PHP_CodeSniffer_File::addFixableWarning方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: process

 /**
  * Processes the tokens that this sniff is interested in.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
  * @param int                  $stackPtr  The position in the stack where
  *                                        the token was found.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $token = $tokens[$stackPtr];
     $comment = $token['content'];
     $isDoubleSlashComment = substr($comment, 0, 2) === '//';
     if ($isDoubleSlashComment) {
         $hasLeadingSpace = $tokens[$stackPtr]['content'][2] === ' ';
         if ($hasLeadingSpace) {
             $hasMoreThanOneLeadingSpace = $tokens[$stackPtr]['content'][3] === ' ';
             if ($hasMoreThanOneLeadingSpace) {
                 $fix = $phpcsFile->addFixableWarning('Double slash comments must start with a single space', $stackPtr, 'SingleLeadingSpaceNeeded');
                 if ($fix) {
                     $fixedComment = preg_replace('/[ ]+/', ' ', $comment);
                     $phpcsFile->fixer->replaceToken($stackPtr, $fixedComment);
                 }
             }
         } else {
             $fix = $phpcsFile->addFixableWarning('Double slash comments must start with a space', $stackPtr, 'LeadingSpaceNeeded');
             if ($fix) {
                 $fixedComment = substr_replace($comment, '// ', 0, 2);
                 $phpcsFile->fixer->replaceToken($stackPtr, $fixedComment);
             }
         }
     }
 }
开发者ID:Nedeas,项目名称:phpcs-umanit-standard,代码行数:35,代码来源:SingleLineCommentSniff.php

示例2: process

 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $currToken = $tokens[$stackPtr];
     if ($currToken['code'] === T_COMMENT) {
         // Accounting for multiple line comments, as single line comments
         // use only '//' and '#'
         // Also ignoring phpdoc comments starting with '///',
         // as there are no coding standards documented for these
         if (substr($currToken['content'], 0, 2) === '/*' || substr($currToken['content'], 0, 3) === '///') {
             return;
         }
         // Checking whether the comment is an empty one
         if (substr($currToken['content'], 0, 2) === '//' && rtrim($currToken['content']) === '//' || $currToken['content'][0] === '#' && rtrim($currToken['content']) === '#') {
             $phpcsFile->addWarning('Unnecessary empty comment found', $stackPtr, 'EmptyComment');
             // Checking whether there is a space between the comment delimiter
             // and the comment
         } elseif (substr($currToken['content'], 0, 2) === '//' && $currToken['content'][2] !== ' ') {
             $error = 'Single space expected between "//" and comment';
             $fix = $phpcsFile->addFixableWarning($error, $stackPtr, 'SingleSpaceBeforeSingleLineComment');
             if ($fix === true) {
                 $content = $currToken['content'];
                 $newContent = preg_replace('/^\\/\\/\\t?/', '// ', $content);
                 $phpcsFile->fixer->replaceToken($stackPtr, $newContent);
             }
             // Finding what the comment delimiter is and checking whether there is a space
             // between the comment delimiter and the comment.
         } elseif ($currToken['content'][0] === '#') {
             // Find number of `#` used.
             $startComment = 0;
             while ($currToken['content'][$startComment] === '#') {
                 $startComment += 1;
             }
             if ($currToken['content'][$startComment] !== ' ') {
                 $error = 'Single space expected between "#" and comment';
                 $fix = $phpcsFile->addFixableWarning($error, $stackPtr, 'SingleSpaceBeforeSingleLineComment');
                 if ($fix === true) {
                     $content = $currToken['content'];
                     $delimiter = substr($currToken['content'], 0, $startComment);
                     if ($content[$startComment + 1] === '\\t') {
                         $newContent = preg_replace('/^' . $delimiter . '\\t/', $delimiter . ' ', $content);
                     } else {
                         $newContent = preg_replace('/^' . $delimiter . '/', $delimiter . ' ', $content);
                     }
                     $phpcsFile->fixer->replaceToken($stackPtr, $newContent);
                 }
             }
         }
     }
 }
开发者ID:guoyu07,项目名称:mediawiki-tools-codesniffer,代码行数:50,代码来源:SpaceBeforeSingleLineCommentSniff.php

示例3: process

 /**
  * 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();
     $commentStart = $stackPtr;
     $commentEnd = $tokens[$stackPtr]['comment_closer'];
     $empty = array(T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR);
     $short = $phpcsFile->findNext($empty, $stackPtr + 1, $commentEnd, true);
     if ($short === false) {
         // No content at all.
         $error = 'Doc comment is empty';
         $phpcsFile->addError($error, $stackPtr, 'Empty');
         return;
     }
     // The first line of the comment should have /** comment openning and short description.
     if ($tokens[$short]['line'] !== $tokens[$stackPtr]['line']) {
         $error = 'Short description must follow doc comment open tag immediatelly';
         $fix = $phpcsFile->addFixableWarning($error, $stackPtr, 'DescriptionOnAnotherLine');
         if ($fix === true) {
             $phpcsFile->fixer->beginChangeset();
             for ($ptr = $stackPtr + 1; $ptr < $short; ++$ptr) {
                 $phpcsFile->fixer->replaceToken($ptr, '');
             }
             $phpcsFile->fixer->addContentBefore($short, ' ');
             $phpcsFile->fixer->endChangeset();
         }
     }
 }
开发者ID:sharkodlak,项目名称:coding-style,代码行数:36,代码来源:DocCommentSniff.php

示例4: process

 /**
  * 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();
     // Only check use statements in the global scope.
     if (empty($tokens[$stackPtr]['conditions']) === false) {
         return;
     }
     // Seek to the end of the statement and get the string before the semi colon.
     $semiColon = $phpcsFile->findEndOfStatement($stackPtr);
     if ($tokens[$semiColon]['code'] !== T_SEMICOLON) {
         return;
     }
     $classPtr = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $semiColon - 1, null, true);
     // Seek along the statement to get the last part, which is the
     // class/interface name.
     while (isset($tokens[$classPtr + 1]) === true && in_array($tokens[$classPtr + 1]['code'], array(T_STRING, T_NS_SEPARATOR)) === true) {
         $classPtr++;
     }
     if ($tokens[$classPtr]['code'] !== T_STRING) {
         return;
     }
     $classUsed = $phpcsFile->findNext(T_STRING, $classPtr + 1, null, false, $tokens[$classPtr]['content']);
     while ($classUsed !== false) {
         $beforeUsage = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $classUsed - 1, null, true);
         // If a backslash is used before the class name then this is some other
         // use statement.
         if ($tokens[$beforeUsage]['code'] !== T_USE && $tokens[$beforeUsage]['code'] !== T_NS_SEPARATOR) {
             return;
         }
         // Trait use statement within a class.
         if ($tokens[$beforeUsage]['code'] === T_USE && empty($tokens[$beforeUsage]['conditions']) === false) {
             return;
         }
         $classUsed = $phpcsFile->findNext(T_STRING, $classUsed + 1, null, false, $tokens[$classPtr]['content']);
     }
     //end while
     $warning = 'Unused use statement';
     $fix = $phpcsFile->addFixableWarning($warning, $stackPtr, 'UnusedUse');
     if ($fix === true) {
         // Remove the whole use statement line.
         $phpcsFile->fixer->beginChangeset();
         for ($i = $stackPtr; $i <= $semiColon; $i++) {
             $phpcsFile->fixer->replaceToken($i, '');
         }
         // Also remove whitespace after the semicolon (new lines).
         while (isset($tokens[$i]) === true && $tokens[$i]['code'] === T_WHITESPACE) {
             $phpcsFile->fixer->replaceToken($i, '');
             if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false) {
                 break;
             }
             $i++;
         }
         $phpcsFile->fixer->endChangeset();
     }
 }
开发者ID:atif-shaikh,项目名称:DCX-Profile,代码行数:64,代码来源:UnusedUseStatementSniff.php

示例5: process

 /**
  * 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();
     $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
     if ($tokens[$next]['code'] === T_IF) {
         $error = 'Usage of ELSE IF is discouraged; use ELSEIF instead';
         $fix = $phpcsFile->addFixableWarning($error, $stackPtr, 'NotAllowed');
         if ($fix === true && $phpcsFile->fixer->enabled === true) {
             $phpcsFile->fixer->beginChangeset();
             $phpcsFile->fixer->replaceToken($stackPtr, 'elseif');
             for ($i = $stackPtr + 1; $i <= $next; $i++) {
                 $phpcsFile->fixer->replaceToken($i, '');
             }
             $phpcsFile->fixer->endChangeset();
         }
     }
 }
开发者ID:dereuromark,项目名称:cakephp-codesniffer,代码行数:26,代码来源:ElseIfDeclarationSniff.php

示例6: process

 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $nextToken = $tokens[$stackPtr + 1];
     if ($nextToken['code'] !== T_WHITESPACE || $nextToken['content'] !== ' ') {
         $error = 'Control structure "%s" must be followed by a single space';
         $data = array($tokens[$stackPtr]['content']);
         $fix = $phpcsFile->addFixableWarning($error, $stackPtr, 'Incorrect', $data);
         if ($fix === true) {
             if ($nextToken['code'] !== T_WHITESPACE) {
                 $phpcsFile->fixer->addContent($stackPtr, ' ');
             } else {
                 // Too many spaces
                 $phpcsFile->fixer->replaceToken($stackPtr + 1, ' ');
             }
         }
     }
 }
开发者ID:WikiToLearn,项目名称:wikitolearn-vendor,代码行数:18,代码来源:SpaceAfterControlStructureSniff.php

示例7: process

 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $prevToken = $tokens[$stackPtr - 1];
     if ($prevToken['code'] !== T_WHITESPACE || $prevToken['content'] !== " ") {
         $fix = $phpcsFile->addFixableWarning('Single space expected before "%s"', $stackPtr + 1, 'SpaceBeforeElse', array($tokens[$stackPtr]['content']));
         if ($fix === true) {
             if ($prevToken['code'] === T_CLOSE_CURLY_BRACKET) {
                 $phpcsFile->fixer->addContentBefore($stackPtr, ' ');
             } else {
                 // Replace all previous whitespace with a space
                 $phpcsFile->fixer->replaceToken($stackPtr - 1, ' ');
                 for ($i = 2; $tokens[$stackPtr - $i]['code'] === T_WHITESPACE; $i++) {
                     $phpcsFile->fixer->replaceToken($stackPtr - $i, '');
                 }
             }
         }
     }
 }
开发者ID:guoyu07,项目名称:mediawiki-tools-codesniffer,代码行数:19,代码来源:IfElseStructureSniff.php

示例8: process

 /**
  * 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();
     $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
     if ($tokens[$next]['code'] === T_IF) {
         $phpcsFile->recordMetric($stackPtr, 'Use of ELSE IF or ELSEIF', 'else if');
         return;
     }
     if ($tokens[$stackPtr]['code'] === T_ELSEIF) {
         $phpcsFile->recordMetric($stackPtr, 'Use of ELSE IF or ELSEIF', 'elseif');
         $error = 'Usage of ELSEIF is discouraged; use ELSE IF instead';
         $fix = $phpcsFile->addFixableWarning($error, $stackPtr, 'NotAllowed');
         if ($fix === true) {
             $phpcsFile->fixer->beginChangeset();
             $phpcsFile->fixer->replaceToken($stackPtr, 'else if');
             $phpcsFile->fixer->endChangeset();
         }
     }
 }
开发者ID:sharkodlak,项目名称:coding-style,代码行数:28,代码来源:ElseIfDeclarationSniff.php

示例9: process

 /**
  * Processes the tokens that this sniff is interested in.
  *
  * @todo Handle multiple todos in a single comment token.
  *
  * @param PHP_CodeSniffer_File $phpcsFile  The file where the token was found.
  * @param int                  $stackPtr  The position in the stack where
  *                                        the token was found.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $token = $tokens[$stackPtr];
     $comment = $token['content'];
     $todoPos = stripos($comment, 'todo');
     // Is there a todo tag?
     if ($todoPos === false) {
         return;
     }
     // Is there something next to the todo tag that can't be trimmed?
     $hasSomethingBefore = isset($comment[$todoPos - 1]) && !in_array(trim($comment[$todoPos - 1]), array('', '@', '/'));
     $hasSomethingAfter = isset($comment[$todoPos + 4]) && trim($comment[$todoPos + 4]) !== '';
     // If not, then it's really a todo tag!
     if (!$hasSomethingBefore && !$hasSomethingAfter) {
         // Lowercase?
         $todoKeyword = substr($comment, $todoPos, 4);
         if (!preg_match('/todo/', $todoKeyword)) {
             $fix = $phpcsFile->addFixableWarning('Todo should be lowercase', $stackPtr, 'UppercaseForbidden');
             if ($fix) {
                 $fixedComment = str_ireplace('todo', 'todo', $comment);
                 $phpcsFile->fixer->replaceToken($stackPtr, $fixedComment);
             }
         }
         // @ prefix?
         if ($todoPos === 0 || $comment[$todoPos - 1] !== '@') {
             $fix = $phpcsFile->addFixableError('Todo is missing @ prefix', $stackPtr, 'MissingAtSignPrefix');
             if ($fix) {
                 $fixedComment = str_ireplace('todo', '@todo', $comment);
                 $phpcsFile->fixer->replaceToken($stackPtr, $fixedComment);
             }
         }
         // Developer's trigram?
         $todo = '@' . substr($comment, $todoPos, 9);
         if (!preg_match('/@(todo|TODO) [A-Z]{3}\\s/', $todo)) {
             $phpcsFile->addWarning('Todo should be followed by the uppercased developer\'s trigram. Expected something like "@todo ABC" where ABC is the trigram, but found "%s"', $stackPtr, 'TrigramSuffix', array($todo));
         }
     }
 }
开发者ID:Nedeas,项目名称:phpcs-umanit-standard,代码行数:50,代码来源:TodoSniff.php

示例10: addError

 /**
  * Generates the error or warning for this sniff.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  * @param int                  $stackPtr  The position of the forbidden style
  *                                        in the token array.
  * @param string               $style     The name of the forbidden style.
  * @param string               $pattern   The pattern used for the match.
  *
  * @return void
  */
 protected function addError($phpcsFile, $stackPtr, $style, $pattern = null)
 {
     $data = array($style);
     $error = 'The use of style %s is ';
     if ($this->error === true) {
         $type = 'Found';
         $error .= 'forbidden';
     } else {
         $type = 'Discouraged';
         $error .= 'discouraged';
     }
     if ($pattern === null) {
         $pattern = $style;
     }
     if ($this->forbiddenStyles[$pattern] !== null) {
         $data[] = $this->forbiddenStyles[$pattern];
         if ($this->error === true) {
             $fix = $phpcsFile->addFixableError($error . '; use %s instead', $stackPtr, $type . 'WithAlternative', $data);
         } else {
             $fix = $phpcsFile->addFixableWarning($error . '; use %s instead', $stackPtr, $type . 'WithAlternative', $data);
         }
         if ($fix === true) {
             $phpcsFile->fixer->replaceToken($stackPtr, $this->forbiddenStyles[$pattern]);
         }
     } else {
         if ($this->error === true) {
             $phpcsFile->addError($error, $stackPtr, $type, $data);
         } else {
             $phpcsFile->addWarning($error, $stackPtr, $type, $data);
         }
     }
 }
开发者ID:felix021,项目名称:myenv,代码行数:43,代码来源:ForbiddenStylesSniff.php

示例11: process

 /**
  * 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']) === true) {
         $scopeOpener = $tokens[$tokens[$stackPtr]['scope_opener']];
         if (!isset($tokens[$stackPtr]['parenthesis_closer'])) {
             return;
         }
         $parenthesisCloser = $tokens[$tokens[$stackPtr]['parenthesis_closer']];
         if ($scopeOpener['line'] === $parenthesisCloser['line']) {
             return;
         }
         //FIXME!
         $fix = $phpcsFile->addFixableError('Opening curly brace must open on the same line as parentheses closer.', $tokens[$stackPtr]['scope_opener'], 'NotAllowed');
         if ($fix === true && $phpcsFile->fixer->enabled === true) {
             $next = $phpcsFile->findNext(T_WHITESPACE, $tokens[$stackPtr]['parenthesis_closer'] + 1, $tokens[$stackPtr]['scope_opener'] - 1, true);
             $phpcsFile->fixer->beginChangeset();
             $phpcsFile->fixer->addContent($tokens[$stackPtr]['parenthesis_closer'], ' w');
             for ($i = $tokens[$stackPtr]['parenthesis_closer'] + 1; $i < $next; $i++) {
                 $phpcsFile->fixer->replaceToken($i, '');
             }
             $phpcsFile->fixer->endChangeset();
         }
         return;
     }
     // Ignore the ELSE in ELSE IF. We'll process the IF part later.
     if ($tokens[$stackPtr]['code'] === T_ELSE && $tokens[$stackPtr + 2]['code'] === T_IF) {
         return;
     }
     if ($tokens[$stackPtr]['code'] === T_WHILE) {
         // This could be from a DO WHILE, which doesn't have an opening brace.
         $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
         if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
             $brace = $tokens[$lastContent];
             if (isset($brace['scope_condition']) === true) {
                 $condition = $tokens[$brace['scope_condition']];
                 if ($condition['code'] === T_DO) {
                     return;
                 }
             }
         }
     }
     // This is a control structure without an opening brace,
     // so it is an inline statement.
     if ($this->error === true) {
         $fix = $phpcsFile->addFixableError('Inline control structures are not allowed', $stackPtr, 'NotAllowed');
     } else {
         $fix = $phpcsFile->addFixableWarning('Inline control structures are discouraged', $stackPtr, 'Discouraged');
     }
     if ($fix === true && $phpcsFile->fixer->enabled === true) {
         $phpcsFile->fixer->beginChangeset();
         if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
             $closer = $tokens[$stackPtr]['parenthesis_closer'];
         } else {
             $closer = $stackPtr;
         }
         $phpcsFile->fixer->addContent($closer, ' { ');
         $semicolon = $phpcsFile->findNext(T_SEMICOLON, $closer + 1);
         $next = $phpcsFile->findNext(T_WHITESPACE, $closer + 1, $semicolon + 1, true);
         // Account for a comment on the end of the line.
         for ($endLine = $semicolon; $endLine < $phpcsFile->numTokens; $endLine++) {
             if (isset($tokens[$endLine + 1]) === false || $tokens[$endLine]['line'] !== $tokens[$endLine + 1]['line']) {
                 break;
             }
         }
         if ($tokens[$endLine]['code'] !== T_COMMENT) {
             $endLine = $semicolon;
         }
         // Put the closing one a line further down
         $indentation = str_repeat("\t", $tokens[$stackPtr]['level']);
         if ($next !== $semicolon) {
             if ($endLine !== $semicolon) {
                 //$n = $phpcsFile->fixer->getTokenContent($next);
                 //$phpcsFile->fixer->replaceToken($next, "\n" . str_repeat("\t", $tokens[$next]['level'] - 1) . $n);
                 $phpcsFile->fixer->addContent($endLine, '}');
             } else {
                 $n = $phpcsFile->fixer->getTokenContent($next);
                 if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) {
                     $phpcsFile->fixer->replaceToken($next, "\n" . str_repeat("\t", $tokens[$next]['level'] + 1) . $n);
                 }
                 $indentation = "\n" . $indentation;
                 $phpcsFile->fixer->addContent($semicolon, $indentation . '}');
             }
         } else {
             if ($endLine !== $semicolon) {
                 $phpcsFile->fixer->replaceToken($semicolon, '');
                 $phpcsFile->fixer->addNewlineBefore($endLine);
                 $phpcsFile->fixer->addContent($endLine, '}');
             } else {
                 $phpcsFile->fixer->replaceToken($semicolon, '}');
             }
//.........这里部分代码省略.........
开发者ID:dereuromark,项目名称:cakephp-codesniffer,代码行数:101,代码来源:InlineControlStructureSniff.php

示例12: process

 /**
  * 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']) === true) {
         $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no');
         return;
     }
     // Ignore the ELSE in ELSE IF. We'll process the IF part later.
     if ($tokens[$stackPtr]['code'] === T_ELSE) {
         $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
         if ($tokens[$next]['code'] === T_IF) {
             return;
         }
     }
     if ($tokens[$stackPtr]['code'] === T_WHILE) {
         // This could be from a DO WHILE, which doesn't have an opening brace.
         $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
         if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
             $brace = $tokens[$lastContent];
             if (isset($brace['scope_condition']) === true) {
                 $condition = $tokens[$brace['scope_condition']];
                 if ($condition['code'] === T_DO) {
                     return;
                 }
             }
         }
         // In Javascript DO WHILE loops without curly braces are legal. This
         // is only valid if a single statement is present between the DO and
         // the WHILE. We can detect this by checking only a single semicolon
         // is present between them.
         if ($phpcsFile->tokenizerType === 'JS') {
             $lastDo = $phpcsFile->findPrevious(T_DO, $stackPtr - 1);
             $lastSemicolon = $phpcsFile->findPrevious(T_SEMICOLON, $stackPtr - 1);
             if ($lastDo !== false && $lastSemicolon !== false && $lastDo < $lastSemicolon) {
                 $precedingSemicolon = $phpcsFile->findPrevious(T_SEMICOLON, $lastSemicolon - 1);
                 if ($precedingSemicolon === false || $precedingSemicolon < $lastDo) {
                     return;
                 }
             }
         }
     }
     //end if
     // This is a control structure without an opening brace,
     // so it is an inline statement.
     if ($this->error === true) {
         $fix = $phpcsFile->addFixableError('Inline control structures are not allowed', $stackPtr, 'NotAllowed');
     } else {
         $fix = $phpcsFile->addFixableWarning('Inline control structures are discouraged', $stackPtr, 'Discouraged');
     }
     $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'yes');
     // Stop here if we are not fixing the error.
     if ($fix !== true) {
         return;
     }
     $phpcsFile->fixer->beginChangeset();
     if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
         $closer = $tokens[$stackPtr]['parenthesis_closer'];
     } else {
         $closer = $stackPtr;
     }
     if ($tokens[$closer + 1]['code'] === T_WHITESPACE || $tokens[$closer + 1]['code'] === T_SEMICOLON) {
         $phpcsFile->fixer->addContent($closer, ' {');
     } else {
         $phpcsFile->fixer->addContent($closer, ' { ');
     }
     $lastNonEmpty = $closer;
     for ($end = $closer + 1; $end < $phpcsFile->numTokens; $end++) {
         if ($tokens[$end]['code'] === T_SEMICOLON) {
             break;
         }
         if ($tokens[$end]['code'] === T_CLOSE_TAG) {
             $end = $lastNonEmpty;
             break;
         }
         if (isset($tokens[$end]['scope_opener']) === true) {
             $type = $tokens[$end]['code'];
             $end = $tokens[$end]['scope_closer'];
             if ($type === T_DO || $type === T_IF || $type === T_ELSEIF) {
                 $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $end + 1, null, true);
                 if ($next === false) {
                     break;
                 }
                 $nextType = $tokens[$next]['code'];
                 // Let additional conditions loop and find their ending.
                 if (($type === T_IF || $type === T_ELSEIF) && ($nextType === T_ELSEIF || $nextType === T_ELSE)) {
                     continue;
                 }
                 // Account for DO... WHILE conditions.
                 if ($type === T_DO && $nextType === T_WHILE) {
                     $end = $phpcsFile->findNext(T_SEMICOLON, $next + 1);
                 }
//.........这里部分代码省略.........
开发者ID:wpillar,项目名称:PHP_CodeSniffer,代码行数:101,代码来源:InlineControlStructureSniff.php

示例13: process


//.........这里部分代码省略.........
             }
         }
         $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
     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 + 1;
                 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 assignment 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 just check for a single space as no
             // alignment is required.
             if ($expected > $this->maxPadding) {
                 if ($found === 1) {
                     continue;
                 } else {
                     $expected = 1;
                 }
             }
             // Skip multi-line assignments if required.
             if ($found === null && $this->ignoreMultiLine === true) {
                 continue;
             }
             $expectedText = $expected;
             $expectedText .= $expected === 1 ? ' space' : ' spaces';
             $foundText = $found;
             if ($foundText === null) {
                 $foundText = 'a new line';
             } else {
                 $foundText .= $foundText === 1 ? ' space' : ' spaces';
             }
             if (count($assignments) === 1) {
                 $type = 'Incorrect';
                 $error = 'Equals sign not aligned correctly; expected %s but found %s';
             } else {
                 $type = 'NotSame';
                 $error = 'Equals sign not aligned with surrounding assignments; expected %s but found %s';
             }
             $errorData = array($expectedText, $foundText);
             if ($this->error === true) {
                 $fix = $phpcsFile->addFixableError($error, $assignment, $type, $errorData);
             } else {
                 $fix = $phpcsFile->addFixableWarning($error, $assignment, $type . 'Warning', $errorData);
             }
             if ($fix === true && $phpcsFile->fixer->enabled === true && $found !== null) {
                 $newContent = str_repeat(' ', $expected);
                 if ($found === 0) {
                     $phpcsFile->fixer->addContentBefore($assignment, $newContent);
                 } else {
                     $phpcsFile->fixer->replaceToken($assignment - 1, $newContent);
                 }
             }
         }
         //end if
     }
     //end foreach
 }
开发者ID:dereuromark,项目名称:cakephp-codesniffer,代码行数:101,代码来源:MultipleStatementAlignmentSniff.php

示例14: process

 /**
  * 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();
     $arrayStart = $tokens[$stackPtr]['bracket_opener'];
     $arrayEnd = $tokens[$arrayStart]['bracket_closer'];
     // Check for empty arrays.
     $content = $phpcsFile->findNext([T_WHITESPACE], $arrayStart + 1, $arrayEnd + 1, true);
     if ($content === $arrayEnd) {
         // Empty array, but if the brackets aren't together, there's a problem.
         if ($arrayEnd - $arrayStart !== 1) {
             $error = 'Empty array declaration must have no space between the brackets.';
             $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceInEmptyArray');
             if ($fix === true) {
                 $phpcsFile->fixer->beginChangeset();
                 for ($i = $arrayStart + 1; $i < $arrayEnd; $i++) {
                     $phpcsFile->fixer->replaceToken($i, '');
                 }
                 $phpcsFile->fixer->endChangeset();
             }
             // We can return here because there is nothing else to check. All code
             // below can assume that the array is not empty.
             return;
         }
     }
     $lastItem = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $arrayEnd - 1, $stackPtr, true);
     // Empty array.
     if ($lastItem === $arrayStart) {
         return;
     }
     // Inline array.
     $isInlineArray = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line'];
     // Check if the last item in a multiline array has a "closing" comma.
     if ($tokens[$lastItem]['code'] !== T_COMMA && $isInlineArray === false) {
         $error = 'A comma must follow the last multiline array item.';
         $fix = $phpcsFile->addFixableError($error, $lastItem, 'NoLastComma');
         if ($fix === true) {
             $phpcsFile->fixer->beginChangeset();
             $phpcsFile->fixer->addContent($lastItem, ',');
             $phpcsFile->fixer->endChangeset();
         }
         return;
     }
     if ($isInlineArray === true) {
         if ($tokens[$lastItem]['code'] === T_COMMA) {
             $error = 'Comma not allowed after last value in single-line array declaration';
             $fix = $phpcsFile->addFixableWarning($error, $lastItem, 'LastComma');
             if ($fix === true) {
                 $phpcsFile->fixer->beginChangeset();
                 $phpcsFile->fixer->replaceToken($lastItem, '');
                 $phpcsFile->fixer->endChangeset();
             }
             return;
         }
         // Inline array must not have spaces within brackets.
         if ($content !== $arrayStart + 1) {
             $error = 'Space found after opening bracket of array';
             $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen');
             if ($fix === true) {
                 $phpcsFile->fixer->beginChangeset();
                 for ($i = $arrayStart + 1; $i < $content; $i++) {
                     $phpcsFile->fixer->replaceToken($i, '');
                 }
                 $phpcsFile->fixer->endChangeset();
             }
         }
         if ($lastItem !== $arrayEnd - 1) {
             $error = 'Space found before closing bracket of array';
             $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose');
             if ($fix === true) {
                 $phpcsFile->fixer->beginChangeset();
                 for ($i = $lastItem + 1; $i < $arrayEnd; $i++) {
                     $phpcsFile->fixer->replaceToken($i, '');
                 }
                 $phpcsFile->fixer->endChangeset();
             }
         }
     }
 }
开发者ID:loadsys,项目名称:loadsys_codesniffer,代码行数:85,代码来源:ArraySniff.php

示例15: process

 /**
  * 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']) === true) {
         $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no');
         return;
     }
     // Ignore the ELSE in ELSE IF. We'll process the IF part later.
     if ($tokens[$stackPtr]['code'] === T_ELSE && $tokens[$stackPtr + 2]['code'] === T_IF) {
         return;
     }
     if ($tokens[$stackPtr]['code'] === T_WHILE) {
         // This could be from a DO WHILE, which doesn't have an opening brace.
         $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
         if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
             $brace = $tokens[$lastContent];
             if (isset($brace['scope_condition']) === true) {
                 $condition = $tokens[$brace['scope_condition']];
                 if ($condition['code'] === T_DO) {
                     return;
                 }
             }
         }
     }
     // This is a control structure without an opening brace,
     // so it is an inline statement.
     if ($this->error === true) {
         $fix = $phpcsFile->addFixableError('Inline control structures are not allowed', $stackPtr, 'NotAllowed');
     } else {
         $fix = $phpcsFile->addFixableWarning('Inline control structures are discouraged', $stackPtr, 'Discouraged');
     }
     $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'yes');
     if ($fix === true) {
         $phpcsFile->fixer->beginChangeset();
         if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
             $closer = $tokens[$stackPtr]['parenthesis_closer'];
         } else {
             $closer = $stackPtr;
         }
         if ($tokens[$closer + 1]['code'] === T_WHITESPACE || $tokens[$closer + 1]['code'] === T_SEMICOLON) {
             $phpcsFile->fixer->addContent($closer, ' {');
         } else {
             $phpcsFile->fixer->addContent($closer, ' { ');
         }
         for ($end = $closer + 1; $end < $phpcsFile->numTokens; $end++) {
             if ($tokens[$end]['code'] === T_SEMICOLON) {
                 break;
             }
             if (isset($tokens[$end]['scope_opener']) === true) {
                 $end = $tokens[$end]['scope_closer'];
                 break;
             }
         }
         $next = $phpcsFile->findNext(T_WHITESPACE, $closer + 1, $end + 1, true);
         // Account for a comment on the end of the line.
         for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
             if (isset($tokens[$endLine + 1]) === false || $tokens[$endLine]['line'] !== $tokens[$endLine + 1]['line']) {
                 break;
             }
         }
         if ($tokens[$endLine]['code'] !== T_COMMENT) {
             $endLine = $end;
         }
         if ($next !== $end) {
             if ($endLine !== $end) {
                 $phpcsFile->fixer->addContent($endLine, '}');
             } else {
                 $phpcsFile->fixer->addContent($end, ' }');
             }
         } else {
             if ($endLine !== $end) {
                 $phpcsFile->fixer->replaceToken($end, '');
                 $phpcsFile->fixer->addNewlineBefore($endLine);
                 $phpcsFile->fixer->addContent($endLine, '}');
             } else {
                 $phpcsFile->fixer->replaceToken($end, '}');
             }
         }
         $phpcsFile->fixer->endChangeset();
     }
     //end if
 }
开发者ID:eredi93,项目名称:forms,代码行数:91,代码来源:InlineControlStructureSniff.php


注:本文中的PHP_CodeSniffer_File::addFixableWarning方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。