@@ -16,126 +16,126 @@ |
||
| 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 [T_DOUBLE_COLON]; |
|
| 27 | - |
|
| 28 | - }//end register() |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 33 | - * |
|
| 34 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 35 | - * @param int $stackPtr The position of the current token in |
|
| 36 | - * the stack passed in $tokens. |
|
| 37 | - * |
|
| 38 | - * @return void |
|
| 39 | - */ |
|
| 40 | - public function process(File $phpcsFile, $stackPtr) |
|
| 41 | - { |
|
| 42 | - $tokens = $phpcsFile->getTokens(); |
|
| 43 | - |
|
| 44 | - // Check if this is a call to includeSystem, includeAsset or includeWidget. |
|
| 45 | - $methodName = strtolower($tokens[($stackPtr + 1)]['content']); |
|
| 46 | - if ($methodName === 'includesystem' |
|
| 47 | - || $methodName === 'includeasset' |
|
| 48 | - || $methodName === 'includewidget' |
|
| 49 | - ) { |
|
| 50 | - $systemName = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 3), null, true); |
|
| 51 | - if ($systemName === false || $tokens[$systemName]['code'] !== T_CONSTANT_ENCAPSED_STRING) { |
|
| 52 | - // Must be using a variable instead of a specific system name. |
|
| 53 | - // We can't accurately check that. |
|
| 54 | - return; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $systemName = trim($tokens[$systemName]['content'], " '"); |
|
| 58 | - } else { |
|
| 59 | - return; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - if ($methodName === 'includeasset') { |
|
| 63 | - $systemName .= 'assettype'; |
|
| 64 | - } else if ($methodName === 'includewidget') { |
|
| 65 | - $systemName .= 'widgettype'; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $systemName = strtolower($systemName); |
|
| 69 | - |
|
| 70 | - // Now check if this system is used anywhere in this scope. |
|
| 71 | - $level = $tokens[$stackPtr]['level']; |
|
| 72 | - for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 73 | - if ($tokens[$i]['level'] < $level) { |
|
| 74 | - // We have gone out of scope. |
|
| 75 | - // If the original include was inside an IF statement that |
|
| 76 | - // is checking if the system exists, check the outer scope |
|
| 77 | - // as well. |
|
| 78 | - if ($tokens[$stackPtr]['level'] === $level) { |
|
| 79 | - // We are still in the base level, so this is the first |
|
| 80 | - // time we have got here. |
|
| 81 | - $conditions = array_keys($tokens[$stackPtr]['conditions']); |
|
| 82 | - if (empty($conditions) === false) { |
|
| 83 | - $cond = array_pop($conditions); |
|
| 84 | - if ($tokens[$cond]['code'] === T_IF) { |
|
| 85 | - $i = $tokens[$cond]['scope_closer']; |
|
| 86 | - $level--; |
|
| 87 | - continue; |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - break; |
|
| 93 | - }//end if |
|
| 94 | - |
|
| 95 | - if ($tokens[$i]['code'] !== T_DOUBLE_COLON |
|
| 96 | - && $tokens[$i]['code'] !== T_EXTENDS |
|
| 97 | - && $tokens[$i]['code'] !== T_IMPLEMENTS |
|
| 98 | - ) { |
|
| 99 | - continue; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - switch ($tokens[$i]['code']) { |
|
| 103 | - case T_DOUBLE_COLON: |
|
| 104 | - $usedName = strtolower($tokens[($i - 1)]['content']); |
|
| 105 | - if ($usedName === $systemName) { |
|
| 106 | - // The included system was used, so it is fine. |
|
| 107 | - return; |
|
| 108 | - } |
|
| 109 | - break; |
|
| 110 | - case T_EXTENDS: |
|
| 111 | - $classNameToken = $phpcsFile->findNext(T_STRING, ($i + 1)); |
|
| 112 | - $className = strtolower($tokens[$classNameToken]['content']); |
|
| 113 | - if ($className === $systemName) { |
|
| 114 | - // The included system was used, so it is fine. |
|
| 115 | - return; |
|
| 116 | - } |
|
| 117 | - break; |
|
| 118 | - case T_IMPLEMENTS: |
|
| 119 | - $endImplements = $phpcsFile->findNext([T_EXTENDS, T_OPEN_CURLY_BRACKET], ($i + 1)); |
|
| 120 | - for ($x = ($i + 1); $x < $endImplements; $x++) { |
|
| 121 | - if ($tokens[$x]['code'] === T_STRING) { |
|
| 122 | - $className = strtolower($tokens[$x]['content']); |
|
| 123 | - if ($className === $systemName) { |
|
| 124 | - // The included system was used, so it is fine. |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - break; |
|
| 130 | - }//end switch |
|
| 131 | - }//end for |
|
| 132 | - |
|
| 133 | - // If we get to here, the system was not use. |
|
| 134 | - $error = 'Included system "%s" is never used'; |
|
| 135 | - $data = [$systemName]; |
|
| 136 | - $phpcsFile->addError($error, $stackPtr, 'Found', $data); |
|
| 137 | - |
|
| 138 | - }//end process() |
|
| 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 [T_DOUBLE_COLON]; |
|
| 27 | + |
|
| 28 | + }//end register() |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 33 | + * |
|
| 34 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 35 | + * @param int $stackPtr The position of the current token in |
|
| 36 | + * the stack passed in $tokens. |
|
| 37 | + * |
|
| 38 | + * @return void |
|
| 39 | + */ |
|
| 40 | + public function process(File $phpcsFile, $stackPtr) |
|
| 41 | + { |
|
| 42 | + $tokens = $phpcsFile->getTokens(); |
|
| 43 | + |
|
| 44 | + // Check if this is a call to includeSystem, includeAsset or includeWidget. |
|
| 45 | + $methodName = strtolower($tokens[($stackPtr + 1)]['content']); |
|
| 46 | + if ($methodName === 'includesystem' |
|
| 47 | + || $methodName === 'includeasset' |
|
| 48 | + || $methodName === 'includewidget' |
|
| 49 | + ) { |
|
| 50 | + $systemName = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 3), null, true); |
|
| 51 | + if ($systemName === false || $tokens[$systemName]['code'] !== T_CONSTANT_ENCAPSED_STRING) { |
|
| 52 | + // Must be using a variable instead of a specific system name. |
|
| 53 | + // We can't accurately check that. |
|
| 54 | + return; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $systemName = trim($tokens[$systemName]['content'], " '"); |
|
| 58 | + } else { |
|
| 59 | + return; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + if ($methodName === 'includeasset') { |
|
| 63 | + $systemName .= 'assettype'; |
|
| 64 | + } else if ($methodName === 'includewidget') { |
|
| 65 | + $systemName .= 'widgettype'; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $systemName = strtolower($systemName); |
|
| 69 | + |
|
| 70 | + // Now check if this system is used anywhere in this scope. |
|
| 71 | + $level = $tokens[$stackPtr]['level']; |
|
| 72 | + for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 73 | + if ($tokens[$i]['level'] < $level) { |
|
| 74 | + // We have gone out of scope. |
|
| 75 | + // If the original include was inside an IF statement that |
|
| 76 | + // is checking if the system exists, check the outer scope |
|
| 77 | + // as well. |
|
| 78 | + if ($tokens[$stackPtr]['level'] === $level) { |
|
| 79 | + // We are still in the base level, so this is the first |
|
| 80 | + // time we have got here. |
|
| 81 | + $conditions = array_keys($tokens[$stackPtr]['conditions']); |
|
| 82 | + if (empty($conditions) === false) { |
|
| 83 | + $cond = array_pop($conditions); |
|
| 84 | + if ($tokens[$cond]['code'] === T_IF) { |
|
| 85 | + $i = $tokens[$cond]['scope_closer']; |
|
| 86 | + $level--; |
|
| 87 | + continue; |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + break; |
|
| 93 | + }//end if |
|
| 94 | + |
|
| 95 | + if ($tokens[$i]['code'] !== T_DOUBLE_COLON |
|
| 96 | + && $tokens[$i]['code'] !== T_EXTENDS |
|
| 97 | + && $tokens[$i]['code'] !== T_IMPLEMENTS |
|
| 98 | + ) { |
|
| 99 | + continue; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + switch ($tokens[$i]['code']) { |
|
| 103 | + case T_DOUBLE_COLON: |
|
| 104 | + $usedName = strtolower($tokens[($i - 1)]['content']); |
|
| 105 | + if ($usedName === $systemName) { |
|
| 106 | + // The included system was used, so it is fine. |
|
| 107 | + return; |
|
| 108 | + } |
|
| 109 | + break; |
|
| 110 | + case T_EXTENDS: |
|
| 111 | + $classNameToken = $phpcsFile->findNext(T_STRING, ($i + 1)); |
|
| 112 | + $className = strtolower($tokens[$classNameToken]['content']); |
|
| 113 | + if ($className === $systemName) { |
|
| 114 | + // The included system was used, so it is fine. |
|
| 115 | + return; |
|
| 116 | + } |
|
| 117 | + break; |
|
| 118 | + case T_IMPLEMENTS: |
|
| 119 | + $endImplements = $phpcsFile->findNext([T_EXTENDS, T_OPEN_CURLY_BRACKET], ($i + 1)); |
|
| 120 | + for ($x = ($i + 1); $x < $endImplements; $x++) { |
|
| 121 | + if ($tokens[$x]['code'] === T_STRING) { |
|
| 122 | + $className = strtolower($tokens[$x]['content']); |
|
| 123 | + if ($className === $systemName) { |
|
| 124 | + // The included system was used, so it is fine. |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + break; |
|
| 130 | + }//end switch |
|
| 131 | + }//end for |
|
| 132 | + |
|
| 133 | + // If we get to here, the system was not use. |
|
| 134 | + $error = 'Included system "%s" is never used'; |
|
| 135 | + $data = [$systemName]; |
|
| 136 | + $phpcsFile->addError($error, $stackPtr, 'Found', $data); |
|
| 137 | + |
|
| 138 | + }//end process() |
|
| 139 | 139 | |
| 140 | 140 | |
| 141 | 141 | }//end class |
@@ -100,33 +100,33 @@ |
||
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | switch ($tokens[$i]['code']) { |
| 103 | - case T_DOUBLE_COLON: |
|
| 104 | - $usedName = strtolower($tokens[($i - 1)]['content']); |
|
| 105 | - if ($usedName === $systemName) { |
|
| 106 | - // The included system was used, so it is fine. |
|
| 107 | - return; |
|
| 108 | - } |
|
| 109 | - break; |
|
| 110 | - case T_EXTENDS: |
|
| 111 | - $classNameToken = $phpcsFile->findNext(T_STRING, ($i + 1)); |
|
| 112 | - $className = strtolower($tokens[$classNameToken]['content']); |
|
| 113 | - if ($className === $systemName) { |
|
| 114 | - // The included system was used, so it is fine. |
|
| 115 | - return; |
|
| 116 | - } |
|
| 117 | - break; |
|
| 118 | - case T_IMPLEMENTS: |
|
| 119 | - $endImplements = $phpcsFile->findNext([T_EXTENDS, T_OPEN_CURLY_BRACKET], ($i + 1)); |
|
| 120 | - for ($x = ($i + 1); $x < $endImplements; $x++) { |
|
| 121 | - if ($tokens[$x]['code'] === T_STRING) { |
|
| 122 | - $className = strtolower($tokens[$x]['content']); |
|
| 123 | - if ($className === $systemName) { |
|
| 124 | - // The included system was used, so it is fine. |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - break; |
|
| 103 | + case T_DOUBLE_COLON: |
|
| 104 | + $usedName = strtolower($tokens[($i - 1)]['content']); |
|
| 105 | + if ($usedName === $systemName) { |
|
| 106 | + // The included system was used, so it is fine. |
|
| 107 | + return; |
|
| 108 | + } |
|
| 109 | + break; |
|
| 110 | + case T_EXTENDS: |
|
| 111 | + $classNameToken = $phpcsFile->findNext(T_STRING, ($i + 1)); |
|
| 112 | + $className = strtolower($tokens[$classNameToken]['content']); |
|
| 113 | + if ($className === $systemName) { |
|
| 114 | + // The included system was used, so it is fine. |
|
| 115 | + return; |
|
| 116 | + } |
|
| 117 | + break; |
|
| 118 | + case T_IMPLEMENTS: |
|
| 119 | + $endImplements = $phpcsFile->findNext([T_EXTENDS, T_OPEN_CURLY_BRACKET], ($i + 1)); |
|
| 120 | + for ($x = ($i + 1); $x < $endImplements; $x++) { |
|
| 121 | + if ($tokens[$x]['code'] === T_STRING) { |
|
| 122 | + $className = strtolower($tokens[$x]['content']); |
|
| 123 | + if ($className === $systemName) { |
|
| 124 | + // The included system was used, so it is fine. |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + break; |
|
| 130 | 130 | }//end switch |
| 131 | 131 | }//end for |
| 132 | 132 | |
@@ -16,83 +16,83 @@ |
||
| 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 [T_DOUBLE_COLON]; |
|
| 27 | - |
|
| 28 | - }//end register() |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 33 | - * |
|
| 34 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 35 | - * @param int $stackPtr The position of the current token in |
|
| 36 | - * the stack passed in $tokens. |
|
| 37 | - * |
|
| 38 | - * @return void |
|
| 39 | - */ |
|
| 40 | - public function process(File $phpcsFile, $stackPtr) |
|
| 41 | - { |
|
| 42 | - $fileName = $phpcsFile->getFilename(); |
|
| 43 | - $matches = []; |
|
| 44 | - if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches) === 0) { |
|
| 45 | - // Not an actions file. |
|
| 46 | - return; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $ownClass = $matches[2]; |
|
| 50 | - $tokens = $phpcsFile->getTokens(); |
|
| 51 | - |
|
| 52 | - $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 2), null, false, true); |
|
| 53 | - $typeName = trim($tokens[$typeName]['content'], " '"); |
|
| 54 | - switch (strtolower($tokens[($stackPtr + 1)]['content'])) { |
|
| 55 | - case 'includesystem' : |
|
| 56 | - $included = strtolower($typeName); |
|
| 57 | - break; |
|
| 58 | - case 'includeasset' : |
|
| 59 | - $included = strtolower($typeName).'assettype'; |
|
| 60 | - break; |
|
| 61 | - case 'includewidget' : |
|
| 62 | - $included = strtolower($typeName).'widgettype'; |
|
| 63 | - break; |
|
| 64 | - default: |
|
| 65 | - return; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - if ($included === strtolower($ownClass)) { |
|
| 69 | - $error = "You do not need to include \"%s\" from within the system's own actions file"; |
|
| 70 | - $data = [$ownClass]; |
|
| 71 | - $phpcsFile->addError($error, $stackPtr, 'NotRequired', $data); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - }//end process() |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Determines the included class name from given token. |
|
| 79 | - * |
|
| 80 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found. |
|
| 81 | - * @param array $tokens The array of file tokens. |
|
| 82 | - * @param int $stackPtr The position in the tokens array of the |
|
| 83 | - * potentially included class. |
|
| 84 | - * |
|
| 85 | - * @return string |
|
| 86 | - */ |
|
| 87 | - protected function getIncludedClassFromToken( |
|
| 88 | - $phpcsFile, |
|
| 89 | - array $tokens, |
|
| 90 | - $stackPtr |
|
| 91 | - ) { |
|
| 92 | - |
|
| 93 | - return false; |
|
| 94 | - |
|
| 95 | - }//end getIncludedClassFromToken() |
|
| 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 [T_DOUBLE_COLON]; |
|
| 27 | + |
|
| 28 | + }//end register() |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 33 | + * |
|
| 34 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 35 | + * @param int $stackPtr The position of the current token in |
|
| 36 | + * the stack passed in $tokens. |
|
| 37 | + * |
|
| 38 | + * @return void |
|
| 39 | + */ |
|
| 40 | + public function process(File $phpcsFile, $stackPtr) |
|
| 41 | + { |
|
| 42 | + $fileName = $phpcsFile->getFilename(); |
|
| 43 | + $matches = []; |
|
| 44 | + if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches) === 0) { |
|
| 45 | + // Not an actions file. |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $ownClass = $matches[2]; |
|
| 50 | + $tokens = $phpcsFile->getTokens(); |
|
| 51 | + |
|
| 52 | + $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 2), null, false, true); |
|
| 53 | + $typeName = trim($tokens[$typeName]['content'], " '"); |
|
| 54 | + switch (strtolower($tokens[($stackPtr + 1)]['content'])) { |
|
| 55 | + case 'includesystem' : |
|
| 56 | + $included = strtolower($typeName); |
|
| 57 | + break; |
|
| 58 | + case 'includeasset' : |
|
| 59 | + $included = strtolower($typeName).'assettype'; |
|
| 60 | + break; |
|
| 61 | + case 'includewidget' : |
|
| 62 | + $included = strtolower($typeName).'widgettype'; |
|
| 63 | + break; |
|
| 64 | + default: |
|
| 65 | + return; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + if ($included === strtolower($ownClass)) { |
|
| 69 | + $error = "You do not need to include \"%s\" from within the system's own actions file"; |
|
| 70 | + $data = [$ownClass]; |
|
| 71 | + $phpcsFile->addError($error, $stackPtr, 'NotRequired', $data); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + }//end process() |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Determines the included class name from given token. |
|
| 79 | + * |
|
| 80 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found. |
|
| 81 | + * @param array $tokens The array of file tokens. |
|
| 82 | + * @param int $stackPtr The position in the tokens array of the |
|
| 83 | + * potentially included class. |
|
| 84 | + * |
|
| 85 | + * @return string |
|
| 86 | + */ |
|
| 87 | + protected function getIncludedClassFromToken( |
|
| 88 | + $phpcsFile, |
|
| 89 | + array $tokens, |
|
| 90 | + $stackPtr |
|
| 91 | + ) { |
|
| 92 | + |
|
| 93 | + return false; |
|
| 94 | + |
|
| 95 | + }//end getIncludedClassFromToken() |
|
| 96 | 96 | |
| 97 | 97 | |
| 98 | 98 | }//end class |
@@ -52,17 +52,17 @@ |
||
| 52 | 52 | $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 2), null, false, true); |
| 53 | 53 | $typeName = trim($tokens[$typeName]['content'], " '"); |
| 54 | 54 | switch (strtolower($tokens[($stackPtr + 1)]['content'])) { |
| 55 | - case 'includesystem' : |
|
| 56 | - $included = strtolower($typeName); |
|
| 57 | - break; |
|
| 58 | - case 'includeasset' : |
|
| 59 | - $included = strtolower($typeName).'assettype'; |
|
| 60 | - break; |
|
| 61 | - case 'includewidget' : |
|
| 62 | - $included = strtolower($typeName).'widgettype'; |
|
| 63 | - break; |
|
| 64 | - default: |
|
| 65 | - return; |
|
| 55 | + case 'includesystem' : |
|
| 56 | + $included = strtolower($typeName); |
|
| 57 | + break; |
|
| 58 | + case 'includeasset' : |
|
| 59 | + $included = strtolower($typeName).'assettype'; |
|
| 60 | + break; |
|
| 61 | + case 'includewidget' : |
|
| 62 | + $included = strtolower($typeName).'widgettype'; |
|
| 63 | + break; |
|
| 64 | + default: |
|
| 65 | + return; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | if ($included === strtolower($ownClass)) { |
@@ -15,67 +15,67 @@ |
||
| 15 | 15 | class AssignThisSniff implements Sniff |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A list of tokenizers this sniff supports. |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - public $supportedTokenizers = ['JS']; |
|
| 24 | - |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Returns an array of tokens this test wants to listen for. |
|
| 28 | - * |
|
| 29 | - * @return array |
|
| 30 | - */ |
|
| 31 | - public function register() |
|
| 32 | - { |
|
| 33 | - return [T_THIS]; |
|
| 34 | - |
|
| 35 | - }//end register() |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Processes this test, when one of its tokens is encountered. |
|
| 40 | - * |
|
| 41 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 42 | - * @param int $stackPtr The position of the current token |
|
| 43 | - * in the stack passed in $tokens. |
|
| 44 | - * |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function process(File $phpcsFile, $stackPtr) |
|
| 48 | - { |
|
| 49 | - $tokens = $phpcsFile->getTokens(); |
|
| 50 | - |
|
| 51 | - // Ignore this.something and other uses of "this" that are not |
|
| 52 | - // direct assignments. |
|
| 53 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 54 | - if ($tokens[$next]['code'] !== T_SEMICOLON) { |
|
| 55 | - if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) { |
|
| 56 | - return; |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - // Something must be assigned to "this". |
|
| 61 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
| 62 | - if ($tokens[$prev]['code'] !== T_EQUAL) { |
|
| 63 | - return; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // A variable needs to be assigned to "this". |
|
| 67 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($prev - 1), null, true); |
|
| 68 | - if ($tokens[$prev]['code'] !== T_STRING) { |
|
| 69 | - return; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // We can only assign "this" to a var called "self". |
|
| 73 | - if ($tokens[$prev]['content'] !== 'self' && $tokens[$prev]['content'] !== '_self') { |
|
| 74 | - $error = 'Keyword "this" can only be assigned to a variable called "self" or "_self"'; |
|
| 75 | - $phpcsFile->addError($error, $prev, 'NotSelf'); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - }//end process() |
|
| 18 | + /** |
|
| 19 | + * A list of tokenizers this sniff supports. |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + public $supportedTokenizers = ['JS']; |
|
| 24 | + |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Returns an array of tokens this test wants to listen for. |
|
| 28 | + * |
|
| 29 | + * @return array |
|
| 30 | + */ |
|
| 31 | + public function register() |
|
| 32 | + { |
|
| 33 | + return [T_THIS]; |
|
| 34 | + |
|
| 35 | + }//end register() |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Processes this test, when one of its tokens is encountered. |
|
| 40 | + * |
|
| 41 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 42 | + * @param int $stackPtr The position of the current token |
|
| 43 | + * in the stack passed in $tokens. |
|
| 44 | + * |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function process(File $phpcsFile, $stackPtr) |
|
| 48 | + { |
|
| 49 | + $tokens = $phpcsFile->getTokens(); |
|
| 50 | + |
|
| 51 | + // Ignore this.something and other uses of "this" that are not |
|
| 52 | + // direct assignments. |
|
| 53 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 54 | + if ($tokens[$next]['code'] !== T_SEMICOLON) { |
|
| 55 | + if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) { |
|
| 56 | + return; |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + // Something must be assigned to "this". |
|
| 61 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
| 62 | + if ($tokens[$prev]['code'] !== T_EQUAL) { |
|
| 63 | + return; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // A variable needs to be assigned to "this". |
|
| 67 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($prev - 1), null, true); |
|
| 68 | + if ($tokens[$prev]['code'] !== T_STRING) { |
|
| 69 | + return; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // We can only assign "this" to a var called "self". |
|
| 73 | + if ($tokens[$prev]['content'] !== 'self' && $tokens[$prev]['content'] !== '_self') { |
|
| 74 | + $error = 'Keyword "this" can only be assigned to a variable called "self" or "_self"'; |
|
| 75 | + $phpcsFile->addError($error, $prev, 'NotSelf'); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + }//end process() |
|
| 79 | 79 | |
| 80 | 80 | |
| 81 | 81 | }//end class |
@@ -16,44 +16,44 @@ |
||
| 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 [T_NEW]; |
|
| 27 | - |
|
| 28 | - }//end register() |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Processes this test, when one of its tokens is encountered. |
|
| 33 | - * |
|
| 34 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 35 | - * @param int $stackPtr The position of the current token |
|
| 36 | - * in the stack passed in $tokens. |
|
| 37 | - * |
|
| 38 | - * @return void |
|
| 39 | - */ |
|
| 40 | - public function process(File $phpcsFile, $stackPtr) |
|
| 41 | - { |
|
| 42 | - $tokens = $phpcsFile->getTokens(); |
|
| 43 | - |
|
| 44 | - $className = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 45 | - if ($tokens[$className]['code'] !== T_STRING) { |
|
| 46 | - return; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - if (substr(strtolower($tokens[$className]['content']), -10) === 'widgettype') { |
|
| 50 | - $widgetType = substr($tokens[$className]['content'], 0, -10); |
|
| 51 | - $error = 'Manual creation of widget objects is banned; use Widget::getWidget(\'%s\'); instead'; |
|
| 52 | - $data = [$widgetType]; |
|
| 53 | - $phpcsFile->addError($error, $stackPtr, 'Found', $data); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - }//end process() |
|
| 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 [T_NEW]; |
|
| 27 | + |
|
| 28 | + }//end register() |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Processes this test, when one of its tokens is encountered. |
|
| 33 | + * |
|
| 34 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 35 | + * @param int $stackPtr The position of the current token |
|
| 36 | + * in the stack passed in $tokens. |
|
| 37 | + * |
|
| 38 | + * @return void |
|
| 39 | + */ |
|
| 40 | + public function process(File $phpcsFile, $stackPtr) |
|
| 41 | + { |
|
| 42 | + $tokens = $phpcsFile->getTokens(); |
|
| 43 | + |
|
| 44 | + $className = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 45 | + if ($tokens[$className]['code'] !== T_STRING) { |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + if (substr(strtolower($tokens[$className]['content']), -10) === 'widgettype') { |
|
| 50 | + $widgetType = substr($tokens[$className]['content'], 0, -10); |
|
| 51 | + $error = 'Manual creation of widget objects is banned; use Widget::getWidget(\'%s\'); instead'; |
|
| 52 | + $data = [$widgetType]; |
|
| 53 | + $phpcsFile->addError($error, $stackPtr, 'Found', $data); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + }//end process() |
|
| 57 | 57 | |
| 58 | 58 | |
| 59 | 59 | }//end class |
@@ -19,66 +19,66 @@ |
||
| 19 | 19 | { |
| 20 | 20 | |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Processes this test, when one of its tokens is encountered. |
|
| 24 | - * |
|
| 25 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 26 | - * @param int $stackPtr The position of the current token |
|
| 27 | - * in the stack passed in $tokens. |
|
| 28 | - * |
|
| 29 | - * @return void |
|
| 30 | - */ |
|
| 31 | - public function process(File $phpcsFile, $stackPtr) |
|
| 32 | - { |
|
| 33 | - parent::process($phpcsFile, $stackPtr); |
|
| 22 | + /** |
|
| 23 | + * Processes this test, when one of its tokens is encountered. |
|
| 24 | + * |
|
| 25 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 26 | + * @param int $stackPtr The position of the current token |
|
| 27 | + * in the stack passed in $tokens. |
|
| 28 | + * |
|
| 29 | + * @return void |
|
| 30 | + */ |
|
| 31 | + public function process(File $phpcsFile, $stackPtr) |
|
| 32 | + { |
|
| 33 | + parent::process($phpcsFile, $stackPtr); |
|
| 34 | 34 | |
| 35 | - $tokens = $phpcsFile->getTokens(); |
|
| 36 | - $find = Tokens::$methodPrefixes; |
|
| 37 | - $find[] = T_WHITESPACE; |
|
| 35 | + $tokens = $phpcsFile->getTokens(); |
|
| 36 | + $find = Tokens::$methodPrefixes; |
|
| 37 | + $find[] = T_WHITESPACE; |
|
| 38 | 38 | |
| 39 | - $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
| 40 | - if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) { |
|
| 41 | - return; |
|
| 42 | - } |
|
| 39 | + $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
| 40 | + if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) { |
|
| 41 | + return; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - $commentStart = $tokens[$commentEnd]['comment_opener']; |
|
| 45 | - $hasApiTag = false; |
|
| 46 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 47 | - if ($tokens[$tag]['content'] === '@api') { |
|
| 48 | - if ($hasApiTag === true) { |
|
| 49 | - // We've come across an API tag already, which means |
|
| 50 | - // we were not the first tag in the API list. |
|
| 51 | - $error = 'The @api tag must come first in the @api tag list in a function comment'; |
|
| 52 | - $phpcsFile->addError($error, $tag, 'ApiNotFirst'); |
|
| 53 | - } |
|
| 44 | + $commentStart = $tokens[$commentEnd]['comment_opener']; |
|
| 45 | + $hasApiTag = false; |
|
| 46 | + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 47 | + if ($tokens[$tag]['content'] === '@api') { |
|
| 48 | + if ($hasApiTag === true) { |
|
| 49 | + // We've come across an API tag already, which means |
|
| 50 | + // we were not the first tag in the API list. |
|
| 51 | + $error = 'The @api tag must come first in the @api tag list in a function comment'; |
|
| 52 | + $phpcsFile->addError($error, $tag, 'ApiNotFirst'); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - $hasApiTag = true; |
|
| 55 | + $hasApiTag = true; |
|
| 56 | 56 | |
| 57 | - // There needs to be a blank line before the @api tag. |
|
| 58 | - $prev = $phpcsFile->findPrevious([T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG], ($tag - 1)); |
|
| 59 | - if ($tokens[$prev]['line'] !== ($tokens[$tag]['line'] - 2)) { |
|
| 60 | - $error = 'There must be one blank line before the @api tag in a function comment'; |
|
| 61 | - $phpcsFile->addError($error, $tag, 'ApiSpacing'); |
|
| 62 | - } |
|
| 63 | - } else if (substr($tokens[$tag]['content'], 0, 5) === '@api-') { |
|
| 64 | - $hasApiTag = true; |
|
| 57 | + // There needs to be a blank line before the @api tag. |
|
| 58 | + $prev = $phpcsFile->findPrevious([T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG], ($tag - 1)); |
|
| 59 | + if ($tokens[$prev]['line'] !== ($tokens[$tag]['line'] - 2)) { |
|
| 60 | + $error = 'There must be one blank line before the @api tag in a function comment'; |
|
| 61 | + $phpcsFile->addError($error, $tag, 'ApiSpacing'); |
|
| 62 | + } |
|
| 63 | + } else if (substr($tokens[$tag]['content'], 0, 5) === '@api-') { |
|
| 64 | + $hasApiTag = true; |
|
| 65 | 65 | |
| 66 | - $prev = $phpcsFile->findPrevious([T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG], ($tag - 1)); |
|
| 67 | - if ($tokens[$prev]['line'] !== ($tokens[$tag]['line'] - 1)) { |
|
| 68 | - $error = 'There must be no blank line before the @%s tag in a function comment'; |
|
| 69 | - $data = [$tokens[$tag]['content']]; |
|
| 70 | - $phpcsFile->addError($error, $tag, 'ApiTagSpacing', $data); |
|
| 71 | - } |
|
| 72 | - }//end if |
|
| 73 | - }//end foreach |
|
| 66 | + $prev = $phpcsFile->findPrevious([T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG], ($tag - 1)); |
|
| 67 | + if ($tokens[$prev]['line'] !== ($tokens[$tag]['line'] - 1)) { |
|
| 68 | + $error = 'There must be no blank line before the @%s tag in a function comment'; |
|
| 69 | + $data = [$tokens[$tag]['content']]; |
|
| 70 | + $phpcsFile->addError($error, $tag, 'ApiTagSpacing', $data); |
|
| 71 | + } |
|
| 72 | + }//end if |
|
| 73 | + }//end foreach |
|
| 74 | 74 | |
| 75 | - if ($hasApiTag === true && substr($tokens[$tag]['content'], 0, 4) !== '@api') { |
|
| 76 | - // API tags must be the last tags in a function comment. |
|
| 77 | - $error = 'The @api tags must be the last tags in a function comment'; |
|
| 78 | - $phpcsFile->addError($error, $commentEnd, 'ApiNotLast'); |
|
| 79 | - } |
|
| 75 | + if ($hasApiTag === true && substr($tokens[$tag]['content'], 0, 4) !== '@api') { |
|
| 76 | + // API tags must be the last tags in a function comment. |
|
| 77 | + $error = 'The @api tags must be the last tags in a function comment'; |
|
| 78 | + $phpcsFile->addError($error, $commentEnd, 'ApiNotLast'); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - }//end process() |
|
| 81 | + }//end process() |
|
| 82 | 82 | |
| 83 | 83 | |
| 84 | 84 | }//end class |
@@ -107,7 +107,7 @@ |
||
| 107 | 107 | * |
| 108 | 108 | * @return void |
| 109 | 109 | */ |
| 110 | - public function addFile($path, $file=null) |
|
| 110 | + public function addFile($path, $file = null) |
|
| 111 | 111 | { |
| 112 | 112 | // No filtering is done for STDIN when the filename |
| 113 | 113 | // has not been specified. |
@@ -21,235 +21,235 @@ |
||
| 21 | 21 | class FileList implements \Iterator, \Countable |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * A list of file paths that are included in the list. |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - private $files = []; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The number of files in the list. |
|
| 33 | - * |
|
| 34 | - * @var integer |
|
| 35 | - */ |
|
| 36 | - private $numFiles = 0; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The config data for the run. |
|
| 40 | - * |
|
| 41 | - * @var \PHP_CodeSniffer\Config |
|
| 42 | - */ |
|
| 43 | - public $config = null; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The ruleset used for the run. |
|
| 47 | - * |
|
| 48 | - * @var \PHP_CodeSniffer\Ruleset |
|
| 49 | - */ |
|
| 50 | - public $ruleset = null; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * An array of patterns to use for skipping files. |
|
| 54 | - * |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - protected $ignorePatterns = []; |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Constructs a file list and loads in an array of file paths to process. |
|
| 62 | - * |
|
| 63 | - * @param \PHP_CodeSniffer\Config $config The config data for the run. |
|
| 64 | - * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run. |
|
| 65 | - * |
|
| 66 | - * @return void |
|
| 67 | - */ |
|
| 68 | - public function __construct(Config $config, Ruleset $ruleset) |
|
| 69 | - { |
|
| 70 | - $this->ruleset = $ruleset; |
|
| 71 | - $this->config = $config; |
|
| 72 | - |
|
| 73 | - $paths = $config->files; |
|
| 74 | - foreach ($paths as $path) { |
|
| 75 | - $isPharFile = Util\Common::isPharFile($path); |
|
| 76 | - if (is_dir($path) === true || $isPharFile === true) { |
|
| 77 | - if ($isPharFile === true) { |
|
| 78 | - $path = 'phar://'.$path; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - $filterClass = $this->getFilterClass(); |
|
| 82 | - |
|
| 83 | - $di = new \RecursiveDirectoryIterator($path, (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS)); |
|
| 84 | - $filter = new $filterClass($di, $path, $config, $ruleset); |
|
| 85 | - $iterator = new \RecursiveIteratorIterator($filter); |
|
| 86 | - |
|
| 87 | - foreach ($iterator as $file) { |
|
| 88 | - $this->files[$file->getPathname()] = null; |
|
| 89 | - $this->numFiles++; |
|
| 90 | - } |
|
| 91 | - } else { |
|
| 92 | - $this->addFile($path); |
|
| 93 | - }//end if |
|
| 94 | - }//end foreach |
|
| 95 | - |
|
| 96 | - reset($this->files); |
|
| 97 | - |
|
| 98 | - }//end __construct() |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Add a file to the list. |
|
| 103 | - * |
|
| 104 | - * If a file object has already been created, it can be passed here. |
|
| 105 | - * If it is left NULL, it will be created when accessed. |
|
| 106 | - * |
|
| 107 | - * @param string $path The path to the file being added. |
|
| 108 | - * @param \PHP_CodeSniffer\Files\File $file The file being added. |
|
| 109 | - * |
|
| 110 | - * @return void |
|
| 111 | - */ |
|
| 112 | - public function addFile($path, $file=null) |
|
| 113 | - { |
|
| 114 | - // No filtering is done for STDIN when the filename |
|
| 115 | - // has not been specified. |
|
| 116 | - if ($path === 'STDIN') { |
|
| 117 | - $this->files[$path] = $file; |
|
| 118 | - $this->numFiles++; |
|
| 119 | - return; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $filterClass = $this->getFilterClass(); |
|
| 123 | - |
|
| 124 | - $di = new \RecursiveArrayIterator([$path]); |
|
| 125 | - $filter = new $filterClass($di, $path, $this->config, $this->ruleset); |
|
| 126 | - $iterator = new \RecursiveIteratorIterator($filter); |
|
| 127 | - |
|
| 128 | - foreach ($iterator as $path) { |
|
| 129 | - $this->files[$path] = $file; |
|
| 130 | - $this->numFiles++; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - }//end addFile() |
|
| 134 | - |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Get the class name of the filter being used for the run. |
|
| 138 | - * |
|
| 139 | - * @return string |
|
| 140 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the specified filter could not be found. |
|
| 141 | - */ |
|
| 142 | - private function getFilterClass() |
|
| 143 | - { |
|
| 144 | - $filterType = $this->config->filter; |
|
| 145 | - |
|
| 146 | - if ($filterType === null) { |
|
| 147 | - $filterClass = '\PHP_CodeSniffer\Filters\Filter'; |
|
| 148 | - } else { |
|
| 149 | - if (strpos($filterType, '.') !== false) { |
|
| 150 | - // This is a path to a custom filter class. |
|
| 151 | - $filename = realpath($filterType); |
|
| 152 | - if ($filename === false) { |
|
| 153 | - $error = "ERROR: Custom filter \"$filterType\" not found".PHP_EOL; |
|
| 154 | - throw new DeepExitException($error, 3); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $filterClass = Autoload::loadFile($filename); |
|
| 158 | - } else { |
|
| 159 | - $filterClass = '\PHP_CodeSniffer\Filters\\'.$filterType; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return $filterClass; |
|
| 164 | - |
|
| 165 | - }//end getFilterClass() |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Rewind the iterator to the first file. |
|
| 170 | - * |
|
| 171 | - * @return void |
|
| 172 | - */ |
|
| 173 | - #[ReturnTypeWillChange] |
|
| 174 | - public function rewind() |
|
| 175 | - { |
|
| 176 | - reset($this->files); |
|
| 177 | - |
|
| 178 | - }//end rewind() |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Get the file that is currently being processed. |
|
| 183 | - * |
|
| 184 | - * @return \PHP_CodeSniffer\Files\File |
|
| 185 | - */ |
|
| 186 | - #[ReturnTypeWillChange] |
|
| 187 | - public function current() |
|
| 188 | - { |
|
| 189 | - $path = key($this->files); |
|
| 190 | - if (isset($this->files[$path]) === false) { |
|
| 191 | - $this->files[$path] = new LocalFile($path, $this->ruleset, $this->config); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - return $this->files[$path]; |
|
| 195 | - |
|
| 196 | - }//end current() |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Return the file path of the current file being processed. |
|
| 201 | - * |
|
| 202 | - * @return void |
|
| 203 | - */ |
|
| 204 | - #[ReturnTypeWillChange] |
|
| 205 | - public function key() |
|
| 206 | - { |
|
| 207 | - return key($this->files); |
|
| 208 | - |
|
| 209 | - }//end key() |
|
| 210 | - |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Move forward to the next file. |
|
| 214 | - * |
|
| 215 | - * @return void |
|
| 216 | - */ |
|
| 217 | - #[ReturnTypeWillChange] |
|
| 218 | - public function next() |
|
| 219 | - { |
|
| 220 | - next($this->files); |
|
| 221 | - |
|
| 222 | - }//end next() |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Checks if current position is valid. |
|
| 227 | - * |
|
| 228 | - * @return boolean |
|
| 229 | - */ |
|
| 230 | - #[ReturnTypeWillChange] |
|
| 231 | - public function valid() |
|
| 232 | - { |
|
| 233 | - if (current($this->files) === false) { |
|
| 234 | - return false; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return true; |
|
| 238 | - |
|
| 239 | - }//end valid() |
|
| 240 | - |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Return the number of files in the list. |
|
| 244 | - * |
|
| 245 | - * @return integer |
|
| 246 | - */ |
|
| 247 | - #[ReturnTypeWillChange] |
|
| 248 | - public function count() |
|
| 249 | - { |
|
| 250 | - return $this->numFiles; |
|
| 251 | - |
|
| 252 | - }//end count() |
|
| 24 | + /** |
|
| 25 | + * A list of file paths that are included in the list. |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + private $files = []; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The number of files in the list. |
|
| 33 | + * |
|
| 34 | + * @var integer |
|
| 35 | + */ |
|
| 36 | + private $numFiles = 0; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The config data for the run. |
|
| 40 | + * |
|
| 41 | + * @var \PHP_CodeSniffer\Config |
|
| 42 | + */ |
|
| 43 | + public $config = null; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The ruleset used for the run. |
|
| 47 | + * |
|
| 48 | + * @var \PHP_CodeSniffer\Ruleset |
|
| 49 | + */ |
|
| 50 | + public $ruleset = null; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * An array of patterns to use for skipping files. |
|
| 54 | + * |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + protected $ignorePatterns = []; |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Constructs a file list and loads in an array of file paths to process. |
|
| 62 | + * |
|
| 63 | + * @param \PHP_CodeSniffer\Config $config The config data for the run. |
|
| 64 | + * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run. |
|
| 65 | + * |
|
| 66 | + * @return void |
|
| 67 | + */ |
|
| 68 | + public function __construct(Config $config, Ruleset $ruleset) |
|
| 69 | + { |
|
| 70 | + $this->ruleset = $ruleset; |
|
| 71 | + $this->config = $config; |
|
| 72 | + |
|
| 73 | + $paths = $config->files; |
|
| 74 | + foreach ($paths as $path) { |
|
| 75 | + $isPharFile = Util\Common::isPharFile($path); |
|
| 76 | + if (is_dir($path) === true || $isPharFile === true) { |
|
| 77 | + if ($isPharFile === true) { |
|
| 78 | + $path = 'phar://'.$path; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + $filterClass = $this->getFilterClass(); |
|
| 82 | + |
|
| 83 | + $di = new \RecursiveDirectoryIterator($path, (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS)); |
|
| 84 | + $filter = new $filterClass($di, $path, $config, $ruleset); |
|
| 85 | + $iterator = new \RecursiveIteratorIterator($filter); |
|
| 86 | + |
|
| 87 | + foreach ($iterator as $file) { |
|
| 88 | + $this->files[$file->getPathname()] = null; |
|
| 89 | + $this->numFiles++; |
|
| 90 | + } |
|
| 91 | + } else { |
|
| 92 | + $this->addFile($path); |
|
| 93 | + }//end if |
|
| 94 | + }//end foreach |
|
| 95 | + |
|
| 96 | + reset($this->files); |
|
| 97 | + |
|
| 98 | + }//end __construct() |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Add a file to the list. |
|
| 103 | + * |
|
| 104 | + * If a file object has already been created, it can be passed here. |
|
| 105 | + * If it is left NULL, it will be created when accessed. |
|
| 106 | + * |
|
| 107 | + * @param string $path The path to the file being added. |
|
| 108 | + * @param \PHP_CodeSniffer\Files\File $file The file being added. |
|
| 109 | + * |
|
| 110 | + * @return void |
|
| 111 | + */ |
|
| 112 | + public function addFile($path, $file=null) |
|
| 113 | + { |
|
| 114 | + // No filtering is done for STDIN when the filename |
|
| 115 | + // has not been specified. |
|
| 116 | + if ($path === 'STDIN') { |
|
| 117 | + $this->files[$path] = $file; |
|
| 118 | + $this->numFiles++; |
|
| 119 | + return; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $filterClass = $this->getFilterClass(); |
|
| 123 | + |
|
| 124 | + $di = new \RecursiveArrayIterator([$path]); |
|
| 125 | + $filter = new $filterClass($di, $path, $this->config, $this->ruleset); |
|
| 126 | + $iterator = new \RecursiveIteratorIterator($filter); |
|
| 127 | + |
|
| 128 | + foreach ($iterator as $path) { |
|
| 129 | + $this->files[$path] = $file; |
|
| 130 | + $this->numFiles++; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + }//end addFile() |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Get the class name of the filter being used for the run. |
|
| 138 | + * |
|
| 139 | + * @return string |
|
| 140 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the specified filter could not be found. |
|
| 141 | + */ |
|
| 142 | + private function getFilterClass() |
|
| 143 | + { |
|
| 144 | + $filterType = $this->config->filter; |
|
| 145 | + |
|
| 146 | + if ($filterType === null) { |
|
| 147 | + $filterClass = '\PHP_CodeSniffer\Filters\Filter'; |
|
| 148 | + } else { |
|
| 149 | + if (strpos($filterType, '.') !== false) { |
|
| 150 | + // This is a path to a custom filter class. |
|
| 151 | + $filename = realpath($filterType); |
|
| 152 | + if ($filename === false) { |
|
| 153 | + $error = "ERROR: Custom filter \"$filterType\" not found".PHP_EOL; |
|
| 154 | + throw new DeepExitException($error, 3); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $filterClass = Autoload::loadFile($filename); |
|
| 158 | + } else { |
|
| 159 | + $filterClass = '\PHP_CodeSniffer\Filters\\'.$filterType; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return $filterClass; |
|
| 164 | + |
|
| 165 | + }//end getFilterClass() |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Rewind the iterator to the first file. |
|
| 170 | + * |
|
| 171 | + * @return void |
|
| 172 | + */ |
|
| 173 | + #[ReturnTypeWillChange] |
|
| 174 | + public function rewind() |
|
| 175 | + { |
|
| 176 | + reset($this->files); |
|
| 177 | + |
|
| 178 | + }//end rewind() |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Get the file that is currently being processed. |
|
| 183 | + * |
|
| 184 | + * @return \PHP_CodeSniffer\Files\File |
|
| 185 | + */ |
|
| 186 | + #[ReturnTypeWillChange] |
|
| 187 | + public function current() |
|
| 188 | + { |
|
| 189 | + $path = key($this->files); |
|
| 190 | + if (isset($this->files[$path]) === false) { |
|
| 191 | + $this->files[$path] = new LocalFile($path, $this->ruleset, $this->config); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + return $this->files[$path]; |
|
| 195 | + |
|
| 196 | + }//end current() |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Return the file path of the current file being processed. |
|
| 201 | + * |
|
| 202 | + * @return void |
|
| 203 | + */ |
|
| 204 | + #[ReturnTypeWillChange] |
|
| 205 | + public function key() |
|
| 206 | + { |
|
| 207 | + return key($this->files); |
|
| 208 | + |
|
| 209 | + }//end key() |
|
| 210 | + |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Move forward to the next file. |
|
| 214 | + * |
|
| 215 | + * @return void |
|
| 216 | + */ |
|
| 217 | + #[ReturnTypeWillChange] |
|
| 218 | + public function next() |
|
| 219 | + { |
|
| 220 | + next($this->files); |
|
| 221 | + |
|
| 222 | + }//end next() |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Checks if current position is valid. |
|
| 227 | + * |
|
| 228 | + * @return boolean |
|
| 229 | + */ |
|
| 230 | + #[ReturnTypeWillChange] |
|
| 231 | + public function valid() |
|
| 232 | + { |
|
| 233 | + if (current($this->files) === false) { |
|
| 234 | + return false; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return true; |
|
| 238 | + |
|
| 239 | + }//end valid() |
|
| 240 | + |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Return the number of files in the list. |
|
| 244 | + * |
|
| 245 | + * @return integer |
|
| 246 | + */ |
|
| 247 | + #[ReturnTypeWillChange] |
|
| 248 | + public function count() |
|
| 249 | + { |
|
| 250 | + return $this->numFiles; |
|
| 251 | + |
|
| 252 | + }//end count() |
|
| 253 | 253 | |
| 254 | 254 | |
| 255 | 255 | }//end class |
@@ -50,7 +50,7 @@ |
||
| 50 | 50 | do { |
| 51 | 51 | $modified[$path] = true; |
| 52 | 52 | $path = dirname($path); |
| 53 | - } while ($path !== $basedir); |
|
| 53 | + }while ($path !== $basedir); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | return $modified; |
@@ -15,52 +15,52 @@ |
||
| 15 | 15 | { |
| 16 | 16 | |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Get a list of blacklisted file paths. |
|
| 20 | - * |
|
| 21 | - * @return array |
|
| 22 | - */ |
|
| 23 | - protected function getBlacklist() |
|
| 24 | - { |
|
| 25 | - return []; |
|
| 26 | - |
|
| 27 | - }//end getBlacklist() |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Get a list of whitelisted file paths. |
|
| 32 | - * |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - protected function getWhitelist() |
|
| 36 | - { |
|
| 37 | - $modified = []; |
|
| 38 | - |
|
| 39 | - $cmd = 'git ls-files -o -m --exclude-standard -- '.escapeshellarg($this->basedir); |
|
| 40 | - $output = []; |
|
| 41 | - exec($cmd, $output); |
|
| 42 | - |
|
| 43 | - $basedir = $this->basedir; |
|
| 44 | - if (is_dir($basedir) === false) { |
|
| 45 | - $basedir = dirname($basedir); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - foreach ($output as $path) { |
|
| 49 | - $path = Util\Common::realpath($path); |
|
| 50 | - |
|
| 51 | - if ($path === false) { |
|
| 52 | - continue; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - do { |
|
| 56 | - $modified[$path] = true; |
|
| 57 | - $path = dirname($path); |
|
| 58 | - } while ($path !== $basedir); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - return $modified; |
|
| 62 | - |
|
| 63 | - }//end getWhitelist() |
|
| 18 | + /** |
|
| 19 | + * Get a list of blacklisted file paths. |
|
| 20 | + * |
|
| 21 | + * @return array |
|
| 22 | + */ |
|
| 23 | + protected function getBlacklist() |
|
| 24 | + { |
|
| 25 | + return []; |
|
| 26 | + |
|
| 27 | + }//end getBlacklist() |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Get a list of whitelisted file paths. |
|
| 32 | + * |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + protected function getWhitelist() |
|
| 36 | + { |
|
| 37 | + $modified = []; |
|
| 38 | + |
|
| 39 | + $cmd = 'git ls-files -o -m --exclude-standard -- '.escapeshellarg($this->basedir); |
|
| 40 | + $output = []; |
|
| 41 | + exec($cmd, $output); |
|
| 42 | + |
|
| 43 | + $basedir = $this->basedir; |
|
| 44 | + if (is_dir($basedir) === false) { |
|
| 45 | + $basedir = dirname($basedir); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + foreach ($output as $path) { |
|
| 49 | + $path = Util\Common::realpath($path); |
|
| 50 | + |
|
| 51 | + if ($path === false) { |
|
| 52 | + continue; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + do { |
|
| 56 | + $modified[$path] = true; |
|
| 57 | + $path = dirname($path); |
|
| 58 | + } while ($path !== $basedir); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + return $modified; |
|
| 62 | + |
|
| 63 | + }//end getWhitelist() |
|
| 64 | 64 | |
| 65 | 65 | |
| 66 | 66 | }//end class |
@@ -16,93 +16,93 @@ |
||
| 16 | 16 | abstract class ExactMatch extends Filter |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A list of files to exclude. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - private $blacklist = null; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * A list of files to include. |
|
| 28 | - * |
|
| 29 | - * If the whitelist is empty, only files in the blacklist will be excluded. |
|
| 30 | - * |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private $whitelist = null; |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Check whether the current element of the iterator is acceptable. |
|
| 38 | - * |
|
| 39 | - * If a file is both blacklisted and whitelisted, it will be deemed unacceptable. |
|
| 40 | - * |
|
| 41 | - * @return bool |
|
| 42 | - */ |
|
| 43 | - public function accept() |
|
| 44 | - { |
|
| 45 | - if (parent::accept() === false) { |
|
| 46 | - return false; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - if ($this->blacklist === null) { |
|
| 50 | - $this->blacklist = $this->getblacklist(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - if ($this->whitelist === null) { |
|
| 54 | - $this->whitelist = $this->getwhitelist(); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $filePath = Util\Common::realpath($this->current()); |
|
| 58 | - |
|
| 59 | - // If file is both blacklisted and whitelisted, the blacklist takes precedence. |
|
| 60 | - if (isset($this->blacklist[$filePath]) === true) { |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - if (empty($this->whitelist) === true && empty($this->blacklist) === false) { |
|
| 65 | - // We are only checking a blacklist, so everything else should be whitelisted. |
|
| 66 | - return true; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return isset($this->whitelist[$filePath]); |
|
| 70 | - |
|
| 71 | - }//end accept() |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Returns an iterator for the current entry. |
|
| 76 | - * |
|
| 77 | - * Ensures that the blacklist and whitelist are preserved so they don't have |
|
| 78 | - * to be generated each time. |
|
| 79 | - * |
|
| 80 | - * @return \RecursiveIterator |
|
| 81 | - */ |
|
| 82 | - public function getChildren() |
|
| 83 | - { |
|
| 84 | - $children = parent::getChildren(); |
|
| 85 | - $children->blacklist = $this->blacklist; |
|
| 86 | - $children->whitelist = $this->whitelist; |
|
| 87 | - return $children; |
|
| 88 | - |
|
| 89 | - }//end getChildren() |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Get a list of blacklisted file paths. |
|
| 94 | - * |
|
| 95 | - * @return array |
|
| 96 | - */ |
|
| 97 | - abstract protected function getBlacklist(); |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get a list of whitelisted file paths. |
|
| 102 | - * |
|
| 103 | - * @return array |
|
| 104 | - */ |
|
| 105 | - abstract protected function getWhitelist(); |
|
| 19 | + /** |
|
| 20 | + * A list of files to exclude. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + private $blacklist = null; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * A list of files to include. |
|
| 28 | + * |
|
| 29 | + * If the whitelist is empty, only files in the blacklist will be excluded. |
|
| 30 | + * |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private $whitelist = null; |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Check whether the current element of the iterator is acceptable. |
|
| 38 | + * |
|
| 39 | + * If a file is both blacklisted and whitelisted, it will be deemed unacceptable. |
|
| 40 | + * |
|
| 41 | + * @return bool |
|
| 42 | + */ |
|
| 43 | + public function accept() |
|
| 44 | + { |
|
| 45 | + if (parent::accept() === false) { |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + if ($this->blacklist === null) { |
|
| 50 | + $this->blacklist = $this->getblacklist(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + if ($this->whitelist === null) { |
|
| 54 | + $this->whitelist = $this->getwhitelist(); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $filePath = Util\Common::realpath($this->current()); |
|
| 58 | + |
|
| 59 | + // If file is both blacklisted and whitelisted, the blacklist takes precedence. |
|
| 60 | + if (isset($this->blacklist[$filePath]) === true) { |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + if (empty($this->whitelist) === true && empty($this->blacklist) === false) { |
|
| 65 | + // We are only checking a blacklist, so everything else should be whitelisted. |
|
| 66 | + return true; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return isset($this->whitelist[$filePath]); |
|
| 70 | + |
|
| 71 | + }//end accept() |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Returns an iterator for the current entry. |
|
| 76 | + * |
|
| 77 | + * Ensures that the blacklist and whitelist are preserved so they don't have |
|
| 78 | + * to be generated each time. |
|
| 79 | + * |
|
| 80 | + * @return \RecursiveIterator |
|
| 81 | + */ |
|
| 82 | + public function getChildren() |
|
| 83 | + { |
|
| 84 | + $children = parent::getChildren(); |
|
| 85 | + $children->blacklist = $this->blacklist; |
|
| 86 | + $children->whitelist = $this->whitelist; |
|
| 87 | + return $children; |
|
| 88 | + |
|
| 89 | + }//end getChildren() |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Get a list of blacklisted file paths. |
|
| 94 | + * |
|
| 95 | + * @return array |
|
| 96 | + */ |
|
| 97 | + abstract protected function getBlacklist(); |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get a list of whitelisted file paths. |
|
| 102 | + * |
|
| 103 | + * @return array |
|
| 104 | + */ |
|
| 105 | + abstract protected function getWhitelist(); |
|
| 106 | 106 | |
| 107 | 107 | |
| 108 | 108 | }//end class |
@@ -632,19 +632,19 @@ |
||
| 632 | 632 | $input = trim($input); |
| 633 | 633 | |
| 634 | 634 | switch ($input) { |
| 635 | - case 's': |
|
| 636 | - break(2); |
|
| 637 | - case 'q': |
|
| 638 | - throw new DeepExitException('', 0); |
|
| 639 | - default: |
|
| 640 | - // Repopulate the sniffs because some of them save their state |
|
| 641 | - // and only clear it when the file changes, but we are rechecking |
|
| 642 | - // the same file. |
|
| 643 | - $file->ruleset->populateTokenListeners(); |
|
| 644 | - $file->reloadContent(); |
|
| 645 | - $file->process(); |
|
| 646 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 647 | - break; |
|
| 635 | + case 's': |
|
| 636 | + break(2); |
|
| 637 | + case 'q': |
|
| 638 | + throw new DeepExitException('', 0); |
|
| 639 | + default: |
|
| 640 | + // Repopulate the sniffs because some of them save their state |
|
| 641 | + // and only clear it when the file changes, but we are rechecking |
|
| 642 | + // the same file. |
|
| 643 | + $file->ruleset->populateTokenListeners(); |
|
| 644 | + $file->reloadContent(); |
|
| 645 | + $file->process(); |
|
| 646 | + $this->reporter->cacheFileReport($file, $this->config); |
|
| 647 | + break; |
|
| 648 | 648 | } |
| 649 | 649 | }//end while |
| 650 | 650 | }//end if |
@@ -248,7 +248,7 @@ |
||
| 248 | 248 | 'xmlwriter', |
| 249 | 249 | 'SimpleXML', |
| 250 | 250 | ]; |
| 251 | - $missingExtensions = []; |
|
| 251 | + $missingExtensions = []; |
|
| 252 | 252 | |
| 253 | 253 | foreach ($requiredExtensions as $extension) { |
| 254 | 254 | if (extension_loaded($extension) === false) { |
@@ -24,866 +24,866 @@ |
||
| 24 | 24 | class Runner |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * The config data for the run. |
|
| 29 | - * |
|
| 30 | - * @var \PHP_CodeSniffer\Config |
|
| 31 | - */ |
|
| 32 | - public $config = null; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The ruleset used for the run. |
|
| 36 | - * |
|
| 37 | - * @var \PHP_CodeSniffer\Ruleset |
|
| 38 | - */ |
|
| 39 | - public $ruleset = null; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The reporter used for generating reports after the run. |
|
| 43 | - * |
|
| 44 | - * @var \PHP_CodeSniffer\Reporter |
|
| 45 | - */ |
|
| 46 | - public $reporter = null; |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Run the PHPCS script. |
|
| 51 | - * |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function runPHPCS() |
|
| 55 | - { |
|
| 56 | - try { |
|
| 57 | - Util\Timing::startTiming(); |
|
| 58 | - Runner::checkRequirements(); |
|
| 59 | - |
|
| 60 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 61 | - define('PHP_CODESNIFFER_CBF', false); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // Creating the Config object populates it with all required settings |
|
| 65 | - // based on the CLI arguments provided to the script and any config |
|
| 66 | - // values the user has set. |
|
| 67 | - $this->config = new Config(); |
|
| 68 | - |
|
| 69 | - // Init the run and load the rulesets to set additional config vars. |
|
| 70 | - $this->init(); |
|
| 71 | - |
|
| 72 | - // Print a list of sniffs in each of the supplied standards. |
|
| 73 | - // We fudge the config here so that each standard is explained in isolation. |
|
| 74 | - if ($this->config->explain === true) { |
|
| 75 | - $standards = $this->config->standards; |
|
| 76 | - foreach ($standards as $standard) { |
|
| 77 | - $this->config->standards = [$standard]; |
|
| 78 | - $ruleset = new Ruleset($this->config); |
|
| 79 | - $ruleset->explain(); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return 0; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // Generate documentation for each of the supplied standards. |
|
| 86 | - if ($this->config->generator !== null) { |
|
| 87 | - $standards = $this->config->standards; |
|
| 88 | - foreach ($standards as $standard) { |
|
| 89 | - $this->config->standards = [$standard]; |
|
| 90 | - $ruleset = new Ruleset($this->config); |
|
| 91 | - $class = 'PHP_CodeSniffer\Generators\\'.$this->config->generator; |
|
| 92 | - $generator = new $class($ruleset); |
|
| 93 | - $generator->generate(); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - return 0; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // Other report formats don't really make sense in interactive mode |
|
| 100 | - // so we hard-code the full report here and when outputting. |
|
| 101 | - // We also ensure parallel processing is off because we need to do one file at a time. |
|
| 102 | - if ($this->config->interactive === true) { |
|
| 103 | - $this->config->reports = ['full' => null]; |
|
| 104 | - $this->config->parallel = 1; |
|
| 105 | - $this->config->showProgress = false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Disable caching if we are processing STDIN as we can't be 100% |
|
| 109 | - // sure where the file came from or if it will change in the future. |
|
| 110 | - if ($this->config->stdin === true) { |
|
| 111 | - $this->config->cache = false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $numErrors = $this->run(); |
|
| 115 | - |
|
| 116 | - // Print all the reports for this run. |
|
| 117 | - $toScreen = $this->reporter->printReports(); |
|
| 118 | - |
|
| 119 | - // Only print timer output if no reports were |
|
| 120 | - // printed to the screen so we don't put additional output |
|
| 121 | - // in something like an XML report. If we are printing to screen, |
|
| 122 | - // the report types would have already worked out who should |
|
| 123 | - // print the timer info. |
|
| 124 | - if ($this->config->interactive === false |
|
| 125 | - && ($toScreen === false |
|
| 126 | - || (($this->reporter->totalErrors + $this->reporter->totalWarnings) === 0 && $this->config->showProgress === true)) |
|
| 127 | - ) { |
|
| 128 | - Util\Timing::printRunTime(); |
|
| 129 | - } |
|
| 130 | - } catch (DeepExitException $e) { |
|
| 131 | - echo $e->getMessage(); |
|
| 132 | - return $e->getCode(); |
|
| 133 | - }//end try |
|
| 134 | - |
|
| 135 | - if ($numErrors === 0) { |
|
| 136 | - // No errors found. |
|
| 137 | - return 0; |
|
| 138 | - } else if ($this->reporter->totalFixable === 0) { |
|
| 139 | - // Errors found, but none of them can be fixed by PHPCBF. |
|
| 140 | - return 1; |
|
| 141 | - } else { |
|
| 142 | - // Errors found, and some can be fixed by PHPCBF. |
|
| 143 | - return 2; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - }//end runPHPCS() |
|
| 147 | - |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Run the PHPCBF script. |
|
| 151 | - * |
|
| 152 | - * @return array |
|
| 153 | - */ |
|
| 154 | - public function runPHPCBF() |
|
| 155 | - { |
|
| 156 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 157 | - define('PHP_CODESNIFFER_CBF', true); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - try { |
|
| 161 | - Util\Timing::startTiming(); |
|
| 162 | - Runner::checkRequirements(); |
|
| 163 | - |
|
| 164 | - // Creating the Config object populates it with all required settings |
|
| 165 | - // based on the CLI arguments provided to the script and any config |
|
| 166 | - // values the user has set. |
|
| 167 | - $this->config = new Config(); |
|
| 168 | - |
|
| 169 | - // When processing STDIN, we can't output anything to the screen |
|
| 170 | - // or it will end up mixed in with the file output. |
|
| 171 | - if ($this->config->stdin === true) { |
|
| 172 | - $this->config->verbosity = 0; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - // Init the run and load the rulesets to set additional config vars. |
|
| 176 | - $this->init(); |
|
| 177 | - |
|
| 178 | - // When processing STDIN, we only process one file at a time and |
|
| 179 | - // we don't process all the way through, so we can't use the parallel |
|
| 180 | - // running system. |
|
| 181 | - if ($this->config->stdin === true) { |
|
| 182 | - $this->config->parallel = 1; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // Override some of the command line settings that might break the fixes. |
|
| 186 | - $this->config->generator = null; |
|
| 187 | - $this->config->explain = false; |
|
| 188 | - $this->config->interactive = false; |
|
| 189 | - $this->config->cache = false; |
|
| 190 | - $this->config->showSources = false; |
|
| 191 | - $this->config->recordErrors = false; |
|
| 192 | - $this->config->reportFile = null; |
|
| 193 | - $this->config->reports = ['cbf' => null]; |
|
| 194 | - |
|
| 195 | - // If a standard tries to set command line arguments itself, some |
|
| 196 | - // may be blocked because PHPCBF is running, so stop the script |
|
| 197 | - // dying if any are found. |
|
| 198 | - $this->config->dieOnUnknownArg = false; |
|
| 199 | - |
|
| 200 | - $this->run(); |
|
| 201 | - $this->reporter->printReports(); |
|
| 202 | - |
|
| 203 | - echo PHP_EOL; |
|
| 204 | - Util\Timing::printRunTime(); |
|
| 205 | - } catch (DeepExitException $e) { |
|
| 206 | - echo $e->getMessage(); |
|
| 207 | - return $e->getCode(); |
|
| 208 | - }//end try |
|
| 209 | - |
|
| 210 | - if ($this->reporter->totalFixed === 0) { |
|
| 211 | - // Nothing was fixed by PHPCBF. |
|
| 212 | - if ($this->reporter->totalFixable === 0) { |
|
| 213 | - // Nothing found that could be fixed. |
|
| 214 | - return 0; |
|
| 215 | - } else { |
|
| 216 | - // Something failed to fix. |
|
| 217 | - return 2; |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - if ($this->reporter->totalFixable === 0) { |
|
| 222 | - // PHPCBF fixed all fixable errors. |
|
| 223 | - return 1; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // PHPCBF fixed some fixable errors, but others failed to fix. |
|
| 227 | - return 2; |
|
| 228 | - |
|
| 229 | - }//end runPHPCBF() |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Exits if the minimum requirements of PHP_CodeSniffer are not met. |
|
| 234 | - * |
|
| 235 | - * @return void |
|
| 236 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the requirements are not met. |
|
| 237 | - */ |
|
| 238 | - public function checkRequirements() |
|
| 239 | - { |
|
| 240 | - // Check the PHP version. |
|
| 241 | - if (PHP_VERSION_ID < 50400) { |
|
| 242 | - $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL; |
|
| 243 | - throw new DeepExitException($error, 3); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - $requiredExtensions = [ |
|
| 247 | - 'tokenizer', |
|
| 248 | - 'xmlwriter', |
|
| 249 | - 'SimpleXML', |
|
| 250 | - ]; |
|
| 251 | - $missingExtensions = []; |
|
| 252 | - |
|
| 253 | - foreach ($requiredExtensions as $extension) { |
|
| 254 | - if (extension_loaded($extension) === false) { |
|
| 255 | - $missingExtensions[] = $extension; |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - if (empty($missingExtensions) === false) { |
|
| 260 | - $last = array_pop($requiredExtensions); |
|
| 261 | - $required = implode(', ', $requiredExtensions); |
|
| 262 | - $required .= ' and '.$last; |
|
| 263 | - |
|
| 264 | - if (count($missingExtensions) === 1) { |
|
| 265 | - $missing = $missingExtensions[0]; |
|
| 266 | - } else { |
|
| 267 | - $last = array_pop($missingExtensions); |
|
| 268 | - $missing = implode(', ', $missingExtensions); |
|
| 269 | - $missing .= ' and '.$last; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - $error = 'ERROR: PHP_CodeSniffer requires the %s extensions to be enabled. Please enable %s.'.PHP_EOL; |
|
| 273 | - $error = sprintf($error, $required, $missing); |
|
| 274 | - throw new DeepExitException($error, 3); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - }//end checkRequirements() |
|
| 278 | - |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Init the rulesets and other high-level settings. |
|
| 282 | - * |
|
| 283 | - * @return void |
|
| 284 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If a referenced standard is not installed. |
|
| 285 | - */ |
|
| 286 | - public function init() |
|
| 287 | - { |
|
| 288 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 289 | - define('PHP_CODESNIFFER_CBF', false); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - // Ensure this option is enabled or else line endings will not always |
|
| 293 | - // be detected properly for files created on a Mac with the /r line ending. |
|
| 294 | - @ini_set('auto_detect_line_endings', true); |
|
| 295 | - |
|
| 296 | - // Disable the PCRE JIT as this caused issues with parallel running. |
|
| 297 | - ini_set('pcre.jit', false); |
|
| 298 | - |
|
| 299 | - // Check that the standards are valid. |
|
| 300 | - foreach ($this->config->standards as $standard) { |
|
| 301 | - if (Util\Standards::isInstalledStandard($standard) === false) { |
|
| 302 | - // They didn't select a valid coding standard, so help them |
|
| 303 | - // out by letting them know which standards are installed. |
|
| 304 | - $error = 'ERROR: the "'.$standard.'" coding standard is not installed. '; |
|
| 305 | - ob_start(); |
|
| 306 | - Util\Standards::printInstalledStandards(); |
|
| 307 | - $error .= ob_get_contents(); |
|
| 308 | - ob_end_clean(); |
|
| 309 | - throw new DeepExitException($error, 3); |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - // Saves passing the Config object into other objects that only need |
|
| 314 | - // the verbosity flag for debug output. |
|
| 315 | - if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
|
| 316 | - define('PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - // Create this class so it is autoloaded and sets up a bunch |
|
| 320 | - // of PHP_CodeSniffer-specific token type constants. |
|
| 321 | - $tokens = new Util\Tokens(); |
|
| 322 | - |
|
| 323 | - // Allow autoloading of custom files inside installed standards. |
|
| 324 | - $installedStandards = Standards::getInstalledStandardDetails(); |
|
| 325 | - foreach ($installedStandards as $name => $details) { |
|
| 326 | - Autoload::addSearchPath($details['path'], $details['namespace']); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - // The ruleset contains all the information about how the files |
|
| 330 | - // should be checked and/or fixed. |
|
| 331 | - try { |
|
| 332 | - $this->ruleset = new Ruleset($this->config); |
|
| 333 | - } catch (RuntimeException $e) { |
|
| 334 | - $error = 'ERROR: '.$e->getMessage().PHP_EOL.PHP_EOL; |
|
| 335 | - $error .= $this->config->printShortUsage(true); |
|
| 336 | - throw new DeepExitException($error, 3); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - }//end init() |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Performs the run. |
|
| 344 | - * |
|
| 345 | - * @return int The number of errors and warnings found. |
|
| 346 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 347 | - * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 348 | - */ |
|
| 349 | - private function run() |
|
| 350 | - { |
|
| 351 | - // The class that manages all reporters for the run. |
|
| 352 | - $this->reporter = new Reporter($this->config); |
|
| 353 | - |
|
| 354 | - // Include bootstrap files. |
|
| 355 | - foreach ($this->config->bootstrap as $bootstrap) { |
|
| 356 | - include $bootstrap; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - if ($this->config->stdin === true) { |
|
| 360 | - $fileContents = $this->config->stdinContent; |
|
| 361 | - if ($fileContents === null) { |
|
| 362 | - $handle = fopen('php://stdin', 'r'); |
|
| 363 | - stream_set_blocking($handle, true); |
|
| 364 | - $fileContents = stream_get_contents($handle); |
|
| 365 | - fclose($handle); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - $todo = new FileList($this->config, $this->ruleset); |
|
| 369 | - $dummy = new DummyFile($fileContents, $this->ruleset, $this->config); |
|
| 370 | - $todo->addFile($dummy->path, $dummy); |
|
| 371 | - } else { |
|
| 372 | - if (empty($this->config->files) === true) { |
|
| 373 | - $error = 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL; |
|
| 374 | - $error .= $this->config->printShortUsage(true); |
|
| 375 | - throw new DeepExitException($error, 3); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 379 | - echo 'Creating file list... '; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - $todo = new FileList($this->config, $this->ruleset); |
|
| 383 | - |
|
| 384 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 385 | - $numFiles = count($todo); |
|
| 386 | - echo "DONE ($numFiles files in queue)".PHP_EOL; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - if ($this->config->cache === true) { |
|
| 390 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 391 | - echo 'Loading cache... '; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - Cache::load($this->ruleset, $this->config); |
|
| 395 | - |
|
| 396 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 397 | - $size = Cache::getSize(); |
|
| 398 | - echo "DONE ($size files in cache)".PHP_EOL; |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - }//end if |
|
| 402 | - |
|
| 403 | - // Turn all sniff errors into exceptions. |
|
| 404 | - set_error_handler([$this, 'handleErrors']); |
|
| 405 | - |
|
| 406 | - // If verbosity is too high, turn off parallelism so the |
|
| 407 | - // debug output is clean. |
|
| 408 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 409 | - $this->config->parallel = 1; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - // If the PCNTL extension isn't installed, we can't fork. |
|
| 413 | - if (function_exists('pcntl_fork') === false) { |
|
| 414 | - $this->config->parallel = 1; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - $lastDir = ''; |
|
| 418 | - $numFiles = count($todo); |
|
| 419 | - |
|
| 420 | - if ($this->config->parallel === 1) { |
|
| 421 | - // Running normally. |
|
| 422 | - $numProcessed = 0; |
|
| 423 | - foreach ($todo as $path => $file) { |
|
| 424 | - if ($file->ignored === false) { |
|
| 425 | - $currDir = dirname($path); |
|
| 426 | - if ($lastDir !== $currDir) { |
|
| 427 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 428 | - echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - $lastDir = $currDir; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - $this->processFile($file); |
|
| 435 | - } else if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 436 | - echo 'Skipping '.basename($file->path).PHP_EOL; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - $numProcessed++; |
|
| 440 | - $this->printProgress($file, $numFiles, $numProcessed); |
|
| 441 | - } |
|
| 442 | - } else { |
|
| 443 | - // Batching and forking. |
|
| 444 | - $childProcs = []; |
|
| 445 | - $numPerBatch = ceil($numFiles / $this->config->parallel); |
|
| 446 | - |
|
| 447 | - for ($batch = 0; $batch < $this->config->parallel; $batch++) { |
|
| 448 | - $startAt = ($batch * $numPerBatch); |
|
| 449 | - if ($startAt >= $numFiles) { |
|
| 450 | - break; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - $endAt = ($startAt + $numPerBatch); |
|
| 454 | - if ($endAt > $numFiles) { |
|
| 455 | - $endAt = $numFiles; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - $childOutFilename = tempnam(sys_get_temp_dir(), 'phpcs-child'); |
|
| 459 | - $pid = pcntl_fork(); |
|
| 460 | - if ($pid === -1) { |
|
| 461 | - throw new RuntimeException('Failed to create child process'); |
|
| 462 | - } else if ($pid !== 0) { |
|
| 463 | - $childProcs[$pid] = $childOutFilename; |
|
| 464 | - } else { |
|
| 465 | - // Move forward to the start of the batch. |
|
| 466 | - $todo->rewind(); |
|
| 467 | - for ($i = 0; $i < $startAt; $i++) { |
|
| 468 | - $todo->next(); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - // Reset the reporter to make sure only figures from this |
|
| 472 | - // file batch are recorded. |
|
| 473 | - $this->reporter->totalFiles = 0; |
|
| 474 | - $this->reporter->totalErrors = 0; |
|
| 475 | - $this->reporter->totalWarnings = 0; |
|
| 476 | - $this->reporter->totalFixable = 0; |
|
| 477 | - $this->reporter->totalFixed = 0; |
|
| 478 | - |
|
| 479 | - // Process the files. |
|
| 480 | - $pathsProcessed = []; |
|
| 481 | - ob_start(); |
|
| 482 | - for ($i = $startAt; $i < $endAt; $i++) { |
|
| 483 | - $path = $todo->key(); |
|
| 484 | - $file = $todo->current(); |
|
| 485 | - |
|
| 486 | - if ($file->ignored === true) { |
|
| 487 | - $todo->next(); |
|
| 488 | - continue; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - $currDir = dirname($path); |
|
| 492 | - if ($lastDir !== $currDir) { |
|
| 493 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 494 | - echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - $lastDir = $currDir; |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - $this->processFile($file); |
|
| 501 | - |
|
| 502 | - $pathsProcessed[] = $path; |
|
| 503 | - $todo->next(); |
|
| 504 | - }//end for |
|
| 505 | - |
|
| 506 | - $debugOutput = ob_get_contents(); |
|
| 507 | - ob_end_clean(); |
|
| 508 | - |
|
| 509 | - // Write information about the run to the filesystem |
|
| 510 | - // so it can be picked up by the main process. |
|
| 511 | - $childOutput = [ |
|
| 512 | - 'totalFiles' => $this->reporter->totalFiles, |
|
| 513 | - 'totalErrors' => $this->reporter->totalErrors, |
|
| 514 | - 'totalWarnings' => $this->reporter->totalWarnings, |
|
| 515 | - 'totalFixable' => $this->reporter->totalFixable, |
|
| 516 | - 'totalFixed' => $this->reporter->totalFixed, |
|
| 517 | - ]; |
|
| 518 | - |
|
| 519 | - $output = '<'.'?php'."\n".' $childOutput = '; |
|
| 520 | - $output .= var_export($childOutput, true); |
|
| 521 | - $output .= ";\n\$debugOutput = "; |
|
| 522 | - $output .= var_export($debugOutput, true); |
|
| 523 | - |
|
| 524 | - if ($this->config->cache === true) { |
|
| 525 | - $childCache = []; |
|
| 526 | - foreach ($pathsProcessed as $path) { |
|
| 527 | - $childCache[$path] = Cache::get($path); |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - $output .= ";\n\$childCache = "; |
|
| 531 | - $output .= var_export($childCache, true); |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - $output .= ";\n?".'>'; |
|
| 535 | - file_put_contents($childOutFilename, $output); |
|
| 536 | - exit(); |
|
| 537 | - }//end if |
|
| 538 | - }//end for |
|
| 539 | - |
|
| 540 | - $success = $this->processChildProcs($childProcs); |
|
| 541 | - if ($success === false) { |
|
| 542 | - throw new RuntimeException('One or more child processes failed to run'); |
|
| 543 | - } |
|
| 544 | - }//end if |
|
| 545 | - |
|
| 546 | - restore_error_handler(); |
|
| 547 | - |
|
| 548 | - if (PHP_CODESNIFFER_VERBOSITY === 0 |
|
| 549 | - && $this->config->interactive === false |
|
| 550 | - && $this->config->showProgress === true |
|
| 551 | - ) { |
|
| 552 | - echo PHP_EOL.PHP_EOL; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - if ($this->config->cache === true) { |
|
| 556 | - Cache::save(); |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - $ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit'); |
|
| 560 | - $ignoreErrors = Config::getConfigData('ignore_errors_on_exit'); |
|
| 561 | - |
|
| 562 | - $return = ($this->reporter->totalErrors + $this->reporter->totalWarnings); |
|
| 563 | - if ($ignoreErrors !== null) { |
|
| 564 | - $ignoreErrors = (bool) $ignoreErrors; |
|
| 565 | - if ($ignoreErrors === true) { |
|
| 566 | - $return -= $this->reporter->totalErrors; |
|
| 567 | - } |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - if ($ignoreWarnings !== null) { |
|
| 571 | - $ignoreWarnings = (bool) $ignoreWarnings; |
|
| 572 | - if ($ignoreWarnings === true) { |
|
| 573 | - $return -= $this->reporter->totalWarnings; |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - return $return; |
|
| 578 | - |
|
| 579 | - }//end run() |
|
| 580 | - |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * Converts all PHP errors into exceptions. |
|
| 584 | - * |
|
| 585 | - * This method forces a sniff to stop processing if it is not |
|
| 586 | - * able to handle a specific piece of code, instead of continuing |
|
| 587 | - * and potentially getting into a loop. |
|
| 588 | - * |
|
| 589 | - * @param int $code The level of error raised. |
|
| 590 | - * @param string $message The error message. |
|
| 591 | - * @param string $file The path of the file that raised the error. |
|
| 592 | - * @param int $line The line number the error was raised at. |
|
| 593 | - * |
|
| 594 | - * @return void |
|
| 595 | - * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 596 | - */ |
|
| 597 | - public function handleErrors($code, $message, $file, $line) |
|
| 598 | - { |
|
| 599 | - if ((error_reporting() & $code) === 0) { |
|
| 600 | - // This type of error is being muted. |
|
| 601 | - return true; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - throw new RuntimeException("$message in $file on line $line"); |
|
| 605 | - |
|
| 606 | - }//end handleErrors() |
|
| 607 | - |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * Processes a single file, including checking and fixing. |
|
| 611 | - * |
|
| 612 | - * @param \PHP_CodeSniffer\Files\File $file The file to be processed. |
|
| 613 | - * |
|
| 614 | - * @return void |
|
| 615 | - * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 616 | - */ |
|
| 617 | - public function processFile($file) |
|
| 618 | - { |
|
| 619 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 620 | - $startTime = microtime(true); |
|
| 621 | - echo 'Processing '.basename($file->path).' '; |
|
| 622 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 623 | - echo PHP_EOL; |
|
| 624 | - } |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - try { |
|
| 628 | - $file->process(); |
|
| 629 | - |
|
| 630 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 631 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
| 632 | - if ($timeTaken < 1000) { |
|
| 633 | - $timeTaken = round($timeTaken); |
|
| 634 | - echo "DONE in {$timeTaken}ms"; |
|
| 635 | - } else { |
|
| 636 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
| 637 | - echo "DONE in $timeTaken secs"; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - if (PHP_CODESNIFFER_CBF === true) { |
|
| 641 | - $errors = $file->getFixableCount(); |
|
| 642 | - echo " ($errors fixable violations)".PHP_EOL; |
|
| 643 | - } else { |
|
| 644 | - $errors = $file->getErrorCount(); |
|
| 645 | - $warnings = $file->getWarningCount(); |
|
| 646 | - echo " ($errors errors, $warnings warnings)".PHP_EOL; |
|
| 647 | - } |
|
| 648 | - } |
|
| 649 | - } catch (\Exception $e) { |
|
| 650 | - $error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage(); |
|
| 651 | - $file->addErrorOnLine($error, 1, 'Internal.Exception'); |
|
| 652 | - }//end try |
|
| 653 | - |
|
| 654 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 655 | - |
|
| 656 | - if ($this->config->interactive === true) { |
|
| 657 | - /* |
|
| 27 | + /** |
|
| 28 | + * The config data for the run. |
|
| 29 | + * |
|
| 30 | + * @var \PHP_CodeSniffer\Config |
|
| 31 | + */ |
|
| 32 | + public $config = null; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The ruleset used for the run. |
|
| 36 | + * |
|
| 37 | + * @var \PHP_CodeSniffer\Ruleset |
|
| 38 | + */ |
|
| 39 | + public $ruleset = null; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The reporter used for generating reports after the run. |
|
| 43 | + * |
|
| 44 | + * @var \PHP_CodeSniffer\Reporter |
|
| 45 | + */ |
|
| 46 | + public $reporter = null; |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Run the PHPCS script. |
|
| 51 | + * |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function runPHPCS() |
|
| 55 | + { |
|
| 56 | + try { |
|
| 57 | + Util\Timing::startTiming(); |
|
| 58 | + Runner::checkRequirements(); |
|
| 59 | + |
|
| 60 | + if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 61 | + define('PHP_CODESNIFFER_CBF', false); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // Creating the Config object populates it with all required settings |
|
| 65 | + // based on the CLI arguments provided to the script and any config |
|
| 66 | + // values the user has set. |
|
| 67 | + $this->config = new Config(); |
|
| 68 | + |
|
| 69 | + // Init the run and load the rulesets to set additional config vars. |
|
| 70 | + $this->init(); |
|
| 71 | + |
|
| 72 | + // Print a list of sniffs in each of the supplied standards. |
|
| 73 | + // We fudge the config here so that each standard is explained in isolation. |
|
| 74 | + if ($this->config->explain === true) { |
|
| 75 | + $standards = $this->config->standards; |
|
| 76 | + foreach ($standards as $standard) { |
|
| 77 | + $this->config->standards = [$standard]; |
|
| 78 | + $ruleset = new Ruleset($this->config); |
|
| 79 | + $ruleset->explain(); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return 0; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // Generate documentation for each of the supplied standards. |
|
| 86 | + if ($this->config->generator !== null) { |
|
| 87 | + $standards = $this->config->standards; |
|
| 88 | + foreach ($standards as $standard) { |
|
| 89 | + $this->config->standards = [$standard]; |
|
| 90 | + $ruleset = new Ruleset($this->config); |
|
| 91 | + $class = 'PHP_CodeSniffer\Generators\\'.$this->config->generator; |
|
| 92 | + $generator = new $class($ruleset); |
|
| 93 | + $generator->generate(); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + return 0; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // Other report formats don't really make sense in interactive mode |
|
| 100 | + // so we hard-code the full report here and when outputting. |
|
| 101 | + // We also ensure parallel processing is off because we need to do one file at a time. |
|
| 102 | + if ($this->config->interactive === true) { |
|
| 103 | + $this->config->reports = ['full' => null]; |
|
| 104 | + $this->config->parallel = 1; |
|
| 105 | + $this->config->showProgress = false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Disable caching if we are processing STDIN as we can't be 100% |
|
| 109 | + // sure where the file came from or if it will change in the future. |
|
| 110 | + if ($this->config->stdin === true) { |
|
| 111 | + $this->config->cache = false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $numErrors = $this->run(); |
|
| 115 | + |
|
| 116 | + // Print all the reports for this run. |
|
| 117 | + $toScreen = $this->reporter->printReports(); |
|
| 118 | + |
|
| 119 | + // Only print timer output if no reports were |
|
| 120 | + // printed to the screen so we don't put additional output |
|
| 121 | + // in something like an XML report. If we are printing to screen, |
|
| 122 | + // the report types would have already worked out who should |
|
| 123 | + // print the timer info. |
|
| 124 | + if ($this->config->interactive === false |
|
| 125 | + && ($toScreen === false |
|
| 126 | + || (($this->reporter->totalErrors + $this->reporter->totalWarnings) === 0 && $this->config->showProgress === true)) |
|
| 127 | + ) { |
|
| 128 | + Util\Timing::printRunTime(); |
|
| 129 | + } |
|
| 130 | + } catch (DeepExitException $e) { |
|
| 131 | + echo $e->getMessage(); |
|
| 132 | + return $e->getCode(); |
|
| 133 | + }//end try |
|
| 134 | + |
|
| 135 | + if ($numErrors === 0) { |
|
| 136 | + // No errors found. |
|
| 137 | + return 0; |
|
| 138 | + } else if ($this->reporter->totalFixable === 0) { |
|
| 139 | + // Errors found, but none of them can be fixed by PHPCBF. |
|
| 140 | + return 1; |
|
| 141 | + } else { |
|
| 142 | + // Errors found, and some can be fixed by PHPCBF. |
|
| 143 | + return 2; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + }//end runPHPCS() |
|
| 147 | + |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Run the PHPCBF script. |
|
| 151 | + * |
|
| 152 | + * @return array |
|
| 153 | + */ |
|
| 154 | + public function runPHPCBF() |
|
| 155 | + { |
|
| 156 | + if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 157 | + define('PHP_CODESNIFFER_CBF', true); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + try { |
|
| 161 | + Util\Timing::startTiming(); |
|
| 162 | + Runner::checkRequirements(); |
|
| 163 | + |
|
| 164 | + // Creating the Config object populates it with all required settings |
|
| 165 | + // based on the CLI arguments provided to the script and any config |
|
| 166 | + // values the user has set. |
|
| 167 | + $this->config = new Config(); |
|
| 168 | + |
|
| 169 | + // When processing STDIN, we can't output anything to the screen |
|
| 170 | + // or it will end up mixed in with the file output. |
|
| 171 | + if ($this->config->stdin === true) { |
|
| 172 | + $this->config->verbosity = 0; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + // Init the run and load the rulesets to set additional config vars. |
|
| 176 | + $this->init(); |
|
| 177 | + |
|
| 178 | + // When processing STDIN, we only process one file at a time and |
|
| 179 | + // we don't process all the way through, so we can't use the parallel |
|
| 180 | + // running system. |
|
| 181 | + if ($this->config->stdin === true) { |
|
| 182 | + $this->config->parallel = 1; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // Override some of the command line settings that might break the fixes. |
|
| 186 | + $this->config->generator = null; |
|
| 187 | + $this->config->explain = false; |
|
| 188 | + $this->config->interactive = false; |
|
| 189 | + $this->config->cache = false; |
|
| 190 | + $this->config->showSources = false; |
|
| 191 | + $this->config->recordErrors = false; |
|
| 192 | + $this->config->reportFile = null; |
|
| 193 | + $this->config->reports = ['cbf' => null]; |
|
| 194 | + |
|
| 195 | + // If a standard tries to set command line arguments itself, some |
|
| 196 | + // may be blocked because PHPCBF is running, so stop the script |
|
| 197 | + // dying if any are found. |
|
| 198 | + $this->config->dieOnUnknownArg = false; |
|
| 199 | + |
|
| 200 | + $this->run(); |
|
| 201 | + $this->reporter->printReports(); |
|
| 202 | + |
|
| 203 | + echo PHP_EOL; |
|
| 204 | + Util\Timing::printRunTime(); |
|
| 205 | + } catch (DeepExitException $e) { |
|
| 206 | + echo $e->getMessage(); |
|
| 207 | + return $e->getCode(); |
|
| 208 | + }//end try |
|
| 209 | + |
|
| 210 | + if ($this->reporter->totalFixed === 0) { |
|
| 211 | + // Nothing was fixed by PHPCBF. |
|
| 212 | + if ($this->reporter->totalFixable === 0) { |
|
| 213 | + // Nothing found that could be fixed. |
|
| 214 | + return 0; |
|
| 215 | + } else { |
|
| 216 | + // Something failed to fix. |
|
| 217 | + return 2; |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + if ($this->reporter->totalFixable === 0) { |
|
| 222 | + // PHPCBF fixed all fixable errors. |
|
| 223 | + return 1; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // PHPCBF fixed some fixable errors, but others failed to fix. |
|
| 227 | + return 2; |
|
| 228 | + |
|
| 229 | + }//end runPHPCBF() |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Exits if the minimum requirements of PHP_CodeSniffer are not met. |
|
| 234 | + * |
|
| 235 | + * @return void |
|
| 236 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the requirements are not met. |
|
| 237 | + */ |
|
| 238 | + public function checkRequirements() |
|
| 239 | + { |
|
| 240 | + // Check the PHP version. |
|
| 241 | + if (PHP_VERSION_ID < 50400) { |
|
| 242 | + $error = 'ERROR: PHP_CodeSniffer requires PHP version 5.4.0 or greater.'.PHP_EOL; |
|
| 243 | + throw new DeepExitException($error, 3); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + $requiredExtensions = [ |
|
| 247 | + 'tokenizer', |
|
| 248 | + 'xmlwriter', |
|
| 249 | + 'SimpleXML', |
|
| 250 | + ]; |
|
| 251 | + $missingExtensions = []; |
|
| 252 | + |
|
| 253 | + foreach ($requiredExtensions as $extension) { |
|
| 254 | + if (extension_loaded($extension) === false) { |
|
| 255 | + $missingExtensions[] = $extension; |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + if (empty($missingExtensions) === false) { |
|
| 260 | + $last = array_pop($requiredExtensions); |
|
| 261 | + $required = implode(', ', $requiredExtensions); |
|
| 262 | + $required .= ' and '.$last; |
|
| 263 | + |
|
| 264 | + if (count($missingExtensions) === 1) { |
|
| 265 | + $missing = $missingExtensions[0]; |
|
| 266 | + } else { |
|
| 267 | + $last = array_pop($missingExtensions); |
|
| 268 | + $missing = implode(', ', $missingExtensions); |
|
| 269 | + $missing .= ' and '.$last; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + $error = 'ERROR: PHP_CodeSniffer requires the %s extensions to be enabled. Please enable %s.'.PHP_EOL; |
|
| 273 | + $error = sprintf($error, $required, $missing); |
|
| 274 | + throw new DeepExitException($error, 3); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + }//end checkRequirements() |
|
| 278 | + |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Init the rulesets and other high-level settings. |
|
| 282 | + * |
|
| 283 | + * @return void |
|
| 284 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If a referenced standard is not installed. |
|
| 285 | + */ |
|
| 286 | + public function init() |
|
| 287 | + { |
|
| 288 | + if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
| 289 | + define('PHP_CODESNIFFER_CBF', false); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + // Ensure this option is enabled or else line endings will not always |
|
| 293 | + // be detected properly for files created on a Mac with the /r line ending. |
|
| 294 | + @ini_set('auto_detect_line_endings', true); |
|
| 295 | + |
|
| 296 | + // Disable the PCRE JIT as this caused issues with parallel running. |
|
| 297 | + ini_set('pcre.jit', false); |
|
| 298 | + |
|
| 299 | + // Check that the standards are valid. |
|
| 300 | + foreach ($this->config->standards as $standard) { |
|
| 301 | + if (Util\Standards::isInstalledStandard($standard) === false) { |
|
| 302 | + // They didn't select a valid coding standard, so help them |
|
| 303 | + // out by letting them know which standards are installed. |
|
| 304 | + $error = 'ERROR: the "'.$standard.'" coding standard is not installed. '; |
|
| 305 | + ob_start(); |
|
| 306 | + Util\Standards::printInstalledStandards(); |
|
| 307 | + $error .= ob_get_contents(); |
|
| 308 | + ob_end_clean(); |
|
| 309 | + throw new DeepExitException($error, 3); |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + // Saves passing the Config object into other objects that only need |
|
| 314 | + // the verbosity flag for debug output. |
|
| 315 | + if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
|
| 316 | + define('PHP_CODESNIFFER_VERBOSITY', $this->config->verbosity); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + // Create this class so it is autoloaded and sets up a bunch |
|
| 320 | + // of PHP_CodeSniffer-specific token type constants. |
|
| 321 | + $tokens = new Util\Tokens(); |
|
| 322 | + |
|
| 323 | + // Allow autoloading of custom files inside installed standards. |
|
| 324 | + $installedStandards = Standards::getInstalledStandardDetails(); |
|
| 325 | + foreach ($installedStandards as $name => $details) { |
|
| 326 | + Autoload::addSearchPath($details['path'], $details['namespace']); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + // The ruleset contains all the information about how the files |
|
| 330 | + // should be checked and/or fixed. |
|
| 331 | + try { |
|
| 332 | + $this->ruleset = new Ruleset($this->config); |
|
| 333 | + } catch (RuntimeException $e) { |
|
| 334 | + $error = 'ERROR: '.$e->getMessage().PHP_EOL.PHP_EOL; |
|
| 335 | + $error .= $this->config->printShortUsage(true); |
|
| 336 | + throw new DeepExitException($error, 3); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + }//end init() |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Performs the run. |
|
| 344 | + * |
|
| 345 | + * @return int The number of errors and warnings found. |
|
| 346 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 347 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 348 | + */ |
|
| 349 | + private function run() |
|
| 350 | + { |
|
| 351 | + // The class that manages all reporters for the run. |
|
| 352 | + $this->reporter = new Reporter($this->config); |
|
| 353 | + |
|
| 354 | + // Include bootstrap files. |
|
| 355 | + foreach ($this->config->bootstrap as $bootstrap) { |
|
| 356 | + include $bootstrap; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + if ($this->config->stdin === true) { |
|
| 360 | + $fileContents = $this->config->stdinContent; |
|
| 361 | + if ($fileContents === null) { |
|
| 362 | + $handle = fopen('php://stdin', 'r'); |
|
| 363 | + stream_set_blocking($handle, true); |
|
| 364 | + $fileContents = stream_get_contents($handle); |
|
| 365 | + fclose($handle); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + $todo = new FileList($this->config, $this->ruleset); |
|
| 369 | + $dummy = new DummyFile($fileContents, $this->ruleset, $this->config); |
|
| 370 | + $todo->addFile($dummy->path, $dummy); |
|
| 371 | + } else { |
|
| 372 | + if (empty($this->config->files) === true) { |
|
| 373 | + $error = 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL; |
|
| 374 | + $error .= $this->config->printShortUsage(true); |
|
| 375 | + throw new DeepExitException($error, 3); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 379 | + echo 'Creating file list... '; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + $todo = new FileList($this->config, $this->ruleset); |
|
| 383 | + |
|
| 384 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 385 | + $numFiles = count($todo); |
|
| 386 | + echo "DONE ($numFiles files in queue)".PHP_EOL; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + if ($this->config->cache === true) { |
|
| 390 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 391 | + echo 'Loading cache... '; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + Cache::load($this->ruleset, $this->config); |
|
| 395 | + |
|
| 396 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 397 | + $size = Cache::getSize(); |
|
| 398 | + echo "DONE ($size files in cache)".PHP_EOL; |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + }//end if |
|
| 402 | + |
|
| 403 | + // Turn all sniff errors into exceptions. |
|
| 404 | + set_error_handler([$this, 'handleErrors']); |
|
| 405 | + |
|
| 406 | + // If verbosity is too high, turn off parallelism so the |
|
| 407 | + // debug output is clean. |
|
| 408 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 409 | + $this->config->parallel = 1; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + // If the PCNTL extension isn't installed, we can't fork. |
|
| 413 | + if (function_exists('pcntl_fork') === false) { |
|
| 414 | + $this->config->parallel = 1; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + $lastDir = ''; |
|
| 418 | + $numFiles = count($todo); |
|
| 419 | + |
|
| 420 | + if ($this->config->parallel === 1) { |
|
| 421 | + // Running normally. |
|
| 422 | + $numProcessed = 0; |
|
| 423 | + foreach ($todo as $path => $file) { |
|
| 424 | + if ($file->ignored === false) { |
|
| 425 | + $currDir = dirname($path); |
|
| 426 | + if ($lastDir !== $currDir) { |
|
| 427 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 428 | + echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + $lastDir = $currDir; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + $this->processFile($file); |
|
| 435 | + } else if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 436 | + echo 'Skipping '.basename($file->path).PHP_EOL; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + $numProcessed++; |
|
| 440 | + $this->printProgress($file, $numFiles, $numProcessed); |
|
| 441 | + } |
|
| 442 | + } else { |
|
| 443 | + // Batching and forking. |
|
| 444 | + $childProcs = []; |
|
| 445 | + $numPerBatch = ceil($numFiles / $this->config->parallel); |
|
| 446 | + |
|
| 447 | + for ($batch = 0; $batch < $this->config->parallel; $batch++) { |
|
| 448 | + $startAt = ($batch * $numPerBatch); |
|
| 449 | + if ($startAt >= $numFiles) { |
|
| 450 | + break; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + $endAt = ($startAt + $numPerBatch); |
|
| 454 | + if ($endAt > $numFiles) { |
|
| 455 | + $endAt = $numFiles; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + $childOutFilename = tempnam(sys_get_temp_dir(), 'phpcs-child'); |
|
| 459 | + $pid = pcntl_fork(); |
|
| 460 | + if ($pid === -1) { |
|
| 461 | + throw new RuntimeException('Failed to create child process'); |
|
| 462 | + } else if ($pid !== 0) { |
|
| 463 | + $childProcs[$pid] = $childOutFilename; |
|
| 464 | + } else { |
|
| 465 | + // Move forward to the start of the batch. |
|
| 466 | + $todo->rewind(); |
|
| 467 | + for ($i = 0; $i < $startAt; $i++) { |
|
| 468 | + $todo->next(); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + // Reset the reporter to make sure only figures from this |
|
| 472 | + // file batch are recorded. |
|
| 473 | + $this->reporter->totalFiles = 0; |
|
| 474 | + $this->reporter->totalErrors = 0; |
|
| 475 | + $this->reporter->totalWarnings = 0; |
|
| 476 | + $this->reporter->totalFixable = 0; |
|
| 477 | + $this->reporter->totalFixed = 0; |
|
| 478 | + |
|
| 479 | + // Process the files. |
|
| 480 | + $pathsProcessed = []; |
|
| 481 | + ob_start(); |
|
| 482 | + for ($i = $startAt; $i < $endAt; $i++) { |
|
| 483 | + $path = $todo->key(); |
|
| 484 | + $file = $todo->current(); |
|
| 485 | + |
|
| 486 | + if ($file->ignored === true) { |
|
| 487 | + $todo->next(); |
|
| 488 | + continue; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + $currDir = dirname($path); |
|
| 492 | + if ($lastDir !== $currDir) { |
|
| 493 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 494 | + echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + $lastDir = $currDir; |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + $this->processFile($file); |
|
| 501 | + |
|
| 502 | + $pathsProcessed[] = $path; |
|
| 503 | + $todo->next(); |
|
| 504 | + }//end for |
|
| 505 | + |
|
| 506 | + $debugOutput = ob_get_contents(); |
|
| 507 | + ob_end_clean(); |
|
| 508 | + |
|
| 509 | + // Write information about the run to the filesystem |
|
| 510 | + // so it can be picked up by the main process. |
|
| 511 | + $childOutput = [ |
|
| 512 | + 'totalFiles' => $this->reporter->totalFiles, |
|
| 513 | + 'totalErrors' => $this->reporter->totalErrors, |
|
| 514 | + 'totalWarnings' => $this->reporter->totalWarnings, |
|
| 515 | + 'totalFixable' => $this->reporter->totalFixable, |
|
| 516 | + 'totalFixed' => $this->reporter->totalFixed, |
|
| 517 | + ]; |
|
| 518 | + |
|
| 519 | + $output = '<'.'?php'."\n".' $childOutput = '; |
|
| 520 | + $output .= var_export($childOutput, true); |
|
| 521 | + $output .= ";\n\$debugOutput = "; |
|
| 522 | + $output .= var_export($debugOutput, true); |
|
| 523 | + |
|
| 524 | + if ($this->config->cache === true) { |
|
| 525 | + $childCache = []; |
|
| 526 | + foreach ($pathsProcessed as $path) { |
|
| 527 | + $childCache[$path] = Cache::get($path); |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + $output .= ";\n\$childCache = "; |
|
| 531 | + $output .= var_export($childCache, true); |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + $output .= ";\n?".'>'; |
|
| 535 | + file_put_contents($childOutFilename, $output); |
|
| 536 | + exit(); |
|
| 537 | + }//end if |
|
| 538 | + }//end for |
|
| 539 | + |
|
| 540 | + $success = $this->processChildProcs($childProcs); |
|
| 541 | + if ($success === false) { |
|
| 542 | + throw new RuntimeException('One or more child processes failed to run'); |
|
| 543 | + } |
|
| 544 | + }//end if |
|
| 545 | + |
|
| 546 | + restore_error_handler(); |
|
| 547 | + |
|
| 548 | + if (PHP_CODESNIFFER_VERBOSITY === 0 |
|
| 549 | + && $this->config->interactive === false |
|
| 550 | + && $this->config->showProgress === true |
|
| 551 | + ) { |
|
| 552 | + echo PHP_EOL.PHP_EOL; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + if ($this->config->cache === true) { |
|
| 556 | + Cache::save(); |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + $ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit'); |
|
| 560 | + $ignoreErrors = Config::getConfigData('ignore_errors_on_exit'); |
|
| 561 | + |
|
| 562 | + $return = ($this->reporter->totalErrors + $this->reporter->totalWarnings); |
|
| 563 | + if ($ignoreErrors !== null) { |
|
| 564 | + $ignoreErrors = (bool) $ignoreErrors; |
|
| 565 | + if ($ignoreErrors === true) { |
|
| 566 | + $return -= $this->reporter->totalErrors; |
|
| 567 | + } |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + if ($ignoreWarnings !== null) { |
|
| 571 | + $ignoreWarnings = (bool) $ignoreWarnings; |
|
| 572 | + if ($ignoreWarnings === true) { |
|
| 573 | + $return -= $this->reporter->totalWarnings; |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + return $return; |
|
| 578 | + |
|
| 579 | + }//end run() |
|
| 580 | + |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * Converts all PHP errors into exceptions. |
|
| 584 | + * |
|
| 585 | + * This method forces a sniff to stop processing if it is not |
|
| 586 | + * able to handle a specific piece of code, instead of continuing |
|
| 587 | + * and potentially getting into a loop. |
|
| 588 | + * |
|
| 589 | + * @param int $code The level of error raised. |
|
| 590 | + * @param string $message The error message. |
|
| 591 | + * @param string $file The path of the file that raised the error. |
|
| 592 | + * @param int $line The line number the error was raised at. |
|
| 593 | + * |
|
| 594 | + * @return void |
|
| 595 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
|
| 596 | + */ |
|
| 597 | + public function handleErrors($code, $message, $file, $line) |
|
| 598 | + { |
|
| 599 | + if ((error_reporting() & $code) === 0) { |
|
| 600 | + // This type of error is being muted. |
|
| 601 | + return true; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + throw new RuntimeException("$message in $file on line $line"); |
|
| 605 | + |
|
| 606 | + }//end handleErrors() |
|
| 607 | + |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * Processes a single file, including checking and fixing. |
|
| 611 | + * |
|
| 612 | + * @param \PHP_CodeSniffer\Files\File $file The file to be processed. |
|
| 613 | + * |
|
| 614 | + * @return void |
|
| 615 | + * @throws \PHP_CodeSniffer\Exceptions\DeepExitException |
|
| 616 | + */ |
|
| 617 | + public function processFile($file) |
|
| 618 | + { |
|
| 619 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 620 | + $startTime = microtime(true); |
|
| 621 | + echo 'Processing '.basename($file->path).' '; |
|
| 622 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
| 623 | + echo PHP_EOL; |
|
| 624 | + } |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + try { |
|
| 628 | + $file->process(); |
|
| 629 | + |
|
| 630 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
| 631 | + $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
| 632 | + if ($timeTaken < 1000) { |
|
| 633 | + $timeTaken = round($timeTaken); |
|
| 634 | + echo "DONE in {$timeTaken}ms"; |
|
| 635 | + } else { |
|
| 636 | + $timeTaken = round(($timeTaken / 1000), 2); |
|
| 637 | + echo "DONE in $timeTaken secs"; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + if (PHP_CODESNIFFER_CBF === true) { |
|
| 641 | + $errors = $file->getFixableCount(); |
|
| 642 | + echo " ($errors fixable violations)".PHP_EOL; |
|
| 643 | + } else { |
|
| 644 | + $errors = $file->getErrorCount(); |
|
| 645 | + $warnings = $file->getWarningCount(); |
|
| 646 | + echo " ($errors errors, $warnings warnings)".PHP_EOL; |
|
| 647 | + } |
|
| 648 | + } |
|
| 649 | + } catch (\Exception $e) { |
|
| 650 | + $error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage(); |
|
| 651 | + $file->addErrorOnLine($error, 1, 'Internal.Exception'); |
|
| 652 | + }//end try |
|
| 653 | + |
|
| 654 | + $this->reporter->cacheFileReport($file, $this->config); |
|
| 655 | + |
|
| 656 | + if ($this->config->interactive === true) { |
|
| 657 | + /* |
|
| 658 | 658 | Running interactively. |
| 659 | 659 | Print the error report for the current file and then wait for user input. |
| 660 | 660 | */ |
| 661 | 661 | |
| 662 | - // Get current violations and then clear the list to make sure |
|
| 663 | - // we only print violations for a single file each time. |
|
| 664 | - $numErrors = null; |
|
| 665 | - while ($numErrors !== 0) { |
|
| 666 | - $numErrors = ($file->getErrorCount() + $file->getWarningCount()); |
|
| 667 | - if ($numErrors === 0) { |
|
| 668 | - continue; |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - $this->reporter->printReport('full'); |
|
| 672 | - |
|
| 673 | - echo '<ENTER> to recheck, [s] to skip or [q] to quit : '; |
|
| 674 | - $input = fgets(STDIN); |
|
| 675 | - $input = trim($input); |
|
| 676 | - |
|
| 677 | - switch ($input) { |
|
| 678 | - case 's': |
|
| 679 | - break(2); |
|
| 680 | - case 'q': |
|
| 681 | - throw new DeepExitException('', 0); |
|
| 682 | - default: |
|
| 683 | - // Repopulate the sniffs because some of them save their state |
|
| 684 | - // and only clear it when the file changes, but we are rechecking |
|
| 685 | - // the same file. |
|
| 686 | - $file->ruleset->populateTokenListeners(); |
|
| 687 | - $file->reloadContent(); |
|
| 688 | - $file->process(); |
|
| 689 | - $this->reporter->cacheFileReport($file, $this->config); |
|
| 690 | - break; |
|
| 691 | - } |
|
| 692 | - }//end while |
|
| 693 | - }//end if |
|
| 694 | - |
|
| 695 | - // Clean up the file to save (a lot of) memory. |
|
| 696 | - $file->cleanUp(); |
|
| 697 | - |
|
| 698 | - }//end processFile() |
|
| 699 | - |
|
| 700 | - |
|
| 701 | - /** |
|
| 702 | - * Waits for child processes to complete and cleans up after them. |
|
| 703 | - * |
|
| 704 | - * The reporting information returned by each child process is merged |
|
| 705 | - * into the main reporter class. |
|
| 706 | - * |
|
| 707 | - * @param array $childProcs An array of child processes to wait for. |
|
| 708 | - * |
|
| 709 | - * @return bool |
|
| 710 | - */ |
|
| 711 | - private function processChildProcs($childProcs) |
|
| 712 | - { |
|
| 713 | - $numProcessed = 0; |
|
| 714 | - $totalBatches = count($childProcs); |
|
| 715 | - |
|
| 716 | - $success = true; |
|
| 717 | - |
|
| 718 | - while (count($childProcs) > 0) { |
|
| 719 | - $pid = pcntl_waitpid(0, $status); |
|
| 720 | - if ($pid <= 0) { |
|
| 721 | - continue; |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - $out = $childProcs[$pid]; |
|
| 725 | - unset($childProcs[$pid]); |
|
| 726 | - if (file_exists($out) === false) { |
|
| 727 | - continue; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - include $out; |
|
| 731 | - unlink($out); |
|
| 732 | - |
|
| 733 | - $numProcessed++; |
|
| 734 | - |
|
| 735 | - if (isset($childOutput) === false) { |
|
| 736 | - // The child process died, so the run has failed. |
|
| 737 | - $file = new DummyFile('', $this->ruleset, $this->config); |
|
| 738 | - $file->setErrorCounts(1, 0, 0, 0); |
|
| 739 | - $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 740 | - $success = false; |
|
| 741 | - continue; |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - $this->reporter->totalFiles += $childOutput['totalFiles']; |
|
| 745 | - $this->reporter->totalErrors += $childOutput['totalErrors']; |
|
| 746 | - $this->reporter->totalWarnings += $childOutput['totalWarnings']; |
|
| 747 | - $this->reporter->totalFixable += $childOutput['totalFixable']; |
|
| 748 | - $this->reporter->totalFixed += $childOutput['totalFixed']; |
|
| 749 | - |
|
| 750 | - if (isset($debugOutput) === true) { |
|
| 751 | - echo $debugOutput; |
|
| 752 | - } |
|
| 753 | - |
|
| 754 | - if (isset($childCache) === true) { |
|
| 755 | - foreach ($childCache as $path => $cache) { |
|
| 756 | - Cache::set($path, $cache); |
|
| 757 | - } |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - // Fake a processed file so we can print progress output for the batch. |
|
| 761 | - $file = new DummyFile('', $this->ruleset, $this->config); |
|
| 762 | - $file->setErrorCounts( |
|
| 763 | - $childOutput['totalErrors'], |
|
| 764 | - $childOutput['totalWarnings'], |
|
| 765 | - $childOutput['totalFixable'], |
|
| 766 | - $childOutput['totalFixed'] |
|
| 767 | - ); |
|
| 768 | - $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 769 | - }//end while |
|
| 770 | - |
|
| 771 | - return $success; |
|
| 772 | - |
|
| 773 | - }//end processChildProcs() |
|
| 774 | - |
|
| 775 | - |
|
| 776 | - /** |
|
| 777 | - * Print progress information for a single processed file. |
|
| 778 | - * |
|
| 779 | - * @param \PHP_CodeSniffer\Files\File $file The file that was processed. |
|
| 780 | - * @param int $numFiles The total number of files to process. |
|
| 781 | - * @param int $numProcessed The number of files that have been processed, |
|
| 782 | - * including this one. |
|
| 783 | - * |
|
| 784 | - * @return void |
|
| 785 | - */ |
|
| 786 | - public function printProgress(File $file, $numFiles, $numProcessed) |
|
| 787 | - { |
|
| 788 | - if (PHP_CODESNIFFER_VERBOSITY > 0 |
|
| 789 | - || $this->config->showProgress === false |
|
| 790 | - ) { |
|
| 791 | - return; |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - // Show progress information. |
|
| 795 | - if ($file->ignored === true) { |
|
| 796 | - echo 'S'; |
|
| 797 | - } else { |
|
| 798 | - $errors = $file->getErrorCount(); |
|
| 799 | - $warnings = $file->getWarningCount(); |
|
| 800 | - $fixable = $file->getFixableCount(); |
|
| 801 | - $fixed = $file->getFixedCount(); |
|
| 802 | - |
|
| 803 | - if (PHP_CODESNIFFER_CBF === true) { |
|
| 804 | - // Files with fixed errors or warnings are F (green). |
|
| 805 | - // Files with unfixable errors or warnings are E (red). |
|
| 806 | - // Files with no errors or warnings are . (black). |
|
| 807 | - if ($fixable > 0) { |
|
| 808 | - if ($this->config->colors === true) { |
|
| 809 | - echo "\033[31m"; |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - echo 'E'; |
|
| 813 | - |
|
| 814 | - if ($this->config->colors === true) { |
|
| 815 | - echo "\033[0m"; |
|
| 816 | - } |
|
| 817 | - } else if ($fixed > 0) { |
|
| 818 | - if ($this->config->colors === true) { |
|
| 819 | - echo "\033[32m"; |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - echo 'F'; |
|
| 823 | - |
|
| 824 | - if ($this->config->colors === true) { |
|
| 825 | - echo "\033[0m"; |
|
| 826 | - } |
|
| 827 | - } else { |
|
| 828 | - echo '.'; |
|
| 829 | - }//end if |
|
| 830 | - } else { |
|
| 831 | - // Files with errors are E (red). |
|
| 832 | - // Files with fixable errors are E (green). |
|
| 833 | - // Files with warnings are W (yellow). |
|
| 834 | - // Files with fixable warnings are W (green). |
|
| 835 | - // Files with no errors or warnings are . (black). |
|
| 836 | - if ($errors > 0) { |
|
| 837 | - if ($this->config->colors === true) { |
|
| 838 | - if ($fixable > 0) { |
|
| 839 | - echo "\033[32m"; |
|
| 840 | - } else { |
|
| 841 | - echo "\033[31m"; |
|
| 842 | - } |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - echo 'E'; |
|
| 846 | - |
|
| 847 | - if ($this->config->colors === true) { |
|
| 848 | - echo "\033[0m"; |
|
| 849 | - } |
|
| 850 | - } else if ($warnings > 0) { |
|
| 851 | - if ($this->config->colors === true) { |
|
| 852 | - if ($fixable > 0) { |
|
| 853 | - echo "\033[32m"; |
|
| 854 | - } else { |
|
| 855 | - echo "\033[33m"; |
|
| 856 | - } |
|
| 857 | - } |
|
| 858 | - |
|
| 859 | - echo 'W'; |
|
| 860 | - |
|
| 861 | - if ($this->config->colors === true) { |
|
| 862 | - echo "\033[0m"; |
|
| 863 | - } |
|
| 864 | - } else { |
|
| 865 | - echo '.'; |
|
| 866 | - }//end if |
|
| 867 | - }//end if |
|
| 868 | - }//end if |
|
| 869 | - |
|
| 870 | - $numPerLine = 60; |
|
| 871 | - if ($numProcessed !== $numFiles && ($numProcessed % $numPerLine) !== 0) { |
|
| 872 | - return; |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - $percent = round(($numProcessed / $numFiles) * 100); |
|
| 876 | - $padding = (strlen($numFiles) - strlen($numProcessed)); |
|
| 877 | - if ($numProcessed === $numFiles |
|
| 878 | - && $numFiles > $numPerLine |
|
| 879 | - && ($numProcessed % $numPerLine) !== 0 |
|
| 880 | - ) { |
|
| 881 | - $padding += ($numPerLine - ($numFiles - (floor($numFiles / $numPerLine) * $numPerLine))); |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - echo str_repeat(' ', $padding)." $numProcessed / $numFiles ($percent%)".PHP_EOL; |
|
| 885 | - |
|
| 886 | - }//end printProgress() |
|
| 662 | + // Get current violations and then clear the list to make sure |
|
| 663 | + // we only print violations for a single file each time. |
|
| 664 | + $numErrors = null; |
|
| 665 | + while ($numErrors !== 0) { |
|
| 666 | + $numErrors = ($file->getErrorCount() + $file->getWarningCount()); |
|
| 667 | + if ($numErrors === 0) { |
|
| 668 | + continue; |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + $this->reporter->printReport('full'); |
|
| 672 | + |
|
| 673 | + echo '<ENTER> to recheck, [s] to skip or [q] to quit : '; |
|
| 674 | + $input = fgets(STDIN); |
|
| 675 | + $input = trim($input); |
|
| 676 | + |
|
| 677 | + switch ($input) { |
|
| 678 | + case 's': |
|
| 679 | + break(2); |
|
| 680 | + case 'q': |
|
| 681 | + throw new DeepExitException('', 0); |
|
| 682 | + default: |
|
| 683 | + // Repopulate the sniffs because some of them save their state |
|
| 684 | + // and only clear it when the file changes, but we are rechecking |
|
| 685 | + // the same file. |
|
| 686 | + $file->ruleset->populateTokenListeners(); |
|
| 687 | + $file->reloadContent(); |
|
| 688 | + $file->process(); |
|
| 689 | + $this->reporter->cacheFileReport($file, $this->config); |
|
| 690 | + break; |
|
| 691 | + } |
|
| 692 | + }//end while |
|
| 693 | + }//end if |
|
| 694 | + |
|
| 695 | + // Clean up the file to save (a lot of) memory. |
|
| 696 | + $file->cleanUp(); |
|
| 697 | + |
|
| 698 | + }//end processFile() |
|
| 699 | + |
|
| 700 | + |
|
| 701 | + /** |
|
| 702 | + * Waits for child processes to complete and cleans up after them. |
|
| 703 | + * |
|
| 704 | + * The reporting information returned by each child process is merged |
|
| 705 | + * into the main reporter class. |
|
| 706 | + * |
|
| 707 | + * @param array $childProcs An array of child processes to wait for. |
|
| 708 | + * |
|
| 709 | + * @return bool |
|
| 710 | + */ |
|
| 711 | + private function processChildProcs($childProcs) |
|
| 712 | + { |
|
| 713 | + $numProcessed = 0; |
|
| 714 | + $totalBatches = count($childProcs); |
|
| 715 | + |
|
| 716 | + $success = true; |
|
| 717 | + |
|
| 718 | + while (count($childProcs) > 0) { |
|
| 719 | + $pid = pcntl_waitpid(0, $status); |
|
| 720 | + if ($pid <= 0) { |
|
| 721 | + continue; |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + $out = $childProcs[$pid]; |
|
| 725 | + unset($childProcs[$pid]); |
|
| 726 | + if (file_exists($out) === false) { |
|
| 727 | + continue; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + include $out; |
|
| 731 | + unlink($out); |
|
| 732 | + |
|
| 733 | + $numProcessed++; |
|
| 734 | + |
|
| 735 | + if (isset($childOutput) === false) { |
|
| 736 | + // The child process died, so the run has failed. |
|
| 737 | + $file = new DummyFile('', $this->ruleset, $this->config); |
|
| 738 | + $file->setErrorCounts(1, 0, 0, 0); |
|
| 739 | + $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 740 | + $success = false; |
|
| 741 | + continue; |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + $this->reporter->totalFiles += $childOutput['totalFiles']; |
|
| 745 | + $this->reporter->totalErrors += $childOutput['totalErrors']; |
|
| 746 | + $this->reporter->totalWarnings += $childOutput['totalWarnings']; |
|
| 747 | + $this->reporter->totalFixable += $childOutput['totalFixable']; |
|
| 748 | + $this->reporter->totalFixed += $childOutput['totalFixed']; |
|
| 749 | + |
|
| 750 | + if (isset($debugOutput) === true) { |
|
| 751 | + echo $debugOutput; |
|
| 752 | + } |
|
| 753 | + |
|
| 754 | + if (isset($childCache) === true) { |
|
| 755 | + foreach ($childCache as $path => $cache) { |
|
| 756 | + Cache::set($path, $cache); |
|
| 757 | + } |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + // Fake a processed file so we can print progress output for the batch. |
|
| 761 | + $file = new DummyFile('', $this->ruleset, $this->config); |
|
| 762 | + $file->setErrorCounts( |
|
| 763 | + $childOutput['totalErrors'], |
|
| 764 | + $childOutput['totalWarnings'], |
|
| 765 | + $childOutput['totalFixable'], |
|
| 766 | + $childOutput['totalFixed'] |
|
| 767 | + ); |
|
| 768 | + $this->printProgress($file, $totalBatches, $numProcessed); |
|
| 769 | + }//end while |
|
| 770 | + |
|
| 771 | + return $success; |
|
| 772 | + |
|
| 773 | + }//end processChildProcs() |
|
| 774 | + |
|
| 775 | + |
|
| 776 | + /** |
|
| 777 | + * Print progress information for a single processed file. |
|
| 778 | + * |
|
| 779 | + * @param \PHP_CodeSniffer\Files\File $file The file that was processed. |
|
| 780 | + * @param int $numFiles The total number of files to process. |
|
| 781 | + * @param int $numProcessed The number of files that have been processed, |
|
| 782 | + * including this one. |
|
| 783 | + * |
|
| 784 | + * @return void |
|
| 785 | + */ |
|
| 786 | + public function printProgress(File $file, $numFiles, $numProcessed) |
|
| 787 | + { |
|
| 788 | + if (PHP_CODESNIFFER_VERBOSITY > 0 |
|
| 789 | + || $this->config->showProgress === false |
|
| 790 | + ) { |
|
| 791 | + return; |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + // Show progress information. |
|
| 795 | + if ($file->ignored === true) { |
|
| 796 | + echo 'S'; |
|
| 797 | + } else { |
|
| 798 | + $errors = $file->getErrorCount(); |
|
| 799 | + $warnings = $file->getWarningCount(); |
|
| 800 | + $fixable = $file->getFixableCount(); |
|
| 801 | + $fixed = $file->getFixedCount(); |
|
| 802 | + |
|
| 803 | + if (PHP_CODESNIFFER_CBF === true) { |
|
| 804 | + // Files with fixed errors or warnings are F (green). |
|
| 805 | + // Files with unfixable errors or warnings are E (red). |
|
| 806 | + // Files with no errors or warnings are . (black). |
|
| 807 | + if ($fixable > 0) { |
|
| 808 | + if ($this->config->colors === true) { |
|
| 809 | + echo "\033[31m"; |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + echo 'E'; |
|
| 813 | + |
|
| 814 | + if ($this->config->colors === true) { |
|
| 815 | + echo "\033[0m"; |
|
| 816 | + } |
|
| 817 | + } else if ($fixed > 0) { |
|
| 818 | + if ($this->config->colors === true) { |
|
| 819 | + echo "\033[32m"; |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + echo 'F'; |
|
| 823 | + |
|
| 824 | + if ($this->config->colors === true) { |
|
| 825 | + echo "\033[0m"; |
|
| 826 | + } |
|
| 827 | + } else { |
|
| 828 | + echo '.'; |
|
| 829 | + }//end if |
|
| 830 | + } else { |
|
| 831 | + // Files with errors are E (red). |
|
| 832 | + // Files with fixable errors are E (green). |
|
| 833 | + // Files with warnings are W (yellow). |
|
| 834 | + // Files with fixable warnings are W (green). |
|
| 835 | + // Files with no errors or warnings are . (black). |
|
| 836 | + if ($errors > 0) { |
|
| 837 | + if ($this->config->colors === true) { |
|
| 838 | + if ($fixable > 0) { |
|
| 839 | + echo "\033[32m"; |
|
| 840 | + } else { |
|
| 841 | + echo "\033[31m"; |
|
| 842 | + } |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + echo 'E'; |
|
| 846 | + |
|
| 847 | + if ($this->config->colors === true) { |
|
| 848 | + echo "\033[0m"; |
|
| 849 | + } |
|
| 850 | + } else if ($warnings > 0) { |
|
| 851 | + if ($this->config->colors === true) { |
|
| 852 | + if ($fixable > 0) { |
|
| 853 | + echo "\033[32m"; |
|
| 854 | + } else { |
|
| 855 | + echo "\033[33m"; |
|
| 856 | + } |
|
| 857 | + } |
|
| 858 | + |
|
| 859 | + echo 'W'; |
|
| 860 | + |
|
| 861 | + if ($this->config->colors === true) { |
|
| 862 | + echo "\033[0m"; |
|
| 863 | + } |
|
| 864 | + } else { |
|
| 865 | + echo '.'; |
|
| 866 | + }//end if |
|
| 867 | + }//end if |
|
| 868 | + }//end if |
|
| 869 | + |
|
| 870 | + $numPerLine = 60; |
|
| 871 | + if ($numProcessed !== $numFiles && ($numProcessed % $numPerLine) !== 0) { |
|
| 872 | + return; |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + $percent = round(($numProcessed / $numFiles) * 100); |
|
| 876 | + $padding = (strlen($numFiles) - strlen($numProcessed)); |
|
| 877 | + if ($numProcessed === $numFiles |
|
| 878 | + && $numFiles > $numPerLine |
|
| 879 | + && ($numProcessed % $numPerLine) !== 0 |
|
| 880 | + ) { |
|
| 881 | + $padding += ($numPerLine - ($numFiles - (floor($numFiles / $numPerLine) * $numPerLine))); |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + echo str_repeat(' ', $padding)." $numProcessed / $numFiles ($percent%)".PHP_EOL; |
|
| 885 | + |
|
| 886 | + }//end printProgress() |
|
| 887 | 887 | |
| 888 | 888 | |
| 889 | 889 | }//end class |