@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | /** |
| 42 | 42 | * Returns an array of tokens this test wants to listen for. |
| 43 | 43 | * |
| 44 | - * @return array |
|
| 44 | + * @return integer[] |
|
| 45 | 45 | */ |
| 46 | 46 | public function register() |
| 47 | 47 | { |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | * @param File $phpcsFile The current file being scanned. |
| 119 | 119 | * @param int $stackPtr Pointer to the first comment line. |
| 120 | 120 | * |
| 121 | - * @return type array Pointers to tokens making up the comment block. |
|
| 121 | + * @return integer[] array Pointers to tokens making up the comment block. |
|
| 122 | 122 | */ |
| 123 | 123 | private function _getCommentBlock(File $phpcsFile, $stackPtr) |
| 124 | 124 | { |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | * Add errors to $phpcsFile, if $commentLines isn't enclosed with blank lines. |
| 146 | 146 | * |
| 147 | 147 | * @param File $phpcsFile The current file being scanned. |
| 148 | - * @param array $commentLines Lines of the comment block being checked. |
|
| 148 | + * @param integer[] $commentLines Lines of the comment block being checked. |
|
| 149 | 149 | * |
| 150 | 150 | * @return bool TRUE if $commentLines is enclosed with at least a blank line |
| 151 | 151 | * before and after, FALSE otherwise. |
@@ -32,155 +32,155 @@ |
||
| 32 | 32 | |
| 33 | 33 | class InlineCommentSniff implements Sniff |
| 34 | 34 | { |
| 35 | - /** |
|
| 36 | - * @var int Limit defining long comments. |
|
| 37 | - * Long comments count $longCommentLimit or more lines. |
|
| 38 | - */ |
|
| 39 | - public $longCommentLimit = 5; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Returns an array of tokens this test wants to listen for. |
|
| 43 | - * |
|
| 44 | - * @return array |
|
| 45 | - */ |
|
| 46 | - public function register() |
|
| 47 | - { |
|
| 48 | - return array( |
|
| 49 | - T_COMMENT |
|
| 50 | - ); |
|
| 51 | - }//end register() |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Processes this test, when one of its tokens is encountered. |
|
| 55 | - * |
|
| 56 | - * @param File $phpcsFile The current file being scanned. |
|
| 57 | - * @param int $stackPtr The position of the current token |
|
| 58 | - * in the stack passed in $tokens. |
|
| 59 | - * |
|
| 60 | - * @return void |
|
| 61 | - */ |
|
| 62 | - public function process(File $phpcsFile, $stackPtr) |
|
| 63 | - { |
|
| 64 | - $tokens = $phpcsFile->getTokens(); |
|
| 65 | - |
|
| 66 | - // keep testing only if it's about the first comment of the block |
|
| 67 | - $previousCommentPtr = $phpcsFile->findPrevious($tokens[$stackPtr]['code'], $stackPtr - 1); |
|
| 68 | - if ($tokens[$previousCommentPtr]['line'] !== $tokens[$stackPtr]['line'] - 1) { |
|
| 69 | - if (TRUE !== $this->_checkCommentStyle($phpcsFile, $stackPtr)) { |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $commentLines = $this->_getCommentBlock($phpcsFile, $stackPtr); |
|
| 74 | - |
|
| 75 | - if (count($commentLines) >= $this->longCommentLimit) { |
|
| 76 | - $this->_checkBlankLinesAroundLongComment($phpcsFile, $commentLines); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - }//end process() |
|
| 80 | - |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Add error to $phpcsFile, if comment pointed by $stackPtr doesn't start |
|
| 84 | - * with '//'. |
|
| 85 | - * |
|
| 86 | - * @param File $phpcsFile The current file being scanned. |
|
| 87 | - * @param int $stackPtr The position of the current token |
|
| 88 | - * that has to be a comment. |
|
| 89 | - * |
|
| 90 | - * @return bool TRUE if the content of the token pointed by $stackPtr starts |
|
| 91 | - * with //, FALSE if an error was added to $phpcsFile. |
|
| 92 | - */ |
|
| 93 | - private function _checkCommentStyle(File $phpcsFile, $stackPtr) |
|
| 94 | - { |
|
| 95 | - $tokens = $phpcsFile->getTokens(); |
|
| 96 | - if ($tokens[$stackPtr]['content']{0} === '#') { |
|
| 97 | - $error = 'Perl-style comments are not allowed; use "// Comment" or DocBlock comments instead'; |
|
| 98 | - $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 99 | - return FALSE; |
|
| 100 | - } else if (substr($tokens[$stackPtr]['content'], 0, 2) === '/*' |
|
| 101 | - || $tokens[$stackPtr]['content']{0} === '*' |
|
| 102 | - ) { |
|
| 103 | - $error = 'Multi lines comments are not allowed; use "// Comment" DocBlock comments instead'; |
|
| 104 | - $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 105 | - return FALSE; |
|
| 106 | - } else if (substr($tokens[$stackPtr]['content'], 0, 2) !== '//') { |
|
| 107 | - $error = 'Use single line or DocBlock comments within code'; |
|
| 108 | - $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 109 | - return FALSE; |
|
| 110 | - } |
|
| 111 | - return TRUE; |
|
| 112 | - }//_checkCommentStyle() |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Gather into an array all comment lines to which $stackPtr belongs. |
|
| 117 | - * |
|
| 118 | - * @param File $phpcsFile The current file being scanned. |
|
| 119 | - * @param int $stackPtr Pointer to the first comment line. |
|
| 120 | - * |
|
| 121 | - * @return type array Pointers to tokens making up the comment block. |
|
| 122 | - */ |
|
| 123 | - private function _getCommentBlock(File $phpcsFile, $stackPtr) |
|
| 124 | - { |
|
| 125 | - $tokens = $phpcsFile->getTokens(); |
|
| 126 | - $commentLines = array($stackPtr); |
|
| 127 | - $nextComment = $stackPtr; |
|
| 128 | - $lastLine = $tokens[$stackPtr]['line']; |
|
| 129 | - |
|
| 130 | - while (($nextComment = $phpcsFile->findNext($tokens[$stackPtr]['code'], ($nextComment + 1), null, false)) !== false) { |
|
| 131 | - if (($tokens[$nextComment]['line'] - 1) !== $lastLine) { |
|
| 132 | - // Not part of the block. |
|
| 133 | - break; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $lastLine = $tokens[$nextComment]['line']; |
|
| 137 | - $commentLines[] = $nextComment; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return $commentLines; |
|
| 141 | - }//_getCommentBlock() |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Add errors to $phpcsFile, if $commentLines isn't enclosed with blank lines. |
|
| 146 | - * |
|
| 147 | - * @param File $phpcsFile The current file being scanned. |
|
| 148 | - * @param array $commentLines Lines of the comment block being checked. |
|
| 149 | - * |
|
| 150 | - * @return bool TRUE if $commentLines is enclosed with at least a blank line |
|
| 151 | - * before and after, FALSE otherwise. |
|
| 152 | - */ |
|
| 153 | - private function _checkBlankLinesAroundLongComment(File $phpcsFile, array $commentLines) |
|
| 154 | - { |
|
| 155 | - $hasBlankLinesAround = TRUE; |
|
| 156 | - $tokens = $phpcsFile->getTokens(); |
|
| 157 | - |
|
| 158 | - // check blank line before the long comment |
|
| 159 | - $firstCommentPtr = reset($commentLines); |
|
| 160 | - $firstPreviousSpacePtr = $firstCommentPtr - 1; |
|
| 161 | - while (T_WHITESPACE === $tokens[$firstPreviousSpacePtr]['code'] && $firstPreviousSpacePtr > 0) { |
|
| 162 | - $firstPreviousSpacePtr--; |
|
| 163 | - } |
|
| 164 | - if ($tokens[$firstPreviousSpacePtr]['line'] >= $tokens[$firstCommentPtr]['line'] - 1) { |
|
| 165 | - $error = "Please add a blank line before comments counting more than {$this->longCommentLimit} lines."; |
|
| 166 | - $phpcsFile->addError($error, $firstCommentPtr, 'LongCommentWithoutSpacing'); |
|
| 167 | - $hasBlankLinesAround = FALSE; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - // check blank line after the long comment |
|
| 171 | - $lastCommentPtr = end($commentLines); |
|
| 172 | - $lastNextSpacePtr = $lastCommentPtr + 1; |
|
| 173 | - while (T_WHITESPACE === $tokens[$lastNextSpacePtr]['code'] && $lastNextSpacePtr < count($tokens)) { |
|
| 174 | - $lastNextSpacePtr++; |
|
| 175 | - } |
|
| 176 | - if ($tokens[$lastNextSpacePtr]['line'] <= $tokens[$lastCommentPtr]['line'] + 1) { |
|
| 177 | - $error = "Please add a blank line after comments counting more than {$this->longCommentLimit} lines."; |
|
| 178 | - $phpcsFile->addError($error, $lastCommentPtr, 'LongCommentWithoutSpacing'); |
|
| 179 | - $hasBlankLinesAround = FALSE; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return $hasBlankLinesAround; |
|
| 183 | - }//end _checkBlanksAroundLongComment() |
|
| 35 | + /** |
|
| 36 | + * @var int Limit defining long comments. |
|
| 37 | + * Long comments count $longCommentLimit or more lines. |
|
| 38 | + */ |
|
| 39 | + public $longCommentLimit = 5; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Returns an array of tokens this test wants to listen for. |
|
| 43 | + * |
|
| 44 | + * @return array |
|
| 45 | + */ |
|
| 46 | + public function register() |
|
| 47 | + { |
|
| 48 | + return array( |
|
| 49 | + T_COMMENT |
|
| 50 | + ); |
|
| 51 | + }//end register() |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Processes this test, when one of its tokens is encountered. |
|
| 55 | + * |
|
| 56 | + * @param File $phpcsFile The current file being scanned. |
|
| 57 | + * @param int $stackPtr The position of the current token |
|
| 58 | + * in the stack passed in $tokens. |
|
| 59 | + * |
|
| 60 | + * @return void |
|
| 61 | + */ |
|
| 62 | + public function process(File $phpcsFile, $stackPtr) |
|
| 63 | + { |
|
| 64 | + $tokens = $phpcsFile->getTokens(); |
|
| 65 | + |
|
| 66 | + // keep testing only if it's about the first comment of the block |
|
| 67 | + $previousCommentPtr = $phpcsFile->findPrevious($tokens[$stackPtr]['code'], $stackPtr - 1); |
|
| 68 | + if ($tokens[$previousCommentPtr]['line'] !== $tokens[$stackPtr]['line'] - 1) { |
|
| 69 | + if (TRUE !== $this->_checkCommentStyle($phpcsFile, $stackPtr)) { |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $commentLines = $this->_getCommentBlock($phpcsFile, $stackPtr); |
|
| 74 | + |
|
| 75 | + if (count($commentLines) >= $this->longCommentLimit) { |
|
| 76 | + $this->_checkBlankLinesAroundLongComment($phpcsFile, $commentLines); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + }//end process() |
|
| 80 | + |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Add error to $phpcsFile, if comment pointed by $stackPtr doesn't start |
|
| 84 | + * with '//'. |
|
| 85 | + * |
|
| 86 | + * @param File $phpcsFile The current file being scanned. |
|
| 87 | + * @param int $stackPtr The position of the current token |
|
| 88 | + * that has to be a comment. |
|
| 89 | + * |
|
| 90 | + * @return bool TRUE if the content of the token pointed by $stackPtr starts |
|
| 91 | + * with //, FALSE if an error was added to $phpcsFile. |
|
| 92 | + */ |
|
| 93 | + private function _checkCommentStyle(File $phpcsFile, $stackPtr) |
|
| 94 | + { |
|
| 95 | + $tokens = $phpcsFile->getTokens(); |
|
| 96 | + if ($tokens[$stackPtr]['content']{0} === '#') { |
|
| 97 | + $error = 'Perl-style comments are not allowed; use "// Comment" or DocBlock comments instead'; |
|
| 98 | + $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 99 | + return FALSE; |
|
| 100 | + } else if (substr($tokens[$stackPtr]['content'], 0, 2) === '/*' |
|
| 101 | + || $tokens[$stackPtr]['content']{0} === '*' |
|
| 102 | + ) { |
|
| 103 | + $error = 'Multi lines comments are not allowed; use "// Comment" DocBlock comments instead'; |
|
| 104 | + $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 105 | + return FALSE; |
|
| 106 | + } else if (substr($tokens[$stackPtr]['content'], 0, 2) !== '//') { |
|
| 107 | + $error = 'Use single line or DocBlock comments within code'; |
|
| 108 | + $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 109 | + return FALSE; |
|
| 110 | + } |
|
| 111 | + return TRUE; |
|
| 112 | + }//_checkCommentStyle() |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Gather into an array all comment lines to which $stackPtr belongs. |
|
| 117 | + * |
|
| 118 | + * @param File $phpcsFile The current file being scanned. |
|
| 119 | + * @param int $stackPtr Pointer to the first comment line. |
|
| 120 | + * |
|
| 121 | + * @return type array Pointers to tokens making up the comment block. |
|
| 122 | + */ |
|
| 123 | + private function _getCommentBlock(File $phpcsFile, $stackPtr) |
|
| 124 | + { |
|
| 125 | + $tokens = $phpcsFile->getTokens(); |
|
| 126 | + $commentLines = array($stackPtr); |
|
| 127 | + $nextComment = $stackPtr; |
|
| 128 | + $lastLine = $tokens[$stackPtr]['line']; |
|
| 129 | + |
|
| 130 | + while (($nextComment = $phpcsFile->findNext($tokens[$stackPtr]['code'], ($nextComment + 1), null, false)) !== false) { |
|
| 131 | + if (($tokens[$nextComment]['line'] - 1) !== $lastLine) { |
|
| 132 | + // Not part of the block. |
|
| 133 | + break; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $lastLine = $tokens[$nextComment]['line']; |
|
| 137 | + $commentLines[] = $nextComment; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return $commentLines; |
|
| 141 | + }//_getCommentBlock() |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Add errors to $phpcsFile, if $commentLines isn't enclosed with blank lines. |
|
| 146 | + * |
|
| 147 | + * @param File $phpcsFile The current file being scanned. |
|
| 148 | + * @param array $commentLines Lines of the comment block being checked. |
|
| 149 | + * |
|
| 150 | + * @return bool TRUE if $commentLines is enclosed with at least a blank line |
|
| 151 | + * before and after, FALSE otherwise. |
|
| 152 | + */ |
|
| 153 | + private function _checkBlankLinesAroundLongComment(File $phpcsFile, array $commentLines) |
|
| 154 | + { |
|
| 155 | + $hasBlankLinesAround = TRUE; |
|
| 156 | + $tokens = $phpcsFile->getTokens(); |
|
| 157 | + |
|
| 158 | + // check blank line before the long comment |
|
| 159 | + $firstCommentPtr = reset($commentLines); |
|
| 160 | + $firstPreviousSpacePtr = $firstCommentPtr - 1; |
|
| 161 | + while (T_WHITESPACE === $tokens[$firstPreviousSpacePtr]['code'] && $firstPreviousSpacePtr > 0) { |
|
| 162 | + $firstPreviousSpacePtr--; |
|
| 163 | + } |
|
| 164 | + if ($tokens[$firstPreviousSpacePtr]['line'] >= $tokens[$firstCommentPtr]['line'] - 1) { |
|
| 165 | + $error = "Please add a blank line before comments counting more than {$this->longCommentLimit} lines."; |
|
| 166 | + $phpcsFile->addError($error, $firstCommentPtr, 'LongCommentWithoutSpacing'); |
|
| 167 | + $hasBlankLinesAround = FALSE; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + // check blank line after the long comment |
|
| 171 | + $lastCommentPtr = end($commentLines); |
|
| 172 | + $lastNextSpacePtr = $lastCommentPtr + 1; |
|
| 173 | + while (T_WHITESPACE === $tokens[$lastNextSpacePtr]['code'] && $lastNextSpacePtr < count($tokens)) { |
|
| 174 | + $lastNextSpacePtr++; |
|
| 175 | + } |
|
| 176 | + if ($tokens[$lastNextSpacePtr]['line'] <= $tokens[$lastCommentPtr]['line'] + 1) { |
|
| 177 | + $error = "Please add a blank line after comments counting more than {$this->longCommentLimit} lines."; |
|
| 178 | + $phpcsFile->addError($error, $lastCommentPtr, 'LongCommentWithoutSpacing'); |
|
| 179 | + $hasBlankLinesAround = FALSE; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return $hasBlankLinesAround; |
|
| 183 | + }//end _checkBlanksAroundLongComment() |
|
| 184 | 184 | |
| 185 | 185 | }//end class |
| 186 | 186 | |
@@ -35,7 +35,7 @@ |
||
| 35 | 35 | /** |
| 36 | 36 | * Returns an array of tokens this test wants to listen for. |
| 37 | 37 | * |
| 38 | - * @return array |
|
| 38 | + * @return integer[] |
|
| 39 | 39 | */ |
| 40 | 40 | public function register() |
| 41 | 41 | { |
@@ -32,67 +32,67 @@ |
||
| 32 | 32 | |
| 33 | 33 | class ByteOrderMarkSniff implements Sniff |
| 34 | 34 | { |
| 35 | - /** |
|
| 36 | - * Returns an array of tokens this test wants to listen for. |
|
| 37 | - * |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function register() |
|
| 41 | - { |
|
| 42 | - return array( T_OPEN_TAG ); |
|
| 43 | - }//end register() |
|
| 35 | + /** |
|
| 36 | + * Returns an array of tokens this test wants to listen for. |
|
| 37 | + * |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function register() |
|
| 41 | + { |
|
| 42 | + return array( T_OPEN_TAG ); |
|
| 43 | + }//end register() |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * List of supported BOM definitions. |
|
| 47 | - * |
|
| 48 | - * Use encoding names as keys and hex BOM representations as values. |
|
| 49 | - * |
|
| 50 | - * @return array |
|
| 51 | - */ |
|
| 52 | - protected function getBomDefinitions() |
|
| 53 | - { |
|
| 54 | - return array( |
|
| 55 | - 'UTF-8' => 'efbbbf', |
|
| 56 | - 'UTF-16 (BE)' => 'feff', |
|
| 57 | - 'UTF-16 (LE)' => 'fffe', |
|
| 58 | - 'UTF-32 (BE)' => '0000feff', |
|
| 59 | - 'UTF-32 (LE)' => 'fffe0000' |
|
| 60 | - ); |
|
| 61 | - }//end getBomDefinitions() |
|
| 45 | + /** |
|
| 46 | + * List of supported BOM definitions. |
|
| 47 | + * |
|
| 48 | + * Use encoding names as keys and hex BOM representations as values. |
|
| 49 | + * |
|
| 50 | + * @return array |
|
| 51 | + */ |
|
| 52 | + protected function getBomDefinitions() |
|
| 53 | + { |
|
| 54 | + return array( |
|
| 55 | + 'UTF-8' => 'efbbbf', |
|
| 56 | + 'UTF-16 (BE)' => 'feff', |
|
| 57 | + 'UTF-16 (LE)' => 'fffe', |
|
| 58 | + 'UTF-32 (BE)' => '0000feff', |
|
| 59 | + 'UTF-32 (LE)' => 'fffe0000' |
|
| 60 | + ); |
|
| 61 | + }//end getBomDefinitions() |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Process tokens. |
|
| 65 | - * |
|
| 66 | - * Actually, only proceed when we're at index 0, this should be the only case |
|
| 67 | - * that will contain BOM. Then check if BOM definition matches what |
|
| 68 | - * we've found as file's inline HTML. Inline HTML could be longer than just BOM |
|
| 69 | - * so make sure you test as much as needed. |
|
| 70 | - * |
|
| 71 | - * @param File $phpcsFile The current file being scanned. |
|
| 72 | - * @param int $stackPtr The position of the current token |
|
| 73 | - * in the stack passed in $tokens. |
|
| 74 | - * |
|
| 75 | - * @return void |
|
| 76 | - */ |
|
| 77 | - public function process(File $phpcsFile, $stackPtr ) |
|
| 78 | - { |
|
| 79 | - // We are only interested if this is the first open tag. |
|
| 80 | - if ($stackPtr !== 0) { |
|
| 81 | - if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 82 | - return; |
|
| 83 | - } |
|
| 84 | - } |
|
| 63 | + /** |
|
| 64 | + * Process tokens. |
|
| 65 | + * |
|
| 66 | + * Actually, only proceed when we're at index 0, this should be the only case |
|
| 67 | + * that will contain BOM. Then check if BOM definition matches what |
|
| 68 | + * we've found as file's inline HTML. Inline HTML could be longer than just BOM |
|
| 69 | + * so make sure you test as much as needed. |
|
| 70 | + * |
|
| 71 | + * @param File $phpcsFile The current file being scanned. |
|
| 72 | + * @param int $stackPtr The position of the current token |
|
| 73 | + * in the stack passed in $tokens. |
|
| 74 | + * |
|
| 75 | + * @return void |
|
| 76 | + */ |
|
| 77 | + public function process(File $phpcsFile, $stackPtr ) |
|
| 78 | + { |
|
| 79 | + // We are only interested if this is the first open tag. |
|
| 80 | + if ($stackPtr !== 0) { |
|
| 81 | + if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 82 | + return; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - $tokens = $phpcsFile->getTokens(); |
|
| 87 | - $fileStartString = $tokens[0]['content']; |
|
| 88 | - foreach ($this->getBomDefinitions() as $bomName => $expectedBomHex) { |
|
| 89 | - $bomByteLength = strlen($expectedBomHex) / 2; |
|
| 90 | - $fileStartHex = bin2hex(substr($fileStartString, 0, $bomByteLength)); |
|
| 91 | - if ($fileStartHex === $expectedBomHex) { |
|
| 92 | - $error = "File contains a $bomName byte order mark (BOM)."; |
|
| 93 | - $phpcsFile->addError($error, $stackPtr); |
|
| 94 | - break; |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - }//end process() |
|
| 86 | + $tokens = $phpcsFile->getTokens(); |
|
| 87 | + $fileStartString = $tokens[0]['content']; |
|
| 88 | + foreach ($this->getBomDefinitions() as $bomName => $expectedBomHex) { |
|
| 89 | + $bomByteLength = strlen($expectedBomHex) / 2; |
|
| 90 | + $fileStartHex = bin2hex(substr($fileStartString, 0, $bomByteLength)); |
|
| 91 | + if ($fileStartHex === $expectedBomHex) { |
|
| 92 | + $error = "File contains a $bomName byte order mark (BOM)."; |
|
| 93 | + $phpcsFile->addError($error, $stackPtr); |
|
| 94 | + break; |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + }//end process() |
|
| 98 | 98 | } |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | /** |
| 37 | 37 | * Returns an array of tokens this test wants to listen for. |
| 38 | 38 | * |
| 39 | - * @return array |
|
| 39 | + * @return integer[] |
|
| 40 | 40 | */ |
| 41 | 41 | public function register() |
| 42 | 42 | { |
@@ -174,6 +174,7 @@ discard block |
||
| 174 | 174 | * |
| 175 | 175 | * @param string $str String to split. |
| 176 | 176 | * @param int $len number of characters per chunk |
| 177 | + * @param string $glue |
|
| 177 | 178 | * |
| 178 | 179 | * @return array string array after splitting |
| 179 | 180 | * |
@@ -33,74 +33,74 @@ discard block |
||
| 33 | 33 | class Utf8EncodingSniff implements Sniff |
| 34 | 34 | { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Returns an array of tokens this test wants to listen for. |
|
| 38 | - * |
|
| 39 | - * @return array |
|
| 40 | - */ |
|
| 41 | - public function register() |
|
| 42 | - { |
|
| 43 | - return array( |
|
| 44 | - T_OPEN_TAG |
|
| 45 | - ); |
|
| 46 | - |
|
| 47 | - }//end register() |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Processes this test, when one of its tokens is encountered. |
|
| 52 | - * |
|
| 53 | - * @param File $phpcsFile The current file being scanned. |
|
| 54 | - * @param int $stackPtr The position of the current token |
|
| 55 | - * in the stack passed in $tokens. |
|
| 56 | - * |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function process(File $phpcsFile, $stackPtr) |
|
| 60 | - { |
|
| 61 | - // We are only interested if this is the first open tag. |
|
| 62 | - if ($stackPtr !== 0) { |
|
| 63 | - if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 64 | - return; |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $file_path = $phpcsFile->getFilename(); |
|
| 69 | - $file_name = basename($file_path); |
|
| 70 | - $file_content = file_get_contents($file_path); |
|
| 71 | - if (false === mb_check_encoding($file_content, 'UTF-8')) { |
|
| 72 | - $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding.'; |
|
| 73 | - $phpcsFile->addError($error, 0); |
|
| 74 | - } |
|
| 75 | - if ( ! self::_checkUtf8W3c($file_content)) { |
|
| 76 | - $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not successfully pass the W3C test.'; |
|
| 77 | - $phpcsFile->addError($error, 0); |
|
| 78 | - } |
|
| 79 | - if ( ! self::_checkUtf8Rfc3629($file_content)) { |
|
| 80 | - $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not meet RFC3629 requirements.'; |
|
| 81 | - $phpcsFile->addError($error, 0); |
|
| 82 | - } |
|
| 83 | - }//end process() |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Checks that the string $content contains only valid UTF-8 chars |
|
| 88 | - * using W3C's method. |
|
| 89 | - * Returns true if $content contains only UTF-8 chars, false otherwise. |
|
| 90 | - * |
|
| 91 | - * @param string $content String to check. |
|
| 92 | - * |
|
| 93 | - * @return bool true if $content contains only UTF-8 chars, false otherwise. |
|
| 94 | - * |
|
| 95 | - * @see http://w3.org/International/questions/qa-forms-utf-8.html |
|
| 96 | - */ |
|
| 97 | - private static function _checkUtf8W3c($content) |
|
| 98 | - { |
|
| 99 | - $content_chunks=self::mb_chunk_split($content, 4096, ''); |
|
| 100 | - foreach($content_chunks as $content_chunk) |
|
| 36 | + /** |
|
| 37 | + * Returns an array of tokens this test wants to listen for. |
|
| 38 | + * |
|
| 39 | + * @return array |
|
| 40 | + */ |
|
| 41 | + public function register() |
|
| 42 | + { |
|
| 43 | + return array( |
|
| 44 | + T_OPEN_TAG |
|
| 45 | + ); |
|
| 46 | + |
|
| 47 | + }//end register() |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Processes this test, when one of its tokens is encountered. |
|
| 52 | + * |
|
| 53 | + * @param File $phpcsFile The current file being scanned. |
|
| 54 | + * @param int $stackPtr The position of the current token |
|
| 55 | + * in the stack passed in $tokens. |
|
| 56 | + * |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function process(File $phpcsFile, $stackPtr) |
|
| 60 | + { |
|
| 61 | + // We are only interested if this is the first open tag. |
|
| 62 | + if ($stackPtr !== 0) { |
|
| 63 | + if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 64 | + return; |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $file_path = $phpcsFile->getFilename(); |
|
| 69 | + $file_name = basename($file_path); |
|
| 70 | + $file_content = file_get_contents($file_path); |
|
| 71 | + if (false === mb_check_encoding($file_content, 'UTF-8')) { |
|
| 72 | + $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding.'; |
|
| 73 | + $phpcsFile->addError($error, 0); |
|
| 74 | + } |
|
| 75 | + if ( ! self::_checkUtf8W3c($file_content)) { |
|
| 76 | + $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not successfully pass the W3C test.'; |
|
| 77 | + $phpcsFile->addError($error, 0); |
|
| 78 | + } |
|
| 79 | + if ( ! self::_checkUtf8Rfc3629($file_content)) { |
|
| 80 | + $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not meet RFC3629 requirements.'; |
|
| 81 | + $phpcsFile->addError($error, 0); |
|
| 82 | + } |
|
| 83 | + }//end process() |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Checks that the string $content contains only valid UTF-8 chars |
|
| 88 | + * using W3C's method. |
|
| 89 | + * Returns true if $content contains only UTF-8 chars, false otherwise. |
|
| 90 | + * |
|
| 91 | + * @param string $content String to check. |
|
| 92 | + * |
|
| 93 | + * @return bool true if $content contains only UTF-8 chars, false otherwise. |
|
| 94 | + * |
|
| 95 | + * @see http://w3.org/International/questions/qa-forms-utf-8.html |
|
| 96 | + */ |
|
| 97 | + private static function _checkUtf8W3c($content) |
|
| 98 | + { |
|
| 99 | + $content_chunks=self::mb_chunk_split($content, 4096, ''); |
|
| 100 | + foreach($content_chunks as $content_chunk) |
|
| 101 | 101 | { |
| 102 | 102 | $preg_result= preg_match( |
| 103 | - '%^(?: |
|
| 103 | + '%^(?: |
|
| 104 | 104 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 105 | 105 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
| 106 | 106 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 111 | 111 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 112 | 112 | )*$%xs', |
| 113 | - $content_chunk |
|
| 113 | + $content_chunk |
|
| 114 | 114 | ); |
| 115 | 115 | if($preg_result!==1) |
| 116 | 116 | { |
@@ -119,66 +119,66 @@ discard block |
||
| 119 | 119 | |
| 120 | 120 | } |
| 121 | 121 | return true; |
| 122 | - }//end _checkUtf8W3c() |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Checks that the string $content contains only valid UTF-8 chars |
|
| 126 | - * using the method described in RFC 3629. |
|
| 127 | - * Returns true if $content contains only UTF-8 chars, false otherwise. |
|
| 128 | - * |
|
| 129 | - * @param string $content String to check. |
|
| 130 | - * |
|
| 131 | - * @return bool true if $content contains only UTF-8 chars, false otherwise. |
|
| 132 | - * |
|
| 133 | - * @see http://www.php.net/manual/en/function.mb-detect-encoding.php#85294 |
|
| 134 | - */ |
|
| 135 | - private static function _checkUtf8Rfc3629($content) |
|
| 136 | - { |
|
| 137 | - $len = strlen($content); |
|
| 138 | - for ($i = 0; $i < $len; $i++) { |
|
| 139 | - $c = ord($content[$i]); |
|
| 140 | - if ($c > 128) { |
|
| 141 | - if (($c >= 254)) { |
|
| 142 | - return false; |
|
| 143 | - } elseif ($c >= 252) { |
|
| 144 | - $bits=6; |
|
| 145 | - } elseif ($c >= 248) { |
|
| 146 | - $bits=5; |
|
| 147 | - } elseif ($c >= 240) { |
|
| 148 | - $bytes = 4; |
|
| 149 | - } elseif ($c >= 224) { |
|
| 150 | - $bytes = 3; |
|
| 151 | - } elseif ($c >= 192) { |
|
| 152 | - $bytes = 2; |
|
| 153 | - } else { |
|
| 154 | - return false; |
|
| 155 | - } if (($i + $bytes) > $len) { |
|
| 156 | - return false; |
|
| 157 | - } while ($bytes > 1) { |
|
| 158 | - $i++; |
|
| 159 | - $b = ord($content[$i]); |
|
| 160 | - if ($b < 128 || $b > 191) { |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 163 | - $bytes--; |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - return true; |
|
| 168 | - }//_checkUtf8Rfc3629() |
|
| 122 | + }//end _checkUtf8W3c() |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Checks that the string $content contains only valid UTF-8 chars |
|
| 126 | + * using the method described in RFC 3629. |
|
| 127 | + * Returns true if $content contains only UTF-8 chars, false otherwise. |
|
| 128 | + * |
|
| 129 | + * @param string $content String to check. |
|
| 130 | + * |
|
| 131 | + * @return bool true if $content contains only UTF-8 chars, false otherwise. |
|
| 132 | + * |
|
| 133 | + * @see http://www.php.net/manual/en/function.mb-detect-encoding.php#85294 |
|
| 134 | + */ |
|
| 135 | + private static function _checkUtf8Rfc3629($content) |
|
| 136 | + { |
|
| 137 | + $len = strlen($content); |
|
| 138 | + for ($i = 0; $i < $len; $i++) { |
|
| 139 | + $c = ord($content[$i]); |
|
| 140 | + if ($c > 128) { |
|
| 141 | + if (($c >= 254)) { |
|
| 142 | + return false; |
|
| 143 | + } elseif ($c >= 252) { |
|
| 144 | + $bits=6; |
|
| 145 | + } elseif ($c >= 248) { |
|
| 146 | + $bits=5; |
|
| 147 | + } elseif ($c >= 240) { |
|
| 148 | + $bytes = 4; |
|
| 149 | + } elseif ($c >= 224) { |
|
| 150 | + $bytes = 3; |
|
| 151 | + } elseif ($c >= 192) { |
|
| 152 | + $bytes = 2; |
|
| 153 | + } else { |
|
| 154 | + return false; |
|
| 155 | + } if (($i + $bytes) > $len) { |
|
| 156 | + return false; |
|
| 157 | + } while ($bytes > 1) { |
|
| 158 | + $i++; |
|
| 159 | + $b = ord($content[$i]); |
|
| 160 | + if ($b < 128 || $b > 191) { |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | + $bytes--; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + return true; |
|
| 168 | + }//_checkUtf8Rfc3629() |
|
| 169 | 169 | |
| 170 | 170 | /** |
| 171 | - * Splits a string to chunks of given size |
|
| 172 | - * This helps to avoid segmentation fault errors when large text is given |
|
| 173 | - * Returns array of strings after splitting |
|
| 174 | - * |
|
| 175 | - * @param string $str String to split. |
|
| 176 | - * @param int $len number of characters per chunk |
|
| 177 | - * |
|
| 178 | - * @return array string array after splitting |
|
| 179 | - * |
|
| 180 | - * @see http://php.net/manual/en/function.chunk-split.php |
|
| 181 | - */ |
|
| 171 | + * Splits a string to chunks of given size |
|
| 172 | + * This helps to avoid segmentation fault errors when large text is given |
|
| 173 | + * Returns array of strings after splitting |
|
| 174 | + * |
|
| 175 | + * @param string $str String to split. |
|
| 176 | + * @param int $len number of characters per chunk |
|
| 177 | + * |
|
| 178 | + * @return array string array after splitting |
|
| 179 | + * |
|
| 180 | + * @see http://php.net/manual/en/function.chunk-split.php |
|
| 181 | + */ |
|
| 182 | 182 | private static function mb_chunk_split($str, $len, $glue) |
| 183 | 183 | { |
| 184 | 184 | if (empty($str)) return false; |
@@ -196,14 +196,14 @@ discard block |
||
| 196 | 196 | return $new; |
| 197 | 197 | }//mb_chunk_split |
| 198 | 198 | /** |
| 199 | - * Supporting function for mb_chunk_split |
|
| 200 | - * |
|
| 201 | - * @param string $str |
|
| 199 | + * Supporting function for mb_chunk_split |
|
| 200 | + * |
|
| 201 | + * @param string $str |
|
| 202 | + * |
|
| 203 | + * @return array |
|
| 202 | 204 | * |
| 203 | - * @return array |
|
| 204 | - * |
|
| 205 | - * @see http://php.net/manual/en/function.chunk-split.php |
|
| 206 | - */ |
|
| 205 | + * @see http://php.net/manual/en/function.chunk-split.php |
|
| 206 | + */ |
|
| 207 | 207 | private static function mbStringToArray ($str) |
| 208 | 208 | { |
| 209 | 209 | if (empty($str)) return false; |
@@ -35,7 +35,7 @@ |
||
| 35 | 35 | /** |
| 36 | 36 | * Returns an array of tokens this test wants to listen for: symbolic and literal operators and. |
| 37 | 37 | * |
| 38 | - * @return array |
|
| 38 | + * @return integer[] |
|
| 39 | 39 | */ |
| 40 | 40 | public function register() |
| 41 | 41 | { |
@@ -33,47 +33,47 @@ |
||
| 33 | 33 | class LogicalOperatorAndSniff implements Sniff |
| 34 | 34 | { |
| 35 | 35 | /** |
| 36 | - * Returns an array of tokens this test wants to listen for: symbolic and literal operators and. |
|
| 37 | - * |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function register() |
|
| 41 | - { |
|
| 42 | - return array( |
|
| 43 | - T_LOGICAL_AND, |
|
| 44 | - ); |
|
| 36 | + * Returns an array of tokens this test wants to listen for: symbolic and literal operators and. |
|
| 37 | + * |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function register() |
|
| 41 | + { |
|
| 42 | + return array( |
|
| 43 | + T_LOGICAL_AND, |
|
| 44 | + ); |
|
| 45 | 45 | |
| 46 | - }//end register() |
|
| 46 | + }//end register() |
|
| 47 | 47 | |
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Processes this test, when one of its tokens is encountered. |
|
| 51 | - * |
|
| 52 | - * @param File $phpcsFile The current file being scanned. |
|
| 53 | - * @param int $stackPtr The position of the current token |
|
| 54 | - * in the stack passed in $tokens. |
|
| 55 | - * |
|
| 56 | - * @return void |
|
| 57 | - */ |
|
| 58 | - public function process(File $phpcsFile, $stackPtr) |
|
| 59 | - { |
|
| 60 | - $tokens = $phpcsFile->getTokens(); |
|
| 49 | + /** |
|
| 50 | + * Processes this test, when one of its tokens is encountered. |
|
| 51 | + * |
|
| 52 | + * @param File $phpcsFile The current file being scanned. |
|
| 53 | + * @param int $stackPtr The position of the current token |
|
| 54 | + * in the stack passed in $tokens. |
|
| 55 | + * |
|
| 56 | + * @return void |
|
| 57 | + */ |
|
| 58 | + public function process(File $phpcsFile, $stackPtr) |
|
| 59 | + { |
|
| 60 | + $tokens = $phpcsFile->getTokens(); |
|
| 61 | 61 | |
| 62 | - $operator_token = $tokens[$stackPtr]; |
|
| 63 | - $operator_string = $operator_token['content']; |
|
| 64 | - $operator_code = $operator_token['code']; |
|
| 62 | + $operator_token = $tokens[$stackPtr]; |
|
| 63 | + $operator_string = $operator_token['content']; |
|
| 64 | + $operator_code = $operator_token['code']; |
|
| 65 | 65 | |
| 66 | - if ($operator_string !== strtoupper($operator_string)) { |
|
| 67 | - $error_message = 'Logical operator should be in upper case;' |
|
| 68 | - . ' use "' . strtoupper($operator_string) |
|
| 69 | - . '" instead of "' . $operator_string . '"'; |
|
| 70 | - $phpcsFile->addError($error_message, $stackPtr, 'LowercaseLogicalOperator'); |
|
| 71 | - } |
|
| 66 | + if ($operator_string !== strtoupper($operator_string)) { |
|
| 67 | + $error_message = 'Logical operator should be in upper case;' |
|
| 68 | + . ' use "' . strtoupper($operator_string) |
|
| 69 | + . '" instead of "' . $operator_string . '"'; |
|
| 70 | + $phpcsFile->addError($error_message, $stackPtr, 'LowercaseLogicalOperator'); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"'; |
|
| 74 | - $phpcsFile->addWarning($warning_message, $stackPtr, 'UseOfLiteralAndOperator'); |
|
| 73 | + $warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"'; |
|
| 74 | + $phpcsFile->addWarning($warning_message, $stackPtr, 'UseOfLiteralAndOperator'); |
|
| 75 | 75 | |
| 76 | - }//end process() |
|
| 76 | + }//end process() |
|
| 77 | 77 | |
| 78 | 78 | |
| 79 | 79 | }//end class |
@@ -35,7 +35,7 @@ |
||
| 35 | 35 | /** |
| 36 | 36 | * Returns an array of tokens this test wants to listen for: literal and symbolic operators or. |
| 37 | 37 | * |
| 38 | - * @return array |
|
| 38 | + * @return integer[] |
|
| 39 | 39 | */ |
| 40 | 40 | public function register() |
| 41 | 41 | { |
@@ -32,51 +32,51 @@ |
||
| 32 | 32 | |
| 33 | 33 | class UppercaseLogicalOperatorOrSniff implements Sniff |
| 34 | 34 | { |
| 35 | - /** |
|
| 36 | - * Returns an array of tokens this test wants to listen for: literal and symbolic operators or. |
|
| 37 | - * |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function register() |
|
| 41 | - { |
|
| 42 | - return array( |
|
| 43 | - T_BOOLEAN_OR, |
|
| 44 | - T_LOGICAL_OR, |
|
| 45 | - ); |
|
| 35 | + /** |
|
| 36 | + * Returns an array of tokens this test wants to listen for: literal and symbolic operators or. |
|
| 37 | + * |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function register() |
|
| 41 | + { |
|
| 42 | + return array( |
|
| 43 | + T_BOOLEAN_OR, |
|
| 44 | + T_LOGICAL_OR, |
|
| 45 | + ); |
|
| 46 | 46 | |
| 47 | - }//end register() |
|
| 47 | + }//end register() |
|
| 48 | 48 | |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Processes this test, when one of its tokens is encountered. |
|
| 52 | - * |
|
| 53 | - * @param File $phpcsFile The current file being scanned. |
|
| 54 | - * @param int $stackPtr The position of the current token |
|
| 55 | - * in the stack passed in $tokens. |
|
| 56 | - * |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function process(File $phpcsFile, $stackPtr) |
|
| 60 | - { |
|
| 61 | - $tokens = $phpcsFile->getTokens(); |
|
| 50 | + /** |
|
| 51 | + * Processes this test, when one of its tokens is encountered. |
|
| 52 | + * |
|
| 53 | + * @param File $phpcsFile The current file being scanned. |
|
| 54 | + * @param int $stackPtr The position of the current token |
|
| 55 | + * in the stack passed in $tokens. |
|
| 56 | + * |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function process(File $phpcsFile, $stackPtr) |
|
| 60 | + { |
|
| 61 | + $tokens = $phpcsFile->getTokens(); |
|
| 62 | 62 | |
| 63 | - $operator_token = $tokens[$stackPtr]; |
|
| 64 | - $operator_string = $operator_token['content']; |
|
| 65 | - $operator_code = $operator_token['code']; |
|
| 63 | + $operator_token = $tokens[$stackPtr]; |
|
| 64 | + $operator_string = $operator_token['content']; |
|
| 65 | + $operator_code = $operator_token['code']; |
|
| 66 | 66 | |
| 67 | - if ($operator_code == T_BOOLEAN_OR) { |
|
| 68 | - $error_message = 'Logical operator "' . $operator_string |
|
| 69 | - . '" is prohibited; use "OR" instead'; |
|
| 70 | - $phpcsFile->addError($error_message, $stackPtr, 'UseOf||InsteadOfOR'); |
|
| 71 | - } |
|
| 72 | - // it is literal, if it is not symbolic |
|
| 73 | - else if ($operator_string !== strtoupper($operator_string)) { |
|
| 74 | - $error_message = 'Logical operator should be in upper case;' |
|
| 75 | - . ' use "' . strtoupper($operator_string) |
|
| 76 | - . '" instead of "' . $operator_string . '"'; |
|
| 77 | - $phpcsFile->addError($error_message, $stackPtr, 'UseOfLowercaseOr'); |
|
| 78 | - } |
|
| 79 | - }//end process() |
|
| 67 | + if ($operator_code == T_BOOLEAN_OR) { |
|
| 68 | + $error_message = 'Logical operator "' . $operator_string |
|
| 69 | + . '" is prohibited; use "OR" instead'; |
|
| 70 | + $phpcsFile->addError($error_message, $stackPtr, 'UseOf||InsteadOfOR'); |
|
| 71 | + } |
|
| 72 | + // it is literal, if it is not symbolic |
|
| 73 | + else if ($operator_string !== strtoupper($operator_string)) { |
|
| 74 | + $error_message = 'Logical operator should be in upper case;' |
|
| 75 | + . ' use "' . strtoupper($operator_string) |
|
| 76 | + . '" instead of "' . $operator_string . '"'; |
|
| 77 | + $phpcsFile->addError($error_message, $stackPtr, 'UseOfLowercaseOr'); |
|
| 78 | + } |
|
| 79 | + }//end process() |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | 82 | }//end class |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | * @param string $dblQtString The double-quoted string content, |
| 88 | 88 | * i.e. without quotes. |
| 89 | 89 | * |
| 90 | - * @return void |
|
| 90 | + * @return boolean |
|
| 91 | 91 | */ |
| 92 | 92 | protected function processDoubleQuotedString (File $phpcsFile, $stackPtr, $dblQtString) |
| 93 | 93 | { |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | * @param string $sglQtString The single-quoted string content, |
| 135 | 135 | * i.e. without quotes. |
| 136 | 136 | * |
| 137 | - * @return void |
|
| 137 | + * @return boolean |
|
| 138 | 138 | */ |
| 139 | 139 | protected function processSingleQuotedString (File $phpcsFile, $stackPtr, $sglQtString) |
| 140 | 140 | { |
@@ -327,137 +327,137 @@ |
||
| 327 | 327 | */ |
| 328 | 328 | class DoubleQuoteUsageSniff extends VariableUsageSniff |
| 329 | 329 | { |
| 330 | - /** |
|
| 331 | - * Returns an array of tokens this test wants to listen for. |
|
| 332 | - * |
|
| 333 | - * @return array |
|
| 334 | - */ |
|
| 335 | - public function register() |
|
| 336 | - { |
|
| 337 | - return array( |
|
| 338 | - T_DOUBLE_QUOTED_STRING, |
|
| 339 | - T_CONSTANT_ENCAPSED_STRING, |
|
| 340 | - ); |
|
| 341 | - }//end register() |
|
| 330 | + /** |
|
| 331 | + * Returns an array of tokens this test wants to listen for. |
|
| 332 | + * |
|
| 333 | + * @return array |
|
| 334 | + */ |
|
| 335 | + public function register() |
|
| 336 | + { |
|
| 337 | + return array( |
|
| 338 | + T_DOUBLE_QUOTED_STRING, |
|
| 339 | + T_CONSTANT_ENCAPSED_STRING, |
|
| 340 | + ); |
|
| 341 | + }//end register() |
|
| 342 | 342 | |
| 343 | - /** |
|
| 344 | - * Processes this test, when one of its tokens is encountered. |
|
| 345 | - * |
|
| 346 | - * @param File $phpcsFile The current file being scanned. |
|
| 347 | - * @param int $stackPtr The position of the current token |
|
| 348 | - * in the stack passed in $tokens. |
|
| 349 | - * |
|
| 350 | - * @return void |
|
| 351 | - */ |
|
| 352 | - public function process(File $phpcsFile, $stackPtr) |
|
| 353 | - { |
|
| 354 | - // no variable are in the string from here |
|
| 355 | - $tokens = $phpcsFile->getTokens(); |
|
| 356 | - $qtString = $tokens[$stackPtr]['content']; |
|
| 357 | - // makes sure that it is about a double quote string, |
|
| 358 | - // since variables are not parsed out of double quoted string |
|
| 359 | - $open_qt_str = substr($qtString, 0, 1); |
|
| 343 | + /** |
|
| 344 | + * Processes this test, when one of its tokens is encountered. |
|
| 345 | + * |
|
| 346 | + * @param File $phpcsFile The current file being scanned. |
|
| 347 | + * @param int $stackPtr The position of the current token |
|
| 348 | + * in the stack passed in $tokens. |
|
| 349 | + * |
|
| 350 | + * @return void |
|
| 351 | + */ |
|
| 352 | + public function process(File $phpcsFile, $stackPtr) |
|
| 353 | + { |
|
| 354 | + // no variable are in the string from here |
|
| 355 | + $tokens = $phpcsFile->getTokens(); |
|
| 356 | + $qtString = $tokens[$stackPtr]['content']; |
|
| 357 | + // makes sure that it is about a double quote string, |
|
| 358 | + // since variables are not parsed out of double quoted string |
|
| 359 | + $open_qt_str = substr($qtString, 0, 1); |
|
| 360 | 360 | |
| 361 | - // clean the enclosing quotes |
|
| 362 | - $qtString = substr($qtString, 1, strlen($qtString) - 1 - 1); |
|
| 361 | + // clean the enclosing quotes |
|
| 362 | + $qtString = substr($qtString, 1, strlen($qtString) - 1 - 1); |
|
| 363 | 363 | |
| 364 | - if (0 === strcmp($open_qt_str, '"')) { |
|
| 365 | - $this->processDoubleQuotedString($phpcsFile, $stackPtr, $qtString); |
|
| 366 | - } else if (0 === strcmp($open_qt_str, "'")) { |
|
| 367 | - $this->processSingleQuotedString($phpcsFile, $stackPtr, $qtString); |
|
| 368 | - } |
|
| 369 | - }//end process() |
|
| 364 | + if (0 === strcmp($open_qt_str, '"')) { |
|
| 365 | + $this->processDoubleQuotedString($phpcsFile, $stackPtr, $qtString); |
|
| 366 | + } else if (0 === strcmp($open_qt_str, "'")) { |
|
| 367 | + $this->processSingleQuotedString($phpcsFile, $stackPtr, $qtString); |
|
| 368 | + } |
|
| 369 | + }//end process() |
|
| 370 | 370 | |
| 371 | 371 | |
| 372 | - /** |
|
| 373 | - * Processes this test, when the token encountered is a double-quoted string. |
|
| 374 | - * |
|
| 375 | - * @param File $phpcsFile The current file being scanned. |
|
| 376 | - * @param int $stackPtr The position of the current token |
|
| 377 | - * in the stack passed in $tokens. |
|
| 378 | - * @param string $qtString The double-quoted string content, |
|
| 379 | - * i.e. without quotes. |
|
| 380 | - * |
|
| 381 | - * @return void |
|
| 382 | - */ |
|
| 383 | - protected function processDoubleQuotedString (File $phpcsFile, $stackPtr, $qtString) |
|
| 384 | - { |
|
| 385 | - // so there should be at least a single quote or a special char |
|
| 386 | - // if there are the 2 kinds of quote and no special char, then add a warning |
|
| 387 | - $has_variable = parent::processDoubleQuotedString($phpcsFile, $stackPtr, '"'.$qtString.'"'); |
|
| 388 | - $has_specific_sequence = $this->_hasSpecificSequence($qtString); |
|
| 389 | - $dbl_qt_at = strpos($qtString, '"'); |
|
| 390 | - $smpl_qt_at = strpos($qtString, "'"); |
|
| 391 | - if (false === $has_variable && false === $has_specific_sequence |
|
| 392 | - && false === $smpl_qt_at |
|
| 393 | - ) { |
|
| 394 | - $error = 'Single-quoted strings should be used unless it contains variables, special chars like \n or single quotes.'; |
|
| 395 | - $phpcsFile->addError($error, $stackPtr); |
|
| 396 | - } else if (false !== $smpl_qt_at && false !== $dbl_qt_at |
|
| 397 | - && false === $has_variable && false === $has_specific_sequence |
|
| 398 | - ) { |
|
| 399 | - $warning = 'It is encouraged to use a single-quoted string, since it doesn\'t contain any variable nor special char though it mixes single and double quotes.'; |
|
| 400 | - $phpcsFile->addWarning($warning, $stackPtr); |
|
| 401 | - } |
|
| 402 | - }//end processDoubleQuotedString() |
|
| 372 | + /** |
|
| 373 | + * Processes this test, when the token encountered is a double-quoted string. |
|
| 374 | + * |
|
| 375 | + * @param File $phpcsFile The current file being scanned. |
|
| 376 | + * @param int $stackPtr The position of the current token |
|
| 377 | + * in the stack passed in $tokens. |
|
| 378 | + * @param string $qtString The double-quoted string content, |
|
| 379 | + * i.e. without quotes. |
|
| 380 | + * |
|
| 381 | + * @return void |
|
| 382 | + */ |
|
| 383 | + protected function processDoubleQuotedString (File $phpcsFile, $stackPtr, $qtString) |
|
| 384 | + { |
|
| 385 | + // so there should be at least a single quote or a special char |
|
| 386 | + // if there are the 2 kinds of quote and no special char, then add a warning |
|
| 387 | + $has_variable = parent::processDoubleQuotedString($phpcsFile, $stackPtr, '"'.$qtString.'"'); |
|
| 388 | + $has_specific_sequence = $this->_hasSpecificSequence($qtString); |
|
| 389 | + $dbl_qt_at = strpos($qtString, '"'); |
|
| 390 | + $smpl_qt_at = strpos($qtString, "'"); |
|
| 391 | + if (false === $has_variable && false === $has_specific_sequence |
|
| 392 | + && false === $smpl_qt_at |
|
| 393 | + ) { |
|
| 394 | + $error = 'Single-quoted strings should be used unless it contains variables, special chars like \n or single quotes.'; |
|
| 395 | + $phpcsFile->addError($error, $stackPtr); |
|
| 396 | + } else if (false !== $smpl_qt_at && false !== $dbl_qt_at |
|
| 397 | + && false === $has_variable && false === $has_specific_sequence |
|
| 398 | + ) { |
|
| 399 | + $warning = 'It is encouraged to use a single-quoted string, since it doesn\'t contain any variable nor special char though it mixes single and double quotes.'; |
|
| 400 | + $phpcsFile->addWarning($warning, $stackPtr); |
|
| 401 | + } |
|
| 402 | + }//end processDoubleQuotedString() |
|
| 403 | 403 | |
| 404 | 404 | |
| 405 | - /** |
|
| 406 | - * Processes this test, when the token encountered is a single-quoted string. |
|
| 407 | - * |
|
| 408 | - * @param File $phpcsFile The current file being scanned. |
|
| 409 | - * @param int $stackPtr The position of the current token |
|
| 410 | - * in the stack passed in $tokens. |
|
| 411 | - * @param string $qtString The single-quoted string content, |
|
| 412 | - * i.e. without quotes. |
|
| 413 | - * |
|
| 414 | - * @return void |
|
| 415 | - */ |
|
| 416 | - protected function processSingleQuotedString (File $phpcsFile, $stackPtr, $qtString) |
|
| 417 | - { |
|
| 418 | - // if there is single quotes without additional double quotes, |
|
| 419 | - // then user is allowed to use double quote to avoid having to |
|
| 420 | - // escape single quotes. Don't add the warning, if an error was |
|
| 421 | - // already added, because a variable was found in a single-quoted |
|
| 422 | - // string. |
|
| 423 | - $has_variable = parent::processSingleQuotedString($phpcsFile, $stackPtr, "'".$qtString."'"); |
|
| 424 | - $dbl_qt_at = strpos($qtString, '"'); |
|
| 425 | - $smpl_qt_at = strpos($qtString, "'"); |
|
| 426 | - if (false === $has_variable && false !== $smpl_qt_at && false === $dbl_qt_at) { |
|
| 427 | - $warning = 'You may also use double-quoted strings if the string contains single quotes, so you do not have to use escape characters.'; |
|
| 428 | - $phpcsFile->addWarning($warning, $stackPtr); |
|
| 429 | - } |
|
| 430 | - }//end processSingleQuotedString() |
|
| 405 | + /** |
|
| 406 | + * Processes this test, when the token encountered is a single-quoted string. |
|
| 407 | + * |
|
| 408 | + * @param File $phpcsFile The current file being scanned. |
|
| 409 | + * @param int $stackPtr The position of the current token |
|
| 410 | + * in the stack passed in $tokens. |
|
| 411 | + * @param string $qtString The single-quoted string content, |
|
| 412 | + * i.e. without quotes. |
|
| 413 | + * |
|
| 414 | + * @return void |
|
| 415 | + */ |
|
| 416 | + protected function processSingleQuotedString (File $phpcsFile, $stackPtr, $qtString) |
|
| 417 | + { |
|
| 418 | + // if there is single quotes without additional double quotes, |
|
| 419 | + // then user is allowed to use double quote to avoid having to |
|
| 420 | + // escape single quotes. Don't add the warning, if an error was |
|
| 421 | + // already added, because a variable was found in a single-quoted |
|
| 422 | + // string. |
|
| 423 | + $has_variable = parent::processSingleQuotedString($phpcsFile, $stackPtr, "'".$qtString."'"); |
|
| 424 | + $dbl_qt_at = strpos($qtString, '"'); |
|
| 425 | + $smpl_qt_at = strpos($qtString, "'"); |
|
| 426 | + if (false === $has_variable && false !== $smpl_qt_at && false === $dbl_qt_at) { |
|
| 427 | + $warning = 'You may also use double-quoted strings if the string contains single quotes, so you do not have to use escape characters.'; |
|
| 428 | + $phpcsFile->addWarning($warning, $stackPtr); |
|
| 429 | + } |
|
| 430 | + }//end processSingleQuotedString() |
|
| 431 | 431 | |
| 432 | - /** |
|
| 433 | - * Return TRUE, if a sequence of chars that is parsed in a specific way |
|
| 434 | - * in double-quoted strings is found, FALSE otherwise. |
|
| 435 | - * |
|
| 436 | - * @param string $string String in which sequence of special chars will |
|
| 437 | - * be researched. |
|
| 438 | - * |
|
| 439 | - * @return TRUE, if a sequence of chars that is parsed in a specific way |
|
| 440 | - * in double-quoted strings is found, FALSE otherwise. |
|
| 441 | - * |
|
| 442 | - * @link http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double |
|
| 443 | - */ |
|
| 444 | - private function _hasSpecificSequence($string) |
|
| 445 | - { |
|
| 446 | - $hasSpecificSequence = FALSE; |
|
| 447 | - $specialMeaningStrs = array('\n', '\r', '\t', '\v', '\f'); |
|
| 448 | - foreach ($specialMeaningStrs as $splStr) { |
|
| 449 | - if (FALSE !== strpos($string, $splStr)) { |
|
| 450 | - $hasSpecificSequence = TRUE; |
|
| 451 | - } |
|
| 452 | - } |
|
| 453 | - $specialMeaningPtrns = array('\[0-7]{1,3}', '\x[0-9A-Fa-f]{1,2}'); |
|
| 454 | - foreach ($specialMeaningPtrns as $splPtrn) { |
|
| 455 | - if (1 === preg_match("/{$splPtrn}/", $string)) { |
|
| 456 | - $hasSpecificSequence = TRUE; |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - return $hasSpecificSequence; |
|
| 460 | - }//end _hasSpecificSequence() |
|
| 432 | + /** |
|
| 433 | + * Return TRUE, if a sequence of chars that is parsed in a specific way |
|
| 434 | + * in double-quoted strings is found, FALSE otherwise. |
|
| 435 | + * |
|
| 436 | + * @param string $string String in which sequence of special chars will |
|
| 437 | + * be researched. |
|
| 438 | + * |
|
| 439 | + * @return TRUE, if a sequence of chars that is parsed in a specific way |
|
| 440 | + * in double-quoted strings is found, FALSE otherwise. |
|
| 441 | + * |
|
| 442 | + * @link http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double |
|
| 443 | + */ |
|
| 444 | + private function _hasSpecificSequence($string) |
|
| 445 | + { |
|
| 446 | + $hasSpecificSequence = FALSE; |
|
| 447 | + $specialMeaningStrs = array('\n', '\r', '\t', '\v', '\f'); |
|
| 448 | + foreach ($specialMeaningStrs as $splStr) { |
|
| 449 | + if (FALSE !== strpos($string, $splStr)) { |
|
| 450 | + $hasSpecificSequence = TRUE; |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | + $specialMeaningPtrns = array('\[0-7]{1,3}', '\x[0-9A-Fa-f]{1,2}'); |
|
| 454 | + foreach ($specialMeaningPtrns as $splPtrn) { |
|
| 455 | + if (1 === preg_match("/{$splPtrn}/", $string)) { |
|
| 456 | + $hasSpecificSequence = TRUE; |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + return $hasSpecificSequence; |
|
| 460 | + }//end _hasSpecificSequence() |
|
| 461 | 461 | |
| 462 | 462 | }//end class |
| 463 | 463 | |
@@ -36,7 +36,7 @@ |
||
| 36 | 36 | /** |
| 37 | 37 | * Returns an array of tokens this test wants to listen for. |
| 38 | 38 | * |
| 39 | - * @return array |
|
| 39 | + * @return string[] |
|
| 40 | 40 | */ |
| 41 | 41 | public function register() |
| 42 | 42 | { |
@@ -33,41 +33,41 @@ |
||
| 33 | 33 | class LogicalNotSpacingSniff implements Sniff |
| 34 | 34 | { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Returns an array of tokens this test wants to listen for. |
|
| 38 | - * |
|
| 39 | - * @return array |
|
| 40 | - */ |
|
| 41 | - public function register() |
|
| 42 | - { |
|
| 43 | - return array( |
|
| 44 | - T_BOOLEAN_NOT, |
|
| 45 | - ); |
|
| 46 | - }//end register() |
|
| 36 | + /** |
|
| 37 | + * Returns an array of tokens this test wants to listen for. |
|
| 38 | + * |
|
| 39 | + * @return array |
|
| 40 | + */ |
|
| 41 | + public function register() |
|
| 42 | + { |
|
| 43 | + return array( |
|
| 44 | + T_BOOLEAN_NOT, |
|
| 45 | + ); |
|
| 46 | + }//end register() |
|
| 47 | 47 | |
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Processes this test, when one of its tokens is encountered. |
|
| 51 | - * |
|
| 52 | - * @param File $phpcsFile The current file being scanned. |
|
| 53 | - * @param int $stackPtr The position of the current token |
|
| 54 | - * in the stack passed in $tokens. |
|
| 55 | - * |
|
| 56 | - * @return void |
|
| 57 | - */ |
|
| 58 | - public function process(File $phpcsFile, $stackPtr) |
|
| 59 | - { |
|
| 60 | - $tokens = $phpcsFile->getTokens(); |
|
| 49 | + /** |
|
| 50 | + * Processes this test, when one of its tokens is encountered. |
|
| 51 | + * |
|
| 52 | + * @param File $phpcsFile The current file being scanned. |
|
| 53 | + * @param int $stackPtr The position of the current token |
|
| 54 | + * in the stack passed in $tokens. |
|
| 55 | + * |
|
| 56 | + * @return void |
|
| 57 | + */ |
|
| 58 | + public function process(File $phpcsFile, $stackPtr) |
|
| 59 | + { |
|
| 60 | + $tokens = $phpcsFile->getTokens(); |
|
| 61 | 61 | |
| 62 | - $operator_token = $tokens[$stackPtr]; |
|
| 62 | + $operator_token = $tokens[$stackPtr]; |
|
| 63 | 63 | |
| 64 | - $previous_token = $tokens[$stackPtr - 1]; |
|
| 65 | - $next_token = $tokens[$stackPtr + 1]; |
|
| 66 | - if (T_WHITESPACE !== $previous_token['code'] || T_WHITESPACE !== $next_token['code']) { |
|
| 67 | - $error = 'Logical operator ! should always be preceded and followed with a whitespace.'; |
|
| 68 | - $phpcsFile->addError($error, $stackPtr); |
|
| 69 | - } |
|
| 70 | - }//end process() |
|
| 64 | + $previous_token = $tokens[$stackPtr - 1]; |
|
| 65 | + $next_token = $tokens[$stackPtr + 1]; |
|
| 66 | + if (T_WHITESPACE !== $previous_token['code'] || T_WHITESPACE !== $next_token['code']) { |
|
| 67 | + $error = 'Logical operator ! should always be preceded and followed with a whitespace.'; |
|
| 68 | + $phpcsFile->addError($error, $stackPtr); |
|
| 69 | + } |
|
| 70 | + }//end process() |
|
| 71 | 71 | |
| 72 | 72 | |
| 73 | 73 | }//end class |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | /** |
| 51 | 51 | * Returns an array of tokens this test wants to listen for. |
| 52 | 52 | * |
| 53 | - * @return array |
|
| 53 | + * @return integer[] |
|
| 54 | 54 | */ |
| 55 | 55 | public function register() |
| 56 | 56 | { |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | * application root. Parent directory of the application root are allowed |
| 135 | 135 | * but not mandatory. |
| 136 | 136 | * |
| 137 | - * @return string|bool The relative path from $appRoot to $filePath, or |
|
| 137 | + * @return false|string The relative path from $appRoot to $filePath, or |
|
| 138 | 138 | * false if $appRoot cannot be found in $filePath. |
| 139 | 139 | */ |
| 140 | 140 | private static function _getLocationPath ($filePath, $appRoot) |
@@ -45,138 +45,138 @@ |
||
| 45 | 45 | |
| 46 | 46 | class ClosingLocationCommentSniff extends AbstractClosingCommentSniff |
| 47 | 47 | { |
| 48 | - public $applicationRoot = '/application/'; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Returns an array of tokens this test wants to listen for. |
|
| 52 | - * |
|
| 53 | - * @return array |
|
| 54 | - */ |
|
| 55 | - public function register() |
|
| 56 | - { |
|
| 57 | - return array( |
|
| 58 | - T_OPEN_TAG |
|
| 59 | - ); |
|
| 60 | - |
|
| 61 | - }//end register() |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Processes this test, when one of its tokens is encountered. |
|
| 66 | - * |
|
| 67 | - * @param File $phpcsFile The current file being scanned. |
|
| 68 | - * @param int $stackPtr The position of the current token |
|
| 69 | - * in the stack passed in $tokens. |
|
| 70 | - * |
|
| 71 | - * @return void |
|
| 72 | - */ |
|
| 73 | - public function process(File $phpcsFile, $stackPtr) |
|
| 74 | - { |
|
| 75 | - // We are only interested if this is the first open tag. |
|
| 76 | - if ($stackPtr !== 0) { |
|
| 77 | - if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 78 | - return; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - $filePath = $phpcsFile->getFilename(); |
|
| 83 | - $tokens = $phpcsFile->getTokens(); |
|
| 84 | - // removes the application root from the beginning of the file path |
|
| 85 | - $locationPath = self::_getLocationPath($filePath, $this->_getAppRoot()); |
|
| 86 | - // add an error, if application root doesn't exist in current file path |
|
| 87 | - if (false === $locationPath) { |
|
| 88 | - $error = 'Unable to find "' . $this->_getAppRoot() . '" in file path "' . $filePath . '". Please set your project\'s application root.'; |
|
| 89 | - $phpcsFile->addError($error, count($tokens) - 1); |
|
| 90 | - return; |
|
| 91 | - } |
|
| 92 | - // generates the expected comment |
|
| 93 | - $commentTemplate = "Location: $locationPath"; |
|
| 94 | - |
|
| 95 | - $currentToken = count($tokens) - 1; |
|
| 96 | - $hasClosingLocationComment = false; |
|
| 97 | - $isNotAWhitespaceOrAComment = false; |
|
| 98 | - while ($currentToken >= 0 |
|
| 99 | - && ! $isNotAWhitespaceOrAComment |
|
| 100 | - && ! $hasClosingLocationComment |
|
| 101 | - ) { |
|
| 102 | - $token = $tokens[$currentToken]; |
|
| 103 | - $tokenCode = $token['code']; |
|
| 104 | - if (T_COMMENT === $tokenCode) { |
|
| 105 | - $commentString = self::_getCommentContent($token['content']); |
|
| 106 | - if (0 === strcmp($commentString, $commentTemplate)) { |
|
| 107 | - $hasClosingLocationComment = true; |
|
| 108 | - } |
|
| 109 | - } else if (T_WHITESPACE === $tokenCode) { |
|
| 110 | - // Whitespaces are allowed between the closing file comment, |
|
| 111 | - //other comments and end of file |
|
| 112 | - } else { |
|
| 113 | - $isNotAWhitespaceOrAComment = true; |
|
| 114 | - } |
|
| 115 | - $currentToken--; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - if ( ! $hasClosingLocationComment) { |
|
| 119 | - $error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".'; |
|
| 120 | - $phpcsFile->addError($error, $currentToken); |
|
| 121 | - } |
|
| 122 | - }//end process() |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Returns the relative path from $appRoot to $filePath, or false if |
|
| 127 | - * $appRoot cannot be found in $filePath, because $appRoot is not a parent |
|
| 128 | - * of $filePath. |
|
| 129 | - * |
|
| 130 | - * @param string $filePath Full path to the file being proceed. |
|
| 131 | - * @param string $appRoot Partial or full path to the CodeIgniter |
|
| 132 | - * application root of the file being proceed. It must not contain the |
|
| 133 | - * full path to the application root, but at least the name of the |
|
| 134 | - * application root. Parent directory of the application root are allowed |
|
| 135 | - * but not mandatory. |
|
| 136 | - * |
|
| 137 | - * @return string|bool The relative path from $appRoot to $filePath, or |
|
| 138 | - * false if $appRoot cannot be found in $filePath. |
|
| 139 | - */ |
|
| 140 | - private static function _getLocationPath ($filePath, $appRoot) |
|
| 141 | - { |
|
| 142 | - // removes the path to application root |
|
| 143 | - // from the beginning of the file path |
|
| 144 | - $appRootAt = strpos($filePath, $appRoot); |
|
| 145 | - if (false === $appRootAt) { |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - $localPath = substr($filePath, $appRootAt + strlen($appRoot)); |
|
| 149 | - // ensures the location path to be a relative path starting with "./". |
|
| 150 | - if ( ! self::_stringStartsWith($localPath, './')) { |
|
| 151 | - $localPath = './' . $localPath; |
|
| 152 | - } else if ( ! self::_stringStartsWith($localPath, '.') |
|
| 153 | - && self::_stringStartsWith($localPath, '/') |
|
| 154 | - ) { |
|
| 155 | - $localPath = '.' . $localPath; |
|
| 156 | - } |
|
| 157 | - return $localPath; |
|
| 158 | - }//end _getLocationPath() |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Returns the application root that should be used first. |
|
| 163 | - * |
|
| 164 | - * There are several ways to configure the application root. |
|
| 165 | - * In order of priority : |
|
| 166 | - * - Configuration variable ci_application_root. |
|
| 167 | - * - Rule property applicationRoot. |
|
| 168 | - * - Default value '/application/' |
|
| 169 | - * |
|
| 170 | - * @return string Path to your project application root. |
|
| 171 | - */ |
|
| 172 | - private function _getAppRoot() |
|
| 173 | - { |
|
| 174 | - $appRoot = Common::getConfigData('ci_application_root'); |
|
| 175 | - if (null === $appRoot) { |
|
| 176 | - $appRoot = $this->applicationRoot; |
|
| 177 | - } |
|
| 178 | - return $appRoot; |
|
| 179 | - }//end _getAppRoot() |
|
| 48 | + public $applicationRoot = '/application/'; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Returns an array of tokens this test wants to listen for. |
|
| 52 | + * |
|
| 53 | + * @return array |
|
| 54 | + */ |
|
| 55 | + public function register() |
|
| 56 | + { |
|
| 57 | + return array( |
|
| 58 | + T_OPEN_TAG |
|
| 59 | + ); |
|
| 60 | + |
|
| 61 | + }//end register() |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Processes this test, when one of its tokens is encountered. |
|
| 66 | + * |
|
| 67 | + * @param File $phpcsFile The current file being scanned. |
|
| 68 | + * @param int $stackPtr The position of the current token |
|
| 69 | + * in the stack passed in $tokens. |
|
| 70 | + * |
|
| 71 | + * @return void |
|
| 72 | + */ |
|
| 73 | + public function process(File $phpcsFile, $stackPtr) |
|
| 74 | + { |
|
| 75 | + // We are only interested if this is the first open tag. |
|
| 76 | + if ($stackPtr !== 0) { |
|
| 77 | + if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 78 | + return; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + $filePath = $phpcsFile->getFilename(); |
|
| 83 | + $tokens = $phpcsFile->getTokens(); |
|
| 84 | + // removes the application root from the beginning of the file path |
|
| 85 | + $locationPath = self::_getLocationPath($filePath, $this->_getAppRoot()); |
|
| 86 | + // add an error, if application root doesn't exist in current file path |
|
| 87 | + if (false === $locationPath) { |
|
| 88 | + $error = 'Unable to find "' . $this->_getAppRoot() . '" in file path "' . $filePath . '". Please set your project\'s application root.'; |
|
| 89 | + $phpcsFile->addError($error, count($tokens) - 1); |
|
| 90 | + return; |
|
| 91 | + } |
|
| 92 | + // generates the expected comment |
|
| 93 | + $commentTemplate = "Location: $locationPath"; |
|
| 94 | + |
|
| 95 | + $currentToken = count($tokens) - 1; |
|
| 96 | + $hasClosingLocationComment = false; |
|
| 97 | + $isNotAWhitespaceOrAComment = false; |
|
| 98 | + while ($currentToken >= 0 |
|
| 99 | + && ! $isNotAWhitespaceOrAComment |
|
| 100 | + && ! $hasClosingLocationComment |
|
| 101 | + ) { |
|
| 102 | + $token = $tokens[$currentToken]; |
|
| 103 | + $tokenCode = $token['code']; |
|
| 104 | + if (T_COMMENT === $tokenCode) { |
|
| 105 | + $commentString = self::_getCommentContent($token['content']); |
|
| 106 | + if (0 === strcmp($commentString, $commentTemplate)) { |
|
| 107 | + $hasClosingLocationComment = true; |
|
| 108 | + } |
|
| 109 | + } else if (T_WHITESPACE === $tokenCode) { |
|
| 110 | + // Whitespaces are allowed between the closing file comment, |
|
| 111 | + //other comments and end of file |
|
| 112 | + } else { |
|
| 113 | + $isNotAWhitespaceOrAComment = true; |
|
| 114 | + } |
|
| 115 | + $currentToken--; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + if ( ! $hasClosingLocationComment) { |
|
| 119 | + $error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".'; |
|
| 120 | + $phpcsFile->addError($error, $currentToken); |
|
| 121 | + } |
|
| 122 | + }//end process() |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Returns the relative path from $appRoot to $filePath, or false if |
|
| 127 | + * $appRoot cannot be found in $filePath, because $appRoot is not a parent |
|
| 128 | + * of $filePath. |
|
| 129 | + * |
|
| 130 | + * @param string $filePath Full path to the file being proceed. |
|
| 131 | + * @param string $appRoot Partial or full path to the CodeIgniter |
|
| 132 | + * application root of the file being proceed. It must not contain the |
|
| 133 | + * full path to the application root, but at least the name of the |
|
| 134 | + * application root. Parent directory of the application root are allowed |
|
| 135 | + * but not mandatory. |
|
| 136 | + * |
|
| 137 | + * @return string|bool The relative path from $appRoot to $filePath, or |
|
| 138 | + * false if $appRoot cannot be found in $filePath. |
|
| 139 | + */ |
|
| 140 | + private static function _getLocationPath ($filePath, $appRoot) |
|
| 141 | + { |
|
| 142 | + // removes the path to application root |
|
| 143 | + // from the beginning of the file path |
|
| 144 | + $appRootAt = strpos($filePath, $appRoot); |
|
| 145 | + if (false === $appRootAt) { |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + $localPath = substr($filePath, $appRootAt + strlen($appRoot)); |
|
| 149 | + // ensures the location path to be a relative path starting with "./". |
|
| 150 | + if ( ! self::_stringStartsWith($localPath, './')) { |
|
| 151 | + $localPath = './' . $localPath; |
|
| 152 | + } else if ( ! self::_stringStartsWith($localPath, '.') |
|
| 153 | + && self::_stringStartsWith($localPath, '/') |
|
| 154 | + ) { |
|
| 155 | + $localPath = '.' . $localPath; |
|
| 156 | + } |
|
| 157 | + return $localPath; |
|
| 158 | + }//end _getLocationPath() |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Returns the application root that should be used first. |
|
| 163 | + * |
|
| 164 | + * There are several ways to configure the application root. |
|
| 165 | + * In order of priority : |
|
| 166 | + * - Configuration variable ci_application_root. |
|
| 167 | + * - Rule property applicationRoot. |
|
| 168 | + * - Default value '/application/' |
|
| 169 | + * |
|
| 170 | + * @return string Path to your project application root. |
|
| 171 | + */ |
|
| 172 | + private function _getAppRoot() |
|
| 173 | + { |
|
| 174 | + $appRoot = Common::getConfigData('ci_application_root'); |
|
| 175 | + if (null === $appRoot) { |
|
| 176 | + $appRoot = $this->applicationRoot; |
|
| 177 | + } |
|
| 178 | + return $appRoot; |
|
| 179 | + }//end _getAppRoot() |
|
| 180 | 180 | }//end class |
| 181 | 181 | |
| 182 | 182 | ?> |
@@ -2,6 +2,10 @@ discard block |
||
| 2 | 2 | if ( ! function_exists('glob_recursive')) |
| 3 | 3 | { |
| 4 | 4 | // Does not support flag GLOB_BRACE |
| 5 | + |
|
| 6 | + /** |
|
| 7 | + * @param string $pattern |
|
| 8 | + */ |
|
| 5 | 9 | function glob_recursive($pattern, $flags = 0) |
| 6 | 10 | { |
| 7 | 11 | $files = glob($pattern, $flags); |
@@ -320,7 +324,7 @@ discard block |
||
| 320 | 324 | * Shortcut for joining an array of command arguments |
| 321 | 325 | * and then running it |
| 322 | 326 | * |
| 323 | - * @param array $cmd_parts - command arguments |
|
| 327 | + * @param string[] $cmd_parts - command arguments |
|
| 324 | 328 | * @param string $join_on - what to join the command arguments with |
| 325 | 329 | */ |
| 326 | 330 | protected function _run(array $cmd_parts, $join_on = ' ') |