@@ -17,42 +17,42 @@ |
||
| 17 | 17 | { |
| 18 | 18 | |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Returns an array of tokens this test wants to listen for. |
|
| 22 | - * |
|
| 23 | - * @return array |
|
| 24 | - */ |
|
| 25 | - public function register() |
|
| 26 | - { |
|
| 27 | - return Tokens::$castTokens; |
|
| 28 | - |
|
| 29 | - }//end register() |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Processes this test, when one of its tokens is encountered. |
|
| 34 | - * |
|
| 35 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 36 | - * @param int $stackPtr The position of the current token in |
|
| 37 | - * the stack passed in $tokens. |
|
| 38 | - * |
|
| 39 | - * @return void |
|
| 40 | - */ |
|
| 41 | - public function process(File $phpcsFile, $stackPtr) |
|
| 42 | - { |
|
| 43 | - $tokens = $phpcsFile->getTokens(); |
|
| 44 | - |
|
| 45 | - if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { |
|
| 46 | - return; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $error = 'A cast statement must not be followed by a space'; |
|
| 50 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceFound'); |
|
| 51 | - if ($fix === true) { |
|
| 52 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - }//end process() |
|
| 20 | + /** |
|
| 21 | + * Returns an array of tokens this test wants to listen for. |
|
| 22 | + * |
|
| 23 | + * @return array |
|
| 24 | + */ |
|
| 25 | + public function register() |
|
| 26 | + { |
|
| 27 | + return Tokens::$castTokens; |
|
| 28 | + |
|
| 29 | + }//end register() |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Processes this test, when one of its tokens is encountered. |
|
| 34 | + * |
|
| 35 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 36 | + * @param int $stackPtr The position of the current token in |
|
| 37 | + * the stack passed in $tokens. |
|
| 38 | + * |
|
| 39 | + * @return void |
|
| 40 | + */ |
|
| 41 | + public function process(File $phpcsFile, $stackPtr) |
|
| 42 | + { |
|
| 43 | + $tokens = $phpcsFile->getTokens(); |
|
| 44 | + |
|
| 45 | + if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $error = 'A cast statement must not be followed by a space'; |
|
| 50 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceFound'); |
|
| 51 | + if ($fix === true) { |
|
| 52 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + }//end process() |
|
| 56 | 56 | |
| 57 | 57 | |
| 58 | 58 | }//end class |
@@ -16,110 +16,110 @@ |
||
| 16 | 16 | class UnnecessaryStringConcatSniff implements Sniff |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A list of tokenizers this sniff supports. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - public $supportedTokenizers = [ |
|
| 25 | - 'PHP', |
|
| 26 | - 'JS', |
|
| 27 | - ]; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * If true, an error will be thrown; otherwise a warning. |
|
| 31 | - * |
|
| 32 | - * @var boolean |
|
| 33 | - */ |
|
| 34 | - public $error = true; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * If true, strings concatenated over multiple lines are allowed. |
|
| 38 | - * |
|
| 39 | - * Useful if you break strings over multiple lines to work |
|
| 40 | - * within a max line length. |
|
| 41 | - * |
|
| 42 | - * @var boolean |
|
| 43 | - */ |
|
| 44 | - public $allowMultiline = false; |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Returns an array of tokens this test wants to listen for. |
|
| 49 | - * |
|
| 50 | - * @return array |
|
| 51 | - */ |
|
| 52 | - public function register() |
|
| 53 | - { |
|
| 54 | - return [ |
|
| 55 | - T_STRING_CONCAT, |
|
| 56 | - T_PLUS, |
|
| 57 | - ]; |
|
| 58 | - |
|
| 59 | - }//end register() |
|
| 60 | - |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 64 | - * |
|
| 65 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 66 | - * @param int $stackPtr The position of the current token |
|
| 67 | - * in the stack passed in $tokens. |
|
| 68 | - * |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - public function process(File $phpcsFile, $stackPtr) |
|
| 72 | - { |
|
| 73 | - // Work out which type of file this is for. |
|
| 74 | - $tokens = $phpcsFile->getTokens(); |
|
| 75 | - if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) { |
|
| 76 | - if ($phpcsFile->tokenizerType === 'JS') { |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - } else { |
|
| 80 | - if ($phpcsFile->tokenizerType === 'PHP') { |
|
| 81 | - return; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
| 86 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 87 | - if ($prev === false || $next === false) { |
|
| 88 | - return; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - if (isset(Tokens::$stringTokens[$tokens[$prev]['code']]) === true |
|
| 92 | - && isset(Tokens::$stringTokens[$tokens[$next]['code']]) === true |
|
| 93 | - ) { |
|
| 94 | - if ($tokens[$prev]['content'][0] === $tokens[$next]['content'][0]) { |
|
| 95 | - // Before we throw an error for PHP, allow strings to be |
|
| 96 | - // combined if they would have < and ? next to each other because |
|
| 97 | - // this trick is sometimes required in PHP strings. |
|
| 98 | - if ($phpcsFile->tokenizerType === 'PHP') { |
|
| 99 | - $prevChar = substr($tokens[$prev]['content'], -2, 1); |
|
| 100 | - $nextChar = $tokens[$next]['content'][1]; |
|
| 101 | - $combined = $prevChar.$nextChar; |
|
| 102 | - if ($combined === '?'.'>' || $combined === '<'.'?') { |
|
| 103 | - return; |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ($this->allowMultiline === true |
|
| 108 | - && $tokens[$prev]['line'] !== $tokens[$next]['line'] |
|
| 109 | - ) { |
|
| 110 | - return; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $error = 'String concat is not required here; use a single string instead'; |
|
| 114 | - if ($this->error === true) { |
|
| 115 | - $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
| 116 | - } else { |
|
| 117 | - $phpcsFile->addWarning($error, $stackPtr, 'Found'); |
|
| 118 | - } |
|
| 119 | - }//end if |
|
| 120 | - }//end if |
|
| 121 | - |
|
| 122 | - }//end process() |
|
| 19 | + /** |
|
| 20 | + * A list of tokenizers this sniff supports. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + public $supportedTokenizers = [ |
|
| 25 | + 'PHP', |
|
| 26 | + 'JS', |
|
| 27 | + ]; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * If true, an error will be thrown; otherwise a warning. |
|
| 31 | + * |
|
| 32 | + * @var boolean |
|
| 33 | + */ |
|
| 34 | + public $error = true; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * If true, strings concatenated over multiple lines are allowed. |
|
| 38 | + * |
|
| 39 | + * Useful if you break strings over multiple lines to work |
|
| 40 | + * within a max line length. |
|
| 41 | + * |
|
| 42 | + * @var boolean |
|
| 43 | + */ |
|
| 44 | + public $allowMultiline = false; |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Returns an array of tokens this test wants to listen for. |
|
| 49 | + * |
|
| 50 | + * @return array |
|
| 51 | + */ |
|
| 52 | + public function register() |
|
| 53 | + { |
|
| 54 | + return [ |
|
| 55 | + T_STRING_CONCAT, |
|
| 56 | + T_PLUS, |
|
| 57 | + ]; |
|
| 58 | + |
|
| 59 | + }//end register() |
|
| 60 | + |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 64 | + * |
|
| 65 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 66 | + * @param int $stackPtr The position of the current token |
|
| 67 | + * in the stack passed in $tokens. |
|
| 68 | + * |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + public function process(File $phpcsFile, $stackPtr) |
|
| 72 | + { |
|
| 73 | + // Work out which type of file this is for. |
|
| 74 | + $tokens = $phpcsFile->getTokens(); |
|
| 75 | + if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) { |
|
| 76 | + if ($phpcsFile->tokenizerType === 'JS') { |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | + } else { |
|
| 80 | + if ($phpcsFile->tokenizerType === 'PHP') { |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
| 86 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 87 | + if ($prev === false || $next === false) { |
|
| 88 | + return; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + if (isset(Tokens::$stringTokens[$tokens[$prev]['code']]) === true |
|
| 92 | + && isset(Tokens::$stringTokens[$tokens[$next]['code']]) === true |
|
| 93 | + ) { |
|
| 94 | + if ($tokens[$prev]['content'][0] === $tokens[$next]['content'][0]) { |
|
| 95 | + // Before we throw an error for PHP, allow strings to be |
|
| 96 | + // combined if they would have < and ? next to each other because |
|
| 97 | + // this trick is sometimes required in PHP strings. |
|
| 98 | + if ($phpcsFile->tokenizerType === 'PHP') { |
|
| 99 | + $prevChar = substr($tokens[$prev]['content'], -2, 1); |
|
| 100 | + $nextChar = $tokens[$next]['content'][1]; |
|
| 101 | + $combined = $prevChar.$nextChar; |
|
| 102 | + if ($combined === '?'.'>' || $combined === '<'.'?') { |
|
| 103 | + return; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ($this->allowMultiline === true |
|
| 108 | + && $tokens[$prev]['line'] !== $tokens[$next]['line'] |
|
| 109 | + ) { |
|
| 110 | + return; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $error = 'String concat is not required here; use a single string instead'; |
|
| 114 | + if ($this->error === true) { |
|
| 115 | + $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
| 116 | + } else { |
|
| 117 | + $phpcsFile->addWarning($error, $stackPtr, 'Found'); |
|
| 118 | + } |
|
| 119 | + }//end if |
|
| 120 | + }//end if |
|
| 121 | + |
|
| 122 | + }//end process() |
|
| 123 | 123 | |
| 124 | 124 | |
| 125 | 125 | }//end class |
@@ -16,65 +16,65 @@ |
||
| 16 | 16 | class ByteOrderMarkSniff implements Sniff |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * List of supported BOM definitions. |
|
| 21 | - * |
|
| 22 | - * Use encoding names as keys and hex BOM representations as values. |
|
| 23 | - * |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - protected $bomDefinitions = [ |
|
| 27 | - 'UTF-8' => 'efbbbf', |
|
| 28 | - 'UTF-16 (BE)' => 'feff', |
|
| 29 | - 'UTF-16 (LE)' => 'fffe', |
|
| 30 | - ]; |
|
| 19 | + /** |
|
| 20 | + * List of supported BOM definitions. |
|
| 21 | + * |
|
| 22 | + * Use encoding names as keys and hex BOM representations as values. |
|
| 23 | + * |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + protected $bomDefinitions = [ |
|
| 27 | + 'UTF-8' => 'efbbbf', |
|
| 28 | + 'UTF-16 (BE)' => 'feff', |
|
| 29 | + 'UTF-16 (LE)' => 'fffe', |
|
| 30 | + ]; |
|
| 31 | 31 | |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Returns an array of tokens this test wants to listen for. |
|
| 35 | - * |
|
| 36 | - * @return array |
|
| 37 | - */ |
|
| 38 | - public function register() |
|
| 39 | - { |
|
| 40 | - return [T_INLINE_HTML]; |
|
| 33 | + /** |
|
| 34 | + * Returns an array of tokens this test wants to listen for. |
|
| 35 | + * |
|
| 36 | + * @return array |
|
| 37 | + */ |
|
| 38 | + public function register() |
|
| 39 | + { |
|
| 40 | + return [T_INLINE_HTML]; |
|
| 41 | 41 | |
| 42 | - }//end register() |
|
| 42 | + }//end register() |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 47 | - * |
|
| 48 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 49 | - * @param int $stackPtr The position of the current token in |
|
| 50 | - * the stack passed in $tokens. |
|
| 51 | - * |
|
| 52 | - * @return void |
|
| 53 | - */ |
|
| 54 | - public function process(File $phpcsFile, $stackPtr) |
|
| 55 | - { |
|
| 56 | - // The BOM will be the very first token in the file. |
|
| 57 | - if ($stackPtr !== 0) { |
|
| 58 | - return; |
|
| 59 | - } |
|
| 45 | + /** |
|
| 46 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 47 | + * |
|
| 48 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 49 | + * @param int $stackPtr The position of the current token in |
|
| 50 | + * the stack passed in $tokens. |
|
| 51 | + * |
|
| 52 | + * @return void |
|
| 53 | + */ |
|
| 54 | + public function process(File $phpcsFile, $stackPtr) |
|
| 55 | + { |
|
| 56 | + // The BOM will be the very first token in the file. |
|
| 57 | + if ($stackPtr !== 0) { |
|
| 58 | + return; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - $tokens = $phpcsFile->getTokens(); |
|
| 61 | + $tokens = $phpcsFile->getTokens(); |
|
| 62 | 62 | |
| 63 | - foreach ($this->bomDefinitions as $bomName => $expectedBomHex) { |
|
| 64 | - $bomByteLength = (strlen($expectedBomHex) / 2); |
|
| 65 | - $htmlBomHex = bin2hex(substr($tokens[$stackPtr]['content'], 0, $bomByteLength)); |
|
| 66 | - if ($htmlBomHex === $expectedBomHex) { |
|
| 67 | - $errorData = [$bomName]; |
|
| 68 | - $error = 'File contains %s byte order mark, which may corrupt your application'; |
|
| 69 | - $phpcsFile->addError($error, $stackPtr, 'Found', $errorData); |
|
| 70 | - $phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'yes'); |
|
| 71 | - return; |
|
| 72 | - } |
|
| 73 | - } |
|
| 63 | + foreach ($this->bomDefinitions as $bomName => $expectedBomHex) { |
|
| 64 | + $bomByteLength = (strlen($expectedBomHex) / 2); |
|
| 65 | + $htmlBomHex = bin2hex(substr($tokens[$stackPtr]['content'], 0, $bomByteLength)); |
|
| 66 | + if ($htmlBomHex === $expectedBomHex) { |
|
| 67 | + $errorData = [$bomName]; |
|
| 68 | + $error = 'File contains %s byte order mark, which may corrupt your application'; |
|
| 69 | + $phpcsFile->addError($error, $stackPtr, 'Found', $errorData); |
|
| 70 | + $phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'yes'); |
|
| 71 | + return; |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - $phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'no'); |
|
| 75 | + $phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'no'); |
|
| 76 | 76 | |
| 77 | - }//end process() |
|
| 77 | + }//end process() |
|
| 78 | 78 | |
| 79 | 79 | |
| 80 | 80 | }//end class |
@@ -95,18 +95,18 @@ |
||
| 95 | 95 | if ($fix === true) { |
| 96 | 96 | $tokens = $phpcsFile->getTokens(); |
| 97 | 97 | switch ($this->eolChar) { |
| 98 | - case '\n': |
|
| 99 | - $eolChar = "\n"; |
|
| 100 | - break; |
|
| 101 | - case '\r': |
|
| 102 | - $eolChar = "\r"; |
|
| 103 | - break; |
|
| 104 | - case '\r\n': |
|
| 105 | - $eolChar = "\r\n"; |
|
| 106 | - break; |
|
| 107 | - default: |
|
| 108 | - $eolChar = $this->eolChar; |
|
| 109 | - break; |
|
| 98 | + case '\n': |
|
| 99 | + $eolChar = "\n"; |
|
| 100 | + break; |
|
| 101 | + case '\r': |
|
| 102 | + $eolChar = "\r"; |
|
| 103 | + break; |
|
| 104 | + case '\r\n': |
|
| 105 | + $eolChar = "\r\n"; |
|
| 106 | + break; |
|
| 107 | + default: |
|
| 108 | + $eolChar = $this->eolChar; |
|
| 109 | + break; |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | for ($i = 0; $i < $phpcsFile->numTokens; $i++) { |
@@ -15,134 +15,134 @@ |
||
| 15 | 15 | class LineEndingsSniff implements Sniff |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A list of tokenizers this sniff supports. |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - public $supportedTokenizers = [ |
|
| 24 | - 'PHP', |
|
| 25 | - 'JS', |
|
| 26 | - 'CSS', |
|
| 27 | - ]; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The valid EOL character. |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - public $eolChar = '\n'; |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Returns an array of tokens this test wants to listen for. |
|
| 39 | - * |
|
| 40 | - * @return array |
|
| 41 | - */ |
|
| 42 | - public function register() |
|
| 43 | - { |
|
| 44 | - return [ |
|
| 45 | - T_OPEN_TAG, |
|
| 46 | - T_OPEN_TAG_WITH_ECHO, |
|
| 47 | - ]; |
|
| 48 | - |
|
| 49 | - }//end register() |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 54 | - * |
|
| 55 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 56 | - * @param int $stackPtr The position of the current token in |
|
| 57 | - * the stack passed in $tokens. |
|
| 58 | - * |
|
| 59 | - * @return int |
|
| 60 | - */ |
|
| 61 | - public function process(File $phpcsFile, $stackPtr) |
|
| 62 | - { |
|
| 63 | - $found = $phpcsFile->eolChar; |
|
| 64 | - $found = str_replace("\n", '\n', $found); |
|
| 65 | - $found = str_replace("\r", '\r', $found); |
|
| 66 | - |
|
| 67 | - $phpcsFile->recordMetric($stackPtr, 'EOL char', $found); |
|
| 68 | - |
|
| 69 | - if ($found === $this->eolChar) { |
|
| 70 | - // Ignore the rest of the file. |
|
| 71 | - return ($phpcsFile->numTokens + 1); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - // Check for single line files without an EOL. This is a very special |
|
| 75 | - // case and the EOL char is set to \n when this happens. |
|
| 76 | - if ($found === '\n') { |
|
| 77 | - $tokens = $phpcsFile->getTokens(); |
|
| 78 | - $lastToken = ($phpcsFile->numTokens - 1); |
|
| 79 | - if ($tokens[$lastToken]['line'] === 1 |
|
| 80 | - && $tokens[$lastToken]['content'] !== "\n" |
|
| 81 | - ) { |
|
| 82 | - return; |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $error = 'End of line character is invalid; expected "%s" but found "%s"'; |
|
| 87 | - $expected = $this->eolChar; |
|
| 88 | - $expected = str_replace("\n", '\n', $expected); |
|
| 89 | - $expected = str_replace("\r", '\r', $expected); |
|
| 90 | - $data = [ |
|
| 91 | - $expected, |
|
| 92 | - $found, |
|
| 93 | - ]; |
|
| 94 | - |
|
| 95 | - // Errors are always reported on line 1, no matter where the first PHP tag is. |
|
| 96 | - $fix = $phpcsFile->addFixableError($error, 0, 'InvalidEOLChar', $data); |
|
| 97 | - |
|
| 98 | - if ($fix === true) { |
|
| 99 | - $tokens = $phpcsFile->getTokens(); |
|
| 100 | - switch ($this->eolChar) { |
|
| 101 | - case '\n': |
|
| 102 | - $eolChar = "\n"; |
|
| 103 | - break; |
|
| 104 | - case '\r': |
|
| 105 | - $eolChar = "\r"; |
|
| 106 | - break; |
|
| 107 | - case '\r\n': |
|
| 108 | - $eolChar = "\r\n"; |
|
| 109 | - break; |
|
| 110 | - default: |
|
| 111 | - $eolChar = $this->eolChar; |
|
| 112 | - break; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - for ($i = 0; $i < $phpcsFile->numTokens; $i++) { |
|
| 116 | - if (isset($tokens[($i + 1)]) === true |
|
| 117 | - && $tokens[($i + 1)]['line'] <= $tokens[$i]['line'] |
|
| 118 | - ) { |
|
| 119 | - continue; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // Token is the last on a line. |
|
| 123 | - if (isset($tokens[$i]['orig_content']) === true) { |
|
| 124 | - $tokenContent = $tokens[$i]['orig_content']; |
|
| 125 | - } else { |
|
| 126 | - $tokenContent = $tokens[$i]['content']; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - if ($tokenContent === '') { |
|
| 130 | - // Special case for JS/CSS close tag. |
|
| 131 | - continue; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - $newContent = rtrim($tokenContent, "\r\n"); |
|
| 135 | - $newContent .= $eolChar; |
|
| 136 | - if ($tokenContent !== $newContent) { |
|
| 137 | - $phpcsFile->fixer->replaceToken($i, $newContent); |
|
| 138 | - } |
|
| 139 | - }//end for |
|
| 140 | - }//end if |
|
| 141 | - |
|
| 142 | - // Ignore the rest of the file. |
|
| 143 | - return ($phpcsFile->numTokens + 1); |
|
| 144 | - |
|
| 145 | - }//end process() |
|
| 18 | + /** |
|
| 19 | + * A list of tokenizers this sniff supports. |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + public $supportedTokenizers = [ |
|
| 24 | + 'PHP', |
|
| 25 | + 'JS', |
|
| 26 | + 'CSS', |
|
| 27 | + ]; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The valid EOL character. |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + public $eolChar = '\n'; |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Returns an array of tokens this test wants to listen for. |
|
| 39 | + * |
|
| 40 | + * @return array |
|
| 41 | + */ |
|
| 42 | + public function register() |
|
| 43 | + { |
|
| 44 | + return [ |
|
| 45 | + T_OPEN_TAG, |
|
| 46 | + T_OPEN_TAG_WITH_ECHO, |
|
| 47 | + ]; |
|
| 48 | + |
|
| 49 | + }//end register() |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 54 | + * |
|
| 55 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 56 | + * @param int $stackPtr The position of the current token in |
|
| 57 | + * the stack passed in $tokens. |
|
| 58 | + * |
|
| 59 | + * @return int |
|
| 60 | + */ |
|
| 61 | + public function process(File $phpcsFile, $stackPtr) |
|
| 62 | + { |
|
| 63 | + $found = $phpcsFile->eolChar; |
|
| 64 | + $found = str_replace("\n", '\n', $found); |
|
| 65 | + $found = str_replace("\r", '\r', $found); |
|
| 66 | + |
|
| 67 | + $phpcsFile->recordMetric($stackPtr, 'EOL char', $found); |
|
| 68 | + |
|
| 69 | + if ($found === $this->eolChar) { |
|
| 70 | + // Ignore the rest of the file. |
|
| 71 | + return ($phpcsFile->numTokens + 1); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + // Check for single line files without an EOL. This is a very special |
|
| 75 | + // case and the EOL char is set to \n when this happens. |
|
| 76 | + if ($found === '\n') { |
|
| 77 | + $tokens = $phpcsFile->getTokens(); |
|
| 78 | + $lastToken = ($phpcsFile->numTokens - 1); |
|
| 79 | + if ($tokens[$lastToken]['line'] === 1 |
|
| 80 | + && $tokens[$lastToken]['content'] !== "\n" |
|
| 81 | + ) { |
|
| 82 | + return; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $error = 'End of line character is invalid; expected "%s" but found "%s"'; |
|
| 87 | + $expected = $this->eolChar; |
|
| 88 | + $expected = str_replace("\n", '\n', $expected); |
|
| 89 | + $expected = str_replace("\r", '\r', $expected); |
|
| 90 | + $data = [ |
|
| 91 | + $expected, |
|
| 92 | + $found, |
|
| 93 | + ]; |
|
| 94 | + |
|
| 95 | + // Errors are always reported on line 1, no matter where the first PHP tag is. |
|
| 96 | + $fix = $phpcsFile->addFixableError($error, 0, 'InvalidEOLChar', $data); |
|
| 97 | + |
|
| 98 | + if ($fix === true) { |
|
| 99 | + $tokens = $phpcsFile->getTokens(); |
|
| 100 | + switch ($this->eolChar) { |
|
| 101 | + case '\n': |
|
| 102 | + $eolChar = "\n"; |
|
| 103 | + break; |
|
| 104 | + case '\r': |
|
| 105 | + $eolChar = "\r"; |
|
| 106 | + break; |
|
| 107 | + case '\r\n': |
|
| 108 | + $eolChar = "\r\n"; |
|
| 109 | + break; |
|
| 110 | + default: |
|
| 111 | + $eolChar = $this->eolChar; |
|
| 112 | + break; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + for ($i = 0; $i < $phpcsFile->numTokens; $i++) { |
|
| 116 | + if (isset($tokens[($i + 1)]) === true |
|
| 117 | + && $tokens[($i + 1)]['line'] <= $tokens[$i]['line'] |
|
| 118 | + ) { |
|
| 119 | + continue; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // Token is the last on a line. |
|
| 123 | + if (isset($tokens[$i]['orig_content']) === true) { |
|
| 124 | + $tokenContent = $tokens[$i]['orig_content']; |
|
| 125 | + } else { |
|
| 126 | + $tokenContent = $tokens[$i]['content']; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + if ($tokenContent === '') { |
|
| 130 | + // Special case for JS/CSS close tag. |
|
| 131 | + continue; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $newContent = rtrim($tokenContent, "\r\n"); |
|
| 135 | + $newContent .= $eolChar; |
|
| 136 | + if ($tokenContent !== $newContent) { |
|
| 137 | + $phpcsFile->fixer->replaceToken($i, $newContent); |
|
| 138 | + } |
|
| 139 | + }//end for |
|
| 140 | + }//end if |
|
| 141 | + |
|
| 142 | + // Ignore the rest of the file. |
|
| 143 | + return ($phpcsFile->numTokens + 1); |
|
| 144 | + |
|
| 145 | + }//end process() |
|
| 146 | 146 | |
| 147 | 147 | |
| 148 | 148 | }//end class |
@@ -47,7 +47,7 @@ |
||
| 47 | 47 | $filename = basename($filename); |
| 48 | 48 | $lowercaseFilename = strtolower($filename); |
| 49 | 49 | if ($filename !== $lowercaseFilename) { |
| 50 | - $data = [ |
|
| 50 | + $data = [ |
|
| 51 | 51 | $filename, |
| 52 | 52 | $lowercaseFilename, |
| 53 | 53 | ]; |
@@ -16,55 +16,55 @@ |
||
| 16 | 16 | { |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Returns an array of tokens this test wants to listen for. |
|
| 21 | - * |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function register() |
|
| 25 | - { |
|
| 26 | - return [ |
|
| 27 | - T_OPEN_TAG, |
|
| 28 | - T_OPEN_TAG_WITH_ECHO, |
|
| 29 | - ]; |
|
| 19 | + /** |
|
| 20 | + * Returns an array of tokens this test wants to listen for. |
|
| 21 | + * |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function register() |
|
| 25 | + { |
|
| 26 | + return [ |
|
| 27 | + T_OPEN_TAG, |
|
| 28 | + T_OPEN_TAG_WITH_ECHO, |
|
| 29 | + ]; |
|
| 30 | 30 | |
| 31 | - }//end register() |
|
| 31 | + }//end register() |
|
| 32 | 32 | |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 36 | - * |
|
| 37 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 38 | - * @param int $stackPtr The position of the current token in |
|
| 39 | - * the stack passed in $tokens. |
|
| 40 | - * |
|
| 41 | - * @return int |
|
| 42 | - */ |
|
| 43 | - public function process(File $phpcsFile, $stackPtr) |
|
| 44 | - { |
|
| 45 | - $filename = $phpcsFile->getFilename(); |
|
| 46 | - if ($filename === 'STDIN') { |
|
| 47 | - return; |
|
| 48 | - } |
|
| 34 | + /** |
|
| 35 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 36 | + * |
|
| 37 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 38 | + * @param int $stackPtr The position of the current token in |
|
| 39 | + * the stack passed in $tokens. |
|
| 40 | + * |
|
| 41 | + * @return int |
|
| 42 | + */ |
|
| 43 | + public function process(File $phpcsFile, $stackPtr) |
|
| 44 | + { |
|
| 45 | + $filename = $phpcsFile->getFilename(); |
|
| 46 | + if ($filename === 'STDIN') { |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - $filename = basename($filename); |
|
| 51 | - $lowercaseFilename = strtolower($filename); |
|
| 52 | - if ($filename !== $lowercaseFilename) { |
|
| 53 | - $data = [ |
|
| 54 | - $filename, |
|
| 55 | - $lowercaseFilename, |
|
| 56 | - ]; |
|
| 57 | - $error = 'Filename "%s" doesn\'t match the expected filename "%s"'; |
|
| 58 | - $phpcsFile->addError($error, $stackPtr, 'NotFound', $data); |
|
| 59 | - $phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'no'); |
|
| 60 | - } else { |
|
| 61 | - $phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'yes'); |
|
| 62 | - } |
|
| 50 | + $filename = basename($filename); |
|
| 51 | + $lowercaseFilename = strtolower($filename); |
|
| 52 | + if ($filename !== $lowercaseFilename) { |
|
| 53 | + $data = [ |
|
| 54 | + $filename, |
|
| 55 | + $lowercaseFilename, |
|
| 56 | + ]; |
|
| 57 | + $error = 'Filename "%s" doesn\'t match the expected filename "%s"'; |
|
| 58 | + $phpcsFile->addError($error, $stackPtr, 'NotFound', $data); |
|
| 59 | + $phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'no'); |
|
| 60 | + } else { |
|
| 61 | + $phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'yes'); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - // Ignore the rest of the file. |
|
| 65 | - return ($phpcsFile->numTokens + 1); |
|
| 64 | + // Ignore the rest of the file. |
|
| 65 | + return ($phpcsFile->numTokens + 1); |
|
| 66 | 66 | |
| 67 | - }//end process() |
|
| 67 | + }//end process() |
|
| 68 | 68 | |
| 69 | 69 | |
| 70 | 70 | }//end class |
@@ -17,62 +17,62 @@ |
||
| 17 | 17 | class FixmeSniff implements Sniff |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * A list of tokenizers this sniff supports. |
|
| 22 | - * |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - public $supportedTokenizers = [ |
|
| 26 | - 'PHP', |
|
| 27 | - 'JS', |
|
| 28 | - ]; |
|
| 20 | + /** |
|
| 21 | + * A list of tokenizers this sniff supports. |
|
| 22 | + * |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + public $supportedTokenizers = [ |
|
| 26 | + 'PHP', |
|
| 27 | + 'JS', |
|
| 28 | + ]; |
|
| 29 | 29 | |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Returns an array of tokens this test wants to listen for. |
|
| 33 | - * |
|
| 34 | - * @return array |
|
| 35 | - */ |
|
| 36 | - public function register() |
|
| 37 | - { |
|
| 38 | - return array_diff(Tokens::$commentTokens, Tokens::$phpcsCommentTokens); |
|
| 31 | + /** |
|
| 32 | + * Returns an array of tokens this test wants to listen for. |
|
| 33 | + * |
|
| 34 | + * @return array |
|
| 35 | + */ |
|
| 36 | + public function register() |
|
| 37 | + { |
|
| 38 | + return array_diff(Tokens::$commentTokens, Tokens::$phpcsCommentTokens); |
|
| 39 | 39 | |
| 40 | - }//end register() |
|
| 40 | + }//end register() |
|
| 41 | 41 | |
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 45 | - * |
|
| 46 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 47 | - * @param int $stackPtr The position of the current token |
|
| 48 | - * in the stack passed in $tokens. |
|
| 49 | - * |
|
| 50 | - * @return void |
|
| 51 | - */ |
|
| 52 | - public function process(File $phpcsFile, $stackPtr) |
|
| 53 | - { |
|
| 54 | - $tokens = $phpcsFile->getTokens(); |
|
| 43 | + /** |
|
| 44 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 45 | + * |
|
| 46 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 47 | + * @param int $stackPtr The position of the current token |
|
| 48 | + * in the stack passed in $tokens. |
|
| 49 | + * |
|
| 50 | + * @return void |
|
| 51 | + */ |
|
| 52 | + public function process(File $phpcsFile, $stackPtr) |
|
| 53 | + { |
|
| 54 | + $tokens = $phpcsFile->getTokens(); |
|
| 55 | 55 | |
| 56 | - $content = $tokens[$stackPtr]['content']; |
|
| 57 | - $matches = []; |
|
| 58 | - preg_match('/(?:\A|[^\p{L}]+)fixme([^\p{L}]+(.*)|\Z)/ui', $content, $matches); |
|
| 59 | - if (empty($matches) === false) { |
|
| 60 | - // Clear whitespace and some common characters not required at |
|
| 61 | - // the end of a fixme message to make the error more informative. |
|
| 62 | - $type = 'CommentFound'; |
|
| 63 | - $fixmeMessage = trim($matches[1]); |
|
| 64 | - $fixmeMessage = trim($fixmeMessage, '-:[](). '); |
|
| 65 | - $error = 'Comment refers to a FIXME task'; |
|
| 66 | - $data = [$fixmeMessage]; |
|
| 67 | - if ($fixmeMessage !== '') { |
|
| 68 | - $type = 'TaskFound'; |
|
| 69 | - $error .= ' "%s"'; |
|
| 70 | - } |
|
| 56 | + $content = $tokens[$stackPtr]['content']; |
|
| 57 | + $matches = []; |
|
| 58 | + preg_match('/(?:\A|[^\p{L}]+)fixme([^\p{L}]+(.*)|\Z)/ui', $content, $matches); |
|
| 59 | + if (empty($matches) === false) { |
|
| 60 | + // Clear whitespace and some common characters not required at |
|
| 61 | + // the end of a fixme message to make the error more informative. |
|
| 62 | + $type = 'CommentFound'; |
|
| 63 | + $fixmeMessage = trim($matches[1]); |
|
| 64 | + $fixmeMessage = trim($fixmeMessage, '-:[](). '); |
|
| 65 | + $error = 'Comment refers to a FIXME task'; |
|
| 66 | + $data = [$fixmeMessage]; |
|
| 67 | + if ($fixmeMessage !== '') { |
|
| 68 | + $type = 'TaskFound'; |
|
| 69 | + $error .= ' "%s"'; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - $phpcsFile->addError($error, $stackPtr, $type, $data); |
|
| 73 | - } |
|
| 72 | + $phpcsFile->addError($error, $stackPtr, $type, $data); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - }//end process() |
|
| 75 | + }//end process() |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | 78 | }//end class |
@@ -16,62 +16,62 @@ |
||
| 16 | 16 | class TodoSniff implements Sniff |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A list of tokenizers this sniff supports. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - public $supportedTokenizers = [ |
|
| 25 | - 'PHP', |
|
| 26 | - 'JS', |
|
| 27 | - ]; |
|
| 19 | + /** |
|
| 20 | + * A list of tokenizers this sniff supports. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + public $supportedTokenizers = [ |
|
| 25 | + 'PHP', |
|
| 26 | + 'JS', |
|
| 27 | + ]; |
|
| 28 | 28 | |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Returns an array of tokens this test wants to listen for. |
|
| 32 | - * |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - public function register() |
|
| 36 | - { |
|
| 37 | - return array_diff(Tokens::$commentTokens, Tokens::$phpcsCommentTokens); |
|
| 30 | + /** |
|
| 31 | + * Returns an array of tokens this test wants to listen for. |
|
| 32 | + * |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + public function register() |
|
| 36 | + { |
|
| 37 | + return array_diff(Tokens::$commentTokens, Tokens::$phpcsCommentTokens); |
|
| 38 | 38 | |
| 39 | - }//end register() |
|
| 39 | + }//end register() |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 44 | - * |
|
| 45 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 46 | - * @param int $stackPtr The position of the current token |
|
| 47 | - * in the stack passed in $tokens. |
|
| 48 | - * |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function process(File $phpcsFile, $stackPtr) |
|
| 52 | - { |
|
| 53 | - $tokens = $phpcsFile->getTokens(); |
|
| 42 | + /** |
|
| 43 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 44 | + * |
|
| 45 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 46 | + * @param int $stackPtr The position of the current token |
|
| 47 | + * in the stack passed in $tokens. |
|
| 48 | + * |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function process(File $phpcsFile, $stackPtr) |
|
| 52 | + { |
|
| 53 | + $tokens = $phpcsFile->getTokens(); |
|
| 54 | 54 | |
| 55 | - $content = $tokens[$stackPtr]['content']; |
|
| 56 | - $matches = []; |
|
| 57 | - preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $content, $matches); |
|
| 58 | - if (empty($matches) === false) { |
|
| 59 | - // Clear whitespace and some common characters not required at |
|
| 60 | - // the end of a to-do message to make the warning more informative. |
|
| 61 | - $type = 'CommentFound'; |
|
| 62 | - $todoMessage = trim($matches[1]); |
|
| 63 | - $todoMessage = trim($todoMessage, '-:[](). '); |
|
| 64 | - $error = 'Comment refers to a TODO task'; |
|
| 65 | - $data = [$todoMessage]; |
|
| 66 | - if ($todoMessage !== '') { |
|
| 67 | - $type = 'TaskFound'; |
|
| 68 | - $error .= ' "%s"'; |
|
| 69 | - } |
|
| 55 | + $content = $tokens[$stackPtr]['content']; |
|
| 56 | + $matches = []; |
|
| 57 | + preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $content, $matches); |
|
| 58 | + if (empty($matches) === false) { |
|
| 59 | + // Clear whitespace and some common characters not required at |
|
| 60 | + // the end of a to-do message to make the warning more informative. |
|
| 61 | + $type = 'CommentFound'; |
|
| 62 | + $todoMessage = trim($matches[1]); |
|
| 63 | + $todoMessage = trim($todoMessage, '-:[](). '); |
|
| 64 | + $error = 'Comment refers to a TODO task'; |
|
| 65 | + $data = [$todoMessage]; |
|
| 66 | + if ($todoMessage !== '') { |
|
| 67 | + $type = 'TaskFound'; |
|
| 68 | + $error .= ' "%s"'; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $phpcsFile->addWarning($error, $stackPtr, $type, $data); |
|
| 72 | - } |
|
| 71 | + $phpcsFile->addWarning($error, $stackPtr, $type, $data); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - }//end process() |
|
| 74 | + }//end process() |
|
| 75 | 75 | |
| 76 | 76 | |
| 77 | 77 | }//end class |
@@ -16,85 +16,85 @@ |
||
| 16 | 16 | class NestingLevelSniff implements Sniff |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A nesting level higher than this value will throw a warning. |
|
| 21 | - * |
|
| 22 | - * @var integer |
|
| 23 | - */ |
|
| 24 | - public $nestingLevel = 5; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * A nesting level higher than this value will throw an error. |
|
| 28 | - * |
|
| 29 | - * @var integer |
|
| 30 | - */ |
|
| 31 | - public $absoluteNestingLevel = 10; |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Returns an array of tokens this test wants to listen for. |
|
| 36 | - * |
|
| 37 | - * @return array |
|
| 38 | - */ |
|
| 39 | - public function register() |
|
| 40 | - { |
|
| 41 | - return [T_FUNCTION]; |
|
| 42 | - |
|
| 43 | - }//end register() |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Processes this test, when one of its tokens is encountered. |
|
| 48 | - * |
|
| 49 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 50 | - * @param int $stackPtr The position of the current token |
|
| 51 | - * in the stack passed in $tokens. |
|
| 52 | - * |
|
| 53 | - * @return void |
|
| 54 | - */ |
|
| 55 | - public function process(File $phpcsFile, $stackPtr) |
|
| 56 | - { |
|
| 57 | - $tokens = $phpcsFile->getTokens(); |
|
| 58 | - |
|
| 59 | - // Ignore abstract methods. |
|
| 60 | - if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
| 61 | - return; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // Detect start and end of this function definition. |
|
| 65 | - $start = $tokens[$stackPtr]['scope_opener']; |
|
| 66 | - $end = $tokens[$stackPtr]['scope_closer']; |
|
| 67 | - |
|
| 68 | - $nestingLevel = 0; |
|
| 69 | - |
|
| 70 | - // Find the maximum nesting level of any token in the function. |
|
| 71 | - for ($i = ($start + 1); $i < $end; $i++) { |
|
| 72 | - $level = $tokens[$i]['level']; |
|
| 73 | - if ($nestingLevel < $level) { |
|
| 74 | - $nestingLevel = $level; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // We subtract the nesting level of the function itself. |
|
| 79 | - $nestingLevel = ($nestingLevel - $tokens[$stackPtr]['level'] - 1); |
|
| 80 | - |
|
| 81 | - if ($nestingLevel > $this->absoluteNestingLevel) { |
|
| 82 | - $error = 'Function\'s nesting level (%s) exceeds allowed maximum of %s'; |
|
| 83 | - $data = [ |
|
| 84 | - $nestingLevel, |
|
| 85 | - $this->absoluteNestingLevel, |
|
| 86 | - ]; |
|
| 87 | - $phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data); |
|
| 88 | - } else if ($nestingLevel > $this->nestingLevel) { |
|
| 89 | - $warning = 'Function\'s nesting level (%s) exceeds %s; consider refactoring the function'; |
|
| 90 | - $data = [ |
|
| 91 | - $nestingLevel, |
|
| 92 | - $this->nestingLevel, |
|
| 93 | - ]; |
|
| 94 | - $phpcsFile->addWarning($warning, $stackPtr, 'TooHigh', $data); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - }//end process() |
|
| 19 | + /** |
|
| 20 | + * A nesting level higher than this value will throw a warning. |
|
| 21 | + * |
|
| 22 | + * @var integer |
|
| 23 | + */ |
|
| 24 | + public $nestingLevel = 5; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * A nesting level higher than this value will throw an error. |
|
| 28 | + * |
|
| 29 | + * @var integer |
|
| 30 | + */ |
|
| 31 | + public $absoluteNestingLevel = 10; |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Returns an array of tokens this test wants to listen for. |
|
| 36 | + * |
|
| 37 | + * @return array |
|
| 38 | + */ |
|
| 39 | + public function register() |
|
| 40 | + { |
|
| 41 | + return [T_FUNCTION]; |
|
| 42 | + |
|
| 43 | + }//end register() |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Processes this test, when one of its tokens is encountered. |
|
| 48 | + * |
|
| 49 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 50 | + * @param int $stackPtr The position of the current token |
|
| 51 | + * in the stack passed in $tokens. |
|
| 52 | + * |
|
| 53 | + * @return void |
|
| 54 | + */ |
|
| 55 | + public function process(File $phpcsFile, $stackPtr) |
|
| 56 | + { |
|
| 57 | + $tokens = $phpcsFile->getTokens(); |
|
| 58 | + |
|
| 59 | + // Ignore abstract methods. |
|
| 60 | + if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
| 61 | + return; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // Detect start and end of this function definition. |
|
| 65 | + $start = $tokens[$stackPtr]['scope_opener']; |
|
| 66 | + $end = $tokens[$stackPtr]['scope_closer']; |
|
| 67 | + |
|
| 68 | + $nestingLevel = 0; |
|
| 69 | + |
|
| 70 | + // Find the maximum nesting level of any token in the function. |
|
| 71 | + for ($i = ($start + 1); $i < $end; $i++) { |
|
| 72 | + $level = $tokens[$i]['level']; |
|
| 73 | + if ($nestingLevel < $level) { |
|
| 74 | + $nestingLevel = $level; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // We subtract the nesting level of the function itself. |
|
| 79 | + $nestingLevel = ($nestingLevel - $tokens[$stackPtr]['level'] - 1); |
|
| 80 | + |
|
| 81 | + if ($nestingLevel > $this->absoluteNestingLevel) { |
|
| 82 | + $error = 'Function\'s nesting level (%s) exceeds allowed maximum of %s'; |
|
| 83 | + $data = [ |
|
| 84 | + $nestingLevel, |
|
| 85 | + $this->absoluteNestingLevel, |
|
| 86 | + ]; |
|
| 87 | + $phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data); |
|
| 88 | + } else if ($nestingLevel > $this->nestingLevel) { |
|
| 89 | + $warning = 'Function\'s nesting level (%s) exceeds %s; consider refactoring the function'; |
|
| 90 | + $data = [ |
|
| 91 | + $nestingLevel, |
|
| 92 | + $this->nestingLevel, |
|
| 93 | + ]; |
|
| 94 | + $phpcsFile->addWarning($warning, $stackPtr, 'TooHigh', $data); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + }//end process() |
|
| 98 | 98 | |
| 99 | 99 | |
| 100 | 100 | }//end class |
@@ -25,13 +25,13 @@ discard block |
||
| 25 | 25 | public static function addIssue( |
| 26 | 26 | $title, |
| 27 | 27 | $description, |
| 28 | - $reporter=NULL, |
|
| 29 | - $projectid=NULL, |
|
| 30 | - array $tags=array(), |
|
| 31 | - $status=NULL, |
|
| 32 | - $assignedTo=NULL, |
|
| 33 | - $reportedDate=NULL, |
|
| 34 | - $reportedMilestone=NULL |
|
| 28 | + $reporter = NULL, |
|
| 29 | + $projectid = NULL, |
|
| 30 | + array $tags = array(), |
|
| 31 | + $status = NULL, |
|
| 32 | + $assignedTo = NULL, |
|
| 33 | + $reportedDate = NULL, |
|
| 34 | + $reportedMilestone = NULL |
|
| 35 | 35 | ) { |
| 36 | 36 | // Get current projectid if not specified. |
| 37 | 37 | if ($projectid === NULL) { |
@@ -160,13 +160,13 @@ discard block |
||
| 160 | 160 | public static function addIssue( |
| 161 | 161 | $title, |
| 162 | 162 | $description, |
| 163 | - $reporter=NULL, |
|
| 164 | - $projectid=NULL, |
|
| 165 | - array $tags=array(), |
|
| 166 | - $status=NULL, |
|
| 167 | - $assignedTo=NULL, |
|
| 168 | - $reportedDate=NULL, |
|
| 169 | - $reportedMilestone=NULL |
|
| 163 | + $reporter = NULL, |
|
| 164 | + $projectid = NULL, |
|
| 165 | + array $tags = array(), |
|
| 166 | + $status = NULL, |
|
| 167 | + $assignedTo = NULL, |
|
| 168 | + $reportedDate = NULL, |
|
| 169 | + $reportedMilestone = NULL |
|
| 170 | 170 | ) { |
| 171 | 171 | // Get current projectid if not specified. |
| 172 | 172 | if ($projectid === NULL) { |
@@ -27,116 +27,116 @@ discard block |
||
| 27 | 27 | * @api-permission public |
| 28 | 28 | */ |
| 29 | 29 | public static function addIssue( |
| 30 | - $title, |
|
| 31 | - $description, |
|
| 32 | - $reporter=NULL, |
|
| 33 | - $projectid=NULL, |
|
| 34 | - array $tags=array(), |
|
| 35 | - $status=NULL, |
|
| 36 | - $assignedTo=NULL, |
|
| 37 | - $reportedDate=NULL, |
|
| 38 | - $reportedMilestone=NULL |
|
| 30 | + $title, |
|
| 31 | + $description, |
|
| 32 | + $reporter=NULL, |
|
| 33 | + $projectid=NULL, |
|
| 34 | + array $tags=array(), |
|
| 35 | + $status=NULL, |
|
| 36 | + $assignedTo=NULL, |
|
| 37 | + $reportedDate=NULL, |
|
| 38 | + $reportedMilestone=NULL |
|
| 39 | 39 | ) { |
| 40 | - // Get current projectid if not specified. |
|
| 41 | - if ($projectid === NULL) { |
|
| 42 | - Channels::includeSystem('Project'); |
|
| 43 | - $projectid = Project::getCurrentProjectId(); |
|
| 44 | - Channels::modifyBasket('project', $projectid); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - Channels::includeSystem('SquizRoadmap'); |
|
| 48 | - Channels::includeSystem('Permission'); |
|
| 49 | - if (Permission::hasPermission($projectid, 'ideas.contribute') === FALSE) { |
|
| 50 | - throw new ChannelException(_('You do not have permission to contribute idea')); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - if ($assignedTo !== NULL) { |
|
| 54 | - if (Permission::hasPermission($projectid, 'ideas.edit.details') === FALSE) { |
|
| 55 | - throw new ChannelException(_('You do not have permission to assign user to idea')); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - if (SquizRoadmap::isVisibleProject($projectid, $assignedTo) === FALSE) { |
|
| 59 | - throw new ChannelException(_('Assigned to user does not have access to issue project.')); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - // Get current user id if not specified. |
|
| 64 | - if ($reporter === NULL) { |
|
| 65 | - Channels::includeSystem('User'); |
|
| 66 | - $reporter = User::getCurrentUserid(); |
|
| 67 | - Channels::modifyBasket('reporter', $reporter); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - if (SquizRoadmap::isVisibleProject($projectid, $reporter) === FALSE) { |
|
| 71 | - throw new ChannelException(_('Contributed by user does not have access to issue project.')); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - // Make sure status is valid. |
|
| 75 | - Channels::includeSystem('SquizRoadmap'); |
|
| 76 | - Channels::includeSystem('SquizRoadmapStatus'); |
|
| 77 | - if ($status === NULL) { |
|
| 78 | - $statuses = SquizRoadmapStatus::getStatus($projectid); |
|
| 79 | - if (empty($statuses) === TRUE) { |
|
| 80 | - throw new ChannelException(_('No defined statuses in project')); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - $status = $statuses[0]['status']; |
|
| 84 | - Channels::modifyBasket('status', $status); |
|
| 85 | - } else if (SquizRoadmapStatus::isValidStatus($projectid, $status) === FALSE) { |
|
| 86 | - throw new ChannelException(sprintf(_('Invalid status: %s'), $status)); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $issueid = DAL::seqNextVal('sq_rdm_issue_seq'); |
|
| 90 | - Channels::addToBasket('issueid', $issueid); |
|
| 91 | - |
|
| 92 | - if ($reportedDate === NULL) { |
|
| 93 | - include_once 'Libs/String/String.inc'; |
|
| 94 | - $reportedDate = String::tsIso8601(time()); |
|
| 95 | - Channels::modifyBasket('reportedDate', $reportedDate); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $title = trim($title); |
|
| 99 | - Channels::modifyBasket('title', $title); |
|
| 100 | - if (empty($title) === TRUE) { |
|
| 101 | - throw new ChannelException(_('Title cannot be empty')); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - $description = SquizRoadmap::stripTags(trim($description)); |
|
| 105 | - Channels::modifyBasket('description', $description); |
|
| 106 | - if (empty($description) === TRUE) { |
|
| 107 | - throw new ChannelException(_('Description cannot be empty')); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - try { |
|
| 111 | - DAL::beginTransaction(); |
|
| 112 | - |
|
| 113 | - $query = DAL::getDALQuery('SquizRoadmapIssue', 'addIssue'); |
|
| 114 | - DAL::executeQuery($query); |
|
| 115 | - |
|
| 116 | - // Add tags for the new issue. |
|
| 117 | - SquizRoadmapIssue::addIssueTags($issueid, $tags); |
|
| 118 | - |
|
| 119 | - // Add reporter and assignee to watch list. |
|
| 120 | - SquizRoadmapIssue::addIssueWatch($issueid, $reporter); |
|
| 121 | - |
|
| 122 | - if ($assignedTo !== NULL) { |
|
| 123 | - SquizRoadmapIssue::addIssueWatch($issueid, $assignedTo); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - SquizRoadmapIssue::clearIssueCache($issueid); |
|
| 127 | - |
|
| 128 | - DAL::commit(); |
|
| 129 | - } catch (Exception $e) { |
|
| 130 | - DAL::rollBack(); |
|
| 131 | - throw new ChannelException($e->getMessage()); |
|
| 132 | - }//end try |
|
| 133 | - |
|
| 134 | - if ($something === NULL) { |
|
| 135 | - if ($bar !== NULL) { |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $issueid; |
|
| 40 | + // Get current projectid if not specified. |
|
| 41 | + if ($projectid === NULL) { |
|
| 42 | + Channels::includeSystem('Project'); |
|
| 43 | + $projectid = Project::getCurrentProjectId(); |
|
| 44 | + Channels::modifyBasket('project', $projectid); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + Channels::includeSystem('SquizRoadmap'); |
|
| 48 | + Channels::includeSystem('Permission'); |
|
| 49 | + if (Permission::hasPermission($projectid, 'ideas.contribute') === FALSE) { |
|
| 50 | + throw new ChannelException(_('You do not have permission to contribute idea')); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + if ($assignedTo !== NULL) { |
|
| 54 | + if (Permission::hasPermission($projectid, 'ideas.edit.details') === FALSE) { |
|
| 55 | + throw new ChannelException(_('You do not have permission to assign user to idea')); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + if (SquizRoadmap::isVisibleProject($projectid, $assignedTo) === FALSE) { |
|
| 59 | + throw new ChannelException(_('Assigned to user does not have access to issue project.')); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + // Get current user id if not specified. |
|
| 64 | + if ($reporter === NULL) { |
|
| 65 | + Channels::includeSystem('User'); |
|
| 66 | + $reporter = User::getCurrentUserid(); |
|
| 67 | + Channels::modifyBasket('reporter', $reporter); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + if (SquizRoadmap::isVisibleProject($projectid, $reporter) === FALSE) { |
|
| 71 | + throw new ChannelException(_('Contributed by user does not have access to issue project.')); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + // Make sure status is valid. |
|
| 75 | + Channels::includeSystem('SquizRoadmap'); |
|
| 76 | + Channels::includeSystem('SquizRoadmapStatus'); |
|
| 77 | + if ($status === NULL) { |
|
| 78 | + $statuses = SquizRoadmapStatus::getStatus($projectid); |
|
| 79 | + if (empty($statuses) === TRUE) { |
|
| 80 | + throw new ChannelException(_('No defined statuses in project')); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + $status = $statuses[0]['status']; |
|
| 84 | + Channels::modifyBasket('status', $status); |
|
| 85 | + } else if (SquizRoadmapStatus::isValidStatus($projectid, $status) === FALSE) { |
|
| 86 | + throw new ChannelException(sprintf(_('Invalid status: %s'), $status)); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $issueid = DAL::seqNextVal('sq_rdm_issue_seq'); |
|
| 90 | + Channels::addToBasket('issueid', $issueid); |
|
| 91 | + |
|
| 92 | + if ($reportedDate === NULL) { |
|
| 93 | + include_once 'Libs/String/String.inc'; |
|
| 94 | + $reportedDate = String::tsIso8601(time()); |
|
| 95 | + Channels::modifyBasket('reportedDate', $reportedDate); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $title = trim($title); |
|
| 99 | + Channels::modifyBasket('title', $title); |
|
| 100 | + if (empty($title) === TRUE) { |
|
| 101 | + throw new ChannelException(_('Title cannot be empty')); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + $description = SquizRoadmap::stripTags(trim($description)); |
|
| 105 | + Channels::modifyBasket('description', $description); |
|
| 106 | + if (empty($description) === TRUE) { |
|
| 107 | + throw new ChannelException(_('Description cannot be empty')); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + try { |
|
| 111 | + DAL::beginTransaction(); |
|
| 112 | + |
|
| 113 | + $query = DAL::getDALQuery('SquizRoadmapIssue', 'addIssue'); |
|
| 114 | + DAL::executeQuery($query); |
|
| 115 | + |
|
| 116 | + // Add tags for the new issue. |
|
| 117 | + SquizRoadmapIssue::addIssueTags($issueid, $tags); |
|
| 118 | + |
|
| 119 | + // Add reporter and assignee to watch list. |
|
| 120 | + SquizRoadmapIssue::addIssueWatch($issueid, $reporter); |
|
| 121 | + |
|
| 122 | + if ($assignedTo !== NULL) { |
|
| 123 | + SquizRoadmapIssue::addIssueWatch($issueid, $assignedTo); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + SquizRoadmapIssue::clearIssueCache($issueid); |
|
| 127 | + |
|
| 128 | + DAL::commit(); |
|
| 129 | + } catch (Exception $e) { |
|
| 130 | + DAL::rollBack(); |
|
| 131 | + throw new ChannelException($e->getMessage()); |
|
| 132 | + }//end try |
|
| 133 | + |
|
| 134 | + if ($something === NULL) { |
|
| 135 | + if ($bar !== NULL) { |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $issueid; |
|
| 140 | 140 | |
| 141 | 141 | }//end addIssue() |
| 142 | 142 | |
@@ -162,21 +162,21 @@ discard block |
||
| 162 | 162 | * |
| 163 | 163 | */ |
| 164 | 164 | public static function addIssue( |
| 165 | - $title, |
|
| 166 | - $description, |
|
| 167 | - $reporter=NULL, |
|
| 168 | - $projectid=NULL, |
|
| 169 | - array $tags=array(), |
|
| 170 | - $status=NULL, |
|
| 171 | - $assignedTo=NULL, |
|
| 172 | - $reportedDate=NULL, |
|
| 173 | - $reportedMilestone=NULL |
|
| 165 | + $title, |
|
| 166 | + $description, |
|
| 167 | + $reporter=NULL, |
|
| 168 | + $projectid=NULL, |
|
| 169 | + array $tags=array(), |
|
| 170 | + $status=NULL, |
|
| 171 | + $assignedTo=NULL, |
|
| 172 | + $reportedDate=NULL, |
|
| 173 | + $reportedMilestone=NULL |
|
| 174 | 174 | ) { |
| 175 | - // Get current projectid if not specified. |
|
| 176 | - if ($projectid === NULL) { |
|
| 177 | - Channels::includeSystem('Project'); |
|
| 178 | - $projectid = Project::getCurrentProjectId(); |
|
| 179 | - Channels::modifyBasket('project', $projectid); |
|
| 180 | - } |
|
| 175 | + // Get current projectid if not specified. |
|
| 176 | + if ($projectid === NULL) { |
|
| 177 | + Channels::includeSystem('Project'); |
|
| 178 | + $projectid = Project::getCurrentProjectId(); |
|
| 179 | + Channels::modifyBasket('project', $projectid); |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | 182 | }//end addIssue() |