@@ -32,7 +32,7 @@ |
||
| 32 | 32 | /** |
| 33 | 33 | * Returns an array of tokens this test wants to listen for. |
| 34 | 34 | * |
| 35 | - * @return array |
|
| 35 | + * @return integer[] |
|
| 36 | 36 | */ |
| 37 | 37 | public function register() |
| 38 | 38 | { |
@@ -29,89 +29,89 @@ |
||
| 29 | 29 | { |
| 30 | 30 | |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Returns an array of tokens this test wants to listen for. |
|
| 34 | - * |
|
| 35 | - * @return array |
|
| 36 | - */ |
|
| 37 | - public function register() |
|
| 38 | - { |
|
| 39 | - return array( |
|
| 40 | - T_CLASS, |
|
| 41 | - T_INTERFACE, |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - }//end register() |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Processes this test, when one of its tokens is encountered. |
|
| 49 | - * |
|
| 50 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 51 | - * @param int $stackPtr The position of the current token |
|
| 52 | - * in the stack passed in $tokens. |
|
| 53 | - * |
|
| 54 | - * @return void |
|
| 55 | - */ |
|
| 56 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 57 | - { |
|
| 58 | - $this->currentFile = $phpcsFile; |
|
| 59 | - |
|
| 60 | - $tokens = $phpcsFile->getTokens(); |
|
| 61 | - $type = strtolower($tokens[$stackPtr]['content']); |
|
| 62 | - $errorData = array($type); |
|
| 63 | - |
|
| 64 | - $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
|
| 65 | - $find[] = T_WHITESPACE; |
|
| 66 | - |
|
| 67 | - $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
| 68 | - if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 69 | - && $tokens[$commentEnd]['code'] !== T_COMMENT |
|
| 70 | - ) { |
|
| 71 | - $phpcsFile->addError('Missing class doc comment', $stackPtr, 'Missing'); |
|
| 72 | - $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'no'); |
|
| 73 | - return; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'yes'); |
|
| 77 | - |
|
| 78 | - if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
| 79 | - $phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle'); |
|
| 80 | - return; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // Check each tag. |
|
| 84 | - $this->processTags($phpcsFile, $stackPtr, $tokens[$commentEnd]['comment_opener']); |
|
| 85 | - |
|
| 86 | - }//end process() |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Process the version tag. |
|
| 91 | - * |
|
| 92 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 93 | - * @param array $tags The tokens for these tags. |
|
| 94 | - * |
|
| 95 | - * @return void |
|
| 96 | - */ |
|
| 97 | - protected function processVersion(PHP_CodeSniffer_File $phpcsFile, array $tags) |
|
| 98 | - { |
|
| 99 | - $tokens = $phpcsFile->getTokens(); |
|
| 100 | - foreach ($tags as $tag) { |
|
| 101 | - if ($tokens[($tag + 2)]['code'] !== T_DOC_COMMENT_STRING) { |
|
| 102 | - // No content. |
|
| 103 | - continue; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $content = $tokens[($tag + 2)]['content']; |
|
| 107 | - if ((strstr($content, 'Release:') === false)) { |
|
| 108 | - $error = 'Invalid version "%s" in doc comment; consider "Release: <package_version>" instead'; |
|
| 109 | - $data = array($content); |
|
| 110 | - $phpcsFile->addWarning($error, $tag, 'InvalidVersion', $data); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - }//end processVersion() |
|
| 32 | + /** |
|
| 33 | + * Returns an array of tokens this test wants to listen for. |
|
| 34 | + * |
|
| 35 | + * @return array |
|
| 36 | + */ |
|
| 37 | + public function register() |
|
| 38 | + { |
|
| 39 | + return array( |
|
| 40 | + T_CLASS, |
|
| 41 | + T_INTERFACE, |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + }//end register() |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Processes this test, when one of its tokens is encountered. |
|
| 49 | + * |
|
| 50 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 51 | + * @param int $stackPtr The position of the current token |
|
| 52 | + * in the stack passed in $tokens. |
|
| 53 | + * |
|
| 54 | + * @return void |
|
| 55 | + */ |
|
| 56 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 57 | + { |
|
| 58 | + $this->currentFile = $phpcsFile; |
|
| 59 | + |
|
| 60 | + $tokens = $phpcsFile->getTokens(); |
|
| 61 | + $type = strtolower($tokens[$stackPtr]['content']); |
|
| 62 | + $errorData = array($type); |
|
| 63 | + |
|
| 64 | + $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
|
| 65 | + $find[] = T_WHITESPACE; |
|
| 66 | + |
|
| 67 | + $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
| 68 | + if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 69 | + && $tokens[$commentEnd]['code'] !== T_COMMENT |
|
| 70 | + ) { |
|
| 71 | + $phpcsFile->addError('Missing class doc comment', $stackPtr, 'Missing'); |
|
| 72 | + $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'no'); |
|
| 73 | + return; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'yes'); |
|
| 77 | + |
|
| 78 | + if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
| 79 | + $phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle'); |
|
| 80 | + return; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // Check each tag. |
|
| 84 | + $this->processTags($phpcsFile, $stackPtr, $tokens[$commentEnd]['comment_opener']); |
|
| 85 | + |
|
| 86 | + }//end process() |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Process the version tag. |
|
| 91 | + * |
|
| 92 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 93 | + * @param array $tags The tokens for these tags. |
|
| 94 | + * |
|
| 95 | + * @return void |
|
| 96 | + */ |
|
| 97 | + protected function processVersion(PHP_CodeSniffer_File $phpcsFile, array $tags) |
|
| 98 | + { |
|
| 99 | + $tokens = $phpcsFile->getTokens(); |
|
| 100 | + foreach ($tags as $tag) { |
|
| 101 | + if ($tokens[($tag + 2)]['code'] !== T_DOC_COMMENT_STRING) { |
|
| 102 | + // No content. |
|
| 103 | + continue; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $content = $tokens[($tag + 2)]['content']; |
|
| 107 | + if ((strstr($content, 'Release:') === false)) { |
|
| 108 | + $error = 'Invalid version "%s" in doc comment; consider "Release: <package_version>" instead'; |
|
| 109 | + $data = array($content); |
|
| 110 | + $phpcsFile->addWarning($error, $tag, 'InvalidVersion', $data); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + }//end processVersion() |
|
| 115 | 115 | |
| 116 | 116 | |
| 117 | 117 | }//end class |
@@ -53,35 +53,35 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @return void |
| 55 | 55 | */ |
| 56 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 56 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 57 | 57 | { |
| 58 | 58 | $this->currentFile = $phpcsFile; |
| 59 | 59 | |
| 60 | 60 | $tokens = $phpcsFile->getTokens(); |
| 61 | - $type = strtolower($tokens[$stackPtr]['content']); |
|
| 62 | - $errorData = array($type); |
|
| 61 | + $type = strtolower( $tokens[ $stackPtr ][ 'content' ] ); |
|
| 62 | + $errorData = array( $type ); |
|
| 63 | 63 | |
| 64 | 64 | $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
| 65 | - $find[] = T_WHITESPACE; |
|
| 65 | + $find[ ] = T_WHITESPACE; |
|
| 66 | 66 | |
| 67 | - $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
| 68 | - if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 69 | - && $tokens[$commentEnd]['code'] !== T_COMMENT |
|
| 67 | + $commentEnd = $phpcsFile->findPrevious( $find, ( $stackPtr - 1 ), null, true ); |
|
| 68 | + if ( $tokens[ $commentEnd ][ 'code' ] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 69 | + && $tokens[ $commentEnd ][ 'code' ] !== T_COMMENT |
|
| 70 | 70 | ) { |
| 71 | - $phpcsFile->addError('Missing class doc comment', $stackPtr, 'Missing'); |
|
| 72 | - $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'no'); |
|
| 71 | + $phpcsFile->addError( 'Missing class doc comment', $stackPtr, 'Missing' ); |
|
| 72 | + $phpcsFile->recordMetric( $stackPtr, 'Class has doc comment', 'no' ); |
|
| 73 | 73 | return; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'yes'); |
|
| 76 | + $phpcsFile->recordMetric( $stackPtr, 'Class has doc comment', 'yes' ); |
|
| 77 | 77 | |
| 78 | - if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
| 79 | - $phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle'); |
|
| 78 | + if ( $tokens[ $commentEnd ][ 'code' ] === T_COMMENT ) { |
|
| 79 | + $phpcsFile->addError( 'You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle' ); |
|
| 80 | 80 | return; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | // Check each tag. |
| 84 | - $this->processTags($phpcsFile, $stackPtr, $tokens[$commentEnd]['comment_opener']); |
|
| 84 | + $this->processTags( $phpcsFile, $stackPtr, $tokens[ $commentEnd ][ 'comment_opener' ] ); |
|
| 85 | 85 | |
| 86 | 86 | }//end process() |
| 87 | 87 | |
@@ -94,20 +94,20 @@ discard block |
||
| 94 | 94 | * |
| 95 | 95 | * @return void |
| 96 | 96 | */ |
| 97 | - protected function processVersion(PHP_CodeSniffer_File $phpcsFile, array $tags) |
|
| 97 | + protected function processVersion( PHP_CodeSniffer_File $phpcsFile, array $tags ) |
|
| 98 | 98 | { |
| 99 | 99 | $tokens = $phpcsFile->getTokens(); |
| 100 | - foreach ($tags as $tag) { |
|
| 101 | - if ($tokens[($tag + 2)]['code'] !== T_DOC_COMMENT_STRING) { |
|
| 100 | + foreach ( $tags as $tag ) { |
|
| 101 | + if ( $tokens[ ( $tag + 2 ) ][ 'code' ] !== T_DOC_COMMENT_STRING ) { |
|
| 102 | 102 | // No content. |
| 103 | 103 | continue; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | - $content = $tokens[($tag + 2)]['content']; |
|
| 107 | - if ((strstr($content, 'Release:') === false)) { |
|
| 106 | + $content = $tokens[ ( $tag + 2 ) ][ 'content' ]; |
|
| 107 | + if ( ( strstr( $content, 'Release:' ) === false ) ) { |
|
| 108 | 108 | $error = 'Invalid version "%s" in doc comment; consider "Release: <package_version>" instead'; |
| 109 | - $data = array($content); |
|
| 110 | - $phpcsFile->addWarning($error, $tag, 'InvalidVersion', $data); |
|
| 109 | + $data = array( $content ); |
|
| 110 | + $phpcsFile->addWarning( $error, $tag, 'InvalidVersion', $data ); |
|
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * @version Release: @package_version@ |
| 26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 27 | 27 | */ |
| 28 | -class PEAR_Sniffs_Commenting_ClassCommentSniff extends PEAR_Sniffs_Commenting_FileCommentSniff |
|
| 29 | -{ |
|
| 28 | +class PEAR_Sniffs_Commenting_ClassCommentSniff extends PEAR_Sniffs_Commenting_FileCommentSniff { |
|
| 30 | 29 | |
| 31 | 30 | |
| 32 | 31 | /** |
@@ -34,8 +33,7 @@ discard block |
||
| 34 | 33 | * |
| 35 | 34 | * @return array |
| 36 | 35 | */ |
| 37 | - public function register() |
|
| 38 | - { |
|
| 36 | + public function register() { |
|
| 39 | 37 | return array( |
| 40 | 38 | T_CLASS, |
| 41 | 39 | T_INTERFACE, |
@@ -53,8 +51,7 @@ discard block |
||
| 53 | 51 | * |
| 54 | 52 | * @return void |
| 55 | 53 | */ |
| 56 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 57 | - { |
|
| 54 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 58 | 55 | $this->currentFile = $phpcsFile; |
| 59 | 56 | |
| 60 | 57 | $tokens = $phpcsFile->getTokens(); |
@@ -94,8 +91,7 @@ discard block |
||
| 94 | 91 | * |
| 95 | 92 | * @return void |
| 96 | 93 | */ |
| 97 | - protected function processVersion(PHP_CodeSniffer_File $phpcsFile, array $tags) |
|
| 98 | - { |
|
| 94 | + protected function processVersion(PHP_CodeSniffer_File $phpcsFile, array $tags) { |
|
| 99 | 95 | $tokens = $phpcsFile->getTokens(); |
| 100 | 96 | foreach ($tags as $tag) { |
| 101 | 97 | if ($tokens[($tag + 2)]['code'] !== T_DOC_COMMENT_STRING) { |
@@ -32,7 +32,7 @@ |
||
| 32 | 32 | /** |
| 33 | 33 | * Returns an array of tokens this test wants to listen for. |
| 34 | 34 | * |
| 35 | - * @return array |
|
| 35 | + * @return integer[] |
|
| 36 | 36 | */ |
| 37 | 37 | public function register() |
| 38 | 38 | { |
@@ -29,363 +29,363 @@ |
||
| 29 | 29 | { |
| 30 | 30 | |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Returns an array of tokens this test wants to listen for. |
|
| 34 | - * |
|
| 35 | - * @return array |
|
| 36 | - */ |
|
| 37 | - public function register() |
|
| 38 | - { |
|
| 39 | - return array(T_FUNCTION); |
|
| 40 | - |
|
| 41 | - }//end register() |
|
| 42 | - |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Processes this test, when one of its tokens is encountered. |
|
| 46 | - * |
|
| 47 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 48 | - * @param int $stackPtr The position of the current token |
|
| 49 | - * in the stack passed in $tokens. |
|
| 50 | - * |
|
| 51 | - * @return void |
|
| 52 | - */ |
|
| 53 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 54 | - { |
|
| 55 | - $tokens = $phpcsFile->getTokens(); |
|
| 56 | - $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
|
| 57 | - $find[] = T_WHITESPACE; |
|
| 58 | - |
|
| 59 | - $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
| 60 | - if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
| 61 | - // Inline comments might just be closing comments for |
|
| 62 | - // control structures or functions instead of function comments |
|
| 63 | - // using the wrong comment type. If there is other code on the line, |
|
| 64 | - // assume they relate to that code. |
|
| 65 | - $prev = $phpcsFile->findPrevious($find, ($commentEnd - 1), null, true); |
|
| 66 | - if ($prev !== false && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) { |
|
| 67 | - $commentEnd = $prev; |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 72 | - && $tokens[$commentEnd]['code'] !== T_COMMENT |
|
| 73 | - ) { |
|
| 74 | - $phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing'); |
|
| 75 | - $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'no'); |
|
| 76 | - return; |
|
| 77 | - } else { |
|
| 78 | - $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'yes'); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
| 82 | - $phpcsFile->addError('You must use "/**" style comments for a function comment', $stackPtr, 'WrongStyle'); |
|
| 83 | - return; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) { |
|
| 87 | - $error = 'There must be no blank lines after the function comment'; |
|
| 88 | - $phpcsFile->addError($error, $commentEnd, 'SpacingAfter'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $commentStart = $tokens[$commentEnd]['comment_opener']; |
|
| 92 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 93 | - if ($tokens[$tag]['content'] === '@see') { |
|
| 94 | - // Make sure the tag isn't empty. |
|
| 95 | - $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
|
| 96 | - if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) { |
|
| 97 | - $error = 'Content missing for @see tag in function comment'; |
|
| 98 | - $phpcsFile->addError($error, $tag, 'EmptySees'); |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $this->processReturn($phpcsFile, $stackPtr, $commentStart); |
|
| 104 | - $this->processThrows($phpcsFile, $stackPtr, $commentStart); |
|
| 105 | - $this->processParams($phpcsFile, $stackPtr, $commentStart); |
|
| 106 | - |
|
| 107 | - }//end process() |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Process the return comment of this function comment. |
|
| 112 | - * |
|
| 113 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 114 | - * @param int $stackPtr The position of the current token |
|
| 115 | - * in the stack passed in $tokens. |
|
| 116 | - * @param int $commentStart The position in the stack where the comment started. |
|
| 117 | - * |
|
| 118 | - * @return void |
|
| 119 | - */ |
|
| 120 | - protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 121 | - { |
|
| 122 | - $tokens = $phpcsFile->getTokens(); |
|
| 123 | - |
|
| 124 | - // Skip constructor and destructor. |
|
| 125 | - $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
| 126 | - $isSpecialMethod = ($methodName === '__construct' || $methodName === '__destruct'); |
|
| 127 | - |
|
| 128 | - $return = null; |
|
| 129 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 130 | - if ($tokens[$tag]['content'] === '@return') { |
|
| 131 | - if ($return !== null) { |
|
| 132 | - $error = 'Only 1 @return tag is allowed in a function comment'; |
|
| 133 | - $phpcsFile->addError($error, $tag, 'DuplicateReturn'); |
|
| 134 | - return; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - $return = $tag; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - if ($isSpecialMethod === true) { |
|
| 142 | - return; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if ($return !== null) { |
|
| 146 | - $content = $tokens[($return + 2)]['content']; |
|
| 147 | - if (empty($content) === true || $tokens[($return + 2)]['code'] !== T_DOC_COMMENT_STRING) { |
|
| 148 | - $error = 'Return type missing for @return tag in function comment'; |
|
| 149 | - $phpcsFile->addError($error, $return, 'MissingReturnType'); |
|
| 150 | - } |
|
| 151 | - } else { |
|
| 152 | - $error = 'Missing @return tag in function comment'; |
|
| 153 | - $phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn'); |
|
| 154 | - }//end if |
|
| 155 | - |
|
| 156 | - }//end processReturn() |
|
| 157 | - |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Process any throw tags that this function comment has. |
|
| 161 | - * |
|
| 162 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 163 | - * @param int $stackPtr The position of the current token |
|
| 164 | - * in the stack passed in $tokens. |
|
| 165 | - * @param int $commentStart The position in the stack where the comment started. |
|
| 166 | - * |
|
| 167 | - * @return void |
|
| 168 | - */ |
|
| 169 | - protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 170 | - { |
|
| 171 | - $tokens = $phpcsFile->getTokens(); |
|
| 172 | - |
|
| 173 | - $throws = array(); |
|
| 174 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 175 | - if ($tokens[$tag]['content'] !== '@throws') { |
|
| 176 | - continue; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $exception = null; |
|
| 180 | - $comment = null; |
|
| 181 | - if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) { |
|
| 182 | - $matches = array(); |
|
| 183 | - preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($tag + 2)]['content'], $matches); |
|
| 184 | - $exception = $matches[1]; |
|
| 185 | - if (isset($matches[2]) === true) { |
|
| 186 | - $comment = $matches[2]; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - if ($exception === null) { |
|
| 191 | - $error = 'Exception type missing for @throws tag in function comment'; |
|
| 192 | - $phpcsFile->addError($error, $tag, 'InvalidThrows'); |
|
| 193 | - } |
|
| 194 | - }//end foreach |
|
| 195 | - |
|
| 196 | - }//end processThrows() |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Process the function parameter comments. |
|
| 201 | - * |
|
| 202 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 203 | - * @param int $stackPtr The position of the current token |
|
| 204 | - * in the stack passed in $tokens. |
|
| 205 | - * @param int $commentStart The position in the stack where the comment started. |
|
| 206 | - * |
|
| 207 | - * @return void |
|
| 208 | - */ |
|
| 209 | - protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 210 | - { |
|
| 211 | - $tokens = $phpcsFile->getTokens(); |
|
| 212 | - |
|
| 213 | - $params = array(); |
|
| 214 | - $maxType = 0; |
|
| 215 | - $maxVar = 0; |
|
| 216 | - foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { |
|
| 217 | - if ($tokens[$tag]['content'] !== '@param') { |
|
| 218 | - continue; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $type = ''; |
|
| 222 | - $typeSpace = 0; |
|
| 223 | - $var = ''; |
|
| 224 | - $varSpace = 0; |
|
| 225 | - $comment = ''; |
|
| 226 | - if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) { |
|
| 227 | - $matches = array(); |
|
| 228 | - preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches); |
|
| 229 | - |
|
| 230 | - if (empty($matches) === false) { |
|
| 231 | - $typeLen = strlen($matches[1]); |
|
| 232 | - $type = trim($matches[1]); |
|
| 233 | - $typeSpace = ($typeLen - strlen($type)); |
|
| 234 | - $typeLen = strlen($type); |
|
| 235 | - if ($typeLen > $maxType) { |
|
| 236 | - $maxType = $typeLen; |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - if (isset($matches[2]) === true) { |
|
| 241 | - $var = $matches[2]; |
|
| 242 | - $varLen = strlen($var); |
|
| 243 | - if ($varLen > $maxVar) { |
|
| 244 | - $maxVar = $varLen; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - if (isset($matches[4]) === true) { |
|
| 248 | - $varSpace = strlen($matches[3]); |
|
| 249 | - $comment = $matches[4]; |
|
| 250 | - |
|
| 251 | - // Any strings until the next tag belong to this comment. |
|
| 252 | - if (isset($tokens[$commentStart]['comment_tags'][($pos + 1)]) === true) { |
|
| 253 | - $end = $tokens[$commentStart]['comment_tags'][($pos + 1)]; |
|
| 254 | - } else { |
|
| 255 | - $end = $tokens[$commentStart]['comment_closer']; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - for ($i = ($tag + 3); $i < $end; $i++) { |
|
| 259 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) { |
|
| 260 | - $comment .= ' '.$tokens[$i]['content']; |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - } else { |
|
| 264 | - $error = 'Missing parameter comment'; |
|
| 265 | - $phpcsFile->addError($error, $tag, 'MissingParamComment'); |
|
| 266 | - } |
|
| 267 | - } else { |
|
| 268 | - $error = 'Missing parameter name'; |
|
| 269 | - $phpcsFile->addError($error, $tag, 'MissingParamName'); |
|
| 270 | - }//end if |
|
| 271 | - } else { |
|
| 272 | - $error = 'Missing parameter type'; |
|
| 273 | - $phpcsFile->addError($error, $tag, 'MissingParamType'); |
|
| 274 | - }//end if |
|
| 275 | - |
|
| 276 | - $params[] = array( |
|
| 277 | - 'tag' => $tag, |
|
| 278 | - 'type' => $type, |
|
| 279 | - 'var' => $var, |
|
| 280 | - 'comment' => $comment, |
|
| 281 | - 'type_space' => $typeSpace, |
|
| 282 | - 'var_space' => $varSpace, |
|
| 283 | - ); |
|
| 284 | - }//end foreach |
|
| 285 | - |
|
| 286 | - $realParams = $phpcsFile->getMethodParameters($stackPtr); |
|
| 287 | - $foundParams = array(); |
|
| 288 | - |
|
| 289 | - // We want to use ... for all variable length arguments, so added |
|
| 290 | - // this prefix to the variable name so comparisons are easier. |
|
| 291 | - foreach ($realParams as $pos => $param) { |
|
| 292 | - if ($param['variable_length'] === true) { |
|
| 293 | - $realParams[$pos]['name'] = '...'.$realParams[$pos]['name']; |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - foreach ($params as $pos => $param) { |
|
| 298 | - if ($param['var'] === '') { |
|
| 299 | - continue; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - $foundParams[] = $param['var']; |
|
| 303 | - |
|
| 304 | - // Check number of spaces after the type. |
|
| 305 | - $spaces = ($maxType - strlen($param['type']) + 1); |
|
| 306 | - if ($param['type_space'] !== $spaces) { |
|
| 307 | - $error = 'Expected %s spaces after parameter type; %s found'; |
|
| 308 | - $data = array( |
|
| 309 | - $spaces, |
|
| 310 | - $param['type_space'], |
|
| 311 | - ); |
|
| 312 | - |
|
| 313 | - $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data); |
|
| 314 | - if ($fix === true) { |
|
| 315 | - $content = $param['type']; |
|
| 316 | - $content .= str_repeat(' ', $spaces); |
|
| 317 | - $content .= $param['var']; |
|
| 318 | - $content .= str_repeat(' ', $param['var_space']); |
|
| 319 | - $content .= $param['comment']; |
|
| 320 | - $phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content); |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - // Make sure the param name is correct. |
|
| 325 | - if (isset($realParams[$pos]) === true) { |
|
| 326 | - $realName = $realParams[$pos]['name']; |
|
| 327 | - if ($realName !== $param['var']) { |
|
| 328 | - $code = 'ParamNameNoMatch'; |
|
| 329 | - $data = array( |
|
| 330 | - $param['var'], |
|
| 331 | - $realName, |
|
| 332 | - ); |
|
| 333 | - |
|
| 334 | - $error = 'Doc comment for parameter %s does not match '; |
|
| 335 | - if (strtolower($param['var']) === strtolower($realName)) { |
|
| 336 | - $error .= 'case of '; |
|
| 337 | - $code = 'ParamNameNoCaseMatch'; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - $error .= 'actual variable name %s'; |
|
| 341 | - |
|
| 342 | - $phpcsFile->addError($error, $param['tag'], $code, $data); |
|
| 343 | - } |
|
| 344 | - } else if (substr($param['var'], -4) !== ',...') { |
|
| 345 | - // We must have an extra parameter comment. |
|
| 346 | - $error = 'Superfluous parameter comment'; |
|
| 347 | - $phpcsFile->addError($error, $param['tag'], 'ExtraParamComment'); |
|
| 348 | - }//end if |
|
| 349 | - |
|
| 350 | - if ($param['comment'] === '') { |
|
| 351 | - continue; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - // Check number of spaces after the var name. |
|
| 355 | - $spaces = ($maxVar - strlen($param['var']) + 1); |
|
| 356 | - if ($param['var_space'] !== $spaces) { |
|
| 357 | - $error = 'Expected %s spaces after parameter name; %s found'; |
|
| 358 | - $data = array( |
|
| 359 | - $spaces, |
|
| 360 | - $param['var_space'], |
|
| 361 | - ); |
|
| 362 | - |
|
| 363 | - $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamName', $data); |
|
| 364 | - if ($fix === true) { |
|
| 365 | - $content = $param['type']; |
|
| 366 | - $content .= str_repeat(' ', $param['type_space']); |
|
| 367 | - $content .= $param['var']; |
|
| 368 | - $content .= str_repeat(' ', $spaces); |
|
| 369 | - $content .= $param['comment']; |
|
| 370 | - $phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content); |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - }//end foreach |
|
| 374 | - |
|
| 375 | - $realNames = array(); |
|
| 376 | - foreach ($realParams as $realParam) { |
|
| 377 | - $realNames[] = $realParam['name']; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - // Report missing comments. |
|
| 381 | - $diff = array_diff($realNames, $foundParams); |
|
| 382 | - foreach ($diff as $neededParam) { |
|
| 383 | - $error = 'Doc comment for parameter "%s" missing'; |
|
| 384 | - $data = array($neededParam); |
|
| 385 | - $phpcsFile->addError($error, $commentStart, 'MissingParamTag', $data); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - }//end processParams() |
|
| 32 | + /** |
|
| 33 | + * Returns an array of tokens this test wants to listen for. |
|
| 34 | + * |
|
| 35 | + * @return array |
|
| 36 | + */ |
|
| 37 | + public function register() |
|
| 38 | + { |
|
| 39 | + return array(T_FUNCTION); |
|
| 40 | + |
|
| 41 | + }//end register() |
|
| 42 | + |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Processes this test, when one of its tokens is encountered. |
|
| 46 | + * |
|
| 47 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 48 | + * @param int $stackPtr The position of the current token |
|
| 49 | + * in the stack passed in $tokens. |
|
| 50 | + * |
|
| 51 | + * @return void |
|
| 52 | + */ |
|
| 53 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 54 | + { |
|
| 55 | + $tokens = $phpcsFile->getTokens(); |
|
| 56 | + $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
|
| 57 | + $find[] = T_WHITESPACE; |
|
| 58 | + |
|
| 59 | + $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
| 60 | + if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
| 61 | + // Inline comments might just be closing comments for |
|
| 62 | + // control structures or functions instead of function comments |
|
| 63 | + // using the wrong comment type. If there is other code on the line, |
|
| 64 | + // assume they relate to that code. |
|
| 65 | + $prev = $phpcsFile->findPrevious($find, ($commentEnd - 1), null, true); |
|
| 66 | + if ($prev !== false && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) { |
|
| 67 | + $commentEnd = $prev; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 72 | + && $tokens[$commentEnd]['code'] !== T_COMMENT |
|
| 73 | + ) { |
|
| 74 | + $phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing'); |
|
| 75 | + $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'no'); |
|
| 76 | + return; |
|
| 77 | + } else { |
|
| 78 | + $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'yes'); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
| 82 | + $phpcsFile->addError('You must use "/**" style comments for a function comment', $stackPtr, 'WrongStyle'); |
|
| 83 | + return; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) { |
|
| 87 | + $error = 'There must be no blank lines after the function comment'; |
|
| 88 | + $phpcsFile->addError($error, $commentEnd, 'SpacingAfter'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $commentStart = $tokens[$commentEnd]['comment_opener']; |
|
| 92 | + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 93 | + if ($tokens[$tag]['content'] === '@see') { |
|
| 94 | + // Make sure the tag isn't empty. |
|
| 95 | + $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
|
| 96 | + if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) { |
|
| 97 | + $error = 'Content missing for @see tag in function comment'; |
|
| 98 | + $phpcsFile->addError($error, $tag, 'EmptySees'); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $this->processReturn($phpcsFile, $stackPtr, $commentStart); |
|
| 104 | + $this->processThrows($phpcsFile, $stackPtr, $commentStart); |
|
| 105 | + $this->processParams($phpcsFile, $stackPtr, $commentStart); |
|
| 106 | + |
|
| 107 | + }//end process() |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Process the return comment of this function comment. |
|
| 112 | + * |
|
| 113 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 114 | + * @param int $stackPtr The position of the current token |
|
| 115 | + * in the stack passed in $tokens. |
|
| 116 | + * @param int $commentStart The position in the stack where the comment started. |
|
| 117 | + * |
|
| 118 | + * @return void |
|
| 119 | + */ |
|
| 120 | + protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 121 | + { |
|
| 122 | + $tokens = $phpcsFile->getTokens(); |
|
| 123 | + |
|
| 124 | + // Skip constructor and destructor. |
|
| 125 | + $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
| 126 | + $isSpecialMethod = ($methodName === '__construct' || $methodName === '__destruct'); |
|
| 127 | + |
|
| 128 | + $return = null; |
|
| 129 | + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 130 | + if ($tokens[$tag]['content'] === '@return') { |
|
| 131 | + if ($return !== null) { |
|
| 132 | + $error = 'Only 1 @return tag is allowed in a function comment'; |
|
| 133 | + $phpcsFile->addError($error, $tag, 'DuplicateReturn'); |
|
| 134 | + return; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + $return = $tag; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + if ($isSpecialMethod === true) { |
|
| 142 | + return; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if ($return !== null) { |
|
| 146 | + $content = $tokens[($return + 2)]['content']; |
|
| 147 | + if (empty($content) === true || $tokens[($return + 2)]['code'] !== T_DOC_COMMENT_STRING) { |
|
| 148 | + $error = 'Return type missing for @return tag in function comment'; |
|
| 149 | + $phpcsFile->addError($error, $return, 'MissingReturnType'); |
|
| 150 | + } |
|
| 151 | + } else { |
|
| 152 | + $error = 'Missing @return tag in function comment'; |
|
| 153 | + $phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn'); |
|
| 154 | + }//end if |
|
| 155 | + |
|
| 156 | + }//end processReturn() |
|
| 157 | + |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Process any throw tags that this function comment has. |
|
| 161 | + * |
|
| 162 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 163 | + * @param int $stackPtr The position of the current token |
|
| 164 | + * in the stack passed in $tokens. |
|
| 165 | + * @param int $commentStart The position in the stack where the comment started. |
|
| 166 | + * |
|
| 167 | + * @return void |
|
| 168 | + */ |
|
| 169 | + protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 170 | + { |
|
| 171 | + $tokens = $phpcsFile->getTokens(); |
|
| 172 | + |
|
| 173 | + $throws = array(); |
|
| 174 | + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 175 | + if ($tokens[$tag]['content'] !== '@throws') { |
|
| 176 | + continue; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $exception = null; |
|
| 180 | + $comment = null; |
|
| 181 | + if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) { |
|
| 182 | + $matches = array(); |
|
| 183 | + preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($tag + 2)]['content'], $matches); |
|
| 184 | + $exception = $matches[1]; |
|
| 185 | + if (isset($matches[2]) === true) { |
|
| 186 | + $comment = $matches[2]; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + if ($exception === null) { |
|
| 191 | + $error = 'Exception type missing for @throws tag in function comment'; |
|
| 192 | + $phpcsFile->addError($error, $tag, 'InvalidThrows'); |
|
| 193 | + } |
|
| 194 | + }//end foreach |
|
| 195 | + |
|
| 196 | + }//end processThrows() |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Process the function parameter comments. |
|
| 201 | + * |
|
| 202 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 203 | + * @param int $stackPtr The position of the current token |
|
| 204 | + * in the stack passed in $tokens. |
|
| 205 | + * @param int $commentStart The position in the stack where the comment started. |
|
| 206 | + * |
|
| 207 | + * @return void |
|
| 208 | + */ |
|
| 209 | + protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 210 | + { |
|
| 211 | + $tokens = $phpcsFile->getTokens(); |
|
| 212 | + |
|
| 213 | + $params = array(); |
|
| 214 | + $maxType = 0; |
|
| 215 | + $maxVar = 0; |
|
| 216 | + foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { |
|
| 217 | + if ($tokens[$tag]['content'] !== '@param') { |
|
| 218 | + continue; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $type = ''; |
|
| 222 | + $typeSpace = 0; |
|
| 223 | + $var = ''; |
|
| 224 | + $varSpace = 0; |
|
| 225 | + $comment = ''; |
|
| 226 | + if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) { |
|
| 227 | + $matches = array(); |
|
| 228 | + preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches); |
|
| 229 | + |
|
| 230 | + if (empty($matches) === false) { |
|
| 231 | + $typeLen = strlen($matches[1]); |
|
| 232 | + $type = trim($matches[1]); |
|
| 233 | + $typeSpace = ($typeLen - strlen($type)); |
|
| 234 | + $typeLen = strlen($type); |
|
| 235 | + if ($typeLen > $maxType) { |
|
| 236 | + $maxType = $typeLen; |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + if (isset($matches[2]) === true) { |
|
| 241 | + $var = $matches[2]; |
|
| 242 | + $varLen = strlen($var); |
|
| 243 | + if ($varLen > $maxVar) { |
|
| 244 | + $maxVar = $varLen; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + if (isset($matches[4]) === true) { |
|
| 248 | + $varSpace = strlen($matches[3]); |
|
| 249 | + $comment = $matches[4]; |
|
| 250 | + |
|
| 251 | + // Any strings until the next tag belong to this comment. |
|
| 252 | + if (isset($tokens[$commentStart]['comment_tags'][($pos + 1)]) === true) { |
|
| 253 | + $end = $tokens[$commentStart]['comment_tags'][($pos + 1)]; |
|
| 254 | + } else { |
|
| 255 | + $end = $tokens[$commentStart]['comment_closer']; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + for ($i = ($tag + 3); $i < $end; $i++) { |
|
| 259 | + if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) { |
|
| 260 | + $comment .= ' '.$tokens[$i]['content']; |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + } else { |
|
| 264 | + $error = 'Missing parameter comment'; |
|
| 265 | + $phpcsFile->addError($error, $tag, 'MissingParamComment'); |
|
| 266 | + } |
|
| 267 | + } else { |
|
| 268 | + $error = 'Missing parameter name'; |
|
| 269 | + $phpcsFile->addError($error, $tag, 'MissingParamName'); |
|
| 270 | + }//end if |
|
| 271 | + } else { |
|
| 272 | + $error = 'Missing parameter type'; |
|
| 273 | + $phpcsFile->addError($error, $tag, 'MissingParamType'); |
|
| 274 | + }//end if |
|
| 275 | + |
|
| 276 | + $params[] = array( |
|
| 277 | + 'tag' => $tag, |
|
| 278 | + 'type' => $type, |
|
| 279 | + 'var' => $var, |
|
| 280 | + 'comment' => $comment, |
|
| 281 | + 'type_space' => $typeSpace, |
|
| 282 | + 'var_space' => $varSpace, |
|
| 283 | + ); |
|
| 284 | + }//end foreach |
|
| 285 | + |
|
| 286 | + $realParams = $phpcsFile->getMethodParameters($stackPtr); |
|
| 287 | + $foundParams = array(); |
|
| 288 | + |
|
| 289 | + // We want to use ... for all variable length arguments, so added |
|
| 290 | + // this prefix to the variable name so comparisons are easier. |
|
| 291 | + foreach ($realParams as $pos => $param) { |
|
| 292 | + if ($param['variable_length'] === true) { |
|
| 293 | + $realParams[$pos]['name'] = '...'.$realParams[$pos]['name']; |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + foreach ($params as $pos => $param) { |
|
| 298 | + if ($param['var'] === '') { |
|
| 299 | + continue; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + $foundParams[] = $param['var']; |
|
| 303 | + |
|
| 304 | + // Check number of spaces after the type. |
|
| 305 | + $spaces = ($maxType - strlen($param['type']) + 1); |
|
| 306 | + if ($param['type_space'] !== $spaces) { |
|
| 307 | + $error = 'Expected %s spaces after parameter type; %s found'; |
|
| 308 | + $data = array( |
|
| 309 | + $spaces, |
|
| 310 | + $param['type_space'], |
|
| 311 | + ); |
|
| 312 | + |
|
| 313 | + $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data); |
|
| 314 | + if ($fix === true) { |
|
| 315 | + $content = $param['type']; |
|
| 316 | + $content .= str_repeat(' ', $spaces); |
|
| 317 | + $content .= $param['var']; |
|
| 318 | + $content .= str_repeat(' ', $param['var_space']); |
|
| 319 | + $content .= $param['comment']; |
|
| 320 | + $phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content); |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + // Make sure the param name is correct. |
|
| 325 | + if (isset($realParams[$pos]) === true) { |
|
| 326 | + $realName = $realParams[$pos]['name']; |
|
| 327 | + if ($realName !== $param['var']) { |
|
| 328 | + $code = 'ParamNameNoMatch'; |
|
| 329 | + $data = array( |
|
| 330 | + $param['var'], |
|
| 331 | + $realName, |
|
| 332 | + ); |
|
| 333 | + |
|
| 334 | + $error = 'Doc comment for parameter %s does not match '; |
|
| 335 | + if (strtolower($param['var']) === strtolower($realName)) { |
|
| 336 | + $error .= 'case of '; |
|
| 337 | + $code = 'ParamNameNoCaseMatch'; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + $error .= 'actual variable name %s'; |
|
| 341 | + |
|
| 342 | + $phpcsFile->addError($error, $param['tag'], $code, $data); |
|
| 343 | + } |
|
| 344 | + } else if (substr($param['var'], -4) !== ',...') { |
|
| 345 | + // We must have an extra parameter comment. |
|
| 346 | + $error = 'Superfluous parameter comment'; |
|
| 347 | + $phpcsFile->addError($error, $param['tag'], 'ExtraParamComment'); |
|
| 348 | + }//end if |
|
| 349 | + |
|
| 350 | + if ($param['comment'] === '') { |
|
| 351 | + continue; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + // Check number of spaces after the var name. |
|
| 355 | + $spaces = ($maxVar - strlen($param['var']) + 1); |
|
| 356 | + if ($param['var_space'] !== $spaces) { |
|
| 357 | + $error = 'Expected %s spaces after parameter name; %s found'; |
|
| 358 | + $data = array( |
|
| 359 | + $spaces, |
|
| 360 | + $param['var_space'], |
|
| 361 | + ); |
|
| 362 | + |
|
| 363 | + $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamName', $data); |
|
| 364 | + if ($fix === true) { |
|
| 365 | + $content = $param['type']; |
|
| 366 | + $content .= str_repeat(' ', $param['type_space']); |
|
| 367 | + $content .= $param['var']; |
|
| 368 | + $content .= str_repeat(' ', $spaces); |
|
| 369 | + $content .= $param['comment']; |
|
| 370 | + $phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content); |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + }//end foreach |
|
| 374 | + |
|
| 375 | + $realNames = array(); |
|
| 376 | + foreach ($realParams as $realParam) { |
|
| 377 | + $realNames[] = $realParam['name']; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + // Report missing comments. |
|
| 381 | + $diff = array_diff($realNames, $foundParams); |
|
| 382 | + foreach ($diff as $neededParam) { |
|
| 383 | + $error = 'Doc comment for parameter "%s" missing'; |
|
| 384 | + $data = array($neededParam); |
|
| 385 | + $phpcsFile->addError($error, $commentStart, 'MissingParamTag', $data); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + }//end processParams() |
|
| 389 | 389 | |
| 390 | 390 | |
| 391 | 391 | }//end class |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | public function register() |
| 38 | 38 | { |
| 39 | - return array(T_FUNCTION); |
|
| 39 | + return array( T_FUNCTION ); |
|
| 40 | 40 | |
| 41 | 41 | }//end register() |
| 42 | 42 | |
@@ -50,59 +50,59 @@ discard block |
||
| 50 | 50 | * |
| 51 | 51 | * @return void |
| 52 | 52 | */ |
| 53 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 53 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 54 | 54 | { |
| 55 | 55 | $tokens = $phpcsFile->getTokens(); |
| 56 | 56 | $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
| 57 | - $find[] = T_WHITESPACE; |
|
| 57 | + $find[ ] = T_WHITESPACE; |
|
| 58 | 58 | |
| 59 | - $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
|
| 60 | - if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
| 59 | + $commentEnd = $phpcsFile->findPrevious( $find, ( $stackPtr - 1 ), null, true ); |
|
| 60 | + if ( $tokens[ $commentEnd ][ 'code' ] === T_COMMENT ) { |
|
| 61 | 61 | // Inline comments might just be closing comments for |
| 62 | 62 | // control structures or functions instead of function comments |
| 63 | 63 | // using the wrong comment type. If there is other code on the line, |
| 64 | 64 | // assume they relate to that code. |
| 65 | - $prev = $phpcsFile->findPrevious($find, ($commentEnd - 1), null, true); |
|
| 66 | - if ($prev !== false && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) { |
|
| 65 | + $prev = $phpcsFile->findPrevious( $find, ( $commentEnd - 1 ), null, true ); |
|
| 66 | + if ( $prev !== false && $tokens[ $prev ][ 'line' ] === $tokens[ $commentEnd ][ 'line' ] ) { |
|
| 67 | 67 | $commentEnd = $prev; |
| 68 | 68 | } |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 72 | - && $tokens[$commentEnd]['code'] !== T_COMMENT |
|
| 71 | + if ( $tokens[ $commentEnd ][ 'code' ] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 72 | + && $tokens[ $commentEnd ][ 'code' ] !== T_COMMENT |
|
| 73 | 73 | ) { |
| 74 | - $phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing'); |
|
| 75 | - $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'no'); |
|
| 74 | + $phpcsFile->addError( 'Missing function doc comment', $stackPtr, 'Missing' ); |
|
| 75 | + $phpcsFile->recordMetric( $stackPtr, 'Function has doc comment', 'no' ); |
|
| 76 | 76 | return; |
| 77 | 77 | } else { |
| 78 | - $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'yes'); |
|
| 78 | + $phpcsFile->recordMetric( $stackPtr, 'Function has doc comment', 'yes' ); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
|
| 82 | - $phpcsFile->addError('You must use "/**" style comments for a function comment', $stackPtr, 'WrongStyle'); |
|
| 81 | + if ( $tokens[ $commentEnd ][ 'code' ] === T_COMMENT ) { |
|
| 82 | + $phpcsFile->addError( 'You must use "/**" style comments for a function comment', $stackPtr, 'WrongStyle' ); |
|
| 83 | 83 | return; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) { |
|
| 86 | + if ( $tokens[ $commentEnd ][ 'line' ] !== ( $tokens[ $stackPtr ][ 'line' ] - 1 ) ) { |
|
| 87 | 87 | $error = 'There must be no blank lines after the function comment'; |
| 88 | - $phpcsFile->addError($error, $commentEnd, 'SpacingAfter'); |
|
| 88 | + $phpcsFile->addError( $error, $commentEnd, 'SpacingAfter' ); |
|
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - $commentStart = $tokens[$commentEnd]['comment_opener']; |
|
| 92 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 93 | - if ($tokens[$tag]['content'] === '@see') { |
|
| 91 | + $commentStart = $tokens[ $commentEnd ][ 'comment_opener' ]; |
|
| 92 | + foreach ( $tokens[ $commentStart ][ 'comment_tags' ] as $tag ) { |
|
| 93 | + if ( $tokens[ $tag ][ 'content' ] === '@see' ) { |
|
| 94 | 94 | // Make sure the tag isn't empty. |
| 95 | - $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
|
| 96 | - if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) { |
|
| 95 | + $string = $phpcsFile->findNext( T_DOC_COMMENT_STRING, $tag, $commentEnd ); |
|
| 96 | + if ( $string === false || $tokens[ $string ][ 'line' ] !== $tokens[ $tag ][ 'line' ] ) { |
|
| 97 | 97 | $error = 'Content missing for @see tag in function comment'; |
| 98 | - $phpcsFile->addError($error, $tag, 'EmptySees'); |
|
| 98 | + $phpcsFile->addError( $error, $tag, 'EmptySees' ); |
|
| 99 | 99 | } |
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - $this->processReturn($phpcsFile, $stackPtr, $commentStart); |
|
| 104 | - $this->processThrows($phpcsFile, $stackPtr, $commentStart); |
|
| 105 | - $this->processParams($phpcsFile, $stackPtr, $commentStart); |
|
| 103 | + $this->processReturn( $phpcsFile, $stackPtr, $commentStart ); |
|
| 104 | + $this->processThrows( $phpcsFile, $stackPtr, $commentStart ); |
|
| 105 | + $this->processParams( $phpcsFile, $stackPtr, $commentStart ); |
|
| 106 | 106 | |
| 107 | 107 | }//end process() |
| 108 | 108 | |
@@ -117,20 +117,20 @@ discard block |
||
| 117 | 117 | * |
| 118 | 118 | * @return void |
| 119 | 119 | */ |
| 120 | - protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 120 | + protected function processReturn( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart ) |
|
| 121 | 121 | { |
| 122 | 122 | $tokens = $phpcsFile->getTokens(); |
| 123 | 123 | |
| 124 | 124 | // Skip constructor and destructor. |
| 125 | - $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
| 126 | - $isSpecialMethod = ($methodName === '__construct' || $methodName === '__destruct'); |
|
| 125 | + $methodName = $phpcsFile->getDeclarationName( $stackPtr ); |
|
| 126 | + $isSpecialMethod = ( $methodName === '__construct' || $methodName === '__destruct' ); |
|
| 127 | 127 | |
| 128 | 128 | $return = null; |
| 129 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 130 | - if ($tokens[$tag]['content'] === '@return') { |
|
| 131 | - if ($return !== null) { |
|
| 129 | + foreach ( $tokens[ $commentStart ][ 'comment_tags' ] as $tag ) { |
|
| 130 | + if ( $tokens[ $tag ][ 'content' ] === '@return' ) { |
|
| 131 | + if ( $return !== null ) { |
|
| 132 | 132 | $error = 'Only 1 @return tag is allowed in a function comment'; |
| 133 | - $phpcsFile->addError($error, $tag, 'DuplicateReturn'); |
|
| 133 | + $phpcsFile->addError( $error, $tag, 'DuplicateReturn' ); |
|
| 134 | 134 | return; |
| 135 | 135 | } |
| 136 | 136 | |
@@ -138,19 +138,19 @@ discard block |
||
| 138 | 138 | } |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | - if ($isSpecialMethod === true) { |
|
| 141 | + if ( $isSpecialMethod === true ) { |
|
| 142 | 142 | return; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - if ($return !== null) { |
|
| 146 | - $content = $tokens[($return + 2)]['content']; |
|
| 147 | - if (empty($content) === true || $tokens[($return + 2)]['code'] !== T_DOC_COMMENT_STRING) { |
|
| 145 | + if ( $return !== null ) { |
|
| 146 | + $content = $tokens[ ( $return + 2 ) ][ 'content' ]; |
|
| 147 | + if ( empty( $content ) === true || $tokens[ ( $return + 2 ) ][ 'code' ] !== T_DOC_COMMENT_STRING ) { |
|
| 148 | 148 | $error = 'Return type missing for @return tag in function comment'; |
| 149 | - $phpcsFile->addError($error, $return, 'MissingReturnType'); |
|
| 149 | + $phpcsFile->addError( $error, $return, 'MissingReturnType' ); |
|
| 150 | 150 | } |
| 151 | 151 | } else { |
| 152 | 152 | $error = 'Missing @return tag in function comment'; |
| 153 | - $phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn'); |
|
| 153 | + $phpcsFile->addError( $error, $tokens[ $commentStart ][ 'comment_closer' ], 'MissingReturn' ); |
|
| 154 | 154 | }//end if |
| 155 | 155 | |
| 156 | 156 | }//end processReturn() |
@@ -166,30 +166,30 @@ discard block |
||
| 166 | 166 | * |
| 167 | 167 | * @return void |
| 168 | 168 | */ |
| 169 | - protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 169 | + protected function processThrows( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart ) |
|
| 170 | 170 | { |
| 171 | 171 | $tokens = $phpcsFile->getTokens(); |
| 172 | 172 | |
| 173 | 173 | $throws = array(); |
| 174 | - foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
|
| 175 | - if ($tokens[$tag]['content'] !== '@throws') { |
|
| 174 | + foreach ( $tokens[ $commentStart ][ 'comment_tags' ] as $tag ) { |
|
| 175 | + if ( $tokens[ $tag ][ 'content' ] !== '@throws' ) { |
|
| 176 | 176 | continue; |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | $exception = null; |
| 180 | 180 | $comment = null; |
| 181 | - if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) { |
|
| 181 | + if ( $tokens[ ( $tag + 2 ) ][ 'code' ] === T_DOC_COMMENT_STRING ) { |
|
| 182 | 182 | $matches = array(); |
| 183 | - preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($tag + 2)]['content'], $matches); |
|
| 184 | - $exception = $matches[1]; |
|
| 185 | - if (isset($matches[2]) === true) { |
|
| 186 | - $comment = $matches[2]; |
|
| 183 | + preg_match( '/([^\s]+)(?:\s+(.*))?/', $tokens[ ( $tag + 2 ) ][ 'content' ], $matches ); |
|
| 184 | + $exception = $matches[ 1 ]; |
|
| 185 | + if ( isset( $matches[ 2 ] ) === true ) { |
|
| 186 | + $comment = $matches[ 2 ]; |
|
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - if ($exception === null) { |
|
| 190 | + if ( $exception === null ) { |
|
| 191 | 191 | $error = 'Exception type missing for @throws tag in function comment'; |
| 192 | - $phpcsFile->addError($error, $tag, 'InvalidThrows'); |
|
| 192 | + $phpcsFile->addError( $error, $tag, 'InvalidThrows' ); |
|
| 193 | 193 | } |
| 194 | 194 | }//end foreach |
| 195 | 195 | |
@@ -206,15 +206,15 @@ discard block |
||
| 206 | 206 | * |
| 207 | 207 | * @return void |
| 208 | 208 | */ |
| 209 | - protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 209 | + protected function processParams( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart ) |
|
| 210 | 210 | { |
| 211 | 211 | $tokens = $phpcsFile->getTokens(); |
| 212 | 212 | |
| 213 | 213 | $params = array(); |
| 214 | 214 | $maxType = 0; |
| 215 | 215 | $maxVar = 0; |
| 216 | - foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { |
|
| 217 | - if ($tokens[$tag]['content'] !== '@param') { |
|
| 216 | + foreach ( $tokens[ $commentStart ][ 'comment_tags' ] as $pos => $tag ) { |
|
| 217 | + if ( $tokens[ $tag ][ 'content' ] !== '@param' ) { |
|
| 218 | 218 | continue; |
| 219 | 219 | } |
| 220 | 220 | |
@@ -223,57 +223,57 @@ discard block |
||
| 223 | 223 | $var = ''; |
| 224 | 224 | $varSpace = 0; |
| 225 | 225 | $comment = ''; |
| 226 | - if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) { |
|
| 226 | + if ( $tokens[ ( $tag + 2 ) ][ 'code' ] === T_DOC_COMMENT_STRING ) { |
|
| 227 | 227 | $matches = array(); |
| 228 | - preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches); |
|
| 229 | - |
|
| 230 | - if (empty($matches) === false) { |
|
| 231 | - $typeLen = strlen($matches[1]); |
|
| 232 | - $type = trim($matches[1]); |
|
| 233 | - $typeSpace = ($typeLen - strlen($type)); |
|
| 234 | - $typeLen = strlen($type); |
|
| 235 | - if ($typeLen > $maxType) { |
|
| 228 | + preg_match( '/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[ ( $tag + 2 ) ][ 'content' ], $matches ); |
|
| 229 | + |
|
| 230 | + if ( empty( $matches ) === false ) { |
|
| 231 | + $typeLen = strlen( $matches[ 1 ] ); |
|
| 232 | + $type = trim( $matches[ 1 ] ); |
|
| 233 | + $typeSpace = ( $typeLen - strlen( $type ) ); |
|
| 234 | + $typeLen = strlen( $type ); |
|
| 235 | + if ( $typeLen > $maxType ) { |
|
| 236 | 236 | $maxType = $typeLen; |
| 237 | 237 | } |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | - if (isset($matches[2]) === true) { |
|
| 241 | - $var = $matches[2]; |
|
| 242 | - $varLen = strlen($var); |
|
| 243 | - if ($varLen > $maxVar) { |
|
| 240 | + if ( isset( $matches[ 2 ] ) === true ) { |
|
| 241 | + $var = $matches[ 2 ]; |
|
| 242 | + $varLen = strlen( $var ); |
|
| 243 | + if ( $varLen > $maxVar ) { |
|
| 244 | 244 | $maxVar = $varLen; |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - if (isset($matches[4]) === true) { |
|
| 248 | - $varSpace = strlen($matches[3]); |
|
| 249 | - $comment = $matches[4]; |
|
| 247 | + if ( isset( $matches[ 4 ] ) === true ) { |
|
| 248 | + $varSpace = strlen( $matches[ 3 ] ); |
|
| 249 | + $comment = $matches[ 4 ]; |
|
| 250 | 250 | |
| 251 | 251 | // Any strings until the next tag belong to this comment. |
| 252 | - if (isset($tokens[$commentStart]['comment_tags'][($pos + 1)]) === true) { |
|
| 253 | - $end = $tokens[$commentStart]['comment_tags'][($pos + 1)]; |
|
| 252 | + if ( isset( $tokens[ $commentStart ][ 'comment_tags' ][ ( $pos + 1 ) ] ) === true ) { |
|
| 253 | + $end = $tokens[ $commentStart ][ 'comment_tags' ][ ( $pos + 1 ) ]; |
|
| 254 | 254 | } else { |
| 255 | - $end = $tokens[$commentStart]['comment_closer']; |
|
| 255 | + $end = $tokens[ $commentStart ][ 'comment_closer' ]; |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - for ($i = ($tag + 3); $i < $end; $i++) { |
|
| 259 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) { |
|
| 260 | - $comment .= ' '.$tokens[$i]['content']; |
|
| 258 | + for ( $i = ( $tag + 3 ); $i < $end; $i++ ) { |
|
| 259 | + if ( $tokens[ $i ][ 'code' ] === T_DOC_COMMENT_STRING ) { |
|
| 260 | + $comment .= ' ' . $tokens[ $i ][ 'content' ]; |
|
| 261 | 261 | } |
| 262 | 262 | } |
| 263 | 263 | } else { |
| 264 | 264 | $error = 'Missing parameter comment'; |
| 265 | - $phpcsFile->addError($error, $tag, 'MissingParamComment'); |
|
| 265 | + $phpcsFile->addError( $error, $tag, 'MissingParamComment' ); |
|
| 266 | 266 | } |
| 267 | 267 | } else { |
| 268 | 268 | $error = 'Missing parameter name'; |
| 269 | - $phpcsFile->addError($error, $tag, 'MissingParamName'); |
|
| 269 | + $phpcsFile->addError( $error, $tag, 'MissingParamName' ); |
|
| 270 | 270 | }//end if |
| 271 | 271 | } else { |
| 272 | 272 | $error = 'Missing parameter type'; |
| 273 | - $phpcsFile->addError($error, $tag, 'MissingParamType'); |
|
| 273 | + $phpcsFile->addError( $error, $tag, 'MissingParamType' ); |
|
| 274 | 274 | }//end if |
| 275 | 275 | |
| 276 | - $params[] = array( |
|
| 276 | + $params[ ] = array( |
|
| 277 | 277 | 'tag' => $tag, |
| 278 | 278 | 'type' => $type, |
| 279 | 279 | 'var' => $var, |
@@ -283,106 +283,106 @@ discard block |
||
| 283 | 283 | ); |
| 284 | 284 | }//end foreach |
| 285 | 285 | |
| 286 | - $realParams = $phpcsFile->getMethodParameters($stackPtr); |
|
| 286 | + $realParams = $phpcsFile->getMethodParameters( $stackPtr ); |
|
| 287 | 287 | $foundParams = array(); |
| 288 | 288 | |
| 289 | 289 | // We want to use ... for all variable length arguments, so added |
| 290 | 290 | // this prefix to the variable name so comparisons are easier. |
| 291 | - foreach ($realParams as $pos => $param) { |
|
| 292 | - if ($param['variable_length'] === true) { |
|
| 293 | - $realParams[$pos]['name'] = '...'.$realParams[$pos]['name']; |
|
| 291 | + foreach ( $realParams as $pos => $param ) { |
|
| 292 | + if ( $param[ 'variable_length' ] === true ) { |
|
| 293 | + $realParams[ $pos ][ 'name' ] = '...' . $realParams[ $pos ][ 'name' ]; |
|
| 294 | 294 | } |
| 295 | 295 | } |
| 296 | 296 | |
| 297 | - foreach ($params as $pos => $param) { |
|
| 298 | - if ($param['var'] === '') { |
|
| 297 | + foreach ( $params as $pos => $param ) { |
|
| 298 | + if ( $param[ 'var' ] === '' ) { |
|
| 299 | 299 | continue; |
| 300 | 300 | } |
| 301 | 301 | |
| 302 | - $foundParams[] = $param['var']; |
|
| 302 | + $foundParams[ ] = $param[ 'var' ]; |
|
| 303 | 303 | |
| 304 | 304 | // Check number of spaces after the type. |
| 305 | - $spaces = ($maxType - strlen($param['type']) + 1); |
|
| 306 | - if ($param['type_space'] !== $spaces) { |
|
| 305 | + $spaces = ( $maxType - strlen( $param[ 'type' ] ) + 1 ); |
|
| 306 | + if ( $param[ 'type_space' ] !== $spaces ) { |
|
| 307 | 307 | $error = 'Expected %s spaces after parameter type; %s found'; |
| 308 | 308 | $data = array( |
| 309 | 309 | $spaces, |
| 310 | - $param['type_space'], |
|
| 310 | + $param[ 'type_space' ], |
|
| 311 | 311 | ); |
| 312 | 312 | |
| 313 | - $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data); |
|
| 314 | - if ($fix === true) { |
|
| 315 | - $content = $param['type']; |
|
| 316 | - $content .= str_repeat(' ', $spaces); |
|
| 317 | - $content .= $param['var']; |
|
| 318 | - $content .= str_repeat(' ', $param['var_space']); |
|
| 319 | - $content .= $param['comment']; |
|
| 320 | - $phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content); |
|
| 313 | + $fix = $phpcsFile->addFixableError( $error, $param[ 'tag' ], 'SpacingAfterParamType', $data ); |
|
| 314 | + if ( $fix === true ) { |
|
| 315 | + $content = $param[ 'type' ]; |
|
| 316 | + $content .= str_repeat( ' ', $spaces ); |
|
| 317 | + $content .= $param[ 'var' ]; |
|
| 318 | + $content .= str_repeat( ' ', $param[ 'var_space' ] ); |
|
| 319 | + $content .= $param[ 'comment' ]; |
|
| 320 | + $phpcsFile->fixer->replaceToken( ( $param[ 'tag' ] + 2 ), $content ); |
|
| 321 | 321 | } |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | // Make sure the param name is correct. |
| 325 | - if (isset($realParams[$pos]) === true) { |
|
| 326 | - $realName = $realParams[$pos]['name']; |
|
| 327 | - if ($realName !== $param['var']) { |
|
| 325 | + if ( isset( $realParams[ $pos ] ) === true ) { |
|
| 326 | + $realName = $realParams[ $pos ][ 'name' ]; |
|
| 327 | + if ( $realName !== $param[ 'var' ] ) { |
|
| 328 | 328 | $code = 'ParamNameNoMatch'; |
| 329 | 329 | $data = array( |
| 330 | - $param['var'], |
|
| 330 | + $param[ 'var' ], |
|
| 331 | 331 | $realName, |
| 332 | 332 | ); |
| 333 | 333 | |
| 334 | 334 | $error = 'Doc comment for parameter %s does not match '; |
| 335 | - if (strtolower($param['var']) === strtolower($realName)) { |
|
| 335 | + if ( strtolower( $param[ 'var' ] ) === strtolower( $realName ) ) { |
|
| 336 | 336 | $error .= 'case of '; |
| 337 | 337 | $code = 'ParamNameNoCaseMatch'; |
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | $error .= 'actual variable name %s'; |
| 341 | 341 | |
| 342 | - $phpcsFile->addError($error, $param['tag'], $code, $data); |
|
| 342 | + $phpcsFile->addError( $error, $param[ 'tag' ], $code, $data ); |
|
| 343 | 343 | } |
| 344 | - } else if (substr($param['var'], -4) !== ',...') { |
|
| 344 | + } else if ( substr( $param[ 'var' ], -4 ) !== ',...' ) { |
|
| 345 | 345 | // We must have an extra parameter comment. |
| 346 | 346 | $error = 'Superfluous parameter comment'; |
| 347 | - $phpcsFile->addError($error, $param['tag'], 'ExtraParamComment'); |
|
| 347 | + $phpcsFile->addError( $error, $param[ 'tag' ], 'ExtraParamComment' ); |
|
| 348 | 348 | }//end if |
| 349 | 349 | |
| 350 | - if ($param['comment'] === '') { |
|
| 350 | + if ( $param[ 'comment' ] === '' ) { |
|
| 351 | 351 | continue; |
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | // Check number of spaces after the var name. |
| 355 | - $spaces = ($maxVar - strlen($param['var']) + 1); |
|
| 356 | - if ($param['var_space'] !== $spaces) { |
|
| 355 | + $spaces = ( $maxVar - strlen( $param[ 'var' ] ) + 1 ); |
|
| 356 | + if ( $param[ 'var_space' ] !== $spaces ) { |
|
| 357 | 357 | $error = 'Expected %s spaces after parameter name; %s found'; |
| 358 | 358 | $data = array( |
| 359 | 359 | $spaces, |
| 360 | - $param['var_space'], |
|
| 360 | + $param[ 'var_space' ], |
|
| 361 | 361 | ); |
| 362 | 362 | |
| 363 | - $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamName', $data); |
|
| 364 | - if ($fix === true) { |
|
| 365 | - $content = $param['type']; |
|
| 366 | - $content .= str_repeat(' ', $param['type_space']); |
|
| 367 | - $content .= $param['var']; |
|
| 368 | - $content .= str_repeat(' ', $spaces); |
|
| 369 | - $content .= $param['comment']; |
|
| 370 | - $phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content); |
|
| 363 | + $fix = $phpcsFile->addFixableError( $error, $param[ 'tag' ], 'SpacingAfterParamName', $data ); |
|
| 364 | + if ( $fix === true ) { |
|
| 365 | + $content = $param[ 'type' ]; |
|
| 366 | + $content .= str_repeat( ' ', $param[ 'type_space' ] ); |
|
| 367 | + $content .= $param[ 'var' ]; |
|
| 368 | + $content .= str_repeat( ' ', $spaces ); |
|
| 369 | + $content .= $param[ 'comment' ]; |
|
| 370 | + $phpcsFile->fixer->replaceToken( ( $param[ 'tag' ] + 2 ), $content ); |
|
| 371 | 371 | } |
| 372 | 372 | } |
| 373 | 373 | }//end foreach |
| 374 | 374 | |
| 375 | 375 | $realNames = array(); |
| 376 | - foreach ($realParams as $realParam) { |
|
| 377 | - $realNames[] = $realParam['name']; |
|
| 376 | + foreach ( $realParams as $realParam ) { |
|
| 377 | + $realNames[ ] = $realParam[ 'name' ]; |
|
| 378 | 378 | } |
| 379 | 379 | |
| 380 | 380 | // Report missing comments. |
| 381 | - $diff = array_diff($realNames, $foundParams); |
|
| 382 | - foreach ($diff as $neededParam) { |
|
| 381 | + $diff = array_diff( $realNames, $foundParams ); |
|
| 382 | + foreach ( $diff as $neededParam ) { |
|
| 383 | 383 | $error = 'Doc comment for parameter "%s" missing'; |
| 384 | - $data = array($neededParam); |
|
| 385 | - $phpcsFile->addError($error, $commentStart, 'MissingParamTag', $data); |
|
| 384 | + $data = array( $neededParam ); |
|
| 385 | + $phpcsFile->addError( $error, $commentStart, 'MissingParamTag', $data ); |
|
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | }//end processParams() |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * @version Release: @package_version@ |
| 26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 27 | 27 | */ |
| 28 | -class PEAR_Sniffs_Commenting_FunctionCommentSniff implements PHP_CodeSniffer_Sniff |
|
| 29 | -{ |
|
| 28 | +class PEAR_Sniffs_Commenting_FunctionCommentSniff implements PHP_CodeSniffer_Sniff { |
|
| 30 | 29 | |
| 31 | 30 | |
| 32 | 31 | /** |
@@ -34,8 +33,7 @@ discard block |
||
| 34 | 33 | * |
| 35 | 34 | * @return array |
| 36 | 35 | */ |
| 37 | - public function register() |
|
| 38 | - { |
|
| 36 | + public function register() { |
|
| 39 | 37 | return array(T_FUNCTION); |
| 40 | 38 | |
| 41 | 39 | }//end register() |
@@ -50,8 +48,7 @@ discard block |
||
| 50 | 48 | * |
| 51 | 49 | * @return void |
| 52 | 50 | */ |
| 53 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 54 | - { |
|
| 51 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 55 | 52 | $tokens = $phpcsFile->getTokens(); |
| 56 | 53 | $find = PHP_CodeSniffer_Tokens::$methodPrefixes; |
| 57 | 54 | $find[] = T_WHITESPACE; |
@@ -117,8 +114,7 @@ discard block |
||
| 117 | 114 | * |
| 118 | 115 | * @return void |
| 119 | 116 | */ |
| 120 | - protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 121 | - { |
|
| 117 | + protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) { |
|
| 122 | 118 | $tokens = $phpcsFile->getTokens(); |
| 123 | 119 | |
| 124 | 120 | // Skip constructor and destructor. |
@@ -166,8 +162,7 @@ discard block |
||
| 166 | 162 | * |
| 167 | 163 | * @return void |
| 168 | 164 | */ |
| 169 | - protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 170 | - { |
|
| 165 | + protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) { |
|
| 171 | 166 | $tokens = $phpcsFile->getTokens(); |
| 172 | 167 | |
| 173 | 168 | $throws = array(); |
@@ -206,8 +201,7 @@ discard block |
||
| 206 | 201 | * |
| 207 | 202 | * @return void |
| 208 | 203 | */ |
| 209 | - protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
|
| 210 | - { |
|
| 204 | + protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) { |
|
| 211 | 205 | $tokens = $phpcsFile->getTokens(); |
| 212 | 206 | |
| 213 | 207 | $params = array(); |
@@ -49,7 +49,7 @@ |
||
| 49 | 49 | /** |
| 50 | 50 | * Returns an array of tokens this test wants to listen for. |
| 51 | 51 | * |
| 52 | - * @return array |
|
| 52 | + * @return integer[] |
|
| 53 | 53 | */ |
| 54 | 54 | public function register() |
| 55 | 55 | { |
@@ -28,252 +28,252 @@ |
||
| 28 | 28 | class PEAR_Sniffs_ControlStructures_MultiLineConditionSniff implements PHP_CodeSniffer_Sniff |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * A list of tokenizers this sniff supports. |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - public $supportedTokenizers = array( |
|
| 37 | - 'PHP', |
|
| 38 | - 'JS', |
|
| 39 | - ); |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The number of spaces code should be indented. |
|
| 43 | - * |
|
| 44 | - * @var int |
|
| 45 | - */ |
|
| 46 | - public $indent = 4; |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Returns an array of tokens this test wants to listen for. |
|
| 51 | - * |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function register() |
|
| 55 | - { |
|
| 56 | - return array( |
|
| 57 | - T_IF, |
|
| 58 | - T_ELSEIF, |
|
| 59 | - ); |
|
| 60 | - |
|
| 61 | - }//end register() |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Processes this test, when one of its tokens is encountered. |
|
| 66 | - * |
|
| 67 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 68 | - * @param int $stackPtr The position of the current token |
|
| 69 | - * in the stack passed in $tokens. |
|
| 70 | - * |
|
| 71 | - * @return void |
|
| 72 | - */ |
|
| 73 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 74 | - { |
|
| 75 | - $tokens = $phpcsFile->getTokens(); |
|
| 76 | - |
|
| 77 | - if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) { |
|
| 78 | - return; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - $openBracket = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 82 | - $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 83 | - $spaceAfterOpen = 0; |
|
| 84 | - if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 85 | - if (strpos($tokens[($openBracket + 1)]['content'], $phpcsFile->eolChar) !== false) { |
|
| 86 | - $spaceAfterOpen = 'newline'; |
|
| 87 | - } else { |
|
| 88 | - $spaceAfterOpen = strlen($tokens[($openBracket + 1)]['content']); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - if ($spaceAfterOpen !== 0) { |
|
| 93 | - $error = 'First condition of a multi-line IF statement must directly follow the opening parenthesis'; |
|
| 94 | - $fix = $phpcsFile->addFixableError($error, ($openBracket + 1), 'SpacingAfterOpenBrace'); |
|
| 95 | - if ($fix === true) { |
|
| 96 | - if ($spaceAfterOpen === 'newline') { |
|
| 97 | - $phpcsFile->fixer->replaceToken(($openBracket + 1), ''); |
|
| 98 | - } else { |
|
| 99 | - $phpcsFile->fixer->replaceToken(($openBracket + 1), ''); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - // We need to work out how far indented the if statement |
|
| 105 | - // itself is, so we can work out how far to indent conditions. |
|
| 106 | - $statementIndent = 0; |
|
| 107 | - for ($i = ($stackPtr - 1); $i >= 0; $i--) { |
|
| 108 | - if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 109 | - $i++; |
|
| 110 | - break; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if ($i >= 0 && $tokens[$i]['code'] === T_WHITESPACE) { |
|
| 115 | - $statementIndent = strlen($tokens[$i]['content']); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - // Each line between the parenthesis should be indented 4 spaces |
|
| 119 | - // and start with an operator, unless the line is inside a |
|
| 120 | - // function call, in which case it is ignored. |
|
| 121 | - $prevLine = $tokens[$openBracket]['line']; |
|
| 122 | - for ($i = ($openBracket + 1); $i <= $closeBracket; $i++) { |
|
| 123 | - if ($i === $closeBracket && $tokens[$openBracket]['line'] !== $tokens[$i]['line']) { |
|
| 124 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($i - 1), null, true); |
|
| 125 | - if ($tokens[$prev]['line'] === $tokens[$i]['line']) { |
|
| 126 | - // Closing bracket is on the same line as a condition. |
|
| 127 | - $error = 'Closing parenthesis of a multi-line IF statement must be on a new line'; |
|
| 128 | - $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketNewLine'); |
|
| 129 | - if ($fix === true) { |
|
| 130 | - // Account for a comment at the end of the line. |
|
| 131 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true); |
|
| 132 | - if ($tokens[$next]['code'] !== T_COMMENT) { |
|
| 133 | - $phpcsFile->fixer->addNewlineBefore($closeBracket); |
|
| 134 | - } else { |
|
| 135 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
| 136 | - $phpcsFile->fixer->beginChangeset(); |
|
| 137 | - $phpcsFile->fixer->replaceToken($closeBracket, ''); |
|
| 138 | - $phpcsFile->fixer->addContentBefore($next, ')'); |
|
| 139 | - $phpcsFile->fixer->endChangeset(); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - }//end if |
|
| 144 | - |
|
| 145 | - if ($tokens[$i]['line'] !== $prevLine) { |
|
| 146 | - if ($tokens[$i]['line'] === $tokens[$closeBracket]['line']) { |
|
| 147 | - $next = $phpcsFile->findNext(T_WHITESPACE, $i, null, true); |
|
| 148 | - if ($next !== $closeBracket) { |
|
| 149 | - $expectedIndent = ($statementIndent + $this->indent); |
|
| 150 | - } else { |
|
| 151 | - // Closing brace needs to be indented to the same level |
|
| 152 | - // as the statement. |
|
| 153 | - $expectedIndent = $statementIndent; |
|
| 154 | - }//end if |
|
| 155 | - } else { |
|
| 156 | - $expectedIndent = ($statementIndent + $this->indent); |
|
| 157 | - }//end if |
|
| 158 | - |
|
| 159 | - if ($tokens[$i]['code'] === T_COMMENT) { |
|
| 160 | - $prevLine = $tokens[$i]['line']; |
|
| 161 | - continue; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - // We changed lines, so this should be a whitespace indent token. |
|
| 165 | - if ($tokens[$i]['code'] !== T_WHITESPACE) { |
|
| 166 | - $foundIndent = 0; |
|
| 167 | - } else { |
|
| 168 | - $foundIndent = strlen($tokens[$i]['content']); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - if ($expectedIndent !== $foundIndent) { |
|
| 172 | - $error = 'Multi-line IF statement not indented correctly; expected %s spaces but found %s'; |
|
| 173 | - $data = array( |
|
| 174 | - $expectedIndent, |
|
| 175 | - $foundIndent, |
|
| 176 | - ); |
|
| 177 | - |
|
| 178 | - $fix = $phpcsFile->addFixableError($error, $i, 'Alignment', $data); |
|
| 179 | - if ($fix === true) { |
|
| 180 | - $spaces = str_repeat(' ', $expectedIndent); |
|
| 181 | - if ($foundIndent === 0) { |
|
| 182 | - $phpcsFile->fixer->addContentBefore($i, $spaces); |
|
| 183 | - } else { |
|
| 184 | - $phpcsFile->fixer->replaceToken($i, $spaces); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $i, null, true); |
|
| 190 | - if ($next !== $closeBracket) { |
|
| 191 | - if (isset(PHP_CodeSniffer_Tokens::$booleanOperators[$tokens[$next]['code']]) === false) { |
|
| 192 | - $error = 'Each line in a multi-line IF statement must begin with a boolean operator'; |
|
| 193 | - $fix = $phpcsFile->addFixableError($error, $i, 'StartWithBoolean'); |
|
| 194 | - if ($fix === true) { |
|
| 195 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($i - 1), $openBracket, true); |
|
| 196 | - if (isset(PHP_CodeSniffer_Tokens::$booleanOperators[$tokens[$prev]['code']]) === true) { |
|
| 197 | - $phpcsFile->fixer->beginChangeset(); |
|
| 198 | - $phpcsFile->fixer->replaceToken($prev, ''); |
|
| 199 | - $phpcsFile->fixer->addContentBefore($next, $tokens[$prev]['content'].' '); |
|
| 200 | - $phpcsFile->fixer->endChangeset(); |
|
| 201 | - } else { |
|
| 202 | - for ($x = ($prev + 1); $x < $next; $x++) { |
|
| 203 | - $phpcsFile->fixer->replaceToken($x, ''); |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - }//end if |
|
| 209 | - |
|
| 210 | - $prevLine = $tokens[$i]['line']; |
|
| 211 | - }//end if |
|
| 212 | - |
|
| 213 | - if ($tokens[$i]['code'] === T_STRING) { |
|
| 214 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true); |
|
| 215 | - if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { |
|
| 216 | - // This is a function call, so skip to the end as they |
|
| 217 | - // have their own indentation rules. |
|
| 218 | - $i = $tokens[$next]['parenthesis_closer']; |
|
| 219 | - $prevLine = $tokens[$i]['line']; |
|
| 220 | - continue; |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - }//end for |
|
| 224 | - |
|
| 225 | - // From here on, we are checking the spacing of the opening and closing |
|
| 226 | - // braces. If this IF statement does not use braces, we end here. |
|
| 227 | - if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
| 228 | - return; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // The opening brace needs to be one space away from the closing parenthesis. |
|
| 232 | - $openBrace = $tokens[$stackPtr]['scope_opener']; |
|
| 233 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), $openBrace, true); |
|
| 234 | - if ($next !== false) { |
|
| 235 | - // Probably comments in between tokens, so don't check. |
|
| 236 | - return; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - if ($tokens[$openBrace]['line'] > $tokens[$closeBracket]['line']) { |
|
| 240 | - $length = -1; |
|
| 241 | - } else if ($openBrace === ($closeBracket + 1)) { |
|
| 242 | - $length = 0; |
|
| 243 | - } else if ($openBrace === ($closeBracket + 2) |
|
| 244 | - && $tokens[($closeBracket + 1)]['code'] === T_WHITESPACE |
|
| 245 | - ) { |
|
| 246 | - $length = strlen($tokens[($closeBracket + 1)]['content']); |
|
| 247 | - } else { |
|
| 248 | - // Confused, so don't check. |
|
| 249 | - $length = 1; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - if ($length === 1) { |
|
| 253 | - return; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - $data = array($length); |
|
| 257 | - $code = 'SpaceBeforeOpenBrace'; |
|
| 258 | - |
|
| 259 | - $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line IF statement; found '; |
|
| 260 | - if ($length === -1) { |
|
| 261 | - $error .= 'newline'; |
|
| 262 | - $code = 'NewlineBeforeOpenBrace'; |
|
| 263 | - } else { |
|
| 264 | - $error .= '%s spaces'; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - $fix = $phpcsFile->addFixableError($error, ($closeBracket + 1), $code, $data); |
|
| 268 | - if ($fix === true) { |
|
| 269 | - if ($length === 0) { |
|
| 270 | - $phpcsFile->fixer->addContent($closeBracket, ' '); |
|
| 271 | - } else { |
|
| 272 | - $phpcsFile->fixer->replaceToken(($closeBracket + 1), ' '); |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - }//end process() |
|
| 31 | + /** |
|
| 32 | + * A list of tokenizers this sniff supports. |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + public $supportedTokenizers = array( |
|
| 37 | + 'PHP', |
|
| 38 | + 'JS', |
|
| 39 | + ); |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The number of spaces code should be indented. |
|
| 43 | + * |
|
| 44 | + * @var int |
|
| 45 | + */ |
|
| 46 | + public $indent = 4; |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Returns an array of tokens this test wants to listen for. |
|
| 51 | + * |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function register() |
|
| 55 | + { |
|
| 56 | + return array( |
|
| 57 | + T_IF, |
|
| 58 | + T_ELSEIF, |
|
| 59 | + ); |
|
| 60 | + |
|
| 61 | + }//end register() |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Processes this test, when one of its tokens is encountered. |
|
| 66 | + * |
|
| 67 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 68 | + * @param int $stackPtr The position of the current token |
|
| 69 | + * in the stack passed in $tokens. |
|
| 70 | + * |
|
| 71 | + * @return void |
|
| 72 | + */ |
|
| 73 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 74 | + { |
|
| 75 | + $tokens = $phpcsFile->getTokens(); |
|
| 76 | + |
|
| 77 | + if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) { |
|
| 78 | + return; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + $openBracket = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 82 | + $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 83 | + $spaceAfterOpen = 0; |
|
| 84 | + if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 85 | + if (strpos($tokens[($openBracket + 1)]['content'], $phpcsFile->eolChar) !== false) { |
|
| 86 | + $spaceAfterOpen = 'newline'; |
|
| 87 | + } else { |
|
| 88 | + $spaceAfterOpen = strlen($tokens[($openBracket + 1)]['content']); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + if ($spaceAfterOpen !== 0) { |
|
| 93 | + $error = 'First condition of a multi-line IF statement must directly follow the opening parenthesis'; |
|
| 94 | + $fix = $phpcsFile->addFixableError($error, ($openBracket + 1), 'SpacingAfterOpenBrace'); |
|
| 95 | + if ($fix === true) { |
|
| 96 | + if ($spaceAfterOpen === 'newline') { |
|
| 97 | + $phpcsFile->fixer->replaceToken(($openBracket + 1), ''); |
|
| 98 | + } else { |
|
| 99 | + $phpcsFile->fixer->replaceToken(($openBracket + 1), ''); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + // We need to work out how far indented the if statement |
|
| 105 | + // itself is, so we can work out how far to indent conditions. |
|
| 106 | + $statementIndent = 0; |
|
| 107 | + for ($i = ($stackPtr - 1); $i >= 0; $i--) { |
|
| 108 | + if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 109 | + $i++; |
|
| 110 | + break; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if ($i >= 0 && $tokens[$i]['code'] === T_WHITESPACE) { |
|
| 115 | + $statementIndent = strlen($tokens[$i]['content']); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + // Each line between the parenthesis should be indented 4 spaces |
|
| 119 | + // and start with an operator, unless the line is inside a |
|
| 120 | + // function call, in which case it is ignored. |
|
| 121 | + $prevLine = $tokens[$openBracket]['line']; |
|
| 122 | + for ($i = ($openBracket + 1); $i <= $closeBracket; $i++) { |
|
| 123 | + if ($i === $closeBracket && $tokens[$openBracket]['line'] !== $tokens[$i]['line']) { |
|
| 124 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($i - 1), null, true); |
|
| 125 | + if ($tokens[$prev]['line'] === $tokens[$i]['line']) { |
|
| 126 | + // Closing bracket is on the same line as a condition. |
|
| 127 | + $error = 'Closing parenthesis of a multi-line IF statement must be on a new line'; |
|
| 128 | + $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketNewLine'); |
|
| 129 | + if ($fix === true) { |
|
| 130 | + // Account for a comment at the end of the line. |
|
| 131 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true); |
|
| 132 | + if ($tokens[$next]['code'] !== T_COMMENT) { |
|
| 133 | + $phpcsFile->fixer->addNewlineBefore($closeBracket); |
|
| 134 | + } else { |
|
| 135 | + $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
| 136 | + $phpcsFile->fixer->beginChangeset(); |
|
| 137 | + $phpcsFile->fixer->replaceToken($closeBracket, ''); |
|
| 138 | + $phpcsFile->fixer->addContentBefore($next, ')'); |
|
| 139 | + $phpcsFile->fixer->endChangeset(); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + }//end if |
|
| 144 | + |
|
| 145 | + if ($tokens[$i]['line'] !== $prevLine) { |
|
| 146 | + if ($tokens[$i]['line'] === $tokens[$closeBracket]['line']) { |
|
| 147 | + $next = $phpcsFile->findNext(T_WHITESPACE, $i, null, true); |
|
| 148 | + if ($next !== $closeBracket) { |
|
| 149 | + $expectedIndent = ($statementIndent + $this->indent); |
|
| 150 | + } else { |
|
| 151 | + // Closing brace needs to be indented to the same level |
|
| 152 | + // as the statement. |
|
| 153 | + $expectedIndent = $statementIndent; |
|
| 154 | + }//end if |
|
| 155 | + } else { |
|
| 156 | + $expectedIndent = ($statementIndent + $this->indent); |
|
| 157 | + }//end if |
|
| 158 | + |
|
| 159 | + if ($tokens[$i]['code'] === T_COMMENT) { |
|
| 160 | + $prevLine = $tokens[$i]['line']; |
|
| 161 | + continue; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + // We changed lines, so this should be a whitespace indent token. |
|
| 165 | + if ($tokens[$i]['code'] !== T_WHITESPACE) { |
|
| 166 | + $foundIndent = 0; |
|
| 167 | + } else { |
|
| 168 | + $foundIndent = strlen($tokens[$i]['content']); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + if ($expectedIndent !== $foundIndent) { |
|
| 172 | + $error = 'Multi-line IF statement not indented correctly; expected %s spaces but found %s'; |
|
| 173 | + $data = array( |
|
| 174 | + $expectedIndent, |
|
| 175 | + $foundIndent, |
|
| 176 | + ); |
|
| 177 | + |
|
| 178 | + $fix = $phpcsFile->addFixableError($error, $i, 'Alignment', $data); |
|
| 179 | + if ($fix === true) { |
|
| 180 | + $spaces = str_repeat(' ', $expectedIndent); |
|
| 181 | + if ($foundIndent === 0) { |
|
| 182 | + $phpcsFile->fixer->addContentBefore($i, $spaces); |
|
| 183 | + } else { |
|
| 184 | + $phpcsFile->fixer->replaceToken($i, $spaces); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $i, null, true); |
|
| 190 | + if ($next !== $closeBracket) { |
|
| 191 | + if (isset(PHP_CodeSniffer_Tokens::$booleanOperators[$tokens[$next]['code']]) === false) { |
|
| 192 | + $error = 'Each line in a multi-line IF statement must begin with a boolean operator'; |
|
| 193 | + $fix = $phpcsFile->addFixableError($error, $i, 'StartWithBoolean'); |
|
| 194 | + if ($fix === true) { |
|
| 195 | + $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($i - 1), $openBracket, true); |
|
| 196 | + if (isset(PHP_CodeSniffer_Tokens::$booleanOperators[$tokens[$prev]['code']]) === true) { |
|
| 197 | + $phpcsFile->fixer->beginChangeset(); |
|
| 198 | + $phpcsFile->fixer->replaceToken($prev, ''); |
|
| 199 | + $phpcsFile->fixer->addContentBefore($next, $tokens[$prev]['content'].' '); |
|
| 200 | + $phpcsFile->fixer->endChangeset(); |
|
| 201 | + } else { |
|
| 202 | + for ($x = ($prev + 1); $x < $next; $x++) { |
|
| 203 | + $phpcsFile->fixer->replaceToken($x, ''); |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + }//end if |
|
| 209 | + |
|
| 210 | + $prevLine = $tokens[$i]['line']; |
|
| 211 | + }//end if |
|
| 212 | + |
|
| 213 | + if ($tokens[$i]['code'] === T_STRING) { |
|
| 214 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true); |
|
| 215 | + if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { |
|
| 216 | + // This is a function call, so skip to the end as they |
|
| 217 | + // have their own indentation rules. |
|
| 218 | + $i = $tokens[$next]['parenthesis_closer']; |
|
| 219 | + $prevLine = $tokens[$i]['line']; |
|
| 220 | + continue; |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + }//end for |
|
| 224 | + |
|
| 225 | + // From here on, we are checking the spacing of the opening and closing |
|
| 226 | + // braces. If this IF statement does not use braces, we end here. |
|
| 227 | + if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
| 228 | + return; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // The opening brace needs to be one space away from the closing parenthesis. |
|
| 232 | + $openBrace = $tokens[$stackPtr]['scope_opener']; |
|
| 233 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), $openBrace, true); |
|
| 234 | + if ($next !== false) { |
|
| 235 | + // Probably comments in between tokens, so don't check. |
|
| 236 | + return; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + if ($tokens[$openBrace]['line'] > $tokens[$closeBracket]['line']) { |
|
| 240 | + $length = -1; |
|
| 241 | + } else if ($openBrace === ($closeBracket + 1)) { |
|
| 242 | + $length = 0; |
|
| 243 | + } else if ($openBrace === ($closeBracket + 2) |
|
| 244 | + && $tokens[($closeBracket + 1)]['code'] === T_WHITESPACE |
|
| 245 | + ) { |
|
| 246 | + $length = strlen($tokens[($closeBracket + 1)]['content']); |
|
| 247 | + } else { |
|
| 248 | + // Confused, so don't check. |
|
| 249 | + $length = 1; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + if ($length === 1) { |
|
| 253 | + return; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + $data = array($length); |
|
| 257 | + $code = 'SpaceBeforeOpenBrace'; |
|
| 258 | + |
|
| 259 | + $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line IF statement; found '; |
|
| 260 | + if ($length === -1) { |
|
| 261 | + $error .= 'newline'; |
|
| 262 | + $code = 'NewlineBeforeOpenBrace'; |
|
| 263 | + } else { |
|
| 264 | + $error .= '%s spaces'; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + $fix = $phpcsFile->addFixableError($error, ($closeBracket + 1), $code, $data); |
|
| 268 | + if ($fix === true) { |
|
| 269 | + if ($length === 0) { |
|
| 270 | + $phpcsFile->fixer->addContent($closeBracket, ' '); |
|
| 271 | + } else { |
|
| 272 | + $phpcsFile->fixer->replaceToken(($closeBracket + 1), ' '); |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + }//end process() |
|
| 277 | 277 | |
| 278 | 278 | |
| 279 | 279 | }//end class |
@@ -70,33 +70,33 @@ discard block |
||
| 70 | 70 | * |
| 71 | 71 | * @return void |
| 72 | 72 | */ |
| 73 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 73 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 74 | 74 | { |
| 75 | 75 | $tokens = $phpcsFile->getTokens(); |
| 76 | 76 | |
| 77 | - if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) { |
|
| 77 | + if ( isset( $tokens[ $stackPtr ][ 'parenthesis_opener' ] ) === false ) { |
|
| 78 | 78 | return; |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - $openBracket = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 82 | - $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 81 | + $openBracket = $tokens[ $stackPtr ][ 'parenthesis_opener' ]; |
|
| 82 | + $closeBracket = $tokens[ $stackPtr ][ 'parenthesis_closer' ]; |
|
| 83 | 83 | $spaceAfterOpen = 0; |
| 84 | - if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 85 | - if (strpos($tokens[($openBracket + 1)]['content'], $phpcsFile->eolChar) !== false) { |
|
| 84 | + if ( $tokens[ ( $openBracket + 1 ) ][ 'code' ] === T_WHITESPACE ) { |
|
| 85 | + if ( strpos( $tokens[ ( $openBracket + 1 ) ][ 'content' ], $phpcsFile->eolChar ) !== false ) { |
|
| 86 | 86 | $spaceAfterOpen = 'newline'; |
| 87 | 87 | } else { |
| 88 | - $spaceAfterOpen = strlen($tokens[($openBracket + 1)]['content']); |
|
| 88 | + $spaceAfterOpen = strlen( $tokens[ ( $openBracket + 1 ) ][ 'content' ] ); |
|
| 89 | 89 | } |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - if ($spaceAfterOpen !== 0) { |
|
| 92 | + if ( $spaceAfterOpen !== 0 ) { |
|
| 93 | 93 | $error = 'First condition of a multi-line IF statement must directly follow the opening parenthesis'; |
| 94 | - $fix = $phpcsFile->addFixableError($error, ($openBracket + 1), 'SpacingAfterOpenBrace'); |
|
| 95 | - if ($fix === true) { |
|
| 96 | - if ($spaceAfterOpen === 'newline') { |
|
| 97 | - $phpcsFile->fixer->replaceToken(($openBracket + 1), ''); |
|
| 94 | + $fix = $phpcsFile->addFixableError( $error, ( $openBracket + 1 ), 'SpacingAfterOpenBrace' ); |
|
| 95 | + if ( $fix === true ) { |
|
| 96 | + if ( $spaceAfterOpen === 'newline' ) { |
|
| 97 | + $phpcsFile->fixer->replaceToken( ( $openBracket + 1 ), '' ); |
|
| 98 | 98 | } else { |
| 99 | - $phpcsFile->fixer->replaceToken(($openBracket + 1), ''); |
|
| 99 | + $phpcsFile->fixer->replaceToken( ( $openBracket + 1 ), '' ); |
|
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 102 | } |
@@ -104,119 +104,119 @@ discard block |
||
| 104 | 104 | // We need to work out how far indented the if statement |
| 105 | 105 | // itself is, so we can work out how far to indent conditions. |
| 106 | 106 | $statementIndent = 0; |
| 107 | - for ($i = ($stackPtr - 1); $i >= 0; $i--) { |
|
| 108 | - if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 107 | + for ( $i = ( $stackPtr - 1 ); $i >= 0; $i-- ) { |
|
| 108 | + if ( $tokens[ $i ][ 'line' ] !== $tokens[ $stackPtr ][ 'line' ] ) { |
|
| 109 | 109 | $i++; |
| 110 | 110 | break; |
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - if ($i >= 0 && $tokens[$i]['code'] === T_WHITESPACE) { |
|
| 115 | - $statementIndent = strlen($tokens[$i]['content']); |
|
| 114 | + if ( $i >= 0 && $tokens[ $i ][ 'code' ] === T_WHITESPACE ) { |
|
| 115 | + $statementIndent = strlen( $tokens[ $i ][ 'content' ] ); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | // Each line between the parenthesis should be indented 4 spaces |
| 119 | 119 | // and start with an operator, unless the line is inside a |
| 120 | 120 | // function call, in which case it is ignored. |
| 121 | - $prevLine = $tokens[$openBracket]['line']; |
|
| 122 | - for ($i = ($openBracket + 1); $i <= $closeBracket; $i++) { |
|
| 123 | - if ($i === $closeBracket && $tokens[$openBracket]['line'] !== $tokens[$i]['line']) { |
|
| 124 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($i - 1), null, true); |
|
| 125 | - if ($tokens[$prev]['line'] === $tokens[$i]['line']) { |
|
| 121 | + $prevLine = $tokens[ $openBracket ][ 'line' ]; |
|
| 122 | + for ( $i = ( $openBracket + 1 ); $i <= $closeBracket; $i++ ) { |
|
| 123 | + if ( $i === $closeBracket && $tokens[ $openBracket ][ 'line' ] !== $tokens[ $i ][ 'line' ] ) { |
|
| 124 | + $prev = $phpcsFile->findPrevious( T_WHITESPACE, ( $i - 1 ), null, true ); |
|
| 125 | + if ( $tokens[ $prev ][ 'line' ] === $tokens[ $i ][ 'line' ] ) { |
|
| 126 | 126 | // Closing bracket is on the same line as a condition. |
| 127 | 127 | $error = 'Closing parenthesis of a multi-line IF statement must be on a new line'; |
| 128 | - $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketNewLine'); |
|
| 129 | - if ($fix === true) { |
|
| 128 | + $fix = $phpcsFile->addFixableError( $error, $closeBracket, 'CloseBracketNewLine' ); |
|
| 129 | + if ( $fix === true ) { |
|
| 130 | 130 | // Account for a comment at the end of the line. |
| 131 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true); |
|
| 132 | - if ($tokens[$next]['code'] !== T_COMMENT) { |
|
| 133 | - $phpcsFile->fixer->addNewlineBefore($closeBracket); |
|
| 131 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $closeBracket + 1 ), null, true ); |
|
| 132 | + if ( $tokens[ $next ][ 'code' ] !== T_COMMENT ) { |
|
| 133 | + $phpcsFile->fixer->addNewlineBefore( $closeBracket ); |
|
| 134 | 134 | } else { |
| 135 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true); |
|
| 135 | + $next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next + 1 ), null, true ); |
|
| 136 | 136 | $phpcsFile->fixer->beginChangeset(); |
| 137 | - $phpcsFile->fixer->replaceToken($closeBracket, ''); |
|
| 138 | - $phpcsFile->fixer->addContentBefore($next, ')'); |
|
| 137 | + $phpcsFile->fixer->replaceToken( $closeBracket, '' ); |
|
| 138 | + $phpcsFile->fixer->addContentBefore( $next, ')' ); |
|
| 139 | 139 | $phpcsFile->fixer->endChangeset(); |
| 140 | 140 | } |
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | }//end if |
| 144 | 144 | |
| 145 | - if ($tokens[$i]['line'] !== $prevLine) { |
|
| 146 | - if ($tokens[$i]['line'] === $tokens[$closeBracket]['line']) { |
|
| 147 | - $next = $phpcsFile->findNext(T_WHITESPACE, $i, null, true); |
|
| 148 | - if ($next !== $closeBracket) { |
|
| 149 | - $expectedIndent = ($statementIndent + $this->indent); |
|
| 145 | + if ( $tokens[ $i ][ 'line' ] !== $prevLine ) { |
|
| 146 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $closeBracket ][ 'line' ] ) { |
|
| 147 | + $next = $phpcsFile->findNext( T_WHITESPACE, $i, null, true ); |
|
| 148 | + if ( $next !== $closeBracket ) { |
|
| 149 | + $expectedIndent = ( $statementIndent + $this->indent ); |
|
| 150 | 150 | } else { |
| 151 | 151 | // Closing brace needs to be indented to the same level |
| 152 | 152 | // as the statement. |
| 153 | 153 | $expectedIndent = $statementIndent; |
| 154 | 154 | }//end if |
| 155 | 155 | } else { |
| 156 | - $expectedIndent = ($statementIndent + $this->indent); |
|
| 156 | + $expectedIndent = ( $statementIndent + $this->indent ); |
|
| 157 | 157 | }//end if |
| 158 | 158 | |
| 159 | - if ($tokens[$i]['code'] === T_COMMENT) { |
|
| 160 | - $prevLine = $tokens[$i]['line']; |
|
| 159 | + if ( $tokens[ $i ][ 'code' ] === T_COMMENT ) { |
|
| 160 | + $prevLine = $tokens[ $i ][ 'line' ]; |
|
| 161 | 161 | continue; |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | // We changed lines, so this should be a whitespace indent token. |
| 165 | - if ($tokens[$i]['code'] !== T_WHITESPACE) { |
|
| 165 | + if ( $tokens[ $i ][ 'code' ] !== T_WHITESPACE ) { |
|
| 166 | 166 | $foundIndent = 0; |
| 167 | 167 | } else { |
| 168 | - $foundIndent = strlen($tokens[$i]['content']); |
|
| 168 | + $foundIndent = strlen( $tokens[ $i ][ 'content' ] ); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - if ($expectedIndent !== $foundIndent) { |
|
| 171 | + if ( $expectedIndent !== $foundIndent ) { |
|
| 172 | 172 | $error = 'Multi-line IF statement not indented correctly; expected %s spaces but found %s'; |
| 173 | 173 | $data = array( |
| 174 | 174 | $expectedIndent, |
| 175 | 175 | $foundIndent, |
| 176 | 176 | ); |
| 177 | 177 | |
| 178 | - $fix = $phpcsFile->addFixableError($error, $i, 'Alignment', $data); |
|
| 179 | - if ($fix === true) { |
|
| 180 | - $spaces = str_repeat(' ', $expectedIndent); |
|
| 181 | - if ($foundIndent === 0) { |
|
| 182 | - $phpcsFile->fixer->addContentBefore($i, $spaces); |
|
| 178 | + $fix = $phpcsFile->addFixableError( $error, $i, 'Alignment', $data ); |
|
| 179 | + if ( $fix === true ) { |
|
| 180 | + $spaces = str_repeat( ' ', $expectedIndent ); |
|
| 181 | + if ( $foundIndent === 0 ) { |
|
| 182 | + $phpcsFile->fixer->addContentBefore( $i, $spaces ); |
|
| 183 | 183 | } else { |
| 184 | - $phpcsFile->fixer->replaceToken($i, $spaces); |
|
| 184 | + $phpcsFile->fixer->replaceToken( $i, $spaces ); |
|
| 185 | 185 | } |
| 186 | 186 | } |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $i, null, true); |
|
| 190 | - if ($next !== $closeBracket) { |
|
| 191 | - if (isset(PHP_CodeSniffer_Tokens::$booleanOperators[$tokens[$next]['code']]) === false) { |
|
| 189 | + $next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, $i, null, true ); |
|
| 190 | + if ( $next !== $closeBracket ) { |
|
| 191 | + if ( isset( PHP_CodeSniffer_Tokens::$booleanOperators[ $tokens[ $next ][ 'code' ] ] ) === false ) { |
|
| 192 | 192 | $error = 'Each line in a multi-line IF statement must begin with a boolean operator'; |
| 193 | - $fix = $phpcsFile->addFixableError($error, $i, 'StartWithBoolean'); |
|
| 194 | - if ($fix === true) { |
|
| 195 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($i - 1), $openBracket, true); |
|
| 196 | - if (isset(PHP_CodeSniffer_Tokens::$booleanOperators[$tokens[$prev]['code']]) === true) { |
|
| 193 | + $fix = $phpcsFile->addFixableError( $error, $i, 'StartWithBoolean' ); |
|
| 194 | + if ( $fix === true ) { |
|
| 195 | + $prev = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $i - 1 ), $openBracket, true ); |
|
| 196 | + if ( isset( PHP_CodeSniffer_Tokens::$booleanOperators[ $tokens[ $prev ][ 'code' ] ] ) === true ) { |
|
| 197 | 197 | $phpcsFile->fixer->beginChangeset(); |
| 198 | - $phpcsFile->fixer->replaceToken($prev, ''); |
|
| 199 | - $phpcsFile->fixer->addContentBefore($next, $tokens[$prev]['content'].' '); |
|
| 198 | + $phpcsFile->fixer->replaceToken( $prev, '' ); |
|
| 199 | + $phpcsFile->fixer->addContentBefore( $next, $tokens[ $prev ][ 'content' ] . ' ' ); |
|
| 200 | 200 | $phpcsFile->fixer->endChangeset(); |
| 201 | 201 | } else { |
| 202 | - for ($x = ($prev + 1); $x < $next; $x++) { |
|
| 203 | - $phpcsFile->fixer->replaceToken($x, ''); |
|
| 202 | + for ( $x = ( $prev + 1 ); $x < $next; $x++ ) { |
|
| 203 | + $phpcsFile->fixer->replaceToken( $x, '' ); |
|
| 204 | 204 | } |
| 205 | 205 | } |
| 206 | 206 | } |
| 207 | 207 | } |
| 208 | 208 | }//end if |
| 209 | 209 | |
| 210 | - $prevLine = $tokens[$i]['line']; |
|
| 210 | + $prevLine = $tokens[ $i ][ 'line' ]; |
|
| 211 | 211 | }//end if |
| 212 | 212 | |
| 213 | - if ($tokens[$i]['code'] === T_STRING) { |
|
| 214 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true); |
|
| 215 | - if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { |
|
| 213 | + if ( $tokens[ $i ][ 'code' ] === T_STRING ) { |
|
| 214 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $i + 1 ), null, true ); |
|
| 215 | + if ( $tokens[ $next ][ 'code' ] === T_OPEN_PARENTHESIS ) { |
|
| 216 | 216 | // This is a function call, so skip to the end as they |
| 217 | 217 | // have their own indentation rules. |
| 218 | - $i = $tokens[$next]['parenthesis_closer']; |
|
| 219 | - $prevLine = $tokens[$i]['line']; |
|
| 218 | + $i = $tokens[ $next ][ 'parenthesis_closer' ]; |
|
| 219 | + $prevLine = $tokens[ $i ][ 'line' ]; |
|
| 220 | 220 | continue; |
| 221 | 221 | } |
| 222 | 222 | } |
@@ -224,52 +224,52 @@ discard block |
||
| 224 | 224 | |
| 225 | 225 | // From here on, we are checking the spacing of the opening and closing |
| 226 | 226 | // braces. If this IF statement does not use braces, we end here. |
| 227 | - if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
| 227 | + if ( isset( $tokens[ $stackPtr ][ 'scope_opener' ] ) === false ) { |
|
| 228 | 228 | return; |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | // The opening brace needs to be one space away from the closing parenthesis. |
| 232 | - $openBrace = $tokens[$stackPtr]['scope_opener']; |
|
| 233 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), $openBrace, true); |
|
| 234 | - if ($next !== false) { |
|
| 232 | + $openBrace = $tokens[ $stackPtr ][ 'scope_opener' ]; |
|
| 233 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $closeBracket + 1 ), $openBrace, true ); |
|
| 234 | + if ( $next !== false ) { |
|
| 235 | 235 | // Probably comments in between tokens, so don't check. |
| 236 | 236 | return; |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - if ($tokens[$openBrace]['line'] > $tokens[$closeBracket]['line']) { |
|
| 239 | + if ( $tokens[ $openBrace ][ 'line' ] > $tokens[ $closeBracket ][ 'line' ] ) { |
|
| 240 | 240 | $length = -1; |
| 241 | - } else if ($openBrace === ($closeBracket + 1)) { |
|
| 241 | + } else if ( $openBrace === ( $closeBracket + 1 ) ) { |
|
| 242 | 242 | $length = 0; |
| 243 | - } else if ($openBrace === ($closeBracket + 2) |
|
| 244 | - && $tokens[($closeBracket + 1)]['code'] === T_WHITESPACE |
|
| 243 | + } else if ( $openBrace === ( $closeBracket + 2 ) |
|
| 244 | + && $tokens[ ( $closeBracket + 1 ) ][ 'code' ] === T_WHITESPACE |
|
| 245 | 245 | ) { |
| 246 | - $length = strlen($tokens[($closeBracket + 1)]['content']); |
|
| 246 | + $length = strlen( $tokens[ ( $closeBracket + 1 ) ][ 'content' ] ); |
|
| 247 | 247 | } else { |
| 248 | 248 | // Confused, so don't check. |
| 249 | 249 | $length = 1; |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | - if ($length === 1) { |
|
| 252 | + if ( $length === 1 ) { |
|
| 253 | 253 | return; |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | - $data = array($length); |
|
| 256 | + $data = array( $length ); |
|
| 257 | 257 | $code = 'SpaceBeforeOpenBrace'; |
| 258 | 258 | |
| 259 | 259 | $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line IF statement; found '; |
| 260 | - if ($length === -1) { |
|
| 260 | + if ( $length === -1 ) { |
|
| 261 | 261 | $error .= 'newline'; |
| 262 | 262 | $code = 'NewlineBeforeOpenBrace'; |
| 263 | 263 | } else { |
| 264 | 264 | $error .= '%s spaces'; |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - $fix = $phpcsFile->addFixableError($error, ($closeBracket + 1), $code, $data); |
|
| 268 | - if ($fix === true) { |
|
| 269 | - if ($length === 0) { |
|
| 270 | - $phpcsFile->fixer->addContent($closeBracket, ' '); |
|
| 267 | + $fix = $phpcsFile->addFixableError( $error, ( $closeBracket + 1 ), $code, $data ); |
|
| 268 | + if ( $fix === true ) { |
|
| 269 | + if ( $length === 0 ) { |
|
| 270 | + $phpcsFile->fixer->addContent( $closeBracket, ' ' ); |
|
| 271 | 271 | } else { |
| 272 | - $phpcsFile->fixer->replaceToken(($closeBracket + 1), ' '); |
|
| 272 | + $phpcsFile->fixer->replaceToken( ( $closeBracket + 1 ), ' ' ); |
|
| 273 | 273 | } |
| 274 | 274 | } |
| 275 | 275 | |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * @version Release: @package_version@ |
| 26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 27 | 27 | */ |
| 28 | -class PEAR_Sniffs_ControlStructures_MultiLineConditionSniff implements PHP_CodeSniffer_Sniff |
|
| 29 | -{ |
|
| 28 | +class PEAR_Sniffs_ControlStructures_MultiLineConditionSniff implements PHP_CodeSniffer_Sniff { |
|
| 30 | 29 | |
| 31 | 30 | /** |
| 32 | 31 | * A list of tokenizers this sniff supports. |
@@ -51,8 +50,7 @@ discard block |
||
| 51 | 50 | * |
| 52 | 51 | * @return array |
| 53 | 52 | */ |
| 54 | - public function register() |
|
| 55 | - { |
|
| 53 | + public function register() { |
|
| 56 | 54 | return array( |
| 57 | 55 | T_IF, |
| 58 | 56 | T_ELSEIF, |
@@ -70,8 +68,7 @@ discard block |
||
| 70 | 68 | * |
| 71 | 69 | * @return void |
| 72 | 70 | */ |
| 73 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 74 | - { |
|
| 71 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 75 | 72 | $tokens = $phpcsFile->getTokens(); |
| 76 | 73 | |
| 77 | 74 | if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) { |
@@ -39,7 +39,7 @@ |
||
| 39 | 39 | /** |
| 40 | 40 | * Returns an array of tokens this test wants to listen for. |
| 41 | 41 | * |
| 42 | - * @return array |
|
| 42 | + * @return string[] |
|
| 43 | 43 | */ |
| 44 | 44 | public function register() |
| 45 | 45 | { |
@@ -28,92 +28,92 @@ |
||
| 28 | 28 | class PEAR_Sniffs_Formatting_MultiLineAssignmentSniff implements PHP_CodeSniffer_Sniff |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The number of spaces code should be indented. |
|
| 33 | - * |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - public $indent = 4; |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Returns an array of tokens this test wants to listen for. |
|
| 41 | - * |
|
| 42 | - * @return array |
|
| 43 | - */ |
|
| 44 | - public function register() |
|
| 45 | - { |
|
| 46 | - return array(T_EQUAL); |
|
| 47 | - |
|
| 48 | - }//end register() |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Processes this test, when one of its tokens is encountered. |
|
| 53 | - * |
|
| 54 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 55 | - * @param int $stackPtr The position of the current token |
|
| 56 | - * in the stack passed in $tokens. |
|
| 57 | - * |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 61 | - { |
|
| 62 | - $tokens = $phpcsFile->getTokens(); |
|
| 63 | - |
|
| 64 | - // Equal sign can't be the last thing on the line. |
|
| 65 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 66 | - if ($next === false) { |
|
| 67 | - // Bad assignment. |
|
| 68 | - return; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 72 | - $error = 'Multi-line assignments must have the equal sign on the second line'; |
|
| 73 | - $phpcsFile->addError($error, $stackPtr, 'EqualSignLine'); |
|
| 74 | - return; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - // Make sure it is the first thing on the line, otherwise we ignore it. |
|
| 78 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), false, true); |
|
| 79 | - if ($prev === false) { |
|
| 80 | - // Bad assignment. |
|
| 81 | - return; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) { |
|
| 85 | - return; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - // Find the required indent based on the ident of the previous line. |
|
| 89 | - $assignmentIndent = 0; |
|
| 90 | - $prevLine = $tokens[$prev]['line']; |
|
| 91 | - for ($i = ($prev - 1); $i >= 0; $i--) { |
|
| 92 | - if ($tokens[$i]['line'] !== $prevLine) { |
|
| 93 | - $i++; |
|
| 94 | - break; |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 99 | - $assignmentIndent = strlen($tokens[$i]['content']); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // Find the actual indent. |
|
| 103 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1)); |
|
| 104 | - |
|
| 105 | - $expectedIndent = ($assignmentIndent + $this->indent); |
|
| 106 | - $foundIndent = strlen($tokens[$prev]['content']); |
|
| 107 | - if ($foundIndent !== $expectedIndent) { |
|
| 108 | - $error = 'Multi-line assignment not indented correctly; expected %s spaces but found %s'; |
|
| 109 | - $data = array( |
|
| 110 | - $expectedIndent, |
|
| 111 | - $foundIndent, |
|
| 112 | - ); |
|
| 113 | - $phpcsFile->addError($error, $stackPtr, 'Indent', $data); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - }//end process() |
|
| 31 | + /** |
|
| 32 | + * The number of spaces code should be indented. |
|
| 33 | + * |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + public $indent = 4; |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Returns an array of tokens this test wants to listen for. |
|
| 41 | + * |
|
| 42 | + * @return array |
|
| 43 | + */ |
|
| 44 | + public function register() |
|
| 45 | + { |
|
| 46 | + return array(T_EQUAL); |
|
| 47 | + |
|
| 48 | + }//end register() |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Processes this test, when one of its tokens is encountered. |
|
| 53 | + * |
|
| 54 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 55 | + * @param int $stackPtr The position of the current token |
|
| 56 | + * in the stack passed in $tokens. |
|
| 57 | + * |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 61 | + { |
|
| 62 | + $tokens = $phpcsFile->getTokens(); |
|
| 63 | + |
|
| 64 | + // Equal sign can't be the last thing on the line. |
|
| 65 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 66 | + if ($next === false) { |
|
| 67 | + // Bad assignment. |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 72 | + $error = 'Multi-line assignments must have the equal sign on the second line'; |
|
| 73 | + $phpcsFile->addError($error, $stackPtr, 'EqualSignLine'); |
|
| 74 | + return; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + // Make sure it is the first thing on the line, otherwise we ignore it. |
|
| 78 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), false, true); |
|
| 79 | + if ($prev === false) { |
|
| 80 | + // Bad assignment. |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) { |
|
| 85 | + return; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + // Find the required indent based on the ident of the previous line. |
|
| 89 | + $assignmentIndent = 0; |
|
| 90 | + $prevLine = $tokens[$prev]['line']; |
|
| 91 | + for ($i = ($prev - 1); $i >= 0; $i--) { |
|
| 92 | + if ($tokens[$i]['line'] !== $prevLine) { |
|
| 93 | + $i++; |
|
| 94 | + break; |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 99 | + $assignmentIndent = strlen($tokens[$i]['content']); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // Find the actual indent. |
|
| 103 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1)); |
|
| 104 | + |
|
| 105 | + $expectedIndent = ($assignmentIndent + $this->indent); |
|
| 106 | + $foundIndent = strlen($tokens[$prev]['content']); |
|
| 107 | + if ($foundIndent !== $expectedIndent) { |
|
| 108 | + $error = 'Multi-line assignment not indented correctly; expected %s spaces but found %s'; |
|
| 109 | + $data = array( |
|
| 110 | + $expectedIndent, |
|
| 111 | + $foundIndent, |
|
| 112 | + ); |
|
| 113 | + $phpcsFile->addError($error, $stackPtr, 'Indent', $data); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + }//end process() |
|
| 117 | 117 | |
| 118 | 118 | |
| 119 | 119 | }//end class |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function register() |
| 45 | 45 | { |
| 46 | - return array(T_EQUAL); |
|
| 46 | + return array( T_EQUAL ); |
|
| 47 | 47 | |
| 48 | 48 | }//end register() |
| 49 | 49 | |
@@ -57,60 +57,60 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return void |
| 59 | 59 | */ |
| 60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 60 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 61 | 61 | { |
| 62 | 62 | $tokens = $phpcsFile->getTokens(); |
| 63 | 63 | |
| 64 | 64 | // Equal sign can't be the last thing on the line. |
| 65 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 66 | - if ($next === false) { |
|
| 65 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
|
| 66 | + if ( $next === false ) { |
|
| 67 | 67 | // Bad assignment. |
| 68 | 68 | return; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 71 | + if ( $tokens[ $next ][ 'line' ] !== $tokens[ $stackPtr ][ 'line' ] ) { |
|
| 72 | 72 | $error = 'Multi-line assignments must have the equal sign on the second line'; |
| 73 | - $phpcsFile->addError($error, $stackPtr, 'EqualSignLine'); |
|
| 73 | + $phpcsFile->addError( $error, $stackPtr, 'EqualSignLine' ); |
|
| 74 | 74 | return; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | // Make sure it is the first thing on the line, otherwise we ignore it. |
| 78 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), false, true); |
|
| 79 | - if ($prev === false) { |
|
| 78 | + $prev = $phpcsFile->findPrevious( T_WHITESPACE, ( $stackPtr - 1 ), false, true ); |
|
| 79 | + if ( $prev === false ) { |
|
| 80 | 80 | // Bad assignment. |
| 81 | 81 | return; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) { |
|
| 84 | + if ( $tokens[ $prev ][ 'line' ] === $tokens[ $stackPtr ][ 'line' ] ) { |
|
| 85 | 85 | return; |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | // Find the required indent based on the ident of the previous line. |
| 89 | 89 | $assignmentIndent = 0; |
| 90 | - $prevLine = $tokens[$prev]['line']; |
|
| 91 | - for ($i = ($prev - 1); $i >= 0; $i--) { |
|
| 92 | - if ($tokens[$i]['line'] !== $prevLine) { |
|
| 90 | + $prevLine = $tokens[ $prev ][ 'line' ]; |
|
| 91 | + for ( $i = ( $prev - 1 ); $i >= 0; $i-- ) { |
|
| 92 | + if ( $tokens[ $i ][ 'line' ] !== $prevLine ) { |
|
| 93 | 93 | $i++; |
| 94 | 94 | break; |
| 95 | 95 | } |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 99 | - $assignmentIndent = strlen($tokens[$i]['content']); |
|
| 98 | + if ( $tokens[ $i ][ 'code' ] === T_WHITESPACE ) { |
|
| 99 | + $assignmentIndent = strlen( $tokens[ $i ][ 'content' ] ); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | // Find the actual indent. |
| 103 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1)); |
|
| 103 | + $prev = $phpcsFile->findPrevious( T_WHITESPACE, ( $stackPtr - 1 ) ); |
|
| 104 | 104 | |
| 105 | - $expectedIndent = ($assignmentIndent + $this->indent); |
|
| 106 | - $foundIndent = strlen($tokens[$prev]['content']); |
|
| 107 | - if ($foundIndent !== $expectedIndent) { |
|
| 105 | + $expectedIndent = ( $assignmentIndent + $this->indent ); |
|
| 106 | + $foundIndent = strlen( $tokens[ $prev ][ 'content' ] ); |
|
| 107 | + if ( $foundIndent !== $expectedIndent ) { |
|
| 108 | 108 | $error = 'Multi-line assignment not indented correctly; expected %s spaces but found %s'; |
| 109 | 109 | $data = array( |
| 110 | 110 | $expectedIndent, |
| 111 | 111 | $foundIndent, |
| 112 | 112 | ); |
| 113 | - $phpcsFile->addError($error, $stackPtr, 'Indent', $data); |
|
| 113 | + $phpcsFile->addError( $error, $stackPtr, 'Indent', $data ); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | }//end process() |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * @version Release: @package_version@ |
| 26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 27 | 27 | */ |
| 28 | -class PEAR_Sniffs_Formatting_MultiLineAssignmentSniff implements PHP_CodeSniffer_Sniff |
|
| 29 | -{ |
|
| 28 | +class PEAR_Sniffs_Formatting_MultiLineAssignmentSniff implements PHP_CodeSniffer_Sniff { |
|
| 30 | 29 | |
| 31 | 30 | /** |
| 32 | 31 | * The number of spaces code should be indented. |
@@ -41,8 +40,7 @@ discard block |
||
| 41 | 40 | * |
| 42 | 41 | * @return array |
| 43 | 42 | */ |
| 44 | - public function register() |
|
| 45 | - { |
|
| 43 | + public function register() { |
|
| 46 | 44 | return array(T_EQUAL); |
| 47 | 45 | |
| 48 | 46 | }//end register() |
@@ -57,8 +55,7 @@ discard block |
||
| 57 | 55 | * |
| 58 | 56 | * @return void |
| 59 | 57 | */ |
| 60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 61 | - { |
|
| 58 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 62 | 59 | $tokens = $phpcsFile->getTokens(); |
| 63 | 60 | |
| 64 | 61 | // Equal sign can't be the last thing on the line. |
@@ -175,7 +175,7 @@ |
||
| 175 | 175 | * @param array $tokens The stack of tokens that make up |
| 176 | 176 | * the file. |
| 177 | 177 | * |
| 178 | - * @return void |
|
| 178 | + * @return boolean |
|
| 179 | 179 | */ |
| 180 | 180 | public function isMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
| 181 | 181 | { |
@@ -28,492 +28,492 @@ |
||
| 28 | 28 | class PEAR_Sniffs_Functions_FunctionCallSignatureSniff implements PHP_CodeSniffer_Sniff |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * A list of tokenizers this sniff supports. |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - public $supportedTokenizers = array( |
|
| 37 | - 'PHP', |
|
| 38 | - 'JS', |
|
| 39 | - ); |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The number of spaces code should be indented. |
|
| 43 | - * |
|
| 44 | - * @var int |
|
| 45 | - */ |
|
| 46 | - public $indent = 4; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * If TRUE, multiple arguments can be defined per line in a multi-line call. |
|
| 50 | - * |
|
| 51 | - * @var bool |
|
| 52 | - */ |
|
| 53 | - public $allowMultipleArguments = true; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * How many spaces should follow the opening bracket. |
|
| 57 | - * |
|
| 58 | - * @var int |
|
| 59 | - */ |
|
| 60 | - public $requiredSpacesAfterOpen = 0; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * How many spaces should precede the closing bracket. |
|
| 64 | - * |
|
| 65 | - * @var int |
|
| 66 | - */ |
|
| 67 | - public $requiredSpacesBeforeClose = 0; |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Returns an array of tokens this test wants to listen for. |
|
| 72 | - * |
|
| 73 | - * @return array |
|
| 74 | - */ |
|
| 75 | - public function register() |
|
| 76 | - { |
|
| 77 | - return PHP_CodeSniffer_Tokens::$functionNameTokens; |
|
| 78 | - |
|
| 79 | - }//end register() |
|
| 80 | - |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Processes this test, when one of its tokens is encountered. |
|
| 84 | - * |
|
| 85 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 86 | - * @param int $stackPtr The position of the current token |
|
| 87 | - * in the stack passed in $tokens. |
|
| 88 | - * |
|
| 89 | - * @return void |
|
| 90 | - */ |
|
| 91 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 92 | - { |
|
| 93 | - $this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen; |
|
| 94 | - $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose; |
|
| 95 | - $tokens = $phpcsFile->getTokens(); |
|
| 96 | - |
|
| 97 | - // Find the next non-empty token. |
|
| 98 | - $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 99 | - |
|
| 100 | - if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { |
|
| 101 | - // Not a function call. |
|
| 102 | - return; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if (isset($tokens[$openBracket]['parenthesis_closer']) === false) { |
|
| 106 | - // Not a function call. |
|
| 107 | - return; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // Find the previous non-empty token. |
|
| 111 | - $search = PHP_CodeSniffer_Tokens::$emptyTokens; |
|
| 112 | - $search[] = T_BITWISE_AND; |
|
| 113 | - $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true); |
|
| 114 | - if ($tokens[$previous]['code'] === T_FUNCTION) { |
|
| 115 | - // It's a function definition, not a function call. |
|
| 116 | - return; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 120 | - |
|
| 121 | - if (($stackPtr + 1) !== $openBracket) { |
|
| 122 | - // Checking this: $value = my_function[*](...). |
|
| 123 | - $error = 'Space before opening parenthesis of function call prohibited'; |
|
| 124 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeOpenBracket'); |
|
| 125 | - if ($fix === true) { |
|
| 126 | - $phpcsFile->fixer->beginChangeset(); |
|
| 127 | - for ($i = ($stackPtr + 1); $i < $openBracket; $i++) { |
|
| 128 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - // Modify the bracket as well to ensure a conflict if the bracket |
|
| 132 | - // has been changed in some way by another sniff. |
|
| 133 | - $phpcsFile->fixer->replaceToken($openBracket, '('); |
|
| 134 | - $phpcsFile->fixer->endChangeset(); |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true); |
|
| 139 | - if ($tokens[$next]['code'] === T_SEMICOLON) { |
|
| 140 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[($closeBracket + 1)]['code']]) === true) { |
|
| 141 | - $error = 'Space after closing parenthesis of function call prohibited'; |
|
| 142 | - $fix = $phpcsFile->addFixableError($error, $closeBracket, 'SpaceAfterCloseBracket'); |
|
| 143 | - if ($fix === true) { |
|
| 144 | - $phpcsFile->fixer->beginChangeset(); |
|
| 145 | - for ($i = ($closeBracket + 1); $i < $next; $i++) { |
|
| 146 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // Modify the bracket as well to ensure a conflict if the bracket |
|
| 150 | - // has been changed in some way by another sniff. |
|
| 151 | - $phpcsFile->fixer->replaceToken($closeBracket, ')'); |
|
| 152 | - $phpcsFile->fixer->endChangeset(); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - // Check if this is a single line or multi-line function call. |
|
| 158 | - if ($this->isMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens) === true) { |
|
| 159 | - $this->processMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens); |
|
| 160 | - } else { |
|
| 161 | - $this->processSingleLineCall($phpcsFile, $stackPtr, $openBracket, $tokens); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - }//end process() |
|
| 165 | - |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Determine if this is a multi-line function call. |
|
| 169 | - * |
|
| 170 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 171 | - * @param int $stackPtr The position of the current token |
|
| 172 | - * in the stack passed in $tokens. |
|
| 173 | - * @param int $openBracket The position of the opening bracket |
|
| 174 | - * in the stack passed in $tokens. |
|
| 175 | - * @param array $tokens The stack of tokens that make up |
|
| 176 | - * the file. |
|
| 177 | - * |
|
| 178 | - * @return void |
|
| 179 | - */ |
|
| 180 | - public function isMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 181 | - { |
|
| 182 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 183 | - if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) { |
|
| 184 | - return true; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - return false; |
|
| 188 | - |
|
| 189 | - }//end isMultiLineCall() |
|
| 190 | - |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Processes single-line calls. |
|
| 194 | - * |
|
| 195 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 196 | - * @param int $stackPtr The position of the current token |
|
| 197 | - * in the stack passed in $tokens. |
|
| 198 | - * @param int $openBracket The position of the opening bracket |
|
| 199 | - * in the stack passed in $tokens. |
|
| 200 | - * @param array $tokens The stack of tokens that make up |
|
| 201 | - * the file. |
|
| 202 | - * |
|
| 203 | - * @return void |
|
| 204 | - */ |
|
| 205 | - public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 206 | - { |
|
| 207 | - $closer = $tokens[$openBracket]['parenthesis_closer']; |
|
| 208 | - if ($openBracket === ($closer - 1)) { |
|
| 209 | - return; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 213 | - // Checking this: $value = my_function([*]...). |
|
| 214 | - $error = 'Space after opening parenthesis of function call prohibited'; |
|
| 215 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpenBracket'); |
|
| 216 | - if ($fix === true) { |
|
| 217 | - $phpcsFile->fixer->replaceToken(($openBracket + 1), ''); |
|
| 218 | - } |
|
| 219 | - } else if ($this->requiredSpacesAfterOpen > 0) { |
|
| 220 | - $spaceAfterOpen = 0; |
|
| 221 | - if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 222 | - $spaceAfterOpen = strlen($tokens[($openBracket + 1)]['content']); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) { |
|
| 226 | - $error = 'Expected %s spaces after opening bracket; %s found'; |
|
| 227 | - $data = array( |
|
| 228 | - $this->requiredSpacesAfterOpen, |
|
| 229 | - $spaceAfterOpen, |
|
| 230 | - ); |
|
| 231 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpenBracket', $data); |
|
| 232 | - if ($fix === true) { |
|
| 233 | - $padding = str_repeat(' ', $this->requiredSpacesAfterOpen); |
|
| 234 | - if ($spaceAfterOpen === 0) { |
|
| 235 | - $phpcsFile->fixer->addContent($openBracket, $padding); |
|
| 236 | - } else { |
|
| 237 | - $phpcsFile->fixer->replaceToken(($openBracket + 1), $padding); |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - }//end if |
|
| 242 | - |
|
| 243 | - // Checking this: $value = my_function(...[*]). |
|
| 244 | - $spaceBeforeClose = 0; |
|
| 245 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($closer - 1), $openBracket, true); |
|
| 246 | - if ($tokens[$prev]['code'] === T_END_HEREDOC || $tokens[$prev]['code'] === T_END_NOWDOC) { |
|
| 247 | - // Need a newline after these tokens, so ignore this rule. |
|
| 248 | - return; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - if ($tokens[$prev]['line'] !== $tokens[$closer]['line']) { |
|
| 252 | - $spaceBeforeClose = 'newline'; |
|
| 253 | - } else if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) { |
|
| 254 | - $spaceBeforeClose = strlen($tokens[($closer - 1)]['content']); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) { |
|
| 258 | - $error = 'Expected %s spaces before closing bracket; %s found'; |
|
| 259 | - $data = array( |
|
| 260 | - $this->requiredSpacesBeforeClose, |
|
| 261 | - $spaceBeforeClose, |
|
| 262 | - ); |
|
| 263 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeCloseBracket', $data); |
|
| 264 | - if ($fix === true) { |
|
| 265 | - $padding = str_repeat(' ', $this->requiredSpacesBeforeClose); |
|
| 266 | - if ($spaceBeforeClose === 0) { |
|
| 267 | - $phpcsFile->fixer->addContentBefore($closer, $padding); |
|
| 268 | - } else { |
|
| 269 | - $phpcsFile->fixer->replaceToken(($closer - 1), $padding); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - }//end processSingleLineCall() |
|
| 275 | - |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Processes multi-line calls. |
|
| 279 | - * |
|
| 280 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 281 | - * @param int $stackPtr The position of the current token |
|
| 282 | - * in the stack passed in $tokens. |
|
| 283 | - * @param int $openBracket The position of the opening bracket |
|
| 284 | - * in the stack passed in $tokens. |
|
| 285 | - * @param array $tokens The stack of tokens that make up |
|
| 286 | - * the file. |
|
| 287 | - * |
|
| 288 | - * @return void |
|
| 289 | - */ |
|
| 290 | - public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 291 | - { |
|
| 292 | - // We need to work out how far indented the function |
|
| 293 | - // call itself is, so we can work out how far to |
|
| 294 | - // indent the arguments. |
|
| 295 | - $start = $phpcsFile->findStartOfStatement($stackPtr); |
|
| 296 | - foreach (array('stackPtr', 'start') as $checkToken) { |
|
| 297 | - $x = $$checkToken; |
|
| 298 | - for ($i = ($x - 1); $i >= 0; $i--) { |
|
| 299 | - if ($tokens[$i]['line'] !== $tokens[$x]['line']) { |
|
| 300 | - $i++; |
|
| 301 | - break; |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - if ($i <= 0) { |
|
| 306 | - $functionIndent = 0; |
|
| 307 | - } else if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 308 | - $functionIndent = strlen($tokens[$i]['content']); |
|
| 309 | - } else if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING) { |
|
| 310 | - $functionIndent = 0; |
|
| 311 | - } else { |
|
| 312 | - $trimmed = ltrim($tokens[$i]['content']); |
|
| 313 | - if ($trimmed === '') { |
|
| 314 | - if ($tokens[$i]['code'] === T_INLINE_HTML) { |
|
| 315 | - $functionIndent = strlen($tokens[$i]['content']); |
|
| 316 | - } else { |
|
| 317 | - $functionIndent = ($tokens[$i]['column'] - 1); |
|
| 318 | - } |
|
| 319 | - } else { |
|
| 320 | - $functionIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $varName = $checkToken.'Indent'; |
|
| 325 | - $$varName = $functionIndent; |
|
| 326 | - }//end foreach |
|
| 327 | - |
|
| 328 | - $functionIndent = max($startIndent, $stackPtrIndent); |
|
| 329 | - |
|
| 330 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($openBracket + 1), null, true); |
|
| 331 | - if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) { |
|
| 332 | - $error = 'Opening parenthesis of a multi-line function call must be the last content on the line'; |
|
| 333 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpenBracket'); |
|
| 334 | - if ($fix === true) { |
|
| 335 | - $phpcsFile->fixer->addContent( |
|
| 336 | - $openBracket, |
|
| 337 | - $phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent)) |
|
| 338 | - ); |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 343 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true); |
|
| 344 | - if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) { |
|
| 345 | - $error = 'Closing parenthesis of a multi-line function call must be on a line by itself'; |
|
| 346 | - $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine'); |
|
| 347 | - if ($fix === true) { |
|
| 348 | - $phpcsFile->fixer->addContentBefore( |
|
| 349 | - $closeBracket, |
|
| 350 | - $phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent)) |
|
| 351 | - ); |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - // Each line between the parenthesis should be indented n spaces. |
|
| 356 | - $lastLine = ($tokens[$openBracket]['line'] - 1); |
|
| 357 | - $argStart = null; |
|
| 358 | - $argEnd = null; |
|
| 359 | - $inArg = false; |
|
| 360 | - |
|
| 361 | - // Start processing at the first argument. |
|
| 362 | - $i = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true); |
|
| 363 | - if ($tokens[($i - 1)]['code'] === T_WHITESPACE |
|
| 364 | - && $tokens[($i - 1)]['line'] === $tokens[$i]['line'] |
|
| 365 | - ) { |
|
| 366 | - // Make sure we check the indent. |
|
| 367 | - $i--; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - for ($i; $i < $closeBracket; $i++) { |
|
| 371 | - if ($i > $argStart && $i < $argEnd) { |
|
| 372 | - $inArg = true; |
|
| 373 | - } else { |
|
| 374 | - $inArg = false; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - if ($tokens[$i]['line'] !== $lastLine) { |
|
| 378 | - $lastLine = $tokens[$i]['line']; |
|
| 379 | - |
|
| 380 | - // Ignore heredoc indentation. |
|
| 381 | - if (isset(PHP_CodeSniffer_Tokens::$heredocTokens[$tokens[$i]['code']]) === true) { |
|
| 382 | - continue; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - // Ignore multi-line string indentation. |
|
| 386 | - if (isset(PHP_CodeSniffer_Tokens::$stringTokens[$tokens[$i]['code']]) === true |
|
| 387 | - && $tokens[$i]['code'] === $tokens[($i - 1)]['code'] |
|
| 388 | - ) { |
|
| 389 | - continue; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - // Ignore inline HTML. |
|
| 393 | - if ($tokens[$i]['code'] === T_INLINE_HTML) { |
|
| 394 | - continue; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - if ($tokens[$i]['line'] !== $tokens[$openBracket]['line']) { |
|
| 398 | - // We changed lines, so this should be a whitespace indent token, but first make |
|
| 399 | - // sure it isn't a blank line because we don't need to check indent unless there |
|
| 400 | - // is actually some code to indent. |
|
| 401 | - if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 402 | - $nextCode = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), ($closeBracket + 1), true); |
|
| 403 | - if ($tokens[$nextCode]['line'] !== $lastLine) { |
|
| 404 | - if ($inArg === false) { |
|
| 405 | - $error = 'Empty lines are not allowed in multi-line function calls'; |
|
| 406 | - $fix = $phpcsFile->addFixableError($error, $i, 'EmptyLine'); |
|
| 407 | - if ($fix === true) { |
|
| 408 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 409 | - } |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - continue; |
|
| 413 | - } |
|
| 414 | - } else { |
|
| 415 | - $nextCode = $i; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - if ($tokens[$nextCode]['line'] === $tokens[$closeBracket]['line']) { |
|
| 419 | - // Closing brace needs to be indented to the same level |
|
| 420 | - // as the function call. |
|
| 421 | - $inArg = false; |
|
| 422 | - $expectedIndent = $functionIndent; |
|
| 423 | - } else { |
|
| 424 | - $expectedIndent = ($functionIndent + $this->indent); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - if ($tokens[$i]['code'] !== T_WHITESPACE |
|
| 428 | - && $tokens[$i]['code'] !== T_DOC_COMMENT_WHITESPACE |
|
| 429 | - ) { |
|
| 430 | - // Just check if it is a multi-line block comment. If so, we can |
|
| 431 | - // calculate the indent from the whitespace before the content. |
|
| 432 | - if ($tokens[$i]['code'] === T_COMMENT |
|
| 433 | - && $tokens[($i - 1)]['code'] === T_COMMENT |
|
| 434 | - ) { |
|
| 435 | - $trimmed = ltrim($tokens[$i]['content']); |
|
| 436 | - $foundIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 437 | - } else { |
|
| 438 | - $foundIndent = 0; |
|
| 439 | - } |
|
| 440 | - } else { |
|
| 441 | - $foundIndent = strlen($tokens[$i]['content']); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - if ($foundIndent < $expectedIndent |
|
| 445 | - || ($inArg === false |
|
| 446 | - && $expectedIndent !== $foundIndent) |
|
| 447 | - ) { |
|
| 448 | - $error = 'Multi-line function call not indented correctly; expected %s spaces but found %s'; |
|
| 449 | - $data = array( |
|
| 450 | - $expectedIndent, |
|
| 451 | - $foundIndent, |
|
| 452 | - ); |
|
| 453 | - |
|
| 454 | - $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data); |
|
| 455 | - if ($fix === true) { |
|
| 456 | - $padding = str_repeat(' ', $expectedIndent); |
|
| 457 | - if ($foundIndent === 0) { |
|
| 458 | - $phpcsFile->fixer->addContentBefore($i, $padding); |
|
| 459 | - } else { |
|
| 460 | - if ($tokens[$i]['code'] === T_COMMENT) { |
|
| 461 | - $comment = $padding.ltrim($tokens[$i]['content']); |
|
| 462 | - $phpcsFile->fixer->replaceToken($i, $comment); |
|
| 463 | - } else { |
|
| 464 | - $phpcsFile->fixer->replaceToken($i, $padding); |
|
| 465 | - } |
|
| 466 | - } |
|
| 467 | - } |
|
| 468 | - }//end if |
|
| 469 | - } else { |
|
| 470 | - $nextCode = $i; |
|
| 471 | - }//end if |
|
| 472 | - |
|
| 473 | - if ($inArg === false) { |
|
| 474 | - $argStart = $nextCode; |
|
| 475 | - $argEnd = $phpcsFile->findEndOfStatement($nextCode); |
|
| 476 | - } |
|
| 477 | - }//end if |
|
| 478 | - |
|
| 479 | - // If we are within an argument we should be ignoring commas |
|
| 480 | - // as these are not signaling the end of an argument. |
|
| 481 | - if ($inArg === false && $tokens[$i]['code'] === T_COMMA) { |
|
| 482 | - $next = $phpcsFile->findNext(array(T_WHITESPACE, T_COMMENT), ($i + 1), $closeBracket, true); |
|
| 483 | - if ($next === false) { |
|
| 484 | - continue; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - if ($this->allowMultipleArguments === false) { |
|
| 488 | - // Comma has to be the last token on the line. |
|
| 489 | - if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
| 490 | - $error = 'Only one argument is allowed per line in a multi-line function call'; |
|
| 491 | - $fix = $phpcsFile->addFixableError($error, $next, 'MultipleArguments'); |
|
| 492 | - if ($fix === true) { |
|
| 493 | - $phpcsFile->fixer->beginChangeset(); |
|
| 494 | - for ($x = ($next - 1); $x > $i; $x--) { |
|
| 495 | - if ($tokens[$x]['code'] !== T_WHITESPACE) { |
|
| 496 | - break; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - $phpcsFile->fixer->replaceToken($x, ''); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - $phpcsFile->fixer->addContentBefore( |
|
| 503 | - $next, |
|
| 504 | - $phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent)) |
|
| 505 | - ); |
|
| 506 | - $phpcsFile->fixer->endChangeset(); |
|
| 507 | - } |
|
| 508 | - } |
|
| 509 | - }//end if |
|
| 510 | - |
|
| 511 | - $argStart = $next; |
|
| 512 | - $argEnd = $phpcsFile->findEndOfStatement($next); |
|
| 513 | - }//end if |
|
| 514 | - }//end for |
|
| 515 | - |
|
| 516 | - }//end processMultiLineCall() |
|
| 31 | + /** |
|
| 32 | + * A list of tokenizers this sniff supports. |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + public $supportedTokenizers = array( |
|
| 37 | + 'PHP', |
|
| 38 | + 'JS', |
|
| 39 | + ); |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The number of spaces code should be indented. |
|
| 43 | + * |
|
| 44 | + * @var int |
|
| 45 | + */ |
|
| 46 | + public $indent = 4; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * If TRUE, multiple arguments can be defined per line in a multi-line call. |
|
| 50 | + * |
|
| 51 | + * @var bool |
|
| 52 | + */ |
|
| 53 | + public $allowMultipleArguments = true; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * How many spaces should follow the opening bracket. |
|
| 57 | + * |
|
| 58 | + * @var int |
|
| 59 | + */ |
|
| 60 | + public $requiredSpacesAfterOpen = 0; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * How many spaces should precede the closing bracket. |
|
| 64 | + * |
|
| 65 | + * @var int |
|
| 66 | + */ |
|
| 67 | + public $requiredSpacesBeforeClose = 0; |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Returns an array of tokens this test wants to listen for. |
|
| 72 | + * |
|
| 73 | + * @return array |
|
| 74 | + */ |
|
| 75 | + public function register() |
|
| 76 | + { |
|
| 77 | + return PHP_CodeSniffer_Tokens::$functionNameTokens; |
|
| 78 | + |
|
| 79 | + }//end register() |
|
| 80 | + |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Processes this test, when one of its tokens is encountered. |
|
| 84 | + * |
|
| 85 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 86 | + * @param int $stackPtr The position of the current token |
|
| 87 | + * in the stack passed in $tokens. |
|
| 88 | + * |
|
| 89 | + * @return void |
|
| 90 | + */ |
|
| 91 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 92 | + { |
|
| 93 | + $this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen; |
|
| 94 | + $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose; |
|
| 95 | + $tokens = $phpcsFile->getTokens(); |
|
| 96 | + |
|
| 97 | + // Find the next non-empty token. |
|
| 98 | + $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 99 | + |
|
| 100 | + if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { |
|
| 101 | + // Not a function call. |
|
| 102 | + return; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if (isset($tokens[$openBracket]['parenthesis_closer']) === false) { |
|
| 106 | + // Not a function call. |
|
| 107 | + return; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // Find the previous non-empty token. |
|
| 111 | + $search = PHP_CodeSniffer_Tokens::$emptyTokens; |
|
| 112 | + $search[] = T_BITWISE_AND; |
|
| 113 | + $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true); |
|
| 114 | + if ($tokens[$previous]['code'] === T_FUNCTION) { |
|
| 115 | + // It's a function definition, not a function call. |
|
| 116 | + return; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 120 | + |
|
| 121 | + if (($stackPtr + 1) !== $openBracket) { |
|
| 122 | + // Checking this: $value = my_function[*](...). |
|
| 123 | + $error = 'Space before opening parenthesis of function call prohibited'; |
|
| 124 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeOpenBracket'); |
|
| 125 | + if ($fix === true) { |
|
| 126 | + $phpcsFile->fixer->beginChangeset(); |
|
| 127 | + for ($i = ($stackPtr + 1); $i < $openBracket; $i++) { |
|
| 128 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + // Modify the bracket as well to ensure a conflict if the bracket |
|
| 132 | + // has been changed in some way by another sniff. |
|
| 133 | + $phpcsFile->fixer->replaceToken($openBracket, '('); |
|
| 134 | + $phpcsFile->fixer->endChangeset(); |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true); |
|
| 139 | + if ($tokens[$next]['code'] === T_SEMICOLON) { |
|
| 140 | + if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[($closeBracket + 1)]['code']]) === true) { |
|
| 141 | + $error = 'Space after closing parenthesis of function call prohibited'; |
|
| 142 | + $fix = $phpcsFile->addFixableError($error, $closeBracket, 'SpaceAfterCloseBracket'); |
|
| 143 | + if ($fix === true) { |
|
| 144 | + $phpcsFile->fixer->beginChangeset(); |
|
| 145 | + for ($i = ($closeBracket + 1); $i < $next; $i++) { |
|
| 146 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // Modify the bracket as well to ensure a conflict if the bracket |
|
| 150 | + // has been changed in some way by another sniff. |
|
| 151 | + $phpcsFile->fixer->replaceToken($closeBracket, ')'); |
|
| 152 | + $phpcsFile->fixer->endChangeset(); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + // Check if this is a single line or multi-line function call. |
|
| 158 | + if ($this->isMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens) === true) { |
|
| 159 | + $this->processMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens); |
|
| 160 | + } else { |
|
| 161 | + $this->processSingleLineCall($phpcsFile, $stackPtr, $openBracket, $tokens); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + }//end process() |
|
| 165 | + |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Determine if this is a multi-line function call. |
|
| 169 | + * |
|
| 170 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 171 | + * @param int $stackPtr The position of the current token |
|
| 172 | + * in the stack passed in $tokens. |
|
| 173 | + * @param int $openBracket The position of the opening bracket |
|
| 174 | + * in the stack passed in $tokens. |
|
| 175 | + * @param array $tokens The stack of tokens that make up |
|
| 176 | + * the file. |
|
| 177 | + * |
|
| 178 | + * @return void |
|
| 179 | + */ |
|
| 180 | + public function isMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 181 | + { |
|
| 182 | + $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 183 | + if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) { |
|
| 184 | + return true; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + return false; |
|
| 188 | + |
|
| 189 | + }//end isMultiLineCall() |
|
| 190 | + |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Processes single-line calls. |
|
| 194 | + * |
|
| 195 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 196 | + * @param int $stackPtr The position of the current token |
|
| 197 | + * in the stack passed in $tokens. |
|
| 198 | + * @param int $openBracket The position of the opening bracket |
|
| 199 | + * in the stack passed in $tokens. |
|
| 200 | + * @param array $tokens The stack of tokens that make up |
|
| 201 | + * the file. |
|
| 202 | + * |
|
| 203 | + * @return void |
|
| 204 | + */ |
|
| 205 | + public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 206 | + { |
|
| 207 | + $closer = $tokens[$openBracket]['parenthesis_closer']; |
|
| 208 | + if ($openBracket === ($closer - 1)) { |
|
| 209 | + return; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 213 | + // Checking this: $value = my_function([*]...). |
|
| 214 | + $error = 'Space after opening parenthesis of function call prohibited'; |
|
| 215 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpenBracket'); |
|
| 216 | + if ($fix === true) { |
|
| 217 | + $phpcsFile->fixer->replaceToken(($openBracket + 1), ''); |
|
| 218 | + } |
|
| 219 | + } else if ($this->requiredSpacesAfterOpen > 0) { |
|
| 220 | + $spaceAfterOpen = 0; |
|
| 221 | + if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 222 | + $spaceAfterOpen = strlen($tokens[($openBracket + 1)]['content']); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) { |
|
| 226 | + $error = 'Expected %s spaces after opening bracket; %s found'; |
|
| 227 | + $data = array( |
|
| 228 | + $this->requiredSpacesAfterOpen, |
|
| 229 | + $spaceAfterOpen, |
|
| 230 | + ); |
|
| 231 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpenBracket', $data); |
|
| 232 | + if ($fix === true) { |
|
| 233 | + $padding = str_repeat(' ', $this->requiredSpacesAfterOpen); |
|
| 234 | + if ($spaceAfterOpen === 0) { |
|
| 235 | + $phpcsFile->fixer->addContent($openBracket, $padding); |
|
| 236 | + } else { |
|
| 237 | + $phpcsFile->fixer->replaceToken(($openBracket + 1), $padding); |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + }//end if |
|
| 242 | + |
|
| 243 | + // Checking this: $value = my_function(...[*]). |
|
| 244 | + $spaceBeforeClose = 0; |
|
| 245 | + $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($closer - 1), $openBracket, true); |
|
| 246 | + if ($tokens[$prev]['code'] === T_END_HEREDOC || $tokens[$prev]['code'] === T_END_NOWDOC) { |
|
| 247 | + // Need a newline after these tokens, so ignore this rule. |
|
| 248 | + return; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + if ($tokens[$prev]['line'] !== $tokens[$closer]['line']) { |
|
| 252 | + $spaceBeforeClose = 'newline'; |
|
| 253 | + } else if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) { |
|
| 254 | + $spaceBeforeClose = strlen($tokens[($closer - 1)]['content']); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) { |
|
| 258 | + $error = 'Expected %s spaces before closing bracket; %s found'; |
|
| 259 | + $data = array( |
|
| 260 | + $this->requiredSpacesBeforeClose, |
|
| 261 | + $spaceBeforeClose, |
|
| 262 | + ); |
|
| 263 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeCloseBracket', $data); |
|
| 264 | + if ($fix === true) { |
|
| 265 | + $padding = str_repeat(' ', $this->requiredSpacesBeforeClose); |
|
| 266 | + if ($spaceBeforeClose === 0) { |
|
| 267 | + $phpcsFile->fixer->addContentBefore($closer, $padding); |
|
| 268 | + } else { |
|
| 269 | + $phpcsFile->fixer->replaceToken(($closer - 1), $padding); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + }//end processSingleLineCall() |
|
| 275 | + |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Processes multi-line calls. |
|
| 279 | + * |
|
| 280 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 281 | + * @param int $stackPtr The position of the current token |
|
| 282 | + * in the stack passed in $tokens. |
|
| 283 | + * @param int $openBracket The position of the opening bracket |
|
| 284 | + * in the stack passed in $tokens. |
|
| 285 | + * @param array $tokens The stack of tokens that make up |
|
| 286 | + * the file. |
|
| 287 | + * |
|
| 288 | + * @return void |
|
| 289 | + */ |
|
| 290 | + public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 291 | + { |
|
| 292 | + // We need to work out how far indented the function |
|
| 293 | + // call itself is, so we can work out how far to |
|
| 294 | + // indent the arguments. |
|
| 295 | + $start = $phpcsFile->findStartOfStatement($stackPtr); |
|
| 296 | + foreach (array('stackPtr', 'start') as $checkToken) { |
|
| 297 | + $x = $$checkToken; |
|
| 298 | + for ($i = ($x - 1); $i >= 0; $i--) { |
|
| 299 | + if ($tokens[$i]['line'] !== $tokens[$x]['line']) { |
|
| 300 | + $i++; |
|
| 301 | + break; |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + if ($i <= 0) { |
|
| 306 | + $functionIndent = 0; |
|
| 307 | + } else if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 308 | + $functionIndent = strlen($tokens[$i]['content']); |
|
| 309 | + } else if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING) { |
|
| 310 | + $functionIndent = 0; |
|
| 311 | + } else { |
|
| 312 | + $trimmed = ltrim($tokens[$i]['content']); |
|
| 313 | + if ($trimmed === '') { |
|
| 314 | + if ($tokens[$i]['code'] === T_INLINE_HTML) { |
|
| 315 | + $functionIndent = strlen($tokens[$i]['content']); |
|
| 316 | + } else { |
|
| 317 | + $functionIndent = ($tokens[$i]['column'] - 1); |
|
| 318 | + } |
|
| 319 | + } else { |
|
| 320 | + $functionIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $varName = $checkToken.'Indent'; |
|
| 325 | + $$varName = $functionIndent; |
|
| 326 | + }//end foreach |
|
| 327 | + |
|
| 328 | + $functionIndent = max($startIndent, $stackPtrIndent); |
|
| 329 | + |
|
| 330 | + $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($openBracket + 1), null, true); |
|
| 331 | + if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) { |
|
| 332 | + $error = 'Opening parenthesis of a multi-line function call must be the last content on the line'; |
|
| 333 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpenBracket'); |
|
| 334 | + if ($fix === true) { |
|
| 335 | + $phpcsFile->fixer->addContent( |
|
| 336 | + $openBracket, |
|
| 337 | + $phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent)) |
|
| 338 | + ); |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 343 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true); |
|
| 344 | + if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) { |
|
| 345 | + $error = 'Closing parenthesis of a multi-line function call must be on a line by itself'; |
|
| 346 | + $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine'); |
|
| 347 | + if ($fix === true) { |
|
| 348 | + $phpcsFile->fixer->addContentBefore( |
|
| 349 | + $closeBracket, |
|
| 350 | + $phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent)) |
|
| 351 | + ); |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + // Each line between the parenthesis should be indented n spaces. |
|
| 356 | + $lastLine = ($tokens[$openBracket]['line'] - 1); |
|
| 357 | + $argStart = null; |
|
| 358 | + $argEnd = null; |
|
| 359 | + $inArg = false; |
|
| 360 | + |
|
| 361 | + // Start processing at the first argument. |
|
| 362 | + $i = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true); |
|
| 363 | + if ($tokens[($i - 1)]['code'] === T_WHITESPACE |
|
| 364 | + && $tokens[($i - 1)]['line'] === $tokens[$i]['line'] |
|
| 365 | + ) { |
|
| 366 | + // Make sure we check the indent. |
|
| 367 | + $i--; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + for ($i; $i < $closeBracket; $i++) { |
|
| 371 | + if ($i > $argStart && $i < $argEnd) { |
|
| 372 | + $inArg = true; |
|
| 373 | + } else { |
|
| 374 | + $inArg = false; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + if ($tokens[$i]['line'] !== $lastLine) { |
|
| 378 | + $lastLine = $tokens[$i]['line']; |
|
| 379 | + |
|
| 380 | + // Ignore heredoc indentation. |
|
| 381 | + if (isset(PHP_CodeSniffer_Tokens::$heredocTokens[$tokens[$i]['code']]) === true) { |
|
| 382 | + continue; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + // Ignore multi-line string indentation. |
|
| 386 | + if (isset(PHP_CodeSniffer_Tokens::$stringTokens[$tokens[$i]['code']]) === true |
|
| 387 | + && $tokens[$i]['code'] === $tokens[($i - 1)]['code'] |
|
| 388 | + ) { |
|
| 389 | + continue; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + // Ignore inline HTML. |
|
| 393 | + if ($tokens[$i]['code'] === T_INLINE_HTML) { |
|
| 394 | + continue; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + if ($tokens[$i]['line'] !== $tokens[$openBracket]['line']) { |
|
| 398 | + // We changed lines, so this should be a whitespace indent token, but first make |
|
| 399 | + // sure it isn't a blank line because we don't need to check indent unless there |
|
| 400 | + // is actually some code to indent. |
|
| 401 | + if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 402 | + $nextCode = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), ($closeBracket + 1), true); |
|
| 403 | + if ($tokens[$nextCode]['line'] !== $lastLine) { |
|
| 404 | + if ($inArg === false) { |
|
| 405 | + $error = 'Empty lines are not allowed in multi-line function calls'; |
|
| 406 | + $fix = $phpcsFile->addFixableError($error, $i, 'EmptyLine'); |
|
| 407 | + if ($fix === true) { |
|
| 408 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
| 409 | + } |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + continue; |
|
| 413 | + } |
|
| 414 | + } else { |
|
| 415 | + $nextCode = $i; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + if ($tokens[$nextCode]['line'] === $tokens[$closeBracket]['line']) { |
|
| 419 | + // Closing brace needs to be indented to the same level |
|
| 420 | + // as the function call. |
|
| 421 | + $inArg = false; |
|
| 422 | + $expectedIndent = $functionIndent; |
|
| 423 | + } else { |
|
| 424 | + $expectedIndent = ($functionIndent + $this->indent); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + if ($tokens[$i]['code'] !== T_WHITESPACE |
|
| 428 | + && $tokens[$i]['code'] !== T_DOC_COMMENT_WHITESPACE |
|
| 429 | + ) { |
|
| 430 | + // Just check if it is a multi-line block comment. If so, we can |
|
| 431 | + // calculate the indent from the whitespace before the content. |
|
| 432 | + if ($tokens[$i]['code'] === T_COMMENT |
|
| 433 | + && $tokens[($i - 1)]['code'] === T_COMMENT |
|
| 434 | + ) { |
|
| 435 | + $trimmed = ltrim($tokens[$i]['content']); |
|
| 436 | + $foundIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 437 | + } else { |
|
| 438 | + $foundIndent = 0; |
|
| 439 | + } |
|
| 440 | + } else { |
|
| 441 | + $foundIndent = strlen($tokens[$i]['content']); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + if ($foundIndent < $expectedIndent |
|
| 445 | + || ($inArg === false |
|
| 446 | + && $expectedIndent !== $foundIndent) |
|
| 447 | + ) { |
|
| 448 | + $error = 'Multi-line function call not indented correctly; expected %s spaces but found %s'; |
|
| 449 | + $data = array( |
|
| 450 | + $expectedIndent, |
|
| 451 | + $foundIndent, |
|
| 452 | + ); |
|
| 453 | + |
|
| 454 | + $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data); |
|
| 455 | + if ($fix === true) { |
|
| 456 | + $padding = str_repeat(' ', $expectedIndent); |
|
| 457 | + if ($foundIndent === 0) { |
|
| 458 | + $phpcsFile->fixer->addContentBefore($i, $padding); |
|
| 459 | + } else { |
|
| 460 | + if ($tokens[$i]['code'] === T_COMMENT) { |
|
| 461 | + $comment = $padding.ltrim($tokens[$i]['content']); |
|
| 462 | + $phpcsFile->fixer->replaceToken($i, $comment); |
|
| 463 | + } else { |
|
| 464 | + $phpcsFile->fixer->replaceToken($i, $padding); |
|
| 465 | + } |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | + }//end if |
|
| 469 | + } else { |
|
| 470 | + $nextCode = $i; |
|
| 471 | + }//end if |
|
| 472 | + |
|
| 473 | + if ($inArg === false) { |
|
| 474 | + $argStart = $nextCode; |
|
| 475 | + $argEnd = $phpcsFile->findEndOfStatement($nextCode); |
|
| 476 | + } |
|
| 477 | + }//end if |
|
| 478 | + |
|
| 479 | + // If we are within an argument we should be ignoring commas |
|
| 480 | + // as these are not signaling the end of an argument. |
|
| 481 | + if ($inArg === false && $tokens[$i]['code'] === T_COMMA) { |
|
| 482 | + $next = $phpcsFile->findNext(array(T_WHITESPACE, T_COMMENT), ($i + 1), $closeBracket, true); |
|
| 483 | + if ($next === false) { |
|
| 484 | + continue; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + if ($this->allowMultipleArguments === false) { |
|
| 488 | + // Comma has to be the last token on the line. |
|
| 489 | + if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
| 490 | + $error = 'Only one argument is allowed per line in a multi-line function call'; |
|
| 491 | + $fix = $phpcsFile->addFixableError($error, $next, 'MultipleArguments'); |
|
| 492 | + if ($fix === true) { |
|
| 493 | + $phpcsFile->fixer->beginChangeset(); |
|
| 494 | + for ($x = ($next - 1); $x > $i; $x--) { |
|
| 495 | + if ($tokens[$x]['code'] !== T_WHITESPACE) { |
|
| 496 | + break; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + $phpcsFile->fixer->replaceToken($x, ''); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + $phpcsFile->fixer->addContentBefore( |
|
| 503 | + $next, |
|
| 504 | + $phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent)) |
|
| 505 | + ); |
|
| 506 | + $phpcsFile->fixer->endChangeset(); |
|
| 507 | + } |
|
| 508 | + } |
|
| 509 | + }//end if |
|
| 510 | + |
|
| 511 | + $argStart = $next; |
|
| 512 | + $argEnd = $phpcsFile->findEndOfStatement($next); |
|
| 513 | + }//end if |
|
| 514 | + }//end for |
|
| 515 | + |
|
| 516 | + }//end processMultiLineCall() |
|
| 517 | 517 | |
| 518 | 518 | |
| 519 | 519 | }//end class |
@@ -88,77 +88,77 @@ discard block |
||
| 88 | 88 | * |
| 89 | 89 | * @return void |
| 90 | 90 | */ |
| 91 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 91 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 92 | 92 | { |
| 93 | 93 | $this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen; |
| 94 | 94 | $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose; |
| 95 | 95 | $tokens = $phpcsFile->getTokens(); |
| 96 | 96 | |
| 97 | 97 | // Find the next non-empty token. |
| 98 | - $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 98 | + $openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); |
|
| 99 | 99 | |
| 100 | - if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { |
|
| 100 | + if ( $tokens[ $openBracket ][ 'code' ] !== T_OPEN_PARENTHESIS ) { |
|
| 101 | 101 | // Not a function call. |
| 102 | 102 | return; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - if (isset($tokens[$openBracket]['parenthesis_closer']) === false) { |
|
| 105 | + if ( isset( $tokens[ $openBracket ][ 'parenthesis_closer' ] ) === false ) { |
|
| 106 | 106 | // Not a function call. |
| 107 | 107 | return; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // Find the previous non-empty token. |
| 111 | 111 | $search = PHP_CodeSniffer_Tokens::$emptyTokens; |
| 112 | - $search[] = T_BITWISE_AND; |
|
| 113 | - $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true); |
|
| 114 | - if ($tokens[$previous]['code'] === T_FUNCTION) { |
|
| 112 | + $search[ ] = T_BITWISE_AND; |
|
| 113 | + $previous = $phpcsFile->findPrevious( $search, ( $stackPtr - 1 ), null, true ); |
|
| 114 | + if ( $tokens[ $previous ][ 'code' ] === T_FUNCTION ) { |
|
| 115 | 115 | // It's a function definition, not a function call. |
| 116 | 116 | return; |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 119 | + $closeBracket = $tokens[ $openBracket ][ 'parenthesis_closer' ]; |
|
| 120 | 120 | |
| 121 | - if (($stackPtr + 1) !== $openBracket) { |
|
| 121 | + if ( ( $stackPtr + 1 ) !== $openBracket ) { |
|
| 122 | 122 | // Checking this: $value = my_function[*](...). |
| 123 | 123 | $error = 'Space before opening parenthesis of function call prohibited'; |
| 124 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeOpenBracket'); |
|
| 125 | - if ($fix === true) { |
|
| 124 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'SpaceBeforeOpenBracket' ); |
|
| 125 | + if ( $fix === true ) { |
|
| 126 | 126 | $phpcsFile->fixer->beginChangeset(); |
| 127 | - for ($i = ($stackPtr + 1); $i < $openBracket; $i++) { |
|
| 128 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 127 | + for ( $i = ( $stackPtr + 1 ); $i < $openBracket; $i++ ) { |
|
| 128 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // Modify the bracket as well to ensure a conflict if the bracket |
| 132 | 132 | // has been changed in some way by another sniff. |
| 133 | - $phpcsFile->fixer->replaceToken($openBracket, '('); |
|
| 133 | + $phpcsFile->fixer->replaceToken( $openBracket, '(' ); |
|
| 134 | 134 | $phpcsFile->fixer->endChangeset(); |
| 135 | 135 | } |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true); |
|
| 139 | - if ($tokens[$next]['code'] === T_SEMICOLON) { |
|
| 140 | - if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[($closeBracket + 1)]['code']]) === true) { |
|
| 138 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $closeBracket + 1 ), null, true ); |
|
| 139 | + if ( $tokens[ $next ][ 'code' ] === T_SEMICOLON ) { |
|
| 140 | + if ( isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $tokens[ ( $closeBracket + 1 ) ][ 'code' ] ] ) === true ) { |
|
| 141 | 141 | $error = 'Space after closing parenthesis of function call prohibited'; |
| 142 | - $fix = $phpcsFile->addFixableError($error, $closeBracket, 'SpaceAfterCloseBracket'); |
|
| 143 | - if ($fix === true) { |
|
| 142 | + $fix = $phpcsFile->addFixableError( $error, $closeBracket, 'SpaceAfterCloseBracket' ); |
|
| 143 | + if ( $fix === true ) { |
|
| 144 | 144 | $phpcsFile->fixer->beginChangeset(); |
| 145 | - for ($i = ($closeBracket + 1); $i < $next; $i++) { |
|
| 146 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 145 | + for ( $i = ( $closeBracket + 1 ); $i < $next; $i++ ) { |
|
| 146 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | // Modify the bracket as well to ensure a conflict if the bracket |
| 150 | 150 | // has been changed in some way by another sniff. |
| 151 | - $phpcsFile->fixer->replaceToken($closeBracket, ')'); |
|
| 151 | + $phpcsFile->fixer->replaceToken( $closeBracket, ')' ); |
|
| 152 | 152 | $phpcsFile->fixer->endChangeset(); |
| 153 | 153 | } |
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | // Check if this is a single line or multi-line function call. |
| 158 | - if ($this->isMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens) === true) { |
|
| 159 | - $this->processMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens); |
|
| 158 | + if ( $this->isMultiLineCall( $phpcsFile, $stackPtr, $openBracket, $tokens ) === true ) { |
|
| 159 | + $this->processMultiLineCall( $phpcsFile, $stackPtr, $openBracket, $tokens ); |
|
| 160 | 160 | } else { |
| 161 | - $this->processSingleLineCall($phpcsFile, $stackPtr, $openBracket, $tokens); |
|
| 161 | + $this->processSingleLineCall( $phpcsFile, $stackPtr, $openBracket, $tokens ); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | }//end process() |
@@ -177,10 +177,10 @@ discard block |
||
| 177 | 177 | * |
| 178 | 178 | * @return void |
| 179 | 179 | */ |
| 180 | - public function isMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 180 | + public function isMultiLineCall( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens ) |
|
| 181 | 181 | { |
| 182 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 183 | - if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) { |
|
| 182 | + $closeBracket = $tokens[ $openBracket ][ 'parenthesis_closer' ]; |
|
| 183 | + if ( $tokens[ $openBracket ][ 'line' ] !== $tokens[ $closeBracket ][ 'line' ] ) { |
|
| 184 | 184 | return true; |
| 185 | 185 | } |
| 186 | 186 | |
@@ -202,39 +202,39 @@ discard block |
||
| 202 | 202 | * |
| 203 | 203 | * @return void |
| 204 | 204 | */ |
| 205 | - public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 205 | + public function processSingleLineCall( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens ) |
|
| 206 | 206 | { |
| 207 | - $closer = $tokens[$openBracket]['parenthesis_closer']; |
|
| 208 | - if ($openBracket === ($closer - 1)) { |
|
| 207 | + $closer = $tokens[ $openBracket ][ 'parenthesis_closer' ]; |
|
| 208 | + if ( $openBracket === ( $closer - 1 ) ) { |
|
| 209 | 209 | return; |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | - if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 212 | + if ( $this->requiredSpacesAfterOpen === 0 && $tokens[ ( $openBracket + 1 ) ][ 'code' ] === T_WHITESPACE ) { |
|
| 213 | 213 | // Checking this: $value = my_function([*]...). |
| 214 | 214 | $error = 'Space after opening parenthesis of function call prohibited'; |
| 215 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpenBracket'); |
|
| 216 | - if ($fix === true) { |
|
| 217 | - $phpcsFile->fixer->replaceToken(($openBracket + 1), ''); |
|
| 215 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'SpaceAfterOpenBracket' ); |
|
| 216 | + if ( $fix === true ) { |
|
| 217 | + $phpcsFile->fixer->replaceToken( ( $openBracket + 1 ), '' ); |
|
| 218 | 218 | } |
| 219 | - } else if ($this->requiredSpacesAfterOpen > 0) { |
|
| 219 | + } else if ( $this->requiredSpacesAfterOpen > 0 ) { |
|
| 220 | 220 | $spaceAfterOpen = 0; |
| 221 | - if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 222 | - $spaceAfterOpen = strlen($tokens[($openBracket + 1)]['content']); |
|
| 221 | + if ( $tokens[ ( $openBracket + 1 ) ][ 'code' ] === T_WHITESPACE ) { |
|
| 222 | + $spaceAfterOpen = strlen( $tokens[ ( $openBracket + 1 ) ][ 'content' ] ); |
|
| 223 | 223 | } |
| 224 | 224 | |
| 225 | - if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) { |
|
| 225 | + if ( $spaceAfterOpen !== $this->requiredSpacesAfterOpen ) { |
|
| 226 | 226 | $error = 'Expected %s spaces after opening bracket; %s found'; |
| 227 | 227 | $data = array( |
| 228 | 228 | $this->requiredSpacesAfterOpen, |
| 229 | 229 | $spaceAfterOpen, |
| 230 | 230 | ); |
| 231 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpenBracket', $data); |
|
| 232 | - if ($fix === true) { |
|
| 233 | - $padding = str_repeat(' ', $this->requiredSpacesAfterOpen); |
|
| 234 | - if ($spaceAfterOpen === 0) { |
|
| 235 | - $phpcsFile->fixer->addContent($openBracket, $padding); |
|
| 231 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'SpaceAfterOpenBracket', $data ); |
|
| 232 | + if ( $fix === true ) { |
|
| 233 | + $padding = str_repeat( ' ', $this->requiredSpacesAfterOpen ); |
|
| 234 | + if ( $spaceAfterOpen === 0 ) { |
|
| 235 | + $phpcsFile->fixer->addContent( $openBracket, $padding ); |
|
| 236 | 236 | } else { |
| 237 | - $phpcsFile->fixer->replaceToken(($openBracket + 1), $padding); |
|
| 237 | + $phpcsFile->fixer->replaceToken( ( $openBracket + 1 ), $padding ); |
|
| 238 | 238 | } |
| 239 | 239 | } |
| 240 | 240 | } |
@@ -242,31 +242,31 @@ discard block |
||
| 242 | 242 | |
| 243 | 243 | // Checking this: $value = my_function(...[*]). |
| 244 | 244 | $spaceBeforeClose = 0; |
| 245 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($closer - 1), $openBracket, true); |
|
| 246 | - if ($tokens[$prev]['code'] === T_END_HEREDOC || $tokens[$prev]['code'] === T_END_NOWDOC) { |
|
| 245 | + $prev = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $closer - 1 ), $openBracket, true ); |
|
| 246 | + if ( $tokens[ $prev ][ 'code' ] === T_END_HEREDOC || $tokens[ $prev ][ 'code' ] === T_END_NOWDOC ) { |
|
| 247 | 247 | // Need a newline after these tokens, so ignore this rule. |
| 248 | 248 | return; |
| 249 | 249 | } |
| 250 | 250 | |
| 251 | - if ($tokens[$prev]['line'] !== $tokens[$closer]['line']) { |
|
| 251 | + if ( $tokens[ $prev ][ 'line' ] !== $tokens[ $closer ][ 'line' ] ) { |
|
| 252 | 252 | $spaceBeforeClose = 'newline'; |
| 253 | - } else if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) { |
|
| 254 | - $spaceBeforeClose = strlen($tokens[($closer - 1)]['content']); |
|
| 253 | + } else if ( $tokens[ ( $closer - 1 ) ][ 'code' ] === T_WHITESPACE ) { |
|
| 254 | + $spaceBeforeClose = strlen( $tokens[ ( $closer - 1 ) ][ 'content' ] ); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) { |
|
| 257 | + if ( $spaceBeforeClose !== $this->requiredSpacesBeforeClose ) { |
|
| 258 | 258 | $error = 'Expected %s spaces before closing bracket; %s found'; |
| 259 | 259 | $data = array( |
| 260 | 260 | $this->requiredSpacesBeforeClose, |
| 261 | 261 | $spaceBeforeClose, |
| 262 | 262 | ); |
| 263 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeCloseBracket', $data); |
|
| 264 | - if ($fix === true) { |
|
| 265 | - $padding = str_repeat(' ', $this->requiredSpacesBeforeClose); |
|
| 266 | - if ($spaceBeforeClose === 0) { |
|
| 267 | - $phpcsFile->fixer->addContentBefore($closer, $padding); |
|
| 263 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'SpaceBeforeCloseBracket', $data ); |
|
| 264 | + if ( $fix === true ) { |
|
| 265 | + $padding = str_repeat( ' ', $this->requiredSpacesBeforeClose ); |
|
| 266 | + if ( $spaceBeforeClose === 0 ) { |
|
| 267 | + $phpcsFile->fixer->addContentBefore( $closer, $padding ); |
|
| 268 | 268 | } else { |
| 269 | - $phpcsFile->fixer->replaceToken(($closer - 1), $padding); |
|
| 269 | + $phpcsFile->fixer->replaceToken( ( $closer - 1 ), $padding ); |
|
| 270 | 270 | } |
| 271 | 271 | } |
| 272 | 272 | } |
@@ -287,125 +287,125 @@ discard block |
||
| 287 | 287 | * |
| 288 | 288 | * @return void |
| 289 | 289 | */ |
| 290 | - public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 290 | + public function processMultiLineCall( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens ) |
|
| 291 | 291 | { |
| 292 | 292 | // We need to work out how far indented the function |
| 293 | 293 | // call itself is, so we can work out how far to |
| 294 | 294 | // indent the arguments. |
| 295 | - $start = $phpcsFile->findStartOfStatement($stackPtr); |
|
| 296 | - foreach (array('stackPtr', 'start') as $checkToken) { |
|
| 295 | + $start = $phpcsFile->findStartOfStatement( $stackPtr ); |
|
| 296 | + foreach ( array( 'stackPtr', 'start' ) as $checkToken ) { |
|
| 297 | 297 | $x = $$checkToken; |
| 298 | - for ($i = ($x - 1); $i >= 0; $i--) { |
|
| 299 | - if ($tokens[$i]['line'] !== $tokens[$x]['line']) { |
|
| 298 | + for ( $i = ( $x - 1 ); $i >= 0; $i-- ) { |
|
| 299 | + if ( $tokens[ $i ][ 'line' ] !== $tokens[ $x ][ 'line' ] ) { |
|
| 300 | 300 | $i++; |
| 301 | 301 | break; |
| 302 | 302 | } |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - if ($i <= 0) { |
|
| 305 | + if ( $i <= 0 ) { |
|
| 306 | 306 | $functionIndent = 0; |
| 307 | - } else if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 308 | - $functionIndent = strlen($tokens[$i]['content']); |
|
| 309 | - } else if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING) { |
|
| 307 | + } else if ( $tokens[ $i ][ 'code' ] === T_WHITESPACE ) { |
|
| 308 | + $functionIndent = strlen( $tokens[ $i ][ 'content' ] ); |
|
| 309 | + } else if ( $tokens[ $i ][ 'code' ] === T_CONSTANT_ENCAPSED_STRING ) { |
|
| 310 | 310 | $functionIndent = 0; |
| 311 | 311 | } else { |
| 312 | - $trimmed = ltrim($tokens[$i]['content']); |
|
| 313 | - if ($trimmed === '') { |
|
| 314 | - if ($tokens[$i]['code'] === T_INLINE_HTML) { |
|
| 315 | - $functionIndent = strlen($tokens[$i]['content']); |
|
| 312 | + $trimmed = ltrim( $tokens[ $i ][ 'content' ] ); |
|
| 313 | + if ( $trimmed === '' ) { |
|
| 314 | + if ( $tokens[ $i ][ 'code' ] === T_INLINE_HTML ) { |
|
| 315 | + $functionIndent = strlen( $tokens[ $i ][ 'content' ] ); |
|
| 316 | 316 | } else { |
| 317 | - $functionIndent = ($tokens[$i]['column'] - 1); |
|
| 317 | + $functionIndent = ( $tokens[ $i ][ 'column' ] - 1 ); |
|
| 318 | 318 | } |
| 319 | 319 | } else { |
| 320 | - $functionIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 320 | + $functionIndent = ( strlen( $tokens[ $i ][ 'content' ] ) - strlen( $trimmed ) ); |
|
| 321 | 321 | } |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | - $varName = $checkToken.'Indent'; |
|
| 324 | + $varName = $checkToken . 'Indent'; |
|
| 325 | 325 | $$varName = $functionIndent; |
| 326 | 326 | }//end foreach |
| 327 | 327 | |
| 328 | - $functionIndent = max($startIndent, $stackPtrIndent); |
|
| 328 | + $functionIndent = max( $startIndent, $stackPtrIndent ); |
|
| 329 | 329 | |
| 330 | - $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($openBracket + 1), null, true); |
|
| 331 | - if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) { |
|
| 330 | + $next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $openBracket + 1 ), null, true ); |
|
| 331 | + if ( $tokens[ $next ][ 'line' ] === $tokens[ $openBracket ][ 'line' ] ) { |
|
| 332 | 332 | $error = 'Opening parenthesis of a multi-line function call must be the last content on the line'; |
| 333 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpenBracket'); |
|
| 334 | - if ($fix === true) { |
|
| 333 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ContentAfterOpenBracket' ); |
|
| 334 | + if ( $fix === true ) { |
|
| 335 | 335 | $phpcsFile->fixer->addContent( |
| 336 | 336 | $openBracket, |
| 337 | - $phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent)) |
|
| 337 | + $phpcsFile->eolChar . str_repeat( ' ', ( $functionIndent + $this->indent ) ) |
|
| 338 | 338 | ); |
| 339 | 339 | } |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 343 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true); |
|
| 344 | - if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) { |
|
| 342 | + $closeBracket = $tokens[ $openBracket ][ 'parenthesis_closer' ]; |
|
| 343 | + $prev = $phpcsFile->findPrevious( T_WHITESPACE, ( $closeBracket - 1 ), null, true ); |
|
| 344 | + if ( $tokens[ $prev ][ 'line' ] === $tokens[ $closeBracket ][ 'line' ] ) { |
|
| 345 | 345 | $error = 'Closing parenthesis of a multi-line function call must be on a line by itself'; |
| 346 | - $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine'); |
|
| 347 | - if ($fix === true) { |
|
| 346 | + $fix = $phpcsFile->addFixableError( $error, $closeBracket, 'CloseBracketLine' ); |
|
| 347 | + if ( $fix === true ) { |
|
| 348 | 348 | $phpcsFile->fixer->addContentBefore( |
| 349 | 349 | $closeBracket, |
| 350 | - $phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent)) |
|
| 350 | + $phpcsFile->eolChar . str_repeat( ' ', ( $functionIndent + $this->indent ) ) |
|
| 351 | 351 | ); |
| 352 | 352 | } |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | // Each line between the parenthesis should be indented n spaces. |
| 356 | - $lastLine = ($tokens[$openBracket]['line'] - 1); |
|
| 356 | + $lastLine = ( $tokens[ $openBracket ][ 'line' ] - 1 ); |
|
| 357 | 357 | $argStart = null; |
| 358 | 358 | $argEnd = null; |
| 359 | 359 | $inArg = false; |
| 360 | 360 | |
| 361 | 361 | // Start processing at the first argument. |
| 362 | - $i = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true); |
|
| 363 | - if ($tokens[($i - 1)]['code'] === T_WHITESPACE |
|
| 364 | - && $tokens[($i - 1)]['line'] === $tokens[$i]['line'] |
|
| 362 | + $i = $phpcsFile->findNext( T_WHITESPACE, ( $openBracket + 1 ), null, true ); |
|
| 363 | + if ( $tokens[ ( $i - 1 ) ][ 'code' ] === T_WHITESPACE |
|
| 364 | + && $tokens[ ( $i - 1 ) ][ 'line' ] === $tokens[ $i ][ 'line' ] |
|
| 365 | 365 | ) { |
| 366 | 366 | // Make sure we check the indent. |
| 367 | 367 | $i--; |
| 368 | 368 | } |
| 369 | 369 | |
| 370 | - for ($i; $i < $closeBracket; $i++) { |
|
| 371 | - if ($i > $argStart && $i < $argEnd) { |
|
| 370 | + for ( $i; $i < $closeBracket; $i++ ) { |
|
| 371 | + if ( $i > $argStart && $i < $argEnd ) { |
|
| 372 | 372 | $inArg = true; |
| 373 | 373 | } else { |
| 374 | 374 | $inArg = false; |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | - if ($tokens[$i]['line'] !== $lastLine) { |
|
| 378 | - $lastLine = $tokens[$i]['line']; |
|
| 377 | + if ( $tokens[ $i ][ 'line' ] !== $lastLine ) { |
|
| 378 | + $lastLine = $tokens[ $i ][ 'line' ]; |
|
| 379 | 379 | |
| 380 | 380 | // Ignore heredoc indentation. |
| 381 | - if (isset(PHP_CodeSniffer_Tokens::$heredocTokens[$tokens[$i]['code']]) === true) { |
|
| 381 | + if ( isset( PHP_CodeSniffer_Tokens::$heredocTokens[ $tokens[ $i ][ 'code' ] ] ) === true ) { |
|
| 382 | 382 | continue; |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | // Ignore multi-line string indentation. |
| 386 | - if (isset(PHP_CodeSniffer_Tokens::$stringTokens[$tokens[$i]['code']]) === true |
|
| 387 | - && $tokens[$i]['code'] === $tokens[($i - 1)]['code'] |
|
| 386 | + if ( isset( PHP_CodeSniffer_Tokens::$stringTokens[ $tokens[ $i ][ 'code' ] ] ) === true |
|
| 387 | + && $tokens[ $i ][ 'code' ] === $tokens[ ( $i - 1 ) ][ 'code' ] |
|
| 388 | 388 | ) { |
| 389 | 389 | continue; |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | // Ignore inline HTML. |
| 393 | - if ($tokens[$i]['code'] === T_INLINE_HTML) { |
|
| 393 | + if ( $tokens[ $i ][ 'code' ] === T_INLINE_HTML ) { |
|
| 394 | 394 | continue; |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | - if ($tokens[$i]['line'] !== $tokens[$openBracket]['line']) { |
|
| 397 | + if ( $tokens[ $i ][ 'line' ] !== $tokens[ $openBracket ][ 'line' ] ) { |
|
| 398 | 398 | // We changed lines, so this should be a whitespace indent token, but first make |
| 399 | 399 | // sure it isn't a blank line because we don't need to check indent unless there |
| 400 | 400 | // is actually some code to indent. |
| 401 | - if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 402 | - $nextCode = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), ($closeBracket + 1), true); |
|
| 403 | - if ($tokens[$nextCode]['line'] !== $lastLine) { |
|
| 404 | - if ($inArg === false) { |
|
| 401 | + if ( $tokens[ $i ][ 'code' ] === T_WHITESPACE ) { |
|
| 402 | + $nextCode = $phpcsFile->findNext( T_WHITESPACE, ( $i + 1 ), ( $closeBracket + 1 ), true ); |
|
| 403 | + if ( $tokens[ $nextCode ][ 'line' ] !== $lastLine ) { |
|
| 404 | + if ( $inArg === false ) { |
|
| 405 | 405 | $error = 'Empty lines are not allowed in multi-line function calls'; |
| 406 | - $fix = $phpcsFile->addFixableError($error, $i, 'EmptyLine'); |
|
| 407 | - if ($fix === true) { |
|
| 408 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 406 | + $fix = $phpcsFile->addFixableError( $error, $i, 'EmptyLine' ); |
|
| 407 | + if ( $fix === true ) { |
|
| 408 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
| 409 | 409 | } |
| 410 | 410 | } |
| 411 | 411 | |
@@ -415,35 +415,35 @@ discard block |
||
| 415 | 415 | $nextCode = $i; |
| 416 | 416 | } |
| 417 | 417 | |
| 418 | - if ($tokens[$nextCode]['line'] === $tokens[$closeBracket]['line']) { |
|
| 418 | + if ( $tokens[ $nextCode ][ 'line' ] === $tokens[ $closeBracket ][ 'line' ] ) { |
|
| 419 | 419 | // Closing brace needs to be indented to the same level |
| 420 | 420 | // as the function call. |
| 421 | 421 | $inArg = false; |
| 422 | 422 | $expectedIndent = $functionIndent; |
| 423 | 423 | } else { |
| 424 | - $expectedIndent = ($functionIndent + $this->indent); |
|
| 424 | + $expectedIndent = ( $functionIndent + $this->indent ); |
|
| 425 | 425 | } |
| 426 | 426 | |
| 427 | - if ($tokens[$i]['code'] !== T_WHITESPACE |
|
| 428 | - && $tokens[$i]['code'] !== T_DOC_COMMENT_WHITESPACE |
|
| 427 | + if ( $tokens[ $i ][ 'code' ] !== T_WHITESPACE |
|
| 428 | + && $tokens[ $i ][ 'code' ] !== T_DOC_COMMENT_WHITESPACE |
|
| 429 | 429 | ) { |
| 430 | 430 | // Just check if it is a multi-line block comment. If so, we can |
| 431 | 431 | // calculate the indent from the whitespace before the content. |
| 432 | - if ($tokens[$i]['code'] === T_COMMENT |
|
| 433 | - && $tokens[($i - 1)]['code'] === T_COMMENT |
|
| 432 | + if ( $tokens[ $i ][ 'code' ] === T_COMMENT |
|
| 433 | + && $tokens[ ( $i - 1 ) ][ 'code' ] === T_COMMENT |
|
| 434 | 434 | ) { |
| 435 | - $trimmed = ltrim($tokens[$i]['content']); |
|
| 436 | - $foundIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 435 | + $trimmed = ltrim( $tokens[ $i ][ 'content' ] ); |
|
| 436 | + $foundIndent = ( strlen( $tokens[ $i ][ 'content' ] ) - strlen( $trimmed ) ); |
|
| 437 | 437 | } else { |
| 438 | 438 | $foundIndent = 0; |
| 439 | 439 | } |
| 440 | 440 | } else { |
| 441 | - $foundIndent = strlen($tokens[$i]['content']); |
|
| 441 | + $foundIndent = strlen( $tokens[ $i ][ 'content' ] ); |
|
| 442 | 442 | } |
| 443 | 443 | |
| 444 | - if ($foundIndent < $expectedIndent |
|
| 445 | - || ($inArg === false |
|
| 446 | - && $expectedIndent !== $foundIndent) |
|
| 444 | + if ( $foundIndent < $expectedIndent |
|
| 445 | + || ( $inArg === false |
|
| 446 | + && $expectedIndent !== $foundIndent ) |
|
| 447 | 447 | ) { |
| 448 | 448 | $error = 'Multi-line function call not indented correctly; expected %s spaces but found %s'; |
| 449 | 449 | $data = array( |
@@ -451,17 +451,17 @@ discard block |
||
| 451 | 451 | $foundIndent, |
| 452 | 452 | ); |
| 453 | 453 | |
| 454 | - $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data); |
|
| 455 | - if ($fix === true) { |
|
| 456 | - $padding = str_repeat(' ', $expectedIndent); |
|
| 457 | - if ($foundIndent === 0) { |
|
| 458 | - $phpcsFile->fixer->addContentBefore($i, $padding); |
|
| 454 | + $fix = $phpcsFile->addFixableError( $error, $i, 'Indent', $data ); |
|
| 455 | + if ( $fix === true ) { |
|
| 456 | + $padding = str_repeat( ' ', $expectedIndent ); |
|
| 457 | + if ( $foundIndent === 0 ) { |
|
| 458 | + $phpcsFile->fixer->addContentBefore( $i, $padding ); |
|
| 459 | 459 | } else { |
| 460 | - if ($tokens[$i]['code'] === T_COMMENT) { |
|
| 461 | - $comment = $padding.ltrim($tokens[$i]['content']); |
|
| 462 | - $phpcsFile->fixer->replaceToken($i, $comment); |
|
| 460 | + if ( $tokens[ $i ][ 'code' ] === T_COMMENT ) { |
|
| 461 | + $comment = $padding . ltrim( $tokens[ $i ][ 'content' ] ); |
|
| 462 | + $phpcsFile->fixer->replaceToken( $i, $comment ); |
|
| 463 | 463 | } else { |
| 464 | - $phpcsFile->fixer->replaceToken($i, $padding); |
|
| 464 | + $phpcsFile->fixer->replaceToken( $i, $padding ); |
|
| 465 | 465 | } |
| 466 | 466 | } |
| 467 | 467 | } |
@@ -470,38 +470,38 @@ discard block |
||
| 470 | 470 | $nextCode = $i; |
| 471 | 471 | }//end if |
| 472 | 472 | |
| 473 | - if ($inArg === false) { |
|
| 473 | + if ( $inArg === false ) { |
|
| 474 | 474 | $argStart = $nextCode; |
| 475 | - $argEnd = $phpcsFile->findEndOfStatement($nextCode); |
|
| 475 | + $argEnd = $phpcsFile->findEndOfStatement( $nextCode ); |
|
| 476 | 476 | } |
| 477 | 477 | }//end if |
| 478 | 478 | |
| 479 | 479 | // If we are within an argument we should be ignoring commas |
| 480 | 480 | // as these are not signaling the end of an argument. |
| 481 | - if ($inArg === false && $tokens[$i]['code'] === T_COMMA) { |
|
| 482 | - $next = $phpcsFile->findNext(array(T_WHITESPACE, T_COMMENT), ($i + 1), $closeBracket, true); |
|
| 483 | - if ($next === false) { |
|
| 481 | + if ( $inArg === false && $tokens[ $i ][ 'code' ] === T_COMMA ) { |
|
| 482 | + $next = $phpcsFile->findNext( array( T_WHITESPACE, T_COMMENT ), ( $i + 1 ), $closeBracket, true ); |
|
| 483 | + if ( $next === false ) { |
|
| 484 | 484 | continue; |
| 485 | 485 | } |
| 486 | 486 | |
| 487 | - if ($this->allowMultipleArguments === false) { |
|
| 487 | + if ( $this->allowMultipleArguments === false ) { |
|
| 488 | 488 | // Comma has to be the last token on the line. |
| 489 | - if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
| 489 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $next ][ 'line' ] ) { |
|
| 490 | 490 | $error = 'Only one argument is allowed per line in a multi-line function call'; |
| 491 | - $fix = $phpcsFile->addFixableError($error, $next, 'MultipleArguments'); |
|
| 492 | - if ($fix === true) { |
|
| 491 | + $fix = $phpcsFile->addFixableError( $error, $next, 'MultipleArguments' ); |
|
| 492 | + if ( $fix === true ) { |
|
| 493 | 493 | $phpcsFile->fixer->beginChangeset(); |
| 494 | - for ($x = ($next - 1); $x > $i; $x--) { |
|
| 495 | - if ($tokens[$x]['code'] !== T_WHITESPACE) { |
|
| 494 | + for ( $x = ( $next - 1 ); $x > $i; $x-- ) { |
|
| 495 | + if ( $tokens[ $x ][ 'code' ] !== T_WHITESPACE ) { |
|
| 496 | 496 | break; |
| 497 | 497 | } |
| 498 | 498 | |
| 499 | - $phpcsFile->fixer->replaceToken($x, ''); |
|
| 499 | + $phpcsFile->fixer->replaceToken( $x, '' ); |
|
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | $phpcsFile->fixer->addContentBefore( |
| 503 | 503 | $next, |
| 504 | - $phpcsFile->eolChar.str_repeat(' ', ($functionIndent + $this->indent)) |
|
| 504 | + $phpcsFile->eolChar . str_repeat( ' ', ( $functionIndent + $this->indent ) ) |
|
| 505 | 505 | ); |
| 506 | 506 | $phpcsFile->fixer->endChangeset(); |
| 507 | 507 | } |
@@ -509,7 +509,7 @@ discard block |
||
| 509 | 509 | }//end if |
| 510 | 510 | |
| 511 | 511 | $argStart = $next; |
| 512 | - $argEnd = $phpcsFile->findEndOfStatement($next); |
|
| 512 | + $argEnd = $phpcsFile->findEndOfStatement( $next ); |
|
| 513 | 513 | }//end if |
| 514 | 514 | }//end for |
| 515 | 515 | |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * @version Release: @package_version@ |
| 26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 27 | 27 | */ |
| 28 | -class PEAR_Sniffs_Functions_FunctionCallSignatureSniff implements PHP_CodeSniffer_Sniff |
|
| 29 | -{ |
|
| 28 | +class PEAR_Sniffs_Functions_FunctionCallSignatureSniff implements PHP_CodeSniffer_Sniff { |
|
| 30 | 29 | |
| 31 | 30 | /** |
| 32 | 31 | * A list of tokenizers this sniff supports. |
@@ -72,8 +71,7 @@ discard block |
||
| 72 | 71 | * |
| 73 | 72 | * @return array |
| 74 | 73 | */ |
| 75 | - public function register() |
|
| 76 | - { |
|
| 74 | + public function register() { |
|
| 77 | 75 | return PHP_CodeSniffer_Tokens::$functionNameTokens; |
| 78 | 76 | |
| 79 | 77 | }//end register() |
@@ -88,8 +86,7 @@ discard block |
||
| 88 | 86 | * |
| 89 | 87 | * @return void |
| 90 | 88 | */ |
| 91 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 92 | - { |
|
| 89 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 93 | 90 | $this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen; |
| 94 | 91 | $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose; |
| 95 | 92 | $tokens = $phpcsFile->getTokens(); |
@@ -177,8 +174,7 @@ discard block |
||
| 177 | 174 | * |
| 178 | 175 | * @return void |
| 179 | 176 | */ |
| 180 | - public function isMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 181 | - { |
|
| 177 | + public function isMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) { |
|
| 182 | 178 | $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
| 183 | 179 | if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) { |
| 184 | 180 | return true; |
@@ -202,8 +198,7 @@ discard block |
||
| 202 | 198 | * |
| 203 | 199 | * @return void |
| 204 | 200 | */ |
| 205 | - public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 206 | - { |
|
| 201 | + public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) { |
|
| 207 | 202 | $closer = $tokens[$openBracket]['parenthesis_closer']; |
| 208 | 203 | if ($openBracket === ($closer - 1)) { |
| 209 | 204 | return; |
@@ -287,8 +282,7 @@ discard block |
||
| 287 | 282 | * |
| 288 | 283 | * @return void |
| 289 | 284 | */ |
| 290 | - public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 291 | - { |
|
| 285 | + public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) { |
|
| 292 | 286 | // We need to work out how far indented the function |
| 293 | 287 | // call itself is, so we can work out how far to |
| 294 | 288 | // indent the arguments. |
@@ -187,7 +187,7 @@ |
||
| 187 | 187 | * @param array $tokens The stack of tokens that make up |
| 188 | 188 | * the file. |
| 189 | 189 | * |
| 190 | - * @return void |
|
| 190 | + * @return boolean |
|
| 191 | 191 | */ |
| 192 | 192 | public function isMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
| 193 | 193 | { |
@@ -28,402 +28,402 @@ |
||
| 28 | 28 | class PEAR_Sniffs_Functions_FunctionDeclarationSniff implements PHP_CodeSniffer_Sniff |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The number of spaces code should be indented. |
|
| 33 | - * |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - public $indent = 4; |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Returns an array of tokens this test wants to listen for. |
|
| 41 | - * |
|
| 42 | - * @return array |
|
| 43 | - */ |
|
| 44 | - public function register() |
|
| 45 | - { |
|
| 46 | - return array( |
|
| 47 | - T_FUNCTION, |
|
| 48 | - T_CLOSURE, |
|
| 49 | - ); |
|
| 50 | - |
|
| 51 | - }//end register() |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Processes this test, when one of its tokens is encountered. |
|
| 56 | - * |
|
| 57 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 58 | - * @param int $stackPtr The position of the current token |
|
| 59 | - * in the stack passed in $tokens. |
|
| 60 | - * |
|
| 61 | - * @return void |
|
| 62 | - */ |
|
| 63 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 64 | - { |
|
| 65 | - $tokens = $phpcsFile->getTokens(); |
|
| 66 | - |
|
| 67 | - if (isset($tokens[$stackPtr]['parenthesis_opener']) === false |
|
| 68 | - || isset($tokens[$stackPtr]['parenthesis_closer']) === false |
|
| 69 | - || $tokens[$stackPtr]['parenthesis_opener'] === null |
|
| 70 | - || $tokens[$stackPtr]['parenthesis_closer'] === null |
|
| 71 | - ) { |
|
| 72 | - return; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - $openBracket = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 76 | - $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 77 | - |
|
| 78 | - // Must be one space after the FUNCTION keyword. |
|
| 79 | - if ($tokens[($stackPtr + 1)]['content'] === $phpcsFile->eolChar) { |
|
| 80 | - $spaces = 'newline'; |
|
| 81 | - } else if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { |
|
| 82 | - $spaces = strlen($tokens[($stackPtr + 1)]['content']); |
|
| 83 | - } else { |
|
| 84 | - $spaces = 0; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - if ($spaces !== 1) { |
|
| 88 | - $error = 'Expected 1 space after FUNCTION keyword; %s found'; |
|
| 89 | - $data = array($spaces); |
|
| 90 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterFunction', $data); |
|
| 91 | - if ($fix === true) { |
|
| 92 | - if ($spaces === 0) { |
|
| 93 | - $phpcsFile->fixer->addContent($stackPtr, ' '); |
|
| 94 | - } else { |
|
| 95 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // Must be one space before the opening parenthesis. For closures, this is |
|
| 101 | - // enforced by the first check because there is no content between the keywords |
|
| 102 | - // and the opening parenthesis. |
|
| 103 | - if ($tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
| 104 | - if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) { |
|
| 105 | - $spaces = 'newline'; |
|
| 106 | - } else if ($tokens[($openBracket - 1)]['code'] === T_WHITESPACE) { |
|
| 107 | - $spaces = strlen($tokens[($openBracket - 1)]['content']); |
|
| 108 | - } else { |
|
| 109 | - $spaces = 0; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if ($spaces !== 0) { |
|
| 113 | - $error = 'Expected 0 spaces before opening parenthesis; %s found'; |
|
| 114 | - $data = array($spaces); |
|
| 115 | - $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParen', $data); |
|
| 116 | - if ($fix === true) { |
|
| 117 | - $phpcsFile->fixer->replaceToken(($openBracket - 1), ''); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - }//end if |
|
| 121 | - |
|
| 122 | - // Must be one space before and after USE keyword for closures. |
|
| 123 | - if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 124 | - $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); |
|
| 125 | - if ($use !== false) { |
|
| 126 | - if ($tokens[($use + 1)]['code'] !== T_WHITESPACE) { |
|
| 127 | - $length = 0; |
|
| 128 | - } else if ($tokens[($use + 1)]['content'] === "\t") { |
|
| 129 | - $length = '\t'; |
|
| 130 | - } else { |
|
| 131 | - $length = strlen($tokens[($use + 1)]['content']); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if ($length !== 1) { |
|
| 135 | - $error = 'Expected 1 space after USE keyword; found %s'; |
|
| 136 | - $data = array($length); |
|
| 137 | - $fix = $phpcsFile->addFixableError($error, $use, 'SpaceAfterUse', $data); |
|
| 138 | - if ($fix === true) { |
|
| 139 | - if ($length === 0) { |
|
| 140 | - $phpcsFile->fixer->addContent($use, ' '); |
|
| 141 | - } else { |
|
| 142 | - $phpcsFile->fixer->replaceToken(($use + 1), ' '); |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - if ($tokens[($use - 1)]['code'] !== T_WHITESPACE) { |
|
| 148 | - $length = 0; |
|
| 149 | - } else if ($tokens[($use - 1)]['content'] === "\t") { |
|
| 150 | - $length = '\t'; |
|
| 151 | - } else { |
|
| 152 | - $length = strlen($tokens[($use - 1)]['content']); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if ($length !== 1) { |
|
| 156 | - $error = 'Expected 1 space before USE keyword; found %s'; |
|
| 157 | - $data = array($length); |
|
| 158 | - $fix = $phpcsFile->addFixableError($error, $use, 'SpaceBeforeUse', $data); |
|
| 159 | - if ($fix === true) { |
|
| 160 | - if ($length === 0) { |
|
| 161 | - $phpcsFile->fixer->addContentBefore($use, ' '); |
|
| 162 | - } else { |
|
| 163 | - $phpcsFile->fixer->replaceToken(($use - 1), ' '); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - }//end if |
|
| 168 | - }//end if |
|
| 169 | - |
|
| 170 | - if ($this->isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens) === true) { |
|
| 171 | - $this->processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens); |
|
| 172 | - } else { |
|
| 173 | - $this->processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - }//end process() |
|
| 177 | - |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Determine if this is a multi-line function declaration. |
|
| 181 | - * |
|
| 182 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 183 | - * @param int $stackPtr The position of the current token |
|
| 184 | - * in the stack passed in $tokens. |
|
| 185 | - * @param int $openBracket The position of the opening bracket |
|
| 186 | - * in the stack passed in $tokens. |
|
| 187 | - * @param array $tokens The stack of tokens that make up |
|
| 188 | - * the file. |
|
| 189 | - * |
|
| 190 | - * @return void |
|
| 191 | - */ |
|
| 192 | - public function isMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 193 | - { |
|
| 194 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 195 | - if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) { |
|
| 196 | - return true; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - // Closures may use the USE keyword and so be multi-line in this way. |
|
| 200 | - if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 201 | - $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); |
|
| 202 | - if ($use !== false) { |
|
| 203 | - // If the opening and closing parenthesis of the use statement |
|
| 204 | - // are also on the same line, this is a single line declaration. |
|
| 205 | - $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1)); |
|
| 206 | - $close = $tokens[$open]['parenthesis_closer']; |
|
| 207 | - if ($tokens[$open]['line'] !== $tokens[$close]['line']) { |
|
| 208 | - return true; |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - return false; |
|
| 214 | - |
|
| 215 | - }//end isMultiLineDeclaration() |
|
| 216 | - |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Processes single-line declarations. |
|
| 220 | - * |
|
| 221 | - * Just uses the Generic BSD-Allman brace sniff. |
|
| 222 | - * |
|
| 223 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 224 | - * @param int $stackPtr The position of the current token |
|
| 225 | - * in the stack passed in $tokens. |
|
| 226 | - * @param array $tokens The stack of tokens that make up |
|
| 227 | - * the file. |
|
| 228 | - * |
|
| 229 | - * @return void |
|
| 230 | - */ |
|
| 231 | - public function processSingleLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
|
| 232 | - { |
|
| 233 | - if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 234 | - if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff', true) === false) { |
|
| 235 | - throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff not found'); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff(); |
|
| 239 | - } else { |
|
| 240 | - if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff', true) === false) { |
|
| 241 | - throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff not found'); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - $sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff(); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $sniff->checkClosures = true; |
|
| 248 | - $sniff->process($phpcsFile, $stackPtr); |
|
| 249 | - |
|
| 250 | - }//end processSingleLineDeclaration() |
|
| 251 | - |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Processes multi-line declarations. |
|
| 255 | - * |
|
| 256 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 257 | - * @param int $stackPtr The position of the current token |
|
| 258 | - * in the stack passed in $tokens. |
|
| 259 | - * @param array $tokens The stack of tokens that make up |
|
| 260 | - * the file. |
|
| 261 | - * |
|
| 262 | - * @return void |
|
| 263 | - */ |
|
| 264 | - public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
|
| 265 | - { |
|
| 266 | - // We need to work out how far indented the function |
|
| 267 | - // declaration itself is, so we can work out how far to |
|
| 268 | - // indent parameters. |
|
| 269 | - $functionIndent = 0; |
|
| 270 | - for ($i = ($stackPtr - 1); $i >= 0; $i--) { |
|
| 271 | - if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 272 | - $i++; |
|
| 273 | - break; |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 278 | - $functionIndent = strlen($tokens[$i]['content']); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - // The closing parenthesis must be on a new line, even |
|
| 282 | - // when checking abstract function definitions. |
|
| 283 | - $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 284 | - $prev = $phpcsFile->findPrevious( |
|
| 285 | - T_WHITESPACE, |
|
| 286 | - ($closeBracket - 1), |
|
| 287 | - null, |
|
| 288 | - true |
|
| 289 | - ); |
|
| 290 | - |
|
| 291 | - if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line']) { |
|
| 292 | - if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) { |
|
| 293 | - $error = 'The closing parenthesis of a multi-line function declaration must be on a new line'; |
|
| 294 | - $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine'); |
|
| 295 | - if ($fix === true) { |
|
| 296 | - $phpcsFile->fixer->addNewlineBefore($closeBracket); |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - // If this is a closure and is using a USE statement, the closing |
|
| 302 | - // parenthesis we need to look at from now on is the closing parenthesis |
|
| 303 | - // of the USE statement. |
|
| 304 | - if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 305 | - $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); |
|
| 306 | - if ($use !== false) { |
|
| 307 | - $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1)); |
|
| 308 | - $closeBracket = $tokens[$open]['parenthesis_closer']; |
|
| 309 | - |
|
| 310 | - $prev = $phpcsFile->findPrevious( |
|
| 311 | - T_WHITESPACE, |
|
| 312 | - ($closeBracket - 1), |
|
| 313 | - null, |
|
| 314 | - true |
|
| 315 | - ); |
|
| 316 | - |
|
| 317 | - if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line']) { |
|
| 318 | - if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) { |
|
| 319 | - $error = 'The closing parenthesis of a multi-line use declaration must be on a new line'; |
|
| 320 | - $fix = $phpcsFile->addFixableError($error, $closeBracket, 'UseCloseBracketLine'); |
|
| 321 | - if ($fix === true) { |
|
| 322 | - $phpcsFile->fixer->addNewlineBefore($closeBracket); |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - }//end if |
|
| 327 | - }//end if |
|
| 328 | - |
|
| 329 | - // Each line between the parenthesis should be indented 4 spaces. |
|
| 330 | - $openBracket = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 331 | - $lastLine = $tokens[$openBracket]['line']; |
|
| 332 | - for ($i = ($openBracket + 1); $i < $closeBracket; $i++) { |
|
| 333 | - if ($tokens[$i]['line'] !== $lastLine) { |
|
| 334 | - if ($i === $tokens[$stackPtr]['parenthesis_closer'] |
|
| 335 | - || ($tokens[$i]['code'] === T_WHITESPACE |
|
| 336 | - && (($i + 1) === $closeBracket |
|
| 337 | - || ($i + 1) === $tokens[$stackPtr]['parenthesis_closer'])) |
|
| 338 | - ) { |
|
| 339 | - // Closing braces need to be indented to the same level |
|
| 340 | - // as the function. |
|
| 341 | - $expectedIndent = $functionIndent; |
|
| 342 | - } else { |
|
| 343 | - $expectedIndent = ($functionIndent + $this->indent); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - // We changed lines, so this should be a whitespace indent token. |
|
| 347 | - if ($tokens[$i]['code'] !== T_WHITESPACE) { |
|
| 348 | - $foundIndent = 0; |
|
| 349 | - } else { |
|
| 350 | - $foundIndent = strlen($tokens[$i]['content']); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - if ($expectedIndent !== $foundIndent) { |
|
| 354 | - $error = 'Multi-line function declaration not indented correctly; expected %s spaces but found %s'; |
|
| 355 | - $data = array( |
|
| 356 | - $expectedIndent, |
|
| 357 | - $foundIndent, |
|
| 358 | - ); |
|
| 359 | - |
|
| 360 | - $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data); |
|
| 361 | - if ($fix === true) { |
|
| 362 | - $spaces = str_repeat(' ', $expectedIndent); |
|
| 363 | - if ($foundIndent === 0) { |
|
| 364 | - $phpcsFile->fixer->addContentBefore($i, $spaces); |
|
| 365 | - } else { |
|
| 366 | - $phpcsFile->fixer->replaceToken($i, $spaces); |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - $lastLine = $tokens[$i]['line']; |
|
| 372 | - }//end if |
|
| 373 | - |
|
| 374 | - if ($tokens[$i]['code'] === T_ARRAY || $tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
| 375 | - // Skip arrays as they have their own indentation rules. |
|
| 376 | - if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
| 377 | - $i = $tokens[$i]['bracket_closer']; |
|
| 378 | - } else { |
|
| 379 | - $i = $tokens[$i]['parenthesis_closer']; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - $lastLine = $tokens[$i]['line']; |
|
| 383 | - continue; |
|
| 384 | - } |
|
| 385 | - }//end for |
|
| 386 | - |
|
| 387 | - if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
| 388 | - return; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - // The opening brace needs to be one space away from the closing parenthesis. |
|
| 392 | - $opener = $tokens[$stackPtr]['scope_opener']; |
|
| 393 | - if ($tokens[$opener]['line'] !== $tokens[$closeBracket]['line']) { |
|
| 394 | - $error = 'The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line'; |
|
| 395 | - $fix = $phpcsFile->addFixableError($error, $opener, 'NewlineBeforeOpenBrace'); |
|
| 396 | - if ($fix === true) { |
|
| 397 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($opener - 1), $closeBracket, true); |
|
| 398 | - $phpcsFile->fixer->beginChangeset(); |
|
| 399 | - $phpcsFile->fixer->addContent($prev, ' {'); |
|
| 400 | - $phpcsFile->fixer->replaceToken($opener, ''); |
|
| 401 | - $phpcsFile->fixer->endChangeset(); |
|
| 402 | - } |
|
| 403 | - } else { |
|
| 404 | - $prev = $tokens[($opener - 1)]; |
|
| 405 | - if ($prev['code'] !== T_WHITESPACE) { |
|
| 406 | - $length = 0; |
|
| 407 | - } else { |
|
| 408 | - $length = strlen($prev['content']); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - if ($length !== 1) { |
|
| 412 | - $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found %s spaces'; |
|
| 413 | - $fix = $phpcsFile->addFixableError($error, ($opener - 1), 'SpaceBeforeOpenBrace', array($length)); |
|
| 414 | - if ($fix === true) { |
|
| 415 | - if ($length === 0) { |
|
| 416 | - $phpcsFile->fixer->addContentBefore($opener, ' '); |
|
| 417 | - } else { |
|
| 418 | - $phpcsFile->fixer->replaceToken(($opener - 1), ' '); |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - return; |
|
| 423 | - }//end if |
|
| 424 | - }//end if |
|
| 425 | - |
|
| 426 | - }//end processMultiLineDeclaration() |
|
| 31 | + /** |
|
| 32 | + * The number of spaces code should be indented. |
|
| 33 | + * |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + public $indent = 4; |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Returns an array of tokens this test wants to listen for. |
|
| 41 | + * |
|
| 42 | + * @return array |
|
| 43 | + */ |
|
| 44 | + public function register() |
|
| 45 | + { |
|
| 46 | + return array( |
|
| 47 | + T_FUNCTION, |
|
| 48 | + T_CLOSURE, |
|
| 49 | + ); |
|
| 50 | + |
|
| 51 | + }//end register() |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Processes this test, when one of its tokens is encountered. |
|
| 56 | + * |
|
| 57 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 58 | + * @param int $stackPtr The position of the current token |
|
| 59 | + * in the stack passed in $tokens. |
|
| 60 | + * |
|
| 61 | + * @return void |
|
| 62 | + */ |
|
| 63 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 64 | + { |
|
| 65 | + $tokens = $phpcsFile->getTokens(); |
|
| 66 | + |
|
| 67 | + if (isset($tokens[$stackPtr]['parenthesis_opener']) === false |
|
| 68 | + || isset($tokens[$stackPtr]['parenthesis_closer']) === false |
|
| 69 | + || $tokens[$stackPtr]['parenthesis_opener'] === null |
|
| 70 | + || $tokens[$stackPtr]['parenthesis_closer'] === null |
|
| 71 | + ) { |
|
| 72 | + return; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + $openBracket = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 76 | + $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 77 | + |
|
| 78 | + // Must be one space after the FUNCTION keyword. |
|
| 79 | + if ($tokens[($stackPtr + 1)]['content'] === $phpcsFile->eolChar) { |
|
| 80 | + $spaces = 'newline'; |
|
| 81 | + } else if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { |
|
| 82 | + $spaces = strlen($tokens[($stackPtr + 1)]['content']); |
|
| 83 | + } else { |
|
| 84 | + $spaces = 0; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + if ($spaces !== 1) { |
|
| 88 | + $error = 'Expected 1 space after FUNCTION keyword; %s found'; |
|
| 89 | + $data = array($spaces); |
|
| 90 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterFunction', $data); |
|
| 91 | + if ($fix === true) { |
|
| 92 | + if ($spaces === 0) { |
|
| 93 | + $phpcsFile->fixer->addContent($stackPtr, ' '); |
|
| 94 | + } else { |
|
| 95 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // Must be one space before the opening parenthesis. For closures, this is |
|
| 101 | + // enforced by the first check because there is no content between the keywords |
|
| 102 | + // and the opening parenthesis. |
|
| 103 | + if ($tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
| 104 | + if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) { |
|
| 105 | + $spaces = 'newline'; |
|
| 106 | + } else if ($tokens[($openBracket - 1)]['code'] === T_WHITESPACE) { |
|
| 107 | + $spaces = strlen($tokens[($openBracket - 1)]['content']); |
|
| 108 | + } else { |
|
| 109 | + $spaces = 0; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if ($spaces !== 0) { |
|
| 113 | + $error = 'Expected 0 spaces before opening parenthesis; %s found'; |
|
| 114 | + $data = array($spaces); |
|
| 115 | + $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParen', $data); |
|
| 116 | + if ($fix === true) { |
|
| 117 | + $phpcsFile->fixer->replaceToken(($openBracket - 1), ''); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + }//end if |
|
| 121 | + |
|
| 122 | + // Must be one space before and after USE keyword for closures. |
|
| 123 | + if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 124 | + $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); |
|
| 125 | + if ($use !== false) { |
|
| 126 | + if ($tokens[($use + 1)]['code'] !== T_WHITESPACE) { |
|
| 127 | + $length = 0; |
|
| 128 | + } else if ($tokens[($use + 1)]['content'] === "\t") { |
|
| 129 | + $length = '\t'; |
|
| 130 | + } else { |
|
| 131 | + $length = strlen($tokens[($use + 1)]['content']); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if ($length !== 1) { |
|
| 135 | + $error = 'Expected 1 space after USE keyword; found %s'; |
|
| 136 | + $data = array($length); |
|
| 137 | + $fix = $phpcsFile->addFixableError($error, $use, 'SpaceAfterUse', $data); |
|
| 138 | + if ($fix === true) { |
|
| 139 | + if ($length === 0) { |
|
| 140 | + $phpcsFile->fixer->addContent($use, ' '); |
|
| 141 | + } else { |
|
| 142 | + $phpcsFile->fixer->replaceToken(($use + 1), ' '); |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + if ($tokens[($use - 1)]['code'] !== T_WHITESPACE) { |
|
| 148 | + $length = 0; |
|
| 149 | + } else if ($tokens[($use - 1)]['content'] === "\t") { |
|
| 150 | + $length = '\t'; |
|
| 151 | + } else { |
|
| 152 | + $length = strlen($tokens[($use - 1)]['content']); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if ($length !== 1) { |
|
| 156 | + $error = 'Expected 1 space before USE keyword; found %s'; |
|
| 157 | + $data = array($length); |
|
| 158 | + $fix = $phpcsFile->addFixableError($error, $use, 'SpaceBeforeUse', $data); |
|
| 159 | + if ($fix === true) { |
|
| 160 | + if ($length === 0) { |
|
| 161 | + $phpcsFile->fixer->addContentBefore($use, ' '); |
|
| 162 | + } else { |
|
| 163 | + $phpcsFile->fixer->replaceToken(($use - 1), ' '); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + }//end if |
|
| 168 | + }//end if |
|
| 169 | + |
|
| 170 | + if ($this->isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens) === true) { |
|
| 171 | + $this->processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens); |
|
| 172 | + } else { |
|
| 173 | + $this->processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + }//end process() |
|
| 177 | + |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Determine if this is a multi-line function declaration. |
|
| 181 | + * |
|
| 182 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 183 | + * @param int $stackPtr The position of the current token |
|
| 184 | + * in the stack passed in $tokens. |
|
| 185 | + * @param int $openBracket The position of the opening bracket |
|
| 186 | + * in the stack passed in $tokens. |
|
| 187 | + * @param array $tokens The stack of tokens that make up |
|
| 188 | + * the file. |
|
| 189 | + * |
|
| 190 | + * @return void |
|
| 191 | + */ |
|
| 192 | + public function isMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 193 | + { |
|
| 194 | + $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 195 | + if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) { |
|
| 196 | + return true; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + // Closures may use the USE keyword and so be multi-line in this way. |
|
| 200 | + if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 201 | + $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); |
|
| 202 | + if ($use !== false) { |
|
| 203 | + // If the opening and closing parenthesis of the use statement |
|
| 204 | + // are also on the same line, this is a single line declaration. |
|
| 205 | + $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1)); |
|
| 206 | + $close = $tokens[$open]['parenthesis_closer']; |
|
| 207 | + if ($tokens[$open]['line'] !== $tokens[$close]['line']) { |
|
| 208 | + return true; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + return false; |
|
| 214 | + |
|
| 215 | + }//end isMultiLineDeclaration() |
|
| 216 | + |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Processes single-line declarations. |
|
| 220 | + * |
|
| 221 | + * Just uses the Generic BSD-Allman brace sniff. |
|
| 222 | + * |
|
| 223 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 224 | + * @param int $stackPtr The position of the current token |
|
| 225 | + * in the stack passed in $tokens. |
|
| 226 | + * @param array $tokens The stack of tokens that make up |
|
| 227 | + * the file. |
|
| 228 | + * |
|
| 229 | + * @return void |
|
| 230 | + */ |
|
| 231 | + public function processSingleLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
|
| 232 | + { |
|
| 233 | + if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 234 | + if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff', true) === false) { |
|
| 235 | + throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff not found'); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff(); |
|
| 239 | + } else { |
|
| 240 | + if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff', true) === false) { |
|
| 241 | + throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff not found'); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + $sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff(); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $sniff->checkClosures = true; |
|
| 248 | + $sniff->process($phpcsFile, $stackPtr); |
|
| 249 | + |
|
| 250 | + }//end processSingleLineDeclaration() |
|
| 251 | + |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Processes multi-line declarations. |
|
| 255 | + * |
|
| 256 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 257 | + * @param int $stackPtr The position of the current token |
|
| 258 | + * in the stack passed in $tokens. |
|
| 259 | + * @param array $tokens The stack of tokens that make up |
|
| 260 | + * the file. |
|
| 261 | + * |
|
| 262 | + * @return void |
|
| 263 | + */ |
|
| 264 | + public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
|
| 265 | + { |
|
| 266 | + // We need to work out how far indented the function |
|
| 267 | + // declaration itself is, so we can work out how far to |
|
| 268 | + // indent parameters. |
|
| 269 | + $functionIndent = 0; |
|
| 270 | + for ($i = ($stackPtr - 1); $i >= 0; $i--) { |
|
| 271 | + if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 272 | + $i++; |
|
| 273 | + break; |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 278 | + $functionIndent = strlen($tokens[$i]['content']); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + // The closing parenthesis must be on a new line, even |
|
| 282 | + // when checking abstract function definitions. |
|
| 283 | + $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 284 | + $prev = $phpcsFile->findPrevious( |
|
| 285 | + T_WHITESPACE, |
|
| 286 | + ($closeBracket - 1), |
|
| 287 | + null, |
|
| 288 | + true |
|
| 289 | + ); |
|
| 290 | + |
|
| 291 | + if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line']) { |
|
| 292 | + if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) { |
|
| 293 | + $error = 'The closing parenthesis of a multi-line function declaration must be on a new line'; |
|
| 294 | + $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine'); |
|
| 295 | + if ($fix === true) { |
|
| 296 | + $phpcsFile->fixer->addNewlineBefore($closeBracket); |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + // If this is a closure and is using a USE statement, the closing |
|
| 302 | + // parenthesis we need to look at from now on is the closing parenthesis |
|
| 303 | + // of the USE statement. |
|
| 304 | + if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 305 | + $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); |
|
| 306 | + if ($use !== false) { |
|
| 307 | + $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1)); |
|
| 308 | + $closeBracket = $tokens[$open]['parenthesis_closer']; |
|
| 309 | + |
|
| 310 | + $prev = $phpcsFile->findPrevious( |
|
| 311 | + T_WHITESPACE, |
|
| 312 | + ($closeBracket - 1), |
|
| 313 | + null, |
|
| 314 | + true |
|
| 315 | + ); |
|
| 316 | + |
|
| 317 | + if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line']) { |
|
| 318 | + if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) { |
|
| 319 | + $error = 'The closing parenthesis of a multi-line use declaration must be on a new line'; |
|
| 320 | + $fix = $phpcsFile->addFixableError($error, $closeBracket, 'UseCloseBracketLine'); |
|
| 321 | + if ($fix === true) { |
|
| 322 | + $phpcsFile->fixer->addNewlineBefore($closeBracket); |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + }//end if |
|
| 327 | + }//end if |
|
| 328 | + |
|
| 329 | + // Each line between the parenthesis should be indented 4 spaces. |
|
| 330 | + $openBracket = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 331 | + $lastLine = $tokens[$openBracket]['line']; |
|
| 332 | + for ($i = ($openBracket + 1); $i < $closeBracket; $i++) { |
|
| 333 | + if ($tokens[$i]['line'] !== $lastLine) { |
|
| 334 | + if ($i === $tokens[$stackPtr]['parenthesis_closer'] |
|
| 335 | + || ($tokens[$i]['code'] === T_WHITESPACE |
|
| 336 | + && (($i + 1) === $closeBracket |
|
| 337 | + || ($i + 1) === $tokens[$stackPtr]['parenthesis_closer'])) |
|
| 338 | + ) { |
|
| 339 | + // Closing braces need to be indented to the same level |
|
| 340 | + // as the function. |
|
| 341 | + $expectedIndent = $functionIndent; |
|
| 342 | + } else { |
|
| 343 | + $expectedIndent = ($functionIndent + $this->indent); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + // We changed lines, so this should be a whitespace indent token. |
|
| 347 | + if ($tokens[$i]['code'] !== T_WHITESPACE) { |
|
| 348 | + $foundIndent = 0; |
|
| 349 | + } else { |
|
| 350 | + $foundIndent = strlen($tokens[$i]['content']); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + if ($expectedIndent !== $foundIndent) { |
|
| 354 | + $error = 'Multi-line function declaration not indented correctly; expected %s spaces but found %s'; |
|
| 355 | + $data = array( |
|
| 356 | + $expectedIndent, |
|
| 357 | + $foundIndent, |
|
| 358 | + ); |
|
| 359 | + |
|
| 360 | + $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data); |
|
| 361 | + if ($fix === true) { |
|
| 362 | + $spaces = str_repeat(' ', $expectedIndent); |
|
| 363 | + if ($foundIndent === 0) { |
|
| 364 | + $phpcsFile->fixer->addContentBefore($i, $spaces); |
|
| 365 | + } else { |
|
| 366 | + $phpcsFile->fixer->replaceToken($i, $spaces); |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + $lastLine = $tokens[$i]['line']; |
|
| 372 | + }//end if |
|
| 373 | + |
|
| 374 | + if ($tokens[$i]['code'] === T_ARRAY || $tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
| 375 | + // Skip arrays as they have their own indentation rules. |
|
| 376 | + if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
| 377 | + $i = $tokens[$i]['bracket_closer']; |
|
| 378 | + } else { |
|
| 379 | + $i = $tokens[$i]['parenthesis_closer']; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + $lastLine = $tokens[$i]['line']; |
|
| 383 | + continue; |
|
| 384 | + } |
|
| 385 | + }//end for |
|
| 386 | + |
|
| 387 | + if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
| 388 | + return; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + // The opening brace needs to be one space away from the closing parenthesis. |
|
| 392 | + $opener = $tokens[$stackPtr]['scope_opener']; |
|
| 393 | + if ($tokens[$opener]['line'] !== $tokens[$closeBracket]['line']) { |
|
| 394 | + $error = 'The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line'; |
|
| 395 | + $fix = $phpcsFile->addFixableError($error, $opener, 'NewlineBeforeOpenBrace'); |
|
| 396 | + if ($fix === true) { |
|
| 397 | + $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($opener - 1), $closeBracket, true); |
|
| 398 | + $phpcsFile->fixer->beginChangeset(); |
|
| 399 | + $phpcsFile->fixer->addContent($prev, ' {'); |
|
| 400 | + $phpcsFile->fixer->replaceToken($opener, ''); |
|
| 401 | + $phpcsFile->fixer->endChangeset(); |
|
| 402 | + } |
|
| 403 | + } else { |
|
| 404 | + $prev = $tokens[($opener - 1)]; |
|
| 405 | + if ($prev['code'] !== T_WHITESPACE) { |
|
| 406 | + $length = 0; |
|
| 407 | + } else { |
|
| 408 | + $length = strlen($prev['content']); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + if ($length !== 1) { |
|
| 412 | + $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found %s spaces'; |
|
| 413 | + $fix = $phpcsFile->addFixableError($error, ($opener - 1), 'SpaceBeforeOpenBrace', array($length)); |
|
| 414 | + if ($fix === true) { |
|
| 415 | + if ($length === 0) { |
|
| 416 | + $phpcsFile->fixer->addContentBefore($opener, ' '); |
|
| 417 | + } else { |
|
| 418 | + $phpcsFile->fixer->replaceToken(($opener - 1), ' '); |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + return; |
|
| 423 | + }//end if |
|
| 424 | + }//end if |
|
| 425 | + |
|
| 426 | + }//end processMultiLineDeclaration() |
|
| 427 | 427 | |
| 428 | 428 | |
| 429 | 429 | }//end class |
@@ -60,39 +60,39 @@ discard block |
||
| 60 | 60 | * |
| 61 | 61 | * @return void |
| 62 | 62 | */ |
| 63 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 63 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 64 | 64 | { |
| 65 | 65 | $tokens = $phpcsFile->getTokens(); |
| 66 | 66 | |
| 67 | - if (isset($tokens[$stackPtr]['parenthesis_opener']) === false |
|
| 68 | - || isset($tokens[$stackPtr]['parenthesis_closer']) === false |
|
| 69 | - || $tokens[$stackPtr]['parenthesis_opener'] === null |
|
| 70 | - || $tokens[$stackPtr]['parenthesis_closer'] === null |
|
| 67 | + if ( isset( $tokens[ $stackPtr ][ 'parenthesis_opener' ] ) === false |
|
| 68 | + || isset( $tokens[ $stackPtr ][ 'parenthesis_closer' ] ) === false |
|
| 69 | + || $tokens[ $stackPtr ][ 'parenthesis_opener' ] === null |
|
| 70 | + || $tokens[ $stackPtr ][ 'parenthesis_closer' ] === null |
|
| 71 | 71 | ) { |
| 72 | 72 | return; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - $openBracket = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 76 | - $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 75 | + $openBracket = $tokens[ $stackPtr ][ 'parenthesis_opener' ]; |
|
| 76 | + $closeBracket = $tokens[ $stackPtr ][ 'parenthesis_closer' ]; |
|
| 77 | 77 | |
| 78 | 78 | // Must be one space after the FUNCTION keyword. |
| 79 | - if ($tokens[($stackPtr + 1)]['content'] === $phpcsFile->eolChar) { |
|
| 79 | + if ( $tokens[ ( $stackPtr + 1 ) ][ 'content' ] === $phpcsFile->eolChar ) { |
|
| 80 | 80 | $spaces = 'newline'; |
| 81 | - } else if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { |
|
| 82 | - $spaces = strlen($tokens[($stackPtr + 1)]['content']); |
|
| 81 | + } else if ( $tokens[ ( $stackPtr + 1 ) ][ 'code' ] === T_WHITESPACE ) { |
|
| 82 | + $spaces = strlen( $tokens[ ( $stackPtr + 1 ) ][ 'content' ] ); |
|
| 83 | 83 | } else { |
| 84 | 84 | $spaces = 0; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - if ($spaces !== 1) { |
|
| 87 | + if ( $spaces !== 1 ) { |
|
| 88 | 88 | $error = 'Expected 1 space after FUNCTION keyword; %s found'; |
| 89 | - $data = array($spaces); |
|
| 90 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterFunction', $data); |
|
| 91 | - if ($fix === true) { |
|
| 92 | - if ($spaces === 0) { |
|
| 93 | - $phpcsFile->fixer->addContent($stackPtr, ' '); |
|
| 89 | + $data = array( $spaces ); |
|
| 90 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'SpaceAfterFunction', $data ); |
|
| 91 | + if ( $fix === true ) { |
|
| 92 | + if ( $spaces === 0 ) { |
|
| 93 | + $phpcsFile->fixer->addContent( $stackPtr, ' ' ); |
|
| 94 | 94 | } else { |
| 95 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
| 95 | + $phpcsFile->fixer->replaceToken( ( $stackPtr + 1 ), ' ' ); |
|
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | } |
@@ -100,77 +100,77 @@ discard block |
||
| 100 | 100 | // Must be one space before the opening parenthesis. For closures, this is |
| 101 | 101 | // enforced by the first check because there is no content between the keywords |
| 102 | 102 | // and the opening parenthesis. |
| 103 | - if ($tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
| 104 | - if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) { |
|
| 103 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_FUNCTION ) { |
|
| 104 | + if ( $tokens[ ( $openBracket - 1 ) ][ 'content' ] === $phpcsFile->eolChar ) { |
|
| 105 | 105 | $spaces = 'newline'; |
| 106 | - } else if ($tokens[($openBracket - 1)]['code'] === T_WHITESPACE) { |
|
| 107 | - $spaces = strlen($tokens[($openBracket - 1)]['content']); |
|
| 106 | + } else if ( $tokens[ ( $openBracket - 1 ) ][ 'code' ] === T_WHITESPACE ) { |
|
| 107 | + $spaces = strlen( $tokens[ ( $openBracket - 1 ) ][ 'content' ] ); |
|
| 108 | 108 | } else { |
| 109 | 109 | $spaces = 0; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | - if ($spaces !== 0) { |
|
| 112 | + if ( $spaces !== 0 ) { |
|
| 113 | 113 | $error = 'Expected 0 spaces before opening parenthesis; %s found'; |
| 114 | - $data = array($spaces); |
|
| 115 | - $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParen', $data); |
|
| 116 | - if ($fix === true) { |
|
| 117 | - $phpcsFile->fixer->replaceToken(($openBracket - 1), ''); |
|
| 114 | + $data = array( $spaces ); |
|
| 115 | + $fix = $phpcsFile->addFixableError( $error, $openBracket, 'SpaceBeforeOpenParen', $data ); |
|
| 116 | + if ( $fix === true ) { |
|
| 117 | + $phpcsFile->fixer->replaceToken( ( $openBracket - 1 ), '' ); |
|
| 118 | 118 | } |
| 119 | 119 | } |
| 120 | 120 | }//end if |
| 121 | 121 | |
| 122 | 122 | // Must be one space before and after USE keyword for closures. |
| 123 | - if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 124 | - $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); |
|
| 125 | - if ($use !== false) { |
|
| 126 | - if ($tokens[($use + 1)]['code'] !== T_WHITESPACE) { |
|
| 123 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_CLOSURE ) { |
|
| 124 | + $use = $phpcsFile->findNext( T_USE, ( $closeBracket + 1 ), $tokens[ $stackPtr ][ 'scope_opener' ] ); |
|
| 125 | + if ( $use !== false ) { |
|
| 126 | + if ( $tokens[ ( $use + 1 ) ][ 'code' ] !== T_WHITESPACE ) { |
|
| 127 | 127 | $length = 0; |
| 128 | - } else if ($tokens[($use + 1)]['content'] === "\t") { |
|
| 128 | + } else if ( $tokens[ ( $use + 1 ) ][ 'content' ] === "\t" ) { |
|
| 129 | 129 | $length = '\t'; |
| 130 | 130 | } else { |
| 131 | - $length = strlen($tokens[($use + 1)]['content']); |
|
| 131 | + $length = strlen( $tokens[ ( $use + 1 ) ][ 'content' ] ); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - if ($length !== 1) { |
|
| 134 | + if ( $length !== 1 ) { |
|
| 135 | 135 | $error = 'Expected 1 space after USE keyword; found %s'; |
| 136 | - $data = array($length); |
|
| 137 | - $fix = $phpcsFile->addFixableError($error, $use, 'SpaceAfterUse', $data); |
|
| 138 | - if ($fix === true) { |
|
| 139 | - if ($length === 0) { |
|
| 140 | - $phpcsFile->fixer->addContent($use, ' '); |
|
| 136 | + $data = array( $length ); |
|
| 137 | + $fix = $phpcsFile->addFixableError( $error, $use, 'SpaceAfterUse', $data ); |
|
| 138 | + if ( $fix === true ) { |
|
| 139 | + if ( $length === 0 ) { |
|
| 140 | + $phpcsFile->fixer->addContent( $use, ' ' ); |
|
| 141 | 141 | } else { |
| 142 | - $phpcsFile->fixer->replaceToken(($use + 1), ' '); |
|
| 142 | + $phpcsFile->fixer->replaceToken( ( $use + 1 ), ' ' ); |
|
| 143 | 143 | } |
| 144 | 144 | } |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - if ($tokens[($use - 1)]['code'] !== T_WHITESPACE) { |
|
| 147 | + if ( $tokens[ ( $use - 1 ) ][ 'code' ] !== T_WHITESPACE ) { |
|
| 148 | 148 | $length = 0; |
| 149 | - } else if ($tokens[($use - 1)]['content'] === "\t") { |
|
| 149 | + } else if ( $tokens[ ( $use - 1 ) ][ 'content' ] === "\t" ) { |
|
| 150 | 150 | $length = '\t'; |
| 151 | 151 | } else { |
| 152 | - $length = strlen($tokens[($use - 1)]['content']); |
|
| 152 | + $length = strlen( $tokens[ ( $use - 1 ) ][ 'content' ] ); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - if ($length !== 1) { |
|
| 155 | + if ( $length !== 1 ) { |
|
| 156 | 156 | $error = 'Expected 1 space before USE keyword; found %s'; |
| 157 | - $data = array($length); |
|
| 158 | - $fix = $phpcsFile->addFixableError($error, $use, 'SpaceBeforeUse', $data); |
|
| 159 | - if ($fix === true) { |
|
| 160 | - if ($length === 0) { |
|
| 161 | - $phpcsFile->fixer->addContentBefore($use, ' '); |
|
| 157 | + $data = array( $length ); |
|
| 158 | + $fix = $phpcsFile->addFixableError( $error, $use, 'SpaceBeforeUse', $data ); |
|
| 159 | + if ( $fix === true ) { |
|
| 160 | + if ( $length === 0 ) { |
|
| 161 | + $phpcsFile->fixer->addContentBefore( $use, ' ' ); |
|
| 162 | 162 | } else { |
| 163 | - $phpcsFile->fixer->replaceToken(($use - 1), ' '); |
|
| 163 | + $phpcsFile->fixer->replaceToken( ( $use - 1 ), ' ' ); |
|
| 164 | 164 | } |
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | }//end if |
| 168 | 168 | }//end if |
| 169 | 169 | |
| 170 | - if ($this->isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens) === true) { |
|
| 171 | - $this->processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens); |
|
| 170 | + if ( $this->isMultiLineDeclaration( $phpcsFile, $stackPtr, $openBracket, $tokens ) === true ) { |
|
| 171 | + $this->processMultiLineDeclaration( $phpcsFile, $stackPtr, $tokens ); |
|
| 172 | 172 | } else { |
| 173 | - $this->processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens); |
|
| 173 | + $this->processSingleLineDeclaration( $phpcsFile, $stackPtr, $tokens ); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | }//end process() |
@@ -189,22 +189,22 @@ discard block |
||
| 189 | 189 | * |
| 190 | 190 | * @return void |
| 191 | 191 | */ |
| 192 | - public function isMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 192 | + public function isMultiLineDeclaration( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens ) |
|
| 193 | 193 | { |
| 194 | - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
|
| 195 | - if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) { |
|
| 194 | + $closeBracket = $tokens[ $openBracket ][ 'parenthesis_closer' ]; |
|
| 195 | + if ( $tokens[ $openBracket ][ 'line' ] !== $tokens[ $closeBracket ][ 'line' ] ) { |
|
| 196 | 196 | return true; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | // Closures may use the USE keyword and so be multi-line in this way. |
| 200 | - if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 201 | - $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); |
|
| 202 | - if ($use !== false) { |
|
| 200 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_CLOSURE ) { |
|
| 201 | + $use = $phpcsFile->findNext( T_USE, ( $closeBracket + 1 ), $tokens[ $stackPtr ][ 'scope_opener' ] ); |
|
| 202 | + if ( $use !== false ) { |
|
| 203 | 203 | // If the opening and closing parenthesis of the use statement |
| 204 | 204 | // are also on the same line, this is a single line declaration. |
| 205 | - $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1)); |
|
| 206 | - $close = $tokens[$open]['parenthesis_closer']; |
|
| 207 | - if ($tokens[$open]['line'] !== $tokens[$close]['line']) { |
|
| 205 | + $open = $phpcsFile->findNext( T_OPEN_PARENTHESIS, ( $use + 1 ) ); |
|
| 206 | + $close = $tokens[ $open ][ 'parenthesis_closer' ]; |
|
| 207 | + if ( $tokens[ $open ][ 'line' ] !== $tokens[ $close ][ 'line' ] ) { |
|
| 208 | 208 | return true; |
| 209 | 209 | } |
| 210 | 210 | } |
@@ -228,24 +228,24 @@ discard block |
||
| 228 | 228 | * |
| 229 | 229 | * @return void |
| 230 | 230 | */ |
| 231 | - public function processSingleLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
|
| 231 | + public function processSingleLineDeclaration( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens ) |
|
| 232 | 232 | { |
| 233 | - if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 234 | - if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff', true) === false) { |
|
| 235 | - throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff not found'); |
|
| 233 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_CLOSURE ) { |
|
| 234 | + if ( class_exists( 'Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff', true ) === false ) { |
|
| 235 | + throw new PHP_CodeSniffer_Exception( 'Class Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff not found' ); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | $sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff(); |
| 239 | 239 | } else { |
| 240 | - if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff', true) === false) { |
|
| 241 | - throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff not found'); |
|
| 240 | + if ( class_exists( 'Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff', true ) === false ) { |
|
| 241 | + throw new PHP_CodeSniffer_Exception( 'Class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff not found' ); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | $sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff(); |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | $sniff->checkClosures = true; |
| 248 | - $sniff->process($phpcsFile, $stackPtr); |
|
| 248 | + $sniff->process( $phpcsFile, $stackPtr ); |
|
| 249 | 249 | |
| 250 | 250 | }//end processSingleLineDeclaration() |
| 251 | 251 | |
@@ -261,39 +261,39 @@ discard block |
||
| 261 | 261 | * |
| 262 | 262 | * @return void |
| 263 | 263 | */ |
| 264 | - public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
|
| 264 | + public function processMultiLineDeclaration( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens ) |
|
| 265 | 265 | { |
| 266 | 266 | // We need to work out how far indented the function |
| 267 | 267 | // declaration itself is, so we can work out how far to |
| 268 | 268 | // indent parameters. |
| 269 | 269 | $functionIndent = 0; |
| 270 | - for ($i = ($stackPtr - 1); $i >= 0; $i--) { |
|
| 271 | - if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 270 | + for ( $i = ( $stackPtr - 1 ); $i >= 0; $i-- ) { |
|
| 271 | + if ( $tokens[ $i ][ 'line' ] !== $tokens[ $stackPtr ][ 'line' ] ) { |
|
| 272 | 272 | $i++; |
| 273 | 273 | break; |
| 274 | 274 | } |
| 275 | 275 | } |
| 276 | 276 | |
| 277 | - if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
| 278 | - $functionIndent = strlen($tokens[$i]['content']); |
|
| 277 | + if ( $tokens[ $i ][ 'code' ] === T_WHITESPACE ) { |
|
| 278 | + $functionIndent = strlen( $tokens[ $i ][ 'content' ] ); |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | // The closing parenthesis must be on a new line, even |
| 282 | 282 | // when checking abstract function definitions. |
| 283 | - $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; |
|
| 283 | + $closeBracket = $tokens[ $stackPtr ][ 'parenthesis_closer' ]; |
|
| 284 | 284 | $prev = $phpcsFile->findPrevious( |
| 285 | 285 | T_WHITESPACE, |
| 286 | - ($closeBracket - 1), |
|
| 286 | + ( $closeBracket - 1 ), |
|
| 287 | 287 | null, |
| 288 | 288 | true |
| 289 | 289 | ); |
| 290 | 290 | |
| 291 | - if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line']) { |
|
| 292 | - if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) { |
|
| 291 | + if ( $tokens[ $closeBracket ][ 'line' ] !== $tokens[ $tokens[ $closeBracket ][ 'parenthesis_opener' ] ][ 'line' ] ) { |
|
| 292 | + if ( $tokens[ $prev ][ 'line' ] === $tokens[ $closeBracket ][ 'line' ] ) { |
|
| 293 | 293 | $error = 'The closing parenthesis of a multi-line function declaration must be on a new line'; |
| 294 | - $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine'); |
|
| 295 | - if ($fix === true) { |
|
| 296 | - $phpcsFile->fixer->addNewlineBefore($closeBracket); |
|
| 294 | + $fix = $phpcsFile->addFixableError( $error, $closeBracket, 'CloseBracketLine' ); |
|
| 295 | + if ( $fix === true ) { |
|
| 296 | + $phpcsFile->fixer->addNewlineBefore( $closeBracket ); |
|
| 297 | 297 | } |
| 298 | 298 | } |
| 299 | 299 | } |
@@ -301,25 +301,25 @@ discard block |
||
| 301 | 301 | // If this is a closure and is using a USE statement, the closing |
| 302 | 302 | // parenthesis we need to look at from now on is the closing parenthesis |
| 303 | 303 | // of the USE statement. |
| 304 | - if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
|
| 305 | - $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); |
|
| 306 | - if ($use !== false) { |
|
| 307 | - $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1)); |
|
| 308 | - $closeBracket = $tokens[$open]['parenthesis_closer']; |
|
| 304 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_CLOSURE ) { |
|
| 305 | + $use = $phpcsFile->findNext( T_USE, ( $closeBracket + 1 ), $tokens[ $stackPtr ][ 'scope_opener' ] ); |
|
| 306 | + if ( $use !== false ) { |
|
| 307 | + $open = $phpcsFile->findNext( T_OPEN_PARENTHESIS, ( $use + 1 ) ); |
|
| 308 | + $closeBracket = $tokens[ $open ][ 'parenthesis_closer' ]; |
|
| 309 | 309 | |
| 310 | 310 | $prev = $phpcsFile->findPrevious( |
| 311 | 311 | T_WHITESPACE, |
| 312 | - ($closeBracket - 1), |
|
| 312 | + ( $closeBracket - 1 ), |
|
| 313 | 313 | null, |
| 314 | 314 | true |
| 315 | 315 | ); |
| 316 | 316 | |
| 317 | - if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line']) { |
|
| 318 | - if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) { |
|
| 317 | + if ( $tokens[ $closeBracket ][ 'line' ] !== $tokens[ $tokens[ $closeBracket ][ 'parenthesis_opener' ] ][ 'line' ] ) { |
|
| 318 | + if ( $tokens[ $prev ][ 'line' ] === $tokens[ $closeBracket ][ 'line' ] ) { |
|
| 319 | 319 | $error = 'The closing parenthesis of a multi-line use declaration must be on a new line'; |
| 320 | - $fix = $phpcsFile->addFixableError($error, $closeBracket, 'UseCloseBracketLine'); |
|
| 321 | - if ($fix === true) { |
|
| 322 | - $phpcsFile->fixer->addNewlineBefore($closeBracket); |
|
| 320 | + $fix = $phpcsFile->addFixableError( $error, $closeBracket, 'UseCloseBracketLine' ); |
|
| 321 | + if ( $fix === true ) { |
|
| 322 | + $phpcsFile->fixer->addNewlineBefore( $closeBracket ); |
|
| 323 | 323 | } |
| 324 | 324 | } |
| 325 | 325 | } |
@@ -327,95 +327,95 @@ discard block |
||
| 327 | 327 | }//end if |
| 328 | 328 | |
| 329 | 329 | // Each line between the parenthesis should be indented 4 spaces. |
| 330 | - $openBracket = $tokens[$stackPtr]['parenthesis_opener']; |
|
| 331 | - $lastLine = $tokens[$openBracket]['line']; |
|
| 332 | - for ($i = ($openBracket + 1); $i < $closeBracket; $i++) { |
|
| 333 | - if ($tokens[$i]['line'] !== $lastLine) { |
|
| 334 | - if ($i === $tokens[$stackPtr]['parenthesis_closer'] |
|
| 335 | - || ($tokens[$i]['code'] === T_WHITESPACE |
|
| 336 | - && (($i + 1) === $closeBracket |
|
| 337 | - || ($i + 1) === $tokens[$stackPtr]['parenthesis_closer'])) |
|
| 330 | + $openBracket = $tokens[ $stackPtr ][ 'parenthesis_opener' ]; |
|
| 331 | + $lastLine = $tokens[ $openBracket ][ 'line' ]; |
|
| 332 | + for ( $i = ( $openBracket + 1 ); $i < $closeBracket; $i++ ) { |
|
| 333 | + if ( $tokens[ $i ][ 'line' ] !== $lastLine ) { |
|
| 334 | + if ( $i === $tokens[ $stackPtr ][ 'parenthesis_closer' ] |
|
| 335 | + || ( $tokens[ $i ][ 'code' ] === T_WHITESPACE |
|
| 336 | + && ( ( $i + 1 ) === $closeBracket |
|
| 337 | + || ( $i + 1 ) === $tokens[ $stackPtr ][ 'parenthesis_closer' ] ) ) |
|
| 338 | 338 | ) { |
| 339 | 339 | // Closing braces need to be indented to the same level |
| 340 | 340 | // as the function. |
| 341 | 341 | $expectedIndent = $functionIndent; |
| 342 | 342 | } else { |
| 343 | - $expectedIndent = ($functionIndent + $this->indent); |
|
| 343 | + $expectedIndent = ( $functionIndent + $this->indent ); |
|
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | // We changed lines, so this should be a whitespace indent token. |
| 347 | - if ($tokens[$i]['code'] !== T_WHITESPACE) { |
|
| 347 | + if ( $tokens[ $i ][ 'code' ] !== T_WHITESPACE ) { |
|
| 348 | 348 | $foundIndent = 0; |
| 349 | 349 | } else { |
| 350 | - $foundIndent = strlen($tokens[$i]['content']); |
|
| 350 | + $foundIndent = strlen( $tokens[ $i ][ 'content' ] ); |
|
| 351 | 351 | } |
| 352 | 352 | |
| 353 | - if ($expectedIndent !== $foundIndent) { |
|
| 353 | + if ( $expectedIndent !== $foundIndent ) { |
|
| 354 | 354 | $error = 'Multi-line function declaration not indented correctly; expected %s spaces but found %s'; |
| 355 | 355 | $data = array( |
| 356 | 356 | $expectedIndent, |
| 357 | 357 | $foundIndent, |
| 358 | 358 | ); |
| 359 | 359 | |
| 360 | - $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data); |
|
| 361 | - if ($fix === true) { |
|
| 362 | - $spaces = str_repeat(' ', $expectedIndent); |
|
| 363 | - if ($foundIndent === 0) { |
|
| 364 | - $phpcsFile->fixer->addContentBefore($i, $spaces); |
|
| 360 | + $fix = $phpcsFile->addFixableError( $error, $i, 'Indent', $data ); |
|
| 361 | + if ( $fix === true ) { |
|
| 362 | + $spaces = str_repeat( ' ', $expectedIndent ); |
|
| 363 | + if ( $foundIndent === 0 ) { |
|
| 364 | + $phpcsFile->fixer->addContentBefore( $i, $spaces ); |
|
| 365 | 365 | } else { |
| 366 | - $phpcsFile->fixer->replaceToken($i, $spaces); |
|
| 366 | + $phpcsFile->fixer->replaceToken( $i, $spaces ); |
|
| 367 | 367 | } |
| 368 | 368 | } |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | - $lastLine = $tokens[$i]['line']; |
|
| 371 | + $lastLine = $tokens[ $i ][ 'line' ]; |
|
| 372 | 372 | }//end if |
| 373 | 373 | |
| 374 | - if ($tokens[$i]['code'] === T_ARRAY || $tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
| 374 | + if ( $tokens[ $i ][ 'code' ] === T_ARRAY || $tokens[ $i ][ 'code' ] === T_OPEN_SHORT_ARRAY ) { |
|
| 375 | 375 | // Skip arrays as they have their own indentation rules. |
| 376 | - if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
| 377 | - $i = $tokens[$i]['bracket_closer']; |
|
| 376 | + if ( $tokens[ $i ][ 'code' ] === T_OPEN_SHORT_ARRAY ) { |
|
| 377 | + $i = $tokens[ $i ][ 'bracket_closer' ]; |
|
| 378 | 378 | } else { |
| 379 | - $i = $tokens[$i]['parenthesis_closer']; |
|
| 379 | + $i = $tokens[ $i ][ 'parenthesis_closer' ]; |
|
| 380 | 380 | } |
| 381 | 381 | |
| 382 | - $lastLine = $tokens[$i]['line']; |
|
| 382 | + $lastLine = $tokens[ $i ][ 'line' ]; |
|
| 383 | 383 | continue; |
| 384 | 384 | } |
| 385 | 385 | }//end for |
| 386 | 386 | |
| 387 | - if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
|
| 387 | + if ( isset( $tokens[ $stackPtr ][ 'scope_opener' ] ) === false ) { |
|
| 388 | 388 | return; |
| 389 | 389 | } |
| 390 | 390 | |
| 391 | 391 | // The opening brace needs to be one space away from the closing parenthesis. |
| 392 | - $opener = $tokens[$stackPtr]['scope_opener']; |
|
| 393 | - if ($tokens[$opener]['line'] !== $tokens[$closeBracket]['line']) { |
|
| 392 | + $opener = $tokens[ $stackPtr ][ 'scope_opener' ]; |
|
| 393 | + if ( $tokens[ $opener ][ 'line' ] !== $tokens[ $closeBracket ][ 'line' ] ) { |
|
| 394 | 394 | $error = 'The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line'; |
| 395 | - $fix = $phpcsFile->addFixableError($error, $opener, 'NewlineBeforeOpenBrace'); |
|
| 396 | - if ($fix === true) { |
|
| 397 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($opener - 1), $closeBracket, true); |
|
| 395 | + $fix = $phpcsFile->addFixableError( $error, $opener, 'NewlineBeforeOpenBrace' ); |
|
| 396 | + if ( $fix === true ) { |
|
| 397 | + $prev = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $opener - 1 ), $closeBracket, true ); |
|
| 398 | 398 | $phpcsFile->fixer->beginChangeset(); |
| 399 | - $phpcsFile->fixer->addContent($prev, ' {'); |
|
| 400 | - $phpcsFile->fixer->replaceToken($opener, ''); |
|
| 399 | + $phpcsFile->fixer->addContent( $prev, ' {' ); |
|
| 400 | + $phpcsFile->fixer->replaceToken( $opener, '' ); |
|
| 401 | 401 | $phpcsFile->fixer->endChangeset(); |
| 402 | 402 | } |
| 403 | 403 | } else { |
| 404 | - $prev = $tokens[($opener - 1)]; |
|
| 405 | - if ($prev['code'] !== T_WHITESPACE) { |
|
| 404 | + $prev = $tokens[ ( $opener - 1 ) ]; |
|
| 405 | + if ( $prev[ 'code' ] !== T_WHITESPACE ) { |
|
| 406 | 406 | $length = 0; |
| 407 | 407 | } else { |
| 408 | - $length = strlen($prev['content']); |
|
| 408 | + $length = strlen( $prev[ 'content' ] ); |
|
| 409 | 409 | } |
| 410 | 410 | |
| 411 | - if ($length !== 1) { |
|
| 411 | + if ( $length !== 1 ) { |
|
| 412 | 412 | $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found %s spaces'; |
| 413 | - $fix = $phpcsFile->addFixableError($error, ($opener - 1), 'SpaceBeforeOpenBrace', array($length)); |
|
| 414 | - if ($fix === true) { |
|
| 415 | - if ($length === 0) { |
|
| 416 | - $phpcsFile->fixer->addContentBefore($opener, ' '); |
|
| 413 | + $fix = $phpcsFile->addFixableError( $error, ( $opener - 1 ), 'SpaceBeforeOpenBrace', array( $length ) ); |
|
| 414 | + if ( $fix === true ) { |
|
| 415 | + if ( $length === 0 ) { |
|
| 416 | + $phpcsFile->fixer->addContentBefore( $opener, ' ' ); |
|
| 417 | 417 | } else { |
| 418 | - $phpcsFile->fixer->replaceToken(($opener - 1), ' '); |
|
| 418 | + $phpcsFile->fixer->replaceToken( ( $opener - 1 ), ' ' ); |
|
| 419 | 419 | } |
| 420 | 420 | } |
| 421 | 421 | |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * @version Release: @package_version@ |
| 26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 27 | 27 | */ |
| 28 | -class PEAR_Sniffs_Functions_FunctionDeclarationSniff implements PHP_CodeSniffer_Sniff |
|
| 29 | -{ |
|
| 28 | +class PEAR_Sniffs_Functions_FunctionDeclarationSniff implements PHP_CodeSniffer_Sniff { |
|
| 30 | 29 | |
| 31 | 30 | /** |
| 32 | 31 | * The number of spaces code should be indented. |
@@ -41,8 +40,7 @@ discard block |
||
| 41 | 40 | * |
| 42 | 41 | * @return array |
| 43 | 42 | */ |
| 44 | - public function register() |
|
| 45 | - { |
|
| 43 | + public function register() { |
|
| 46 | 44 | return array( |
| 47 | 45 | T_FUNCTION, |
| 48 | 46 | T_CLOSURE, |
@@ -60,8 +58,7 @@ discard block |
||
| 60 | 58 | * |
| 61 | 59 | * @return void |
| 62 | 60 | */ |
| 63 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 64 | - { |
|
| 61 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 65 | 62 | $tokens = $phpcsFile->getTokens(); |
| 66 | 63 | |
| 67 | 64 | if (isset($tokens[$stackPtr]['parenthesis_opener']) === false |
@@ -189,8 +186,7 @@ discard block |
||
| 189 | 186 | * |
| 190 | 187 | * @return void |
| 191 | 188 | */ |
| 192 | - public function isMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) |
|
| 193 | - { |
|
| 189 | + public function isMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) { |
|
| 194 | 190 | $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
| 195 | 191 | if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) { |
| 196 | 192 | return true; |
@@ -228,8 +224,7 @@ discard block |
||
| 228 | 224 | * |
| 229 | 225 | * @return void |
| 230 | 226 | */ |
| 231 | - public function processSingleLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
|
| 232 | - { |
|
| 227 | + public function processSingleLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) { |
|
| 233 | 228 | if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
| 234 | 229 | if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff', true) === false) { |
| 235 | 230 | throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff not found'); |
@@ -261,8 +256,7 @@ discard block |
||
| 261 | 256 | * |
| 262 | 257 | * @return void |
| 263 | 258 | */ |
| 264 | - public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
|
| 265 | - { |
|
| 259 | + public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) { |
|
| 266 | 260 | // We need to work out how far indented the function |
| 267 | 261 | // declaration itself is, so we can work out how far to |
| 268 | 262 | // indent parameters. |
@@ -35,7 +35,7 @@ |
||
| 35 | 35 | /** |
| 36 | 36 | * Returns an array of tokens this test wants to listen for. |
| 37 | 37 | * |
| 38 | - * @return array |
|
| 38 | + * @return integer[] |
|
| 39 | 39 | */ |
| 40 | 40 | public function register() |
| 41 | 41 | { |
@@ -32,81 +32,81 @@ |
||
| 32 | 32 | { |
| 33 | 33 | |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Returns an array of tokens this test wants to listen for. |
|
| 37 | - * |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function register() |
|
| 41 | - { |
|
| 42 | - return array( |
|
| 43 | - T_CLASS, |
|
| 44 | - T_INTERFACE, |
|
| 45 | - ); |
|
| 35 | + /** |
|
| 36 | + * Returns an array of tokens this test wants to listen for. |
|
| 37 | + * |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function register() |
|
| 41 | + { |
|
| 42 | + return array( |
|
| 43 | + T_CLASS, |
|
| 44 | + T_INTERFACE, |
|
| 45 | + ); |
|
| 46 | 46 | |
| 47 | - }//end register() |
|
| 47 | + }//end register() |
|
| 48 | 48 | |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Processes this test, when one of its tokens is encountered. |
|
| 52 | - * |
|
| 53 | - * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. |
|
| 54 | - * @param int $stackPtr The position of the current token |
|
| 55 | - * in the stack passed in $tokens. |
|
| 56 | - * |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 60 | - { |
|
| 61 | - $tokens = $phpcsFile->getTokens(); |
|
| 50 | + /** |
|
| 51 | + * Processes this test, when one of its tokens is encountered. |
|
| 52 | + * |
|
| 53 | + * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. |
|
| 54 | + * @param int $stackPtr The position of the current token |
|
| 55 | + * in the stack passed in $tokens. |
|
| 56 | + * |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 60 | + { |
|
| 61 | + $tokens = $phpcsFile->getTokens(); |
|
| 62 | 62 | |
| 63 | - $className = $phpcsFile->findNext(T_STRING, $stackPtr); |
|
| 64 | - $name = trim($tokens[$className]['content']); |
|
| 65 | - $errorData = array(ucfirst($tokens[$stackPtr]['content'])); |
|
| 63 | + $className = $phpcsFile->findNext(T_STRING, $stackPtr); |
|
| 64 | + $name = trim($tokens[$className]['content']); |
|
| 65 | + $errorData = array(ucfirst($tokens[$stackPtr]['content'])); |
|
| 66 | 66 | |
| 67 | - // Make sure the first letter is a capital. |
|
| 68 | - if (preg_match('|^[A-Z]|', $name) === 0) { |
|
| 69 | - $error = '%s name must begin with a capital letter'; |
|
| 70 | - $phpcsFile->addError($error, $stackPtr, 'StartWithCapital', $errorData); |
|
| 71 | - } |
|
| 67 | + // Make sure the first letter is a capital. |
|
| 68 | + if (preg_match('|^[A-Z]|', $name) === 0) { |
|
| 69 | + $error = '%s name must begin with a capital letter'; |
|
| 70 | + $phpcsFile->addError($error, $stackPtr, 'StartWithCapital', $errorData); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - // Check that each new word starts with a capital as well, but don't |
|
| 74 | - // check the first word, as it is checked above. |
|
| 75 | - $validName = true; |
|
| 76 | - $nameBits = explode('_', $name); |
|
| 77 | - $firstBit = array_shift($nameBits); |
|
| 78 | - foreach ($nameBits as $bit) { |
|
| 79 | - if ($bit === '' || $bit{0} !== strtoupper($bit{0})) { |
|
| 80 | - $validName = false; |
|
| 81 | - break; |
|
| 82 | - } |
|
| 83 | - } |
|
| 73 | + // Check that each new word starts with a capital as well, but don't |
|
| 74 | + // check the first word, as it is checked above. |
|
| 75 | + $validName = true; |
|
| 76 | + $nameBits = explode('_', $name); |
|
| 77 | + $firstBit = array_shift($nameBits); |
|
| 78 | + foreach ($nameBits as $bit) { |
|
| 79 | + if ($bit === '' || $bit{0} !== strtoupper($bit{0})) { |
|
| 80 | + $validName = false; |
|
| 81 | + break; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - if ($validName === false) { |
|
| 86 | - // Strip underscores because they cause the suggested name |
|
| 87 | - // to be incorrect. |
|
| 88 | - $nameBits = explode('_', trim($name, '_')); |
|
| 89 | - $firstBit = array_shift($nameBits); |
|
| 90 | - if ($firstBit === '') { |
|
| 91 | - $error = '%s name is not valid'; |
|
| 92 | - $phpcsFile->addError($error, $stackPtr, 'Invalid', $errorData); |
|
| 93 | - } else { |
|
| 94 | - $newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_'; |
|
| 95 | - foreach ($nameBits as $bit) { |
|
| 96 | - if ($bit !== '') { |
|
| 97 | - $newName .= strtoupper($bit{0}).substr($bit, 1).'_'; |
|
| 98 | - } |
|
| 99 | - } |
|
| 85 | + if ($validName === false) { |
|
| 86 | + // Strip underscores because they cause the suggested name |
|
| 87 | + // to be incorrect. |
|
| 88 | + $nameBits = explode('_', trim($name, '_')); |
|
| 89 | + $firstBit = array_shift($nameBits); |
|
| 90 | + if ($firstBit === '') { |
|
| 91 | + $error = '%s name is not valid'; |
|
| 92 | + $phpcsFile->addError($error, $stackPtr, 'Invalid', $errorData); |
|
| 93 | + } else { |
|
| 94 | + $newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_'; |
|
| 95 | + foreach ($nameBits as $bit) { |
|
| 96 | + if ($bit !== '') { |
|
| 97 | + $newName .= strtoupper($bit{0}).substr($bit, 1).'_'; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - $newName = rtrim($newName, '_'); |
|
| 102 | - $error = '%s name is not valid; consider %s instead'; |
|
| 103 | - $data = $errorData; |
|
| 104 | - $data[] = $newName; |
|
| 105 | - $phpcsFile->addError($error, $stackPtr, 'Invalid', $data); |
|
| 106 | - } |
|
| 107 | - }//end if |
|
| 101 | + $newName = rtrim($newName, '_'); |
|
| 102 | + $error = '%s name is not valid; consider %s instead'; |
|
| 103 | + $data = $errorData; |
|
| 104 | + $data[] = $newName; |
|
| 105 | + $phpcsFile->addError($error, $stackPtr, 'Invalid', $data); |
|
| 106 | + } |
|
| 107 | + }//end if |
|
| 108 | 108 | |
| 109 | - }//end process() |
|
| 109 | + }//end process() |
|
| 110 | 110 | |
| 111 | 111 | |
| 112 | 112 | }//end class |
@@ -56,53 +56,53 @@ |
||
| 56 | 56 | * |
| 57 | 57 | * @return void |
| 58 | 58 | */ |
| 59 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 59 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 60 | 60 | { |
| 61 | 61 | $tokens = $phpcsFile->getTokens(); |
| 62 | 62 | |
| 63 | - $className = $phpcsFile->findNext(T_STRING, $stackPtr); |
|
| 64 | - $name = trim($tokens[$className]['content']); |
|
| 65 | - $errorData = array(ucfirst($tokens[$stackPtr]['content'])); |
|
| 63 | + $className = $phpcsFile->findNext( T_STRING, $stackPtr ); |
|
| 64 | + $name = trim( $tokens[ $className ][ 'content' ] ); |
|
| 65 | + $errorData = array( ucfirst( $tokens[ $stackPtr ][ 'content' ] ) ); |
|
| 66 | 66 | |
| 67 | 67 | // Make sure the first letter is a capital. |
| 68 | - if (preg_match('|^[A-Z]|', $name) === 0) { |
|
| 68 | + if ( preg_match( '|^[A-Z]|', $name ) === 0 ) { |
|
| 69 | 69 | $error = '%s name must begin with a capital letter'; |
| 70 | - $phpcsFile->addError($error, $stackPtr, 'StartWithCapital', $errorData); |
|
| 70 | + $phpcsFile->addError( $error, $stackPtr, 'StartWithCapital', $errorData ); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | // Check that each new word starts with a capital as well, but don't |
| 74 | 74 | // check the first word, as it is checked above. |
| 75 | 75 | $validName = true; |
| 76 | - $nameBits = explode('_', $name); |
|
| 77 | - $firstBit = array_shift($nameBits); |
|
| 78 | - foreach ($nameBits as $bit) { |
|
| 79 | - if ($bit === '' || $bit{0} !== strtoupper($bit{0})) { |
|
| 76 | + $nameBits = explode( '_', $name ); |
|
| 77 | + $firstBit = array_shift( $nameBits ); |
|
| 78 | + foreach ( $nameBits as $bit ) { |
|
| 79 | + if ( $bit === '' || $bit{0} !== strtoupper( $bit{0}) ) { |
|
| 80 | 80 | $validName = false; |
| 81 | 81 | break; |
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - if ($validName === false) { |
|
| 85 | + if ( $validName === false ) { |
|
| 86 | 86 | // Strip underscores because they cause the suggested name |
| 87 | 87 | // to be incorrect. |
| 88 | - $nameBits = explode('_', trim($name, '_')); |
|
| 89 | - $firstBit = array_shift($nameBits); |
|
| 90 | - if ($firstBit === '') { |
|
| 88 | + $nameBits = explode( '_', trim( $name, '_' ) ); |
|
| 89 | + $firstBit = array_shift( $nameBits ); |
|
| 90 | + if ( $firstBit === '' ) { |
|
| 91 | 91 | $error = '%s name is not valid'; |
| 92 | - $phpcsFile->addError($error, $stackPtr, 'Invalid', $errorData); |
|
| 92 | + $phpcsFile->addError( $error, $stackPtr, 'Invalid', $errorData ); |
|
| 93 | 93 | } else { |
| 94 | - $newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_'; |
|
| 95 | - foreach ($nameBits as $bit) { |
|
| 96 | - if ($bit !== '') { |
|
| 97 | - $newName .= strtoupper($bit{0}).substr($bit, 1).'_'; |
|
| 94 | + $newName = strtoupper( $firstBit{0}) . substr( $firstBit, 1 ) . '_'; |
|
| 95 | + foreach ( $nameBits as $bit ) { |
|
| 96 | + if ( $bit !== '' ) { |
|
| 97 | + $newName .= strtoupper( $bit{0}) . substr( $bit, 1 ) . '_'; |
|
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - $newName = rtrim($newName, '_'); |
|
| 101 | + $newName = rtrim( $newName, '_' ); |
|
| 102 | 102 | $error = '%s name is not valid; consider %s instead'; |
| 103 | 103 | $data = $errorData; |
| 104 | - $data[] = $newName; |
|
| 105 | - $phpcsFile->addError($error, $stackPtr, 'Invalid', $data); |
|
| 104 | + $data[ ] = $newName; |
|
| 105 | + $phpcsFile->addError( $error, $stackPtr, 'Invalid', $data ); |
|
| 106 | 106 | } |
| 107 | 107 | }//end if |
| 108 | 108 | |
@@ -28,8 +28,7 @@ discard block |
||
| 28 | 28 | * @version Release: @package_version@ |
| 29 | 29 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 30 | 30 | */ |
| 31 | -class PEAR_Sniffs_NamingConventions_ValidClassNameSniff implements PHP_CodeSniffer_Sniff |
|
| 32 | -{ |
|
| 31 | +class PEAR_Sniffs_NamingConventions_ValidClassNameSniff implements PHP_CodeSniffer_Sniff { |
|
| 33 | 32 | |
| 34 | 33 | |
| 35 | 34 | /** |
@@ -37,8 +36,7 @@ discard block |
||
| 37 | 36 | * |
| 38 | 37 | * @return array |
| 39 | 38 | */ |
| 40 | - public function register() |
|
| 41 | - { |
|
| 39 | + public function register() { |
|
| 42 | 40 | return array( |
| 43 | 41 | T_CLASS, |
| 44 | 42 | T_INTERFACE, |
@@ -56,8 +54,7 @@ discard block |
||
| 56 | 54 | * |
| 57 | 55 | * @return void |
| 58 | 56 | */ |
| 59 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 60 | - { |
|
| 57 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 61 | 58 | $tokens = $phpcsFile->getTokens(); |
| 62 | 59 | |
| 63 | 60 | $className = $phpcsFile->findNext(T_STRING, $stackPtr); |
@@ -39,7 +39,7 @@ |
||
| 39 | 39 | /** |
| 40 | 40 | * Returns an array of tokens this test wants to listen for. |
| 41 | 41 | * |
| 42 | - * @return array |
|
| 42 | + * @return integer[] |
|
| 43 | 43 | */ |
| 44 | 44 | public function register() |
| 45 | 45 | { |
@@ -28,221 +28,221 @@ |
||
| 28 | 28 | class PSR2_Sniffs_ControlStructures_SwitchDeclarationSniff implements PHP_CodeSniffer_Sniff |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The number of spaces code should be indented. |
|
| 33 | - * |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - public $indent = 4; |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Returns an array of tokens this test wants to listen for. |
|
| 41 | - * |
|
| 42 | - * @return array |
|
| 43 | - */ |
|
| 44 | - public function register() |
|
| 45 | - { |
|
| 46 | - return array(T_SWITCH); |
|
| 47 | - |
|
| 48 | - }//end register() |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Processes this test, when one of its tokens is encountered. |
|
| 53 | - * |
|
| 54 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 55 | - * @param int $stackPtr The position of the current token in the |
|
| 56 | - * stack passed in $tokens. |
|
| 57 | - * |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 61 | - { |
|
| 62 | - $tokens = $phpcsFile->getTokens(); |
|
| 63 | - |
|
| 64 | - // We can't process SWITCH statements unless we know where they start and end. |
|
| 65 | - if (isset($tokens[$stackPtr]['scope_opener']) === false |
|
| 66 | - || isset($tokens[$stackPtr]['scope_closer']) === false |
|
| 67 | - ) { |
|
| 68 | - return; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $switch = $tokens[$stackPtr]; |
|
| 72 | - $nextCase = $stackPtr; |
|
| 73 | - $caseAlignment = ($switch['column'] + $this->indent); |
|
| 74 | - $caseCount = 0; |
|
| 75 | - $foundDefault = false; |
|
| 76 | - |
|
| 77 | - while (($nextCase = $this->_findNextCase($phpcsFile, ($nextCase + 1), $switch['scope_closer'])) !== false) { |
|
| 78 | - if ($tokens[$nextCase]['code'] === T_DEFAULT) { |
|
| 79 | - $type = 'default'; |
|
| 80 | - $foundDefault = true; |
|
| 81 | - } else { |
|
| 82 | - $type = 'case'; |
|
| 83 | - $caseCount++; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) { |
|
| 87 | - $expected = strtolower($tokens[$nextCase]['content']); |
|
| 88 | - $error = strtoupper($type).' keyword must be lowercase; expected "%s" but found "%s"'; |
|
| 89 | - $data = array( |
|
| 90 | - $expected, |
|
| 91 | - $tokens[$nextCase]['content'], |
|
| 92 | - ); |
|
| 93 | - |
|
| 94 | - $fix = $phpcsFile->addFixableError($error, $nextCase, $type.'NotLower', $data); |
|
| 95 | - if ($fix === true) { |
|
| 96 | - $phpcsFile->fixer->replaceToken($nextCase, $expected); |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - if ($type === 'case' |
|
| 101 | - && ($tokens[($nextCase + 1)]['code'] !== T_WHITESPACE |
|
| 102 | - || $tokens[($nextCase + 1)]['content'] !== ' ') |
|
| 103 | - ) { |
|
| 104 | - $error = 'CASE keyword must be followed by a single space'; |
|
| 105 | - $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase'); |
|
| 106 | - if ($fix === true) { |
|
| 107 | - if ($tokens[($nextCase + 1)]['code'] !== T_WHITESPACE) { |
|
| 108 | - $phpcsFile->fixer->addContent($nextCase, ' '); |
|
| 109 | - } else { |
|
| 110 | - $phpcsFile->fixer->replaceToken(($nextCase + 1), ' '); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $opener = $tokens[$nextCase]['scope_opener']; |
|
| 116 | - $nextCloser = $tokens[$nextCase]['scope_closer']; |
|
| 117 | - if ($tokens[$opener]['code'] === T_COLON) { |
|
| 118 | - if ($tokens[($opener - 1)]['code'] === T_WHITESPACE) { |
|
| 119 | - $error = 'There must be no space before the colon in a '.strtoupper($type).' statement'; |
|
| 120 | - $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon'.strtoupper($type)); |
|
| 121 | - if ($fix === true) { |
|
| 122 | - $phpcsFile->fixer->replaceToken(($opener - 1), ''); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), null, true); |
|
| 127 | - if ($tokens[$next]['line'] === $tokens[$opener]['line'] |
|
| 128 | - && $tokens[$next]['code'] === T_COMMENT |
|
| 129 | - ) { |
|
| 130 | - // Skip comments on the same line. |
|
| 131 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if ($tokens[$next]['line'] !== ($tokens[$opener]['line'] + 1)) { |
|
| 135 | - $error = 'The '.strtoupper($type).' body must start on the line following the statement'; |
|
| 136 | - $fix = $phpcsFile->addFixableError($error, $nextCase, 'BodyOnNextLine'.strtoupper($type)); |
|
| 137 | - if ($fix === true) { |
|
| 138 | - if ($tokens[$next]['line'] === $tokens[$opener]['line']) { |
|
| 139 | - $padding = str_repeat(' ', ($caseAlignment + $this->indent - 1)); |
|
| 140 | - $phpcsFile->fixer->addContentBefore($next, $phpcsFile->eolChar.$padding); |
|
| 141 | - } else { |
|
| 142 | - $phpcsFile->fixer->beginChangeset(); |
|
| 143 | - for ($i = ($opener + 1); $i < $next; $i++) { |
|
| 144 | - if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
| 145 | - break; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $phpcsFile->fixer->addNewLineBefore($i); |
|
| 152 | - $phpcsFile->fixer->endChangeset(); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - }//end if |
|
| 156 | - |
|
| 157 | - if ($tokens[$nextCloser]['scope_condition'] === $nextCase) { |
|
| 158 | - // Only need to check some things once, even if the |
|
| 159 | - // closer is shared between multiple case statements, or even |
|
| 160 | - // the default case. |
|
| 161 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($nextCloser - 1), $nextCase, true); |
|
| 162 | - if ($tokens[$prev]['line'] === $tokens[$nextCloser]['line']) { |
|
| 163 | - $error = 'Terminating statement must be on a line by itself'; |
|
| 164 | - $fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakNotNewLine'); |
|
| 165 | - if ($fix === true) { |
|
| 166 | - $phpcsFile->fixer->addNewLine($prev); |
|
| 167 | - $phpcsFile->fixer->replaceToken($nextCloser, trim($tokens[$nextCloser]['content'])); |
|
| 168 | - } |
|
| 169 | - } else { |
|
| 170 | - $diff = ($caseAlignment + $this->indent - $tokens[$nextCloser]['column']); |
|
| 171 | - if ($diff !== 0) { |
|
| 172 | - $error = 'Terminating statement must be indented to the same level as the CASE body'; |
|
| 173 | - $fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent'); |
|
| 174 | - if ($fix === true) { |
|
| 175 | - if ($diff > 0) { |
|
| 176 | - $phpcsFile->fixer->addContentBefore($nextCloser, str_repeat(' ', $diff)); |
|
| 177 | - } else { |
|
| 178 | - $phpcsFile->fixer->substrToken(($nextCloser - 1), 0, $diff); |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - }//end if |
|
| 183 | - }//end if |
|
| 184 | - } else { |
|
| 185 | - $error = strtoupper($type).' statements must be defined using a colon'; |
|
| 186 | - $phpcsFile->addError($error, $nextCase, 'WrongOpener'.$type); |
|
| 187 | - }//end if |
|
| 188 | - |
|
| 189 | - // We only want cases from here on in. |
|
| 190 | - if ($type !== 'case') { |
|
| 191 | - continue; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - $nextCode = $phpcsFile->findNext( |
|
| 195 | - T_WHITESPACE, |
|
| 196 | - ($tokens[$nextCase]['scope_opener'] + 1), |
|
| 197 | - $nextCloser, |
|
| 198 | - true |
|
| 199 | - ); |
|
| 200 | - |
|
| 201 | - if ($tokens[$nextCode]['code'] !== T_CASE && $tokens[$nextCode]['code'] !== T_DEFAULT) { |
|
| 202 | - // This case statement has content. If the next case or default comes |
|
| 203 | - // before the closer, it means we dont have a terminating statement |
|
| 204 | - // and instead need a comment. |
|
| 205 | - $nextCode = $this->_findNextCase($phpcsFile, ($tokens[$nextCase]['scope_opener'] + 1), $nextCloser); |
|
| 206 | - if ($nextCode !== false) { |
|
| 207 | - $prevCode = $phpcsFile->findPrevious(T_WHITESPACE, ($nextCode - 1), $nextCase, true); |
|
| 208 | - if ($tokens[$prevCode]['code'] !== T_COMMENT) { |
|
| 209 | - $error = 'There must be a comment when fall-through is intentional in a non-empty case body'; |
|
| 210 | - $phpcsFile->addError($error, $nextCase, 'TerminatingComment'); |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - }//end while |
|
| 215 | - |
|
| 216 | - }//end process() |
|
| 217 | - |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Find the next CASE or DEFAULT statement from a point in the file. |
|
| 221 | - * |
|
| 222 | - * Note that nested switches are ignored. |
|
| 223 | - * |
|
| 224 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 225 | - * @param int $stackPtr The position to start looking at. |
|
| 226 | - * @param int $end The position to stop looking at. |
|
| 227 | - * |
|
| 228 | - * @return int | bool |
|
| 229 | - */ |
|
| 230 | - private function _findNextCase(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $end) |
|
| 231 | - { |
|
| 232 | - $tokens = $phpcsFile->getTokens(); |
|
| 233 | - while (($stackPtr = $phpcsFile->findNext(array(T_CASE, T_DEFAULT, T_SWITCH), $stackPtr, $end)) !== false) { |
|
| 234 | - // Skip nested SWITCH statements; they are handled on their own. |
|
| 235 | - if ($tokens[$stackPtr]['code'] === T_SWITCH) { |
|
| 236 | - $stackPtr = $tokens[$stackPtr]['scope_closer']; |
|
| 237 | - continue; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - break; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - return $stackPtr; |
|
| 244 | - |
|
| 245 | - }//end _findNextCase() |
|
| 31 | + /** |
|
| 32 | + * The number of spaces code should be indented. |
|
| 33 | + * |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + public $indent = 4; |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Returns an array of tokens this test wants to listen for. |
|
| 41 | + * |
|
| 42 | + * @return array |
|
| 43 | + */ |
|
| 44 | + public function register() |
|
| 45 | + { |
|
| 46 | + return array(T_SWITCH); |
|
| 47 | + |
|
| 48 | + }//end register() |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Processes this test, when one of its tokens is encountered. |
|
| 53 | + * |
|
| 54 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 55 | + * @param int $stackPtr The position of the current token in the |
|
| 56 | + * stack passed in $tokens. |
|
| 57 | + * |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 61 | + { |
|
| 62 | + $tokens = $phpcsFile->getTokens(); |
|
| 63 | + |
|
| 64 | + // We can't process SWITCH statements unless we know where they start and end. |
|
| 65 | + if (isset($tokens[$stackPtr]['scope_opener']) === false |
|
| 66 | + || isset($tokens[$stackPtr]['scope_closer']) === false |
|
| 67 | + ) { |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $switch = $tokens[$stackPtr]; |
|
| 72 | + $nextCase = $stackPtr; |
|
| 73 | + $caseAlignment = ($switch['column'] + $this->indent); |
|
| 74 | + $caseCount = 0; |
|
| 75 | + $foundDefault = false; |
|
| 76 | + |
|
| 77 | + while (($nextCase = $this->_findNextCase($phpcsFile, ($nextCase + 1), $switch['scope_closer'])) !== false) { |
|
| 78 | + if ($tokens[$nextCase]['code'] === T_DEFAULT) { |
|
| 79 | + $type = 'default'; |
|
| 80 | + $foundDefault = true; |
|
| 81 | + } else { |
|
| 82 | + $type = 'case'; |
|
| 83 | + $caseCount++; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) { |
|
| 87 | + $expected = strtolower($tokens[$nextCase]['content']); |
|
| 88 | + $error = strtoupper($type).' keyword must be lowercase; expected "%s" but found "%s"'; |
|
| 89 | + $data = array( |
|
| 90 | + $expected, |
|
| 91 | + $tokens[$nextCase]['content'], |
|
| 92 | + ); |
|
| 93 | + |
|
| 94 | + $fix = $phpcsFile->addFixableError($error, $nextCase, $type.'NotLower', $data); |
|
| 95 | + if ($fix === true) { |
|
| 96 | + $phpcsFile->fixer->replaceToken($nextCase, $expected); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + if ($type === 'case' |
|
| 101 | + && ($tokens[($nextCase + 1)]['code'] !== T_WHITESPACE |
|
| 102 | + || $tokens[($nextCase + 1)]['content'] !== ' ') |
|
| 103 | + ) { |
|
| 104 | + $error = 'CASE keyword must be followed by a single space'; |
|
| 105 | + $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase'); |
|
| 106 | + if ($fix === true) { |
|
| 107 | + if ($tokens[($nextCase + 1)]['code'] !== T_WHITESPACE) { |
|
| 108 | + $phpcsFile->fixer->addContent($nextCase, ' '); |
|
| 109 | + } else { |
|
| 110 | + $phpcsFile->fixer->replaceToken(($nextCase + 1), ' '); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $opener = $tokens[$nextCase]['scope_opener']; |
|
| 116 | + $nextCloser = $tokens[$nextCase]['scope_closer']; |
|
| 117 | + if ($tokens[$opener]['code'] === T_COLON) { |
|
| 118 | + if ($tokens[($opener - 1)]['code'] === T_WHITESPACE) { |
|
| 119 | + $error = 'There must be no space before the colon in a '.strtoupper($type).' statement'; |
|
| 120 | + $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon'.strtoupper($type)); |
|
| 121 | + if ($fix === true) { |
|
| 122 | + $phpcsFile->fixer->replaceToken(($opener - 1), ''); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), null, true); |
|
| 127 | + if ($tokens[$next]['line'] === $tokens[$opener]['line'] |
|
| 128 | + && $tokens[$next]['code'] === T_COMMENT |
|
| 129 | + ) { |
|
| 130 | + // Skip comments on the same line. |
|
| 131 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if ($tokens[$next]['line'] !== ($tokens[$opener]['line'] + 1)) { |
|
| 135 | + $error = 'The '.strtoupper($type).' body must start on the line following the statement'; |
|
| 136 | + $fix = $phpcsFile->addFixableError($error, $nextCase, 'BodyOnNextLine'.strtoupper($type)); |
|
| 137 | + if ($fix === true) { |
|
| 138 | + if ($tokens[$next]['line'] === $tokens[$opener]['line']) { |
|
| 139 | + $padding = str_repeat(' ', ($caseAlignment + $this->indent - 1)); |
|
| 140 | + $phpcsFile->fixer->addContentBefore($next, $phpcsFile->eolChar.$padding); |
|
| 141 | + } else { |
|
| 142 | + $phpcsFile->fixer->beginChangeset(); |
|
| 143 | + for ($i = ($opener + 1); $i < $next; $i++) { |
|
| 144 | + if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
| 145 | + break; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $phpcsFile->fixer->addNewLineBefore($i); |
|
| 152 | + $phpcsFile->fixer->endChangeset(); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + }//end if |
|
| 156 | + |
|
| 157 | + if ($tokens[$nextCloser]['scope_condition'] === $nextCase) { |
|
| 158 | + // Only need to check some things once, even if the |
|
| 159 | + // closer is shared between multiple case statements, or even |
|
| 160 | + // the default case. |
|
| 161 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($nextCloser - 1), $nextCase, true); |
|
| 162 | + if ($tokens[$prev]['line'] === $tokens[$nextCloser]['line']) { |
|
| 163 | + $error = 'Terminating statement must be on a line by itself'; |
|
| 164 | + $fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakNotNewLine'); |
|
| 165 | + if ($fix === true) { |
|
| 166 | + $phpcsFile->fixer->addNewLine($prev); |
|
| 167 | + $phpcsFile->fixer->replaceToken($nextCloser, trim($tokens[$nextCloser]['content'])); |
|
| 168 | + } |
|
| 169 | + } else { |
|
| 170 | + $diff = ($caseAlignment + $this->indent - $tokens[$nextCloser]['column']); |
|
| 171 | + if ($diff !== 0) { |
|
| 172 | + $error = 'Terminating statement must be indented to the same level as the CASE body'; |
|
| 173 | + $fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent'); |
|
| 174 | + if ($fix === true) { |
|
| 175 | + if ($diff > 0) { |
|
| 176 | + $phpcsFile->fixer->addContentBefore($nextCloser, str_repeat(' ', $diff)); |
|
| 177 | + } else { |
|
| 178 | + $phpcsFile->fixer->substrToken(($nextCloser - 1), 0, $diff); |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + }//end if |
|
| 183 | + }//end if |
|
| 184 | + } else { |
|
| 185 | + $error = strtoupper($type).' statements must be defined using a colon'; |
|
| 186 | + $phpcsFile->addError($error, $nextCase, 'WrongOpener'.$type); |
|
| 187 | + }//end if |
|
| 188 | + |
|
| 189 | + // We only want cases from here on in. |
|
| 190 | + if ($type !== 'case') { |
|
| 191 | + continue; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + $nextCode = $phpcsFile->findNext( |
|
| 195 | + T_WHITESPACE, |
|
| 196 | + ($tokens[$nextCase]['scope_opener'] + 1), |
|
| 197 | + $nextCloser, |
|
| 198 | + true |
|
| 199 | + ); |
|
| 200 | + |
|
| 201 | + if ($tokens[$nextCode]['code'] !== T_CASE && $tokens[$nextCode]['code'] !== T_DEFAULT) { |
|
| 202 | + // This case statement has content. If the next case or default comes |
|
| 203 | + // before the closer, it means we dont have a terminating statement |
|
| 204 | + // and instead need a comment. |
|
| 205 | + $nextCode = $this->_findNextCase($phpcsFile, ($tokens[$nextCase]['scope_opener'] + 1), $nextCloser); |
|
| 206 | + if ($nextCode !== false) { |
|
| 207 | + $prevCode = $phpcsFile->findPrevious(T_WHITESPACE, ($nextCode - 1), $nextCase, true); |
|
| 208 | + if ($tokens[$prevCode]['code'] !== T_COMMENT) { |
|
| 209 | + $error = 'There must be a comment when fall-through is intentional in a non-empty case body'; |
|
| 210 | + $phpcsFile->addError($error, $nextCase, 'TerminatingComment'); |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + }//end while |
|
| 215 | + |
|
| 216 | + }//end process() |
|
| 217 | + |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Find the next CASE or DEFAULT statement from a point in the file. |
|
| 221 | + * |
|
| 222 | + * Note that nested switches are ignored. |
|
| 223 | + * |
|
| 224 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 225 | + * @param int $stackPtr The position to start looking at. |
|
| 226 | + * @param int $end The position to stop looking at. |
|
| 227 | + * |
|
| 228 | + * @return int | bool |
|
| 229 | + */ |
|
| 230 | + private function _findNextCase(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $end) |
|
| 231 | + { |
|
| 232 | + $tokens = $phpcsFile->getTokens(); |
|
| 233 | + while (($stackPtr = $phpcsFile->findNext(array(T_CASE, T_DEFAULT, T_SWITCH), $stackPtr, $end)) !== false) { |
|
| 234 | + // Skip nested SWITCH statements; they are handled on their own. |
|
| 235 | + if ($tokens[$stackPtr]['code'] === T_SWITCH) { |
|
| 236 | + $stackPtr = $tokens[$stackPtr]['scope_closer']; |
|
| 237 | + continue; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + break; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + return $stackPtr; |
|
| 244 | + |
|
| 245 | + }//end _findNextCase() |
|
| 246 | 246 | |
| 247 | 247 | |
| 248 | 248 | }//end class |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function register() |
| 45 | 45 | { |
| 46 | - return array(T_SWITCH); |
|
| 46 | + return array( T_SWITCH ); |
|
| 47 | 47 | |
| 48 | 48 | }//end register() |
| 49 | 49 | |
@@ -57,25 +57,25 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return void |
| 59 | 59 | */ |
| 60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 60 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 61 | 61 | { |
| 62 | 62 | $tokens = $phpcsFile->getTokens(); |
| 63 | 63 | |
| 64 | 64 | // We can't process SWITCH statements unless we know where they start and end. |
| 65 | - if (isset($tokens[$stackPtr]['scope_opener']) === false |
|
| 66 | - || isset($tokens[$stackPtr]['scope_closer']) === false |
|
| 65 | + if ( isset( $tokens[ $stackPtr ][ 'scope_opener' ] ) === false |
|
| 66 | + || isset( $tokens[ $stackPtr ][ 'scope_closer' ] ) === false |
|
| 67 | 67 | ) { |
| 68 | 68 | return; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - $switch = $tokens[$stackPtr]; |
|
| 71 | + $switch = $tokens[ $stackPtr ]; |
|
| 72 | 72 | $nextCase = $stackPtr; |
| 73 | - $caseAlignment = ($switch['column'] + $this->indent); |
|
| 73 | + $caseAlignment = ( $switch[ 'column' ] + $this->indent ); |
|
| 74 | 74 | $caseCount = 0; |
| 75 | 75 | $foundDefault = false; |
| 76 | 76 | |
| 77 | - while (($nextCase = $this->_findNextCase($phpcsFile, ($nextCase + 1), $switch['scope_closer'])) !== false) { |
|
| 78 | - if ($tokens[$nextCase]['code'] === T_DEFAULT) { |
|
| 77 | + while ( ( $nextCase = $this->_findNextCase( $phpcsFile, ( $nextCase + 1 ), $switch[ 'scope_closer' ] ) ) !== false ) { |
|
| 78 | + if ( $tokens[ $nextCase ][ 'code' ] === T_DEFAULT ) { |
|
| 79 | 79 | $type = 'default'; |
| 80 | 80 | $foundDefault = true; |
| 81 | 81 | } else { |
@@ -83,131 +83,131 @@ discard block |
||
| 83 | 83 | $caseCount++; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) { |
|
| 87 | - $expected = strtolower($tokens[$nextCase]['content']); |
|
| 88 | - $error = strtoupper($type).' keyword must be lowercase; expected "%s" but found "%s"'; |
|
| 86 | + if ( $tokens[ $nextCase ][ 'content' ] !== strtolower( $tokens[ $nextCase ][ 'content' ] ) ) { |
|
| 87 | + $expected = strtolower( $tokens[ $nextCase ][ 'content' ] ); |
|
| 88 | + $error = strtoupper( $type ) . ' keyword must be lowercase; expected "%s" but found "%s"'; |
|
| 89 | 89 | $data = array( |
| 90 | 90 | $expected, |
| 91 | - $tokens[$nextCase]['content'], |
|
| 91 | + $tokens[ $nextCase ][ 'content' ], |
|
| 92 | 92 | ); |
| 93 | 93 | |
| 94 | - $fix = $phpcsFile->addFixableError($error, $nextCase, $type.'NotLower', $data); |
|
| 95 | - if ($fix === true) { |
|
| 96 | - $phpcsFile->fixer->replaceToken($nextCase, $expected); |
|
| 94 | + $fix = $phpcsFile->addFixableError( $error, $nextCase, $type . 'NotLower', $data ); |
|
| 95 | + if ( $fix === true ) { |
|
| 96 | + $phpcsFile->fixer->replaceToken( $nextCase, $expected ); |
|
| 97 | 97 | } |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - if ($type === 'case' |
|
| 101 | - && ($tokens[($nextCase + 1)]['code'] !== T_WHITESPACE |
|
| 102 | - || $tokens[($nextCase + 1)]['content'] !== ' ') |
|
| 100 | + if ( $type === 'case' |
|
| 101 | + && ( $tokens[ ( $nextCase + 1 ) ][ 'code' ] !== T_WHITESPACE |
|
| 102 | + || $tokens[ ( $nextCase + 1 ) ][ 'content' ] !== ' ' ) |
|
| 103 | 103 | ) { |
| 104 | 104 | $error = 'CASE keyword must be followed by a single space'; |
| 105 | - $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase'); |
|
| 106 | - if ($fix === true) { |
|
| 107 | - if ($tokens[($nextCase + 1)]['code'] !== T_WHITESPACE) { |
|
| 108 | - $phpcsFile->fixer->addContent($nextCase, ' '); |
|
| 105 | + $fix = $phpcsFile->addFixableError( $error, $nextCase, 'SpacingAfterCase' ); |
|
| 106 | + if ( $fix === true ) { |
|
| 107 | + if ( $tokens[ ( $nextCase + 1 ) ][ 'code' ] !== T_WHITESPACE ) { |
|
| 108 | + $phpcsFile->fixer->addContent( $nextCase, ' ' ); |
|
| 109 | 109 | } else { |
| 110 | - $phpcsFile->fixer->replaceToken(($nextCase + 1), ' '); |
|
| 110 | + $phpcsFile->fixer->replaceToken( ( $nextCase + 1 ), ' ' ); |
|
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - $opener = $tokens[$nextCase]['scope_opener']; |
|
| 116 | - $nextCloser = $tokens[$nextCase]['scope_closer']; |
|
| 117 | - if ($tokens[$opener]['code'] === T_COLON) { |
|
| 118 | - if ($tokens[($opener - 1)]['code'] === T_WHITESPACE) { |
|
| 119 | - $error = 'There must be no space before the colon in a '.strtoupper($type).' statement'; |
|
| 120 | - $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon'.strtoupper($type)); |
|
| 121 | - if ($fix === true) { |
|
| 122 | - $phpcsFile->fixer->replaceToken(($opener - 1), ''); |
|
| 115 | + $opener = $tokens[ $nextCase ][ 'scope_opener' ]; |
|
| 116 | + $nextCloser = $tokens[ $nextCase ][ 'scope_closer' ]; |
|
| 117 | + if ( $tokens[ $opener ][ 'code' ] === T_COLON ) { |
|
| 118 | + if ( $tokens[ ( $opener - 1 ) ][ 'code' ] === T_WHITESPACE ) { |
|
| 119 | + $error = 'There must be no space before the colon in a ' . strtoupper( $type ) . ' statement'; |
|
| 120 | + $fix = $phpcsFile->addFixableError( $error, $nextCase, 'SpaceBeforeColon' . strtoupper( $type ) ); |
|
| 121 | + if ( $fix === true ) { |
|
| 122 | + $phpcsFile->fixer->replaceToken( ( $opener - 1 ), '' ); |
|
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), null, true); |
|
| 127 | - if ($tokens[$next]['line'] === $tokens[$opener]['line'] |
|
| 128 | - && $tokens[$next]['code'] === T_COMMENT |
|
| 126 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $opener + 1 ), null, true ); |
|
| 127 | + if ( $tokens[ $next ][ 'line' ] === $tokens[ $opener ][ 'line' ] |
|
| 128 | + && $tokens[ $next ][ 'code' ] === T_COMMENT |
|
| 129 | 129 | ) { |
| 130 | 130 | // Skip comments on the same line. |
| 131 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true); |
|
| 131 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $next + 1 ), null, true ); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - if ($tokens[$next]['line'] !== ($tokens[$opener]['line'] + 1)) { |
|
| 135 | - $error = 'The '.strtoupper($type).' body must start on the line following the statement'; |
|
| 136 | - $fix = $phpcsFile->addFixableError($error, $nextCase, 'BodyOnNextLine'.strtoupper($type)); |
|
| 137 | - if ($fix === true) { |
|
| 138 | - if ($tokens[$next]['line'] === $tokens[$opener]['line']) { |
|
| 139 | - $padding = str_repeat(' ', ($caseAlignment + $this->indent - 1)); |
|
| 140 | - $phpcsFile->fixer->addContentBefore($next, $phpcsFile->eolChar.$padding); |
|
| 134 | + if ( $tokens[ $next ][ 'line' ] !== ( $tokens[ $opener ][ 'line' ] + 1 ) ) { |
|
| 135 | + $error = 'The ' . strtoupper( $type ) . ' body must start on the line following the statement'; |
|
| 136 | + $fix = $phpcsFile->addFixableError( $error, $nextCase, 'BodyOnNextLine' . strtoupper( $type ) ); |
|
| 137 | + if ( $fix === true ) { |
|
| 138 | + if ( $tokens[ $next ][ 'line' ] === $tokens[ $opener ][ 'line' ] ) { |
|
| 139 | + $padding = str_repeat( ' ', ( $caseAlignment + $this->indent - 1 ) ); |
|
| 140 | + $phpcsFile->fixer->addContentBefore( $next, $phpcsFile->eolChar . $padding ); |
|
| 141 | 141 | } else { |
| 142 | 142 | $phpcsFile->fixer->beginChangeset(); |
| 143 | - for ($i = ($opener + 1); $i < $next; $i++) { |
|
| 144 | - if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
| 143 | + for ( $i = ( $opener + 1 ); $i < $next; $i++ ) { |
|
| 144 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $next ][ 'line' ] ) { |
|
| 145 | 145 | break; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 148 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - $phpcsFile->fixer->addNewLineBefore($i); |
|
| 151 | + $phpcsFile->fixer->addNewLineBefore( $i ); |
|
| 152 | 152 | $phpcsFile->fixer->endChangeset(); |
| 153 | 153 | } |
| 154 | 154 | } |
| 155 | 155 | }//end if |
| 156 | 156 | |
| 157 | - if ($tokens[$nextCloser]['scope_condition'] === $nextCase) { |
|
| 157 | + if ( $tokens[ $nextCloser ][ 'scope_condition' ] === $nextCase ) { |
|
| 158 | 158 | // Only need to check some things once, even if the |
| 159 | 159 | // closer is shared between multiple case statements, or even |
| 160 | 160 | // the default case. |
| 161 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($nextCloser - 1), $nextCase, true); |
|
| 162 | - if ($tokens[$prev]['line'] === $tokens[$nextCloser]['line']) { |
|
| 161 | + $prev = $phpcsFile->findPrevious( T_WHITESPACE, ( $nextCloser - 1 ), $nextCase, true ); |
|
| 162 | + if ( $tokens[ $prev ][ 'line' ] === $tokens[ $nextCloser ][ 'line' ] ) { |
|
| 163 | 163 | $error = 'Terminating statement must be on a line by itself'; |
| 164 | - $fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakNotNewLine'); |
|
| 165 | - if ($fix === true) { |
|
| 166 | - $phpcsFile->fixer->addNewLine($prev); |
|
| 167 | - $phpcsFile->fixer->replaceToken($nextCloser, trim($tokens[$nextCloser]['content'])); |
|
| 164 | + $fix = $phpcsFile->addFixableError( $error, $nextCloser, 'BreakNotNewLine' ); |
|
| 165 | + if ( $fix === true ) { |
|
| 166 | + $phpcsFile->fixer->addNewLine( $prev ); |
|
| 167 | + $phpcsFile->fixer->replaceToken( $nextCloser, trim( $tokens[ $nextCloser ][ 'content' ] ) ); |
|
| 168 | 168 | } |
| 169 | 169 | } else { |
| 170 | - $diff = ($caseAlignment + $this->indent - $tokens[$nextCloser]['column']); |
|
| 171 | - if ($diff !== 0) { |
|
| 170 | + $diff = ( $caseAlignment + $this->indent - $tokens[ $nextCloser ][ 'column' ] ); |
|
| 171 | + if ( $diff !== 0 ) { |
|
| 172 | 172 | $error = 'Terminating statement must be indented to the same level as the CASE body'; |
| 173 | - $fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent'); |
|
| 174 | - if ($fix === true) { |
|
| 175 | - if ($diff > 0) { |
|
| 176 | - $phpcsFile->fixer->addContentBefore($nextCloser, str_repeat(' ', $diff)); |
|
| 173 | + $fix = $phpcsFile->addFixableError( $error, $nextCloser, 'BreakIndent' ); |
|
| 174 | + if ( $fix === true ) { |
|
| 175 | + if ( $diff > 0 ) { |
|
| 176 | + $phpcsFile->fixer->addContentBefore( $nextCloser, str_repeat( ' ', $diff ) ); |
|
| 177 | 177 | } else { |
| 178 | - $phpcsFile->fixer->substrToken(($nextCloser - 1), 0, $diff); |
|
| 178 | + $phpcsFile->fixer->substrToken( ( $nextCloser - 1 ), 0, $diff ); |
|
| 179 | 179 | } |
| 180 | 180 | } |
| 181 | 181 | } |
| 182 | 182 | }//end if |
| 183 | 183 | }//end if |
| 184 | 184 | } else { |
| 185 | - $error = strtoupper($type).' statements must be defined using a colon'; |
|
| 186 | - $phpcsFile->addError($error, $nextCase, 'WrongOpener'.$type); |
|
| 185 | + $error = strtoupper( $type ) . ' statements must be defined using a colon'; |
|
| 186 | + $phpcsFile->addError( $error, $nextCase, 'WrongOpener' . $type ); |
|
| 187 | 187 | }//end if |
| 188 | 188 | |
| 189 | 189 | // We only want cases from here on in. |
| 190 | - if ($type !== 'case') { |
|
| 190 | + if ( $type !== 'case' ) { |
|
| 191 | 191 | continue; |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | $nextCode = $phpcsFile->findNext( |
| 195 | 195 | T_WHITESPACE, |
| 196 | - ($tokens[$nextCase]['scope_opener'] + 1), |
|
| 196 | + ( $tokens[ $nextCase ][ 'scope_opener' ] + 1 ), |
|
| 197 | 197 | $nextCloser, |
| 198 | 198 | true |
| 199 | 199 | ); |
| 200 | 200 | |
| 201 | - if ($tokens[$nextCode]['code'] !== T_CASE && $tokens[$nextCode]['code'] !== T_DEFAULT) { |
|
| 201 | + if ( $tokens[ $nextCode ][ 'code' ] !== T_CASE && $tokens[ $nextCode ][ 'code' ] !== T_DEFAULT ) { |
|
| 202 | 202 | // This case statement has content. If the next case or default comes |
| 203 | 203 | // before the closer, it means we dont have a terminating statement |
| 204 | 204 | // and instead need a comment. |
| 205 | - $nextCode = $this->_findNextCase($phpcsFile, ($tokens[$nextCase]['scope_opener'] + 1), $nextCloser); |
|
| 206 | - if ($nextCode !== false) { |
|
| 207 | - $prevCode = $phpcsFile->findPrevious(T_WHITESPACE, ($nextCode - 1), $nextCase, true); |
|
| 208 | - if ($tokens[$prevCode]['code'] !== T_COMMENT) { |
|
| 205 | + $nextCode = $this->_findNextCase( $phpcsFile, ( $tokens[ $nextCase ][ 'scope_opener' ] + 1 ), $nextCloser ); |
|
| 206 | + if ( $nextCode !== false ) { |
|
| 207 | + $prevCode = $phpcsFile->findPrevious( T_WHITESPACE, ( $nextCode - 1 ), $nextCase, true ); |
|
| 208 | + if ( $tokens[ $prevCode ][ 'code' ] !== T_COMMENT ) { |
|
| 209 | 209 | $error = 'There must be a comment when fall-through is intentional in a non-empty case body'; |
| 210 | - $phpcsFile->addError($error, $nextCase, 'TerminatingComment'); |
|
| 210 | + $phpcsFile->addError( $error, $nextCase, 'TerminatingComment' ); |
|
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | 213 | } |
@@ -227,13 +227,13 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | * @return int | bool |
| 229 | 229 | */ |
| 230 | - private function _findNextCase(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $end) |
|
| 230 | + private function _findNextCase( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $end ) |
|
| 231 | 231 | { |
| 232 | 232 | $tokens = $phpcsFile->getTokens(); |
| 233 | - while (($stackPtr = $phpcsFile->findNext(array(T_CASE, T_DEFAULT, T_SWITCH), $stackPtr, $end)) !== false) { |
|
| 233 | + while ( ( $stackPtr = $phpcsFile->findNext( array( T_CASE, T_DEFAULT, T_SWITCH ), $stackPtr, $end ) ) !== false ) { |
|
| 234 | 234 | // Skip nested SWITCH statements; they are handled on their own. |
| 235 | - if ($tokens[$stackPtr]['code'] === T_SWITCH) { |
|
| 236 | - $stackPtr = $tokens[$stackPtr]['scope_closer']; |
|
| 235 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_SWITCH ) { |
|
| 236 | + $stackPtr = $tokens[ $stackPtr ][ 'scope_closer' ]; |
|
| 237 | 237 | continue; |
| 238 | 238 | } |
| 239 | 239 | |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * @version Release: @package_version@ |
| 26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 27 | 27 | */ |
| 28 | -class PSR2_Sniffs_ControlStructures_SwitchDeclarationSniff implements PHP_CodeSniffer_Sniff |
|
| 29 | -{ |
|
| 28 | +class PSR2_Sniffs_ControlStructures_SwitchDeclarationSniff implements PHP_CodeSniffer_Sniff { |
|
| 30 | 29 | |
| 31 | 30 | /** |
| 32 | 31 | * The number of spaces code should be indented. |
@@ -41,8 +40,7 @@ discard block |
||
| 41 | 40 | * |
| 42 | 41 | * @return array |
| 43 | 42 | */ |
| 44 | - public function register() |
|
| 45 | - { |
|
| 43 | + public function register() { |
|
| 46 | 44 | return array(T_SWITCH); |
| 47 | 45 | |
| 48 | 46 | }//end register() |
@@ -57,8 +55,7 @@ discard block |
||
| 57 | 55 | * |
| 58 | 56 | * @return void |
| 59 | 57 | */ |
| 60 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 61 | - { |
|
| 58 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 62 | 59 | $tokens = $phpcsFile->getTokens(); |
| 63 | 60 | |
| 64 | 61 | // We can't process SWITCH statements unless we know where they start and end. |
@@ -227,8 +224,7 @@ discard block |
||
| 227 | 224 | * |
| 228 | 225 | * @return int | bool |
| 229 | 226 | */ |
| 230 | - private function _findNextCase(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $end) |
|
| 231 | - { |
|
| 227 | + private function _findNextCase(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $end) { |
|
| 232 | 228 | $tokens = $phpcsFile->getTokens(); |
| 233 | 229 | while (($stackPtr = $phpcsFile->findNext(array(T_CASE, T_DEFAULT, T_SWITCH), $stackPtr, $end)) !== false) { |
| 234 | 230 | // Skip nested SWITCH statements; they are handled on their own. |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | /** |
| 33 | 33 | * Returns an array of tokens this test wants to listen for. |
| 34 | 34 | * |
| 35 | - * @return array |
|
| 35 | + * @return integer[] |
|
| 36 | 36 | */ |
| 37 | 37 | public function register() |
| 38 | 38 | { |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * @param int $stackPtr The position of the current token in |
| 49 | 49 | * the stack passed in $tokens. |
| 50 | 50 | * |
| 51 | - * @return void |
|
| 51 | + * @return integer |
|
| 52 | 52 | */ |
| 53 | 53 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 54 | 54 | { |
@@ -29,72 +29,72 @@ |
||
| 29 | 29 | { |
| 30 | 30 | |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Returns an array of tokens this test wants to listen for. |
|
| 34 | - * |
|
| 35 | - * @return array |
|
| 36 | - */ |
|
| 37 | - public function register() |
|
| 38 | - { |
|
| 39 | - return array(T_OPEN_TAG); |
|
| 32 | + /** |
|
| 33 | + * Returns an array of tokens this test wants to listen for. |
|
| 34 | + * |
|
| 35 | + * @return array |
|
| 36 | + */ |
|
| 37 | + public function register() |
|
| 38 | + { |
|
| 39 | + return array(T_OPEN_TAG); |
|
| 40 | 40 | |
| 41 | - }//end register() |
|
| 41 | + }//end register() |
|
| 42 | 42 | |
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 46 | - * |
|
| 47 | - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 48 | - * @param int $stackPtr The position of the current token in |
|
| 49 | - * the stack passed in $tokens. |
|
| 50 | - * |
|
| 51 | - * @return void |
|
| 52 | - */ |
|
| 53 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 54 | - { |
|
| 55 | - $tokens = $phpcsFile->getTokens(); |
|
| 44 | + /** |
|
| 45 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 46 | + * |
|
| 47 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 48 | + * @param int $stackPtr The position of the current token in |
|
| 49 | + * the stack passed in $tokens. |
|
| 50 | + * |
|
| 51 | + * @return void |
|
| 52 | + */ |
|
| 53 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 54 | + { |
|
| 55 | + $tokens = $phpcsFile->getTokens(); |
|
| 56 | 56 | |
| 57 | - // Make sure this file only contains PHP code. |
|
| 58 | - for ($i = 0; $i < $phpcsFile->numTokens; $i++) { |
|
| 59 | - if ($tokens[$i]['code'] === T_INLINE_HTML |
|
| 60 | - && trim($tokens[$i]['content']) !== '' |
|
| 61 | - ) { |
|
| 62 | - return $phpcsFile->numTokens; |
|
| 63 | - } |
|
| 64 | - } |
|
| 57 | + // Make sure this file only contains PHP code. |
|
| 58 | + for ($i = 0; $i < $phpcsFile->numTokens; $i++) { |
|
| 59 | + if ($tokens[$i]['code'] === T_INLINE_HTML |
|
| 60 | + && trim($tokens[$i]['content']) !== '' |
|
| 61 | + ) { |
|
| 62 | + return $phpcsFile->numTokens; |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - // Find the last non-empty token. |
|
| 67 | - for ($last = ($phpcsFile->numTokens - 1); $last > 0; $last--) { |
|
| 68 | - if (trim($tokens[$last]['content']) !== '') { |
|
| 69 | - break; |
|
| 70 | - } |
|
| 71 | - } |
|
| 66 | + // Find the last non-empty token. |
|
| 67 | + for ($last = ($phpcsFile->numTokens - 1); $last > 0; $last--) { |
|
| 68 | + if (trim($tokens[$last]['content']) !== '') { |
|
| 69 | + break; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - if ($tokens[$last]['code'] === T_CLOSE_TAG) { |
|
| 74 | - $error = 'A closing tag is not permitted at the end of a PHP file'; |
|
| 75 | - $fix = $phpcsFile->addFixableError($error, $last, 'NotAllowed'); |
|
| 76 | - if ($fix === true) { |
|
| 77 | - $phpcsFile->fixer->beginChangeset(); |
|
| 78 | - $phpcsFile->fixer->replaceToken($last, $phpcsFile->eolChar); |
|
| 79 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($last - 1), null, true); |
|
| 80 | - if ($tokens[$prev]['code'] !== T_SEMICOLON |
|
| 81 | - && $tokens[$prev]['code'] !== T_CLOSE_CURLY_BRACKET |
|
| 82 | - ) { |
|
| 83 | - $phpcsFile->fixer->addContent($prev, ';'); |
|
| 84 | - } |
|
| 73 | + if ($tokens[$last]['code'] === T_CLOSE_TAG) { |
|
| 74 | + $error = 'A closing tag is not permitted at the end of a PHP file'; |
|
| 75 | + $fix = $phpcsFile->addFixableError($error, $last, 'NotAllowed'); |
|
| 76 | + if ($fix === true) { |
|
| 77 | + $phpcsFile->fixer->beginChangeset(); |
|
| 78 | + $phpcsFile->fixer->replaceToken($last, $phpcsFile->eolChar); |
|
| 79 | + $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($last - 1), null, true); |
|
| 80 | + if ($tokens[$prev]['code'] !== T_SEMICOLON |
|
| 81 | + && $tokens[$prev]['code'] !== T_CLOSE_CURLY_BRACKET |
|
| 82 | + ) { |
|
| 83 | + $phpcsFile->fixer->addContent($prev, ';'); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - $phpcsFile->fixer->endChangeset(); |
|
| 87 | - } |
|
| 86 | + $phpcsFile->fixer->endChangeset(); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'yes'); |
|
| 90 | - } else { |
|
| 91 | - $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'no'); |
|
| 92 | - } |
|
| 89 | + $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'yes'); |
|
| 90 | + } else { |
|
| 91 | + $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'no'); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - // Ignore the rest of the file. |
|
| 95 | - return $phpcsFile->numTokens; |
|
| 94 | + // Ignore the rest of the file. |
|
| 95 | + return $phpcsFile->numTokens; |
|
| 96 | 96 | |
| 97 | - }//end process() |
|
| 97 | + }//end process() |
|
| 98 | 98 | |
| 99 | 99 | |
| 100 | 100 | }//end class |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | public function register() |
| 38 | 38 | { |
| 39 | - return array(T_OPEN_TAG); |
|
| 39 | + return array( T_OPEN_TAG ); |
|
| 40 | 40 | |
| 41 | 41 | }//end register() |
| 42 | 42 | |
@@ -50,45 +50,45 @@ discard block |
||
| 50 | 50 | * |
| 51 | 51 | * @return void |
| 52 | 52 | */ |
| 53 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 53 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
|
| 54 | 54 | { |
| 55 | 55 | $tokens = $phpcsFile->getTokens(); |
| 56 | 56 | |
| 57 | 57 | // Make sure this file only contains PHP code. |
| 58 | - for ($i = 0; $i < $phpcsFile->numTokens; $i++) { |
|
| 59 | - if ($tokens[$i]['code'] === T_INLINE_HTML |
|
| 60 | - && trim($tokens[$i]['content']) !== '' |
|
| 58 | + for ( $i = 0; $i < $phpcsFile->numTokens; $i++ ) { |
|
| 59 | + if ( $tokens[ $i ][ 'code' ] === T_INLINE_HTML |
|
| 60 | + && trim( $tokens[ $i ][ 'content' ] ) !== '' |
|
| 61 | 61 | ) { |
| 62 | 62 | return $phpcsFile->numTokens; |
| 63 | 63 | } |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Find the last non-empty token. |
| 67 | - for ($last = ($phpcsFile->numTokens - 1); $last > 0; $last--) { |
|
| 68 | - if (trim($tokens[$last]['content']) !== '') { |
|
| 67 | + for ( $last = ( $phpcsFile->numTokens - 1 ); $last > 0; $last-- ) { |
|
| 68 | + if ( trim( $tokens[ $last ][ 'content' ] ) !== '' ) { |
|
| 69 | 69 | break; |
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - if ($tokens[$last]['code'] === T_CLOSE_TAG) { |
|
| 73 | + if ( $tokens[ $last ][ 'code' ] === T_CLOSE_TAG ) { |
|
| 74 | 74 | $error = 'A closing tag is not permitted at the end of a PHP file'; |
| 75 | - $fix = $phpcsFile->addFixableError($error, $last, 'NotAllowed'); |
|
| 76 | - if ($fix === true) { |
|
| 75 | + $fix = $phpcsFile->addFixableError( $error, $last, 'NotAllowed' ); |
|
| 76 | + if ( $fix === true ) { |
|
| 77 | 77 | $phpcsFile->fixer->beginChangeset(); |
| 78 | - $phpcsFile->fixer->replaceToken($last, $phpcsFile->eolChar); |
|
| 79 | - $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($last - 1), null, true); |
|
| 80 | - if ($tokens[$prev]['code'] !== T_SEMICOLON |
|
| 81 | - && $tokens[$prev]['code'] !== T_CLOSE_CURLY_BRACKET |
|
| 78 | + $phpcsFile->fixer->replaceToken( $last, $phpcsFile->eolChar ); |
|
| 79 | + $prev = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $last - 1 ), null, true ); |
|
| 80 | + if ( $tokens[ $prev ][ 'code' ] !== T_SEMICOLON |
|
| 81 | + && $tokens[ $prev ][ 'code' ] !== T_CLOSE_CURLY_BRACKET |
|
| 82 | 82 | ) { |
| 83 | - $phpcsFile->fixer->addContent($prev, ';'); |
|
| 83 | + $phpcsFile->fixer->addContent( $prev, ';' ); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | $phpcsFile->fixer->endChangeset(); |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'yes'); |
|
| 89 | + $phpcsFile->recordMetric( $stackPtr, 'PHP closing tag at end of PHP-only file', 'yes' ); |
|
| 90 | 90 | } else { |
| 91 | - $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'no'); |
|
| 91 | + $phpcsFile->recordMetric( $stackPtr, 'PHP closing tag at end of PHP-only file', 'no' ); |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | // Ignore the rest of the file. |
@@ -25,8 +25,7 @@ discard block |
||
| 25 | 25 | * @version Release: @package_version@ |
| 26 | 26 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 27 | 27 | */ |
| 28 | -class PSR2_Sniffs_Files_ClosingTagSniff implements PHP_CodeSniffer_Sniff |
|
| 29 | -{ |
|
| 28 | +class PSR2_Sniffs_Files_ClosingTagSniff implements PHP_CodeSniffer_Sniff { |
|
| 30 | 29 | |
| 31 | 30 | |
| 32 | 31 | /** |
@@ -34,8 +33,7 @@ discard block |
||
| 34 | 33 | * |
| 35 | 34 | * @return array |
| 36 | 35 | */ |
| 37 | - public function register() |
|
| 38 | - { |
|
| 36 | + public function register() { |
|
| 39 | 37 | return array(T_OPEN_TAG); |
| 40 | 38 | |
| 41 | 39 | }//end register() |
@@ -50,8 +48,7 @@ discard block |
||
| 50 | 48 | * |
| 51 | 49 | * @return void |
| 52 | 50 | */ |
| 53 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 54 | - { |
|
| 51 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
| 55 | 52 | $tokens = $phpcsFile->getTokens(); |
| 56 | 53 | |
| 57 | 54 | // Make sure this file only contains PHP code. |