@@ -1,4 +1,4 @@ |
||
| 1 | -<?php declare(strict_types=1); |
|
| 1 | +<?php declare(strict_types = 1); |
|
| 2 | 2 | /** |
| 3 | 3 | * Hummingbird Anime Client |
| 4 | 4 | * |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare(strict_types=1); |
|
| 1 | +<?php declare(strict_types = 1); |
|
| 2 | 2 | /** |
| 3 | 3 | * Hummingbird Anime Client |
| 4 | 4 | * |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | $response->getStatusCode(), |
| 144 | 144 | 'API Error', |
| 145 | 145 | 'There was a problem getting data from an external source.', |
| 146 | - (string) $response->getBody() |
|
| 146 | + (string)$response->getBody() |
|
| 147 | 147 | ]); |
| 148 | 148 | } |
| 149 | 149 | } |
@@ -289,7 +289,7 @@ discard block |
||
| 289 | 289 | |
| 290 | 290 | $params = []; |
| 291 | 291 | |
| 292 | - switch($failure->failedRule) { |
|
| 292 | + switch ($failure->failedRule) { |
|
| 293 | 293 | case 'Aura\Router\Rule\Allows': |
| 294 | 294 | $params = [ |
| 295 | 295 | 'http_code' => 405, |
@@ -289,7 +289,8 @@ |
||
| 289 | 289 | |
| 290 | 290 | $params = []; |
| 291 | 291 | |
| 292 | - switch($failure->failedRule) { |
|
| 292 | + switch($failure->failedRule) |
|
| 293 | + { |
|
| 293 | 294 | case 'Aura\Router\Rule\Allows': |
| 294 | 295 | $params = [ |
| 295 | 296 | 'http_code' => 405, |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare(strict_types=1); |
|
| 1 | +<?php declare(strict_types = 1); |
|
| 2 | 2 | /** |
| 3 | 3 | * Hummingbird Anime Client |
| 4 | 4 | * |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | public function __get($key) |
| 73 | 73 | { |
| 74 | - $routing_config =& $this->route_config; |
|
| 74 | + $routing_config = & $this->route_config; |
|
| 75 | 75 | |
| 76 | 76 | if (array_key_exists($key, $routing_config)) |
| 77 | 77 | { |
@@ -34,71 +34,71 @@ |
||
| 34 | 34 | |
| 35 | 35 | class AbstractClosingCommentSniff implements Sniff |
| 36 | 36 | { |
| 37 | - /** |
|
| 38 | - * As an abstract class, this sniff is not associated to any token. |
|
| 39 | - */ |
|
| 40 | - public function register() |
|
| 41 | - { |
|
| 42 | - return array(); |
|
| 43 | - } |
|
| 37 | + /** |
|
| 38 | + * As an abstract class, this sniff is not associated to any token. |
|
| 39 | + */ |
|
| 40 | + public function register() |
|
| 41 | + { |
|
| 42 | + return array(); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * As an abstract class, this sniff is not dedicated to process a token. |
|
| 47 | - */ |
|
| 48 | - public function process(File $phpcsFile, $stackPtr) |
|
| 49 | - { |
|
| 50 | - $error = __CLASS__.'::'.__METHOD__.' is abstract. Please develop this method in a child class.'; |
|
| 51 | - throw new PHP_CodeSniffer_Exception($error); |
|
| 52 | - } |
|
| 45 | + /** |
|
| 46 | + * As an abstract class, this sniff is not dedicated to process a token. |
|
| 47 | + */ |
|
| 48 | + public function process(File $phpcsFile, $stackPtr) |
|
| 49 | + { |
|
| 50 | + $error = __CLASS__.'::'.__METHOD__.' is abstract. Please develop this method in a child class.'; |
|
| 51 | + throw new PHP_CodeSniffer_Exception($error); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Returns the comment without its delimiter(s) as well as leading |
|
| 56 | - * and traling whitespaces. |
|
| 57 | - * |
|
| 58 | - * It removes the first #, the two first / (i.e. //) or the first /* |
|
| 59 | - * and last \*\/. If a comment starts with /**, then the last * will remain |
|
| 60 | - * as well as whitespaces between this star and the comment content. |
|
| 61 | - * |
|
| 62 | - * @param string $comment Comment containing either comment delimiter(s) and |
|
| 63 | - * trailing or leading whitspaces to clean. |
|
| 64 | - * |
|
| 65 | - * @return string Comment without comment delimiter(s) and whitespaces. |
|
| 66 | - */ |
|
| 67 | - protected static function _getCommentContent ($comment) |
|
| 68 | - { |
|
| 69 | - if (self::_stringStartsWith($comment, '#')) { |
|
| 70 | - $comment = substr($comment, 1); |
|
| 71 | - } else if (self::_stringStartsWith($comment, '//')) { |
|
| 72 | - $comment = substr($comment, 2); |
|
| 73 | - } else if (self::_stringStartsWith($comment, '/*')) { |
|
| 74 | - $comment = substr($comment, 2, strlen($comment) - 2 - 2); |
|
| 75 | - } |
|
| 76 | - $comment = trim($comment); |
|
| 77 | - return $comment; |
|
| 78 | - }//_getCommentContent() |
|
| 54 | + /** |
|
| 55 | + * Returns the comment without its delimiter(s) as well as leading |
|
| 56 | + * and traling whitespaces. |
|
| 57 | + * |
|
| 58 | + * It removes the first #, the two first / (i.e. //) or the first /* |
|
| 59 | + * and last \*\/. If a comment starts with /**, then the last * will remain |
|
| 60 | + * as well as whitespaces between this star and the comment content. |
|
| 61 | + * |
|
| 62 | + * @param string $comment Comment containing either comment delimiter(s) and |
|
| 63 | + * trailing or leading whitspaces to clean. |
|
| 64 | + * |
|
| 65 | + * @return string Comment without comment delimiter(s) and whitespaces. |
|
| 66 | + */ |
|
| 67 | + protected static function _getCommentContent ($comment) |
|
| 68 | + { |
|
| 69 | + if (self::_stringStartsWith($comment, '#')) { |
|
| 70 | + $comment = substr($comment, 1); |
|
| 71 | + } else if (self::_stringStartsWith($comment, '//')) { |
|
| 72 | + $comment = substr($comment, 2); |
|
| 73 | + } else if (self::_stringStartsWith($comment, '/*')) { |
|
| 74 | + $comment = substr($comment, 2, strlen($comment) - 2 - 2); |
|
| 75 | + } |
|
| 76 | + $comment = trim($comment); |
|
| 77 | + return $comment; |
|
| 78 | + }//_getCommentContent() |
|
| 79 | 79 | |
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Binary safe string comparison between $needle and |
|
| 83 | - * the beginning of $haystack. Returns true if $haystack starts with |
|
| 84 | - * $needle, false otherwise. |
|
| 85 | - * |
|
| 86 | - * @param string $haystack The string to search in. |
|
| 87 | - * @param string $needle The string to search for. |
|
| 88 | - * |
|
| 89 | - * @return bool true if $haystack starts with $needle, false otherwise. |
|
| 90 | - */ |
|
| 91 | - protected static function _stringStartsWith ($haystack, $needle) |
|
| 92 | - { |
|
| 93 | - $startsWith = false; |
|
| 94 | - if (strlen($needle) <= strlen($haystack)) { |
|
| 95 | - $haystackBeginning = substr($haystack, 0, strlen($needle)); |
|
| 96 | - if (0 === strcmp($haystackBeginning, $needle)) { |
|
| 97 | - $startsWith = true; |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - return $startsWith; |
|
| 101 | - }//_stringStartsWith() |
|
| 81 | + /** |
|
| 82 | + * Binary safe string comparison between $needle and |
|
| 83 | + * the beginning of $haystack. Returns true if $haystack starts with |
|
| 84 | + * $needle, false otherwise. |
|
| 85 | + * |
|
| 86 | + * @param string $haystack The string to search in. |
|
| 87 | + * @param string $needle The string to search for. |
|
| 88 | + * |
|
| 89 | + * @return bool true if $haystack starts with $needle, false otherwise. |
|
| 90 | + */ |
|
| 91 | + protected static function _stringStartsWith ($haystack, $needle) |
|
| 92 | + { |
|
| 93 | + $startsWith = false; |
|
| 94 | + if (strlen($needle) <= strlen($haystack)) { |
|
| 95 | + $haystackBeginning = substr($haystack, 0, strlen($needle)); |
|
| 96 | + if (0 === strcmp($haystackBeginning, $needle)) { |
|
| 97 | + $startsWith = true; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + return $startsWith; |
|
| 101 | + }//_stringStartsWith() |
|
| 102 | 102 | }//end class |
| 103 | 103 | |
| 104 | 104 | ?> |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | */ |
| 48 | 48 | public function process(File $phpcsFile, $stackPtr) |
| 49 | 49 | { |
| 50 | - $error = __CLASS__.'::'.__METHOD__.' is abstract. Please develop this method in a child class.'; |
|
| 50 | + $error = __CLASS__ . '::' . __METHOD__ . ' is abstract. Please develop this method in a child class.'; |
|
| 51 | 51 | throw new PHP_CodeSniffer_Exception($error); |
| 52 | 52 | } |
| 53 | 53 | |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @return string Comment without comment delimiter(s) and whitespaces. |
| 66 | 66 | */ |
| 67 | - protected static function _getCommentContent ($comment) |
|
| 67 | + protected static function _getCommentContent($comment) |
|
| 68 | 68 | { |
| 69 | 69 | if (self::_stringStartsWith($comment, '#')) { |
| 70 | 70 | $comment = substr($comment, 1); |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | * |
| 89 | 89 | * @return bool true if $haystack starts with $needle, false otherwise. |
| 90 | 90 | */ |
| 91 | - protected static function _stringStartsWith ($haystack, $needle) |
|
| 91 | + protected static function _stringStartsWith($haystack, $needle) |
|
| 92 | 92 | { |
| 93 | 93 | $startsWith = false; |
| 94 | 94 | if (strlen($needle) <= strlen($haystack)) { |
@@ -32,8 +32,7 @@ discard block |
||
| 32 | 32 | use PHP_CodeSniffer\Sniffs\Sniff; |
| 33 | 33 | use PHP_CodeSniffer\Files\File; |
| 34 | 34 | |
| 35 | -class AbstractClosingCommentSniff implements Sniff |
|
| 36 | -{ |
|
| 35 | +class AbstractClosingCommentSniff implements Sniff { |
|
| 37 | 36 | /** |
| 38 | 37 | * As an abstract class, this sniff is not associated to any token. |
| 39 | 38 | */ |
@@ -66,11 +65,16 @@ discard block |
||
| 66 | 65 | */ |
| 67 | 66 | protected static function _getCommentContent ($comment) |
| 68 | 67 | { |
| 69 | - if (self::_stringStartsWith($comment, '#')) { |
|
| 68 | + if (self::_stringStartsWith($comment, '#')) |
|
| 69 | + { |
|
| 70 | 70 | $comment = substr($comment, 1); |
| 71 | - } else if (self::_stringStartsWith($comment, '//')) { |
|
| 71 | + } |
|
| 72 | + else if (self::_stringStartsWith($comment, '//')) |
|
| 73 | + { |
|
| 72 | 74 | $comment = substr($comment, 2); |
| 73 | - } else if (self::_stringStartsWith($comment, '/*')) { |
|
| 75 | + } |
|
| 76 | + else if (self::_stringStartsWith($comment, '/*')) |
|
| 77 | + { |
|
| 74 | 78 | $comment = substr($comment, 2, strlen($comment) - 2 - 2); |
| 75 | 79 | } |
| 76 | 80 | $comment = trim($comment); |
@@ -91,9 +95,11 @@ discard block |
||
| 91 | 95 | protected static function _stringStartsWith ($haystack, $needle) |
| 92 | 96 | { |
| 93 | 97 | $startsWith = false; |
| 94 | - if (strlen($needle) <= strlen($haystack)) { |
|
| 98 | + if (strlen($needle) <= strlen($haystack)) |
|
| 99 | + { |
|
| 95 | 100 | $haystackBeginning = substr($haystack, 0, strlen($needle)); |
| 96 | - if (0 === strcmp($haystackBeginning, $needle)) { |
|
| 101 | + if (0 === strcmp($haystackBeginning, $needle)) |
|
| 102 | + { |
|
| 97 | 103 | $startsWith = true; |
| 98 | 104 | } |
| 99 | 105 | } |
@@ -90,11 +90,11 @@ |
||
| 90 | 90 | */ |
| 91 | 91 | protected static function _stringStartsWith ($haystack, $needle) |
| 92 | 92 | { |
| 93 | - $startsWith = false; |
|
| 93 | + $startsWith = FALSE; |
|
| 94 | 94 | if (strlen($needle) <= strlen($haystack)) { |
| 95 | 95 | $haystackBeginning = substr($haystack, 0, strlen($needle)); |
| 96 | 96 | if (0 === strcmp($haystackBeginning, $needle)) { |
| 97 | - $startsWith = true; |
|
| 97 | + $startsWith = TRUE; |
|
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | 100 | return $startsWith; |
@@ -39,71 +39,71 @@ |
||
| 39 | 39 | class ClosingFileCommentSniff extends AbstractClosingCommentSniff |
| 40 | 40 | { |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Returns an array of tokens this test wants to listen for. |
|
| 44 | - * |
|
| 45 | - * @return array |
|
| 46 | - */ |
|
| 47 | - public function register() |
|
| 48 | - { |
|
| 49 | - return array( |
|
| 50 | - T_OPEN_TAG, |
|
| 51 | - ); |
|
| 42 | + /** |
|
| 43 | + * Returns an array of tokens this test wants to listen for. |
|
| 44 | + * |
|
| 45 | + * @return array |
|
| 46 | + */ |
|
| 47 | + public function register() |
|
| 48 | + { |
|
| 49 | + return array( |
|
| 50 | + T_OPEN_TAG, |
|
| 51 | + ); |
|
| 52 | 52 | |
| 53 | - }//end register() |
|
| 53 | + }//end register() |
|
| 54 | 54 | |
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Processes this test, when one of its tokens is encountered. |
|
| 58 | - * |
|
| 59 | - * @param File $phpcsFile The current file being scanned. |
|
| 60 | - * @param int $stackPtr The position of the current token |
|
| 61 | - * in the stack passed in $tokens. |
|
| 62 | - * |
|
| 63 | - * @return void |
|
| 64 | - */ |
|
| 65 | - public function process(File $phpcsFile, $stackPtr) |
|
| 66 | - { |
|
| 67 | - // We are only interested if this is the first open tag. |
|
| 68 | - if ($stackPtr !== 0) { |
|
| 69 | - if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - } |
|
| 56 | + /** |
|
| 57 | + * Processes this test, when one of its tokens is encountered. |
|
| 58 | + * |
|
| 59 | + * @param File $phpcsFile The current file being scanned. |
|
| 60 | + * @param int $stackPtr The position of the current token |
|
| 61 | + * in the stack passed in $tokens. |
|
| 62 | + * |
|
| 63 | + * @return void |
|
| 64 | + */ |
|
| 65 | + public function process(File $phpcsFile, $stackPtr) |
|
| 66 | + { |
|
| 67 | + // We are only interested if this is the first open tag. |
|
| 68 | + if ($stackPtr !== 0) { |
|
| 69 | + if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - $fullFilename = $phpcsFile->getFilename(); |
|
| 75 | - $filename = basename($fullFilename); |
|
| 76 | - $commentTemplate = "End of file $filename"; |
|
| 74 | + $fullFilename = $phpcsFile->getFilename(); |
|
| 75 | + $filename = basename($fullFilename); |
|
| 76 | + $commentTemplate = "End of file $filename"; |
|
| 77 | 77 | |
| 78 | - $tokens = $phpcsFile->getTokens(); |
|
| 79 | - $currentToken = count($tokens) - 1; |
|
| 80 | - $hasClosingFileComment = false; |
|
| 81 | - $isNotAWhitespaceOrAComment = false; |
|
| 82 | - while ($currentToken >= 0 |
|
| 83 | - && ! $isNotAWhitespaceOrAComment |
|
| 84 | - && ! $hasClosingFileComment |
|
| 85 | - ) { |
|
| 86 | - $token = $tokens[$currentToken]; |
|
| 87 | - $tokenCode = $token['code']; |
|
| 88 | - if (T_COMMENT === $tokenCode) { |
|
| 89 | - $commentString = self::_getCommentContent($token['content']); |
|
| 90 | - if (0 === strcmp($commentString, $commentTemplate)) { |
|
| 91 | - $hasClosingFileComment = true; |
|
| 92 | - } |
|
| 93 | - } else if (T_WHITESPACE === $tokenCode) { |
|
| 94 | - // Whitespaces are allowed between the closing file comment, |
|
| 95 | - // other comments and end of file |
|
| 96 | - } else { |
|
| 97 | - $isNotAWhitespaceOrAComment = true; |
|
| 98 | - } |
|
| 99 | - $currentToken--; |
|
| 100 | - } |
|
| 78 | + $tokens = $phpcsFile->getTokens(); |
|
| 79 | + $currentToken = count($tokens) - 1; |
|
| 80 | + $hasClosingFileComment = false; |
|
| 81 | + $isNotAWhitespaceOrAComment = false; |
|
| 82 | + while ($currentToken >= 0 |
|
| 83 | + && ! $isNotAWhitespaceOrAComment |
|
| 84 | + && ! $hasClosingFileComment |
|
| 85 | + ) { |
|
| 86 | + $token = $tokens[$currentToken]; |
|
| 87 | + $tokenCode = $token['code']; |
|
| 88 | + if (T_COMMENT === $tokenCode) { |
|
| 89 | + $commentString = self::_getCommentContent($token['content']); |
|
| 90 | + if (0 === strcmp($commentString, $commentTemplate)) { |
|
| 91 | + $hasClosingFileComment = true; |
|
| 92 | + } |
|
| 93 | + } else if (T_WHITESPACE === $tokenCode) { |
|
| 94 | + // Whitespaces are allowed between the closing file comment, |
|
| 95 | + // other comments and end of file |
|
| 96 | + } else { |
|
| 97 | + $isNotAWhitespaceOrAComment = true; |
|
| 98 | + } |
|
| 99 | + $currentToken--; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - if ( ! $hasClosingFileComment) { |
|
| 103 | - $error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".'; |
|
| 104 | - $phpcsFile->addError($error, $currentToken); |
|
| 105 | - } |
|
| 106 | - }//end process() |
|
| 102 | + if ( ! $hasClosingFileComment) { |
|
| 103 | + $error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".'; |
|
| 104 | + $phpcsFile->addError($error, $currentToken); |
|
| 105 | + } |
|
| 106 | + }//end process() |
|
| 107 | 107 | }//end class |
| 108 | 108 | |
| 109 | 109 | ?> |
@@ -36,8 +36,7 @@ discard block |
||
| 36 | 36 | |
| 37 | 37 | use PHP_CodeSniffer\Files\File; |
| 38 | 38 | |
| 39 | -class ClosingFileCommentSniff extends AbstractClosingCommentSniff |
|
| 40 | -{ |
|
| 39 | +class ClosingFileCommentSniff extends AbstractClosingCommentSniff { |
|
| 41 | 40 | |
| 42 | 41 | /** |
| 43 | 42 | * Returns an array of tokens this test wants to listen for. |
@@ -65,8 +64,10 @@ discard block |
||
| 65 | 64 | public function process(File $phpcsFile, $stackPtr) |
| 66 | 65 | { |
| 67 | 66 | // We are only interested if this is the first open tag. |
| 68 | - if ($stackPtr !== 0) { |
|
| 69 | - if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 67 | + if ($stackPtr !== 0) |
|
| 68 | + { |
|
| 69 | + if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) |
|
| 70 | + { |
|
| 70 | 71 | return; |
| 71 | 72 | } |
| 72 | 73 | } |
@@ -85,21 +86,28 @@ discard block |
||
| 85 | 86 | ) { |
| 86 | 87 | $token = $tokens[$currentToken]; |
| 87 | 88 | $tokenCode = $token['code']; |
| 88 | - if (T_COMMENT === $tokenCode) { |
|
| 89 | + if (T_COMMENT === $tokenCode) |
|
| 90 | + { |
|
| 89 | 91 | $commentString = self::_getCommentContent($token['content']); |
| 90 | - if (0 === strcmp($commentString, $commentTemplate)) { |
|
| 92 | + if (0 === strcmp($commentString, $commentTemplate)) |
|
| 93 | + { |
|
| 91 | 94 | $hasClosingFileComment = true; |
| 92 | 95 | } |
| 93 | - } else if (T_WHITESPACE === $tokenCode) { |
|
| 96 | + } |
|
| 97 | + else if (T_WHITESPACE === $tokenCode) |
|
| 98 | + { |
|
| 94 | 99 | // Whitespaces are allowed between the closing file comment, |
| 95 | 100 | // other comments and end of file |
| 96 | - } else { |
|
| 101 | + } |
|
| 102 | + else |
|
| 103 | + { |
|
| 97 | 104 | $isNotAWhitespaceOrAComment = true; |
| 98 | 105 | } |
| 99 | 106 | $currentToken--; |
| 100 | 107 | } |
| 101 | 108 | |
| 102 | - if ( ! $hasClosingFileComment) { |
|
| 109 | + if ( ! $hasClosingFileComment) |
|
| 110 | + { |
|
| 103 | 111 | $error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".'; |
| 104 | 112 | $phpcsFile->addError($error, $currentToken); |
| 105 | 113 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | { |
| 67 | 67 | // We are only interested if this is the first open tag. |
| 68 | 68 | if ($stackPtr !== 0) { |
| 69 | - if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 69 | + if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== FALSE) { |
|
| 70 | 70 | return; |
| 71 | 71 | } |
| 72 | 72 | } |
@@ -77,8 +77,8 @@ discard block |
||
| 77 | 77 | |
| 78 | 78 | $tokens = $phpcsFile->getTokens(); |
| 79 | 79 | $currentToken = count($tokens) - 1; |
| 80 | - $hasClosingFileComment = false; |
|
| 81 | - $isNotAWhitespaceOrAComment = false; |
|
| 80 | + $hasClosingFileComment = FALSE; |
|
| 81 | + $isNotAWhitespaceOrAComment = FALSE; |
|
| 82 | 82 | while ($currentToken >= 0 |
| 83 | 83 | && ! $isNotAWhitespaceOrAComment |
| 84 | 84 | && ! $hasClosingFileComment |
@@ -88,13 +88,13 @@ discard block |
||
| 88 | 88 | if (T_COMMENT === $tokenCode) { |
| 89 | 89 | $commentString = self::_getCommentContent($token['content']); |
| 90 | 90 | if (0 === strcmp($commentString, $commentTemplate)) { |
| 91 | - $hasClosingFileComment = true; |
|
| 91 | + $hasClosingFileComment = TRUE; |
|
| 92 | 92 | } |
| 93 | 93 | } else if (T_WHITESPACE === $tokenCode) { |
| 94 | 94 | // Whitespaces are allowed between the closing file comment, |
| 95 | 95 | // other comments and end of file |
| 96 | 96 | } else { |
| 97 | - $isNotAWhitespaceOrAComment = true; |
|
| 97 | + $isNotAWhitespaceOrAComment = TRUE; |
|
| 98 | 98 | } |
| 99 | 99 | $currentToken--; |
| 100 | 100 | } |
@@ -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 | { |
@@ -36,101 +36,101 @@ |
||
| 36 | 36 | { |
| 37 | 37 | |
| 38 | 38 | |
| 39 | - public $php5Constructors = '1'; |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Constructs the test with the tokens it wishes to listen for. |
|
| 44 | - * |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function __construct() |
|
| 48 | - { |
|
| 49 | - parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true); |
|
| 50 | - |
|
| 51 | - }//end __construct() |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Processes this test when one of its tokens is encountered. |
|
| 56 | - * |
|
| 57 | - * @param File $phpcsFile The current file being scanned. |
|
| 58 | - * @param int $stackPtr The position of the current token |
|
| 59 | - * in the stack passed in $tokens. |
|
| 60 | - * @param int $currScope A pointer to the start of the scope. |
|
| 61 | - * |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - protected function processTokenWithinScope( |
|
| 65 | - File $phpcsFile, |
|
| 66 | - $stackPtr, |
|
| 67 | - $currScope |
|
| 68 | - ) { |
|
| 69 | - $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
| 70 | - $className = $phpcsFile->getDeclarationName($currScope); |
|
| 39 | + public $php5Constructors = '1'; |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Constructs the test with the tokens it wishes to listen for. |
|
| 44 | + * |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function __construct() |
|
| 48 | + { |
|
| 49 | + parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true); |
|
| 50 | + |
|
| 51 | + }//end __construct() |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Processes this test when one of its tokens is encountered. |
|
| 56 | + * |
|
| 57 | + * @param File $phpcsFile The current file being scanned. |
|
| 58 | + * @param int $stackPtr The position of the current token |
|
| 59 | + * in the stack passed in $tokens. |
|
| 60 | + * @param int $currScope A pointer to the start of the scope. |
|
| 61 | + * |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + protected function processTokenWithinScope( |
|
| 65 | + File $phpcsFile, |
|
| 66 | + $stackPtr, |
|
| 67 | + $currScope |
|
| 68 | + ) { |
|
| 69 | + $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
| 70 | + $className = $phpcsFile->getDeclarationName($currScope); |
|
| 71 | 71 | |
| 72 | 72 | $isPhp4Constructor = strcasecmp($methodName, $className) === 0; |
| 73 | 73 | $isPhp5Constructor = strcasecmp($methodName, '__construct') === 0; |
| 74 | - if ($this->php5Constructors != '0') { |
|
| 75 | - if ($isPhp4Constructor) { |
|
| 76 | - $error = "PHP4 style constructors are not allowed; use \"__construct\" instead"; |
|
| 77 | - $phpcsFile->addError($error, $stackPtr); |
|
| 78 | - } |
|
| 79 | - } else { |
|
| 80 | - if ($isPhp5Constructor) { |
|
| 81 | - $error = "PHP5 style constructors are not allowed; use \"$className\" instead"; |
|
| 82 | - $phpcsFile->addError($error, $stackPtr); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - if ( ! $isPhp4Constructor && ! $isPhp5Constructor ) { |
|
| 86 | - return; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $tokens = $phpcsFile->getTokens(); |
|
| 90 | - |
|
| 91 | - $parentClassName = $phpcsFile->findExtendedClassName($currScope); |
|
| 92 | - $wrongConstructor = ''; |
|
| 93 | - // prepares the error message and wrong constructor |
|
| 94 | - if ($this->php5Constructors != '0') { |
|
| 95 | - $error = 'PHP4 style calls to parent constructors are not allowed.'; |
|
| 96 | - $error = "$error Please use \"parent::__construct\" instead."; |
|
| 97 | - if (false !== $parentClassName) { |
|
| 98 | - $wrongConstructor = $parentClassName; |
|
| 99 | - } |
|
| 100 | - // Else $wrongConstructor will be empty |
|
| 101 | - // and the test expression will always be false. |
|
| 102 | - // It doesn't check that no parent method should be called |
|
| 103 | - // when no parent class is defined. |
|
| 104 | - } else { |
|
| 105 | - $error = 'PHP5 style calls to parent constructors are not allowed.'; |
|
| 106 | - if (false !== $parentClassName) { |
|
| 107 | - $error = "$error Please use \"parent::$parentClassName\" instead."; |
|
| 108 | - } |
|
| 109 | - $wrongConstructor = '__construct'; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // looks for the use of a wrong constructor. |
|
| 113 | - $endFunctionIndex = $tokens[$stackPtr]['scope_closer']; |
|
| 114 | - $doubleColonIndex = $phpcsFile->findNext( |
|
| 115 | - array(T_DOUBLE_COLON), |
|
| 116 | - $stackPtr, |
|
| 117 | - $endFunctionIndex |
|
| 118 | - ); |
|
| 119 | - while ($doubleColonIndex) { |
|
| 120 | - if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING |
|
| 121 | - && $tokens[($doubleColonIndex + 1)]['content'] === $wrongConstructor |
|
| 122 | - ) { |
|
| 123 | - $phpcsFile->addError($error, ($doubleColonIndex + 1)); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $doubleColonIndex = $phpcsFile->findNext( |
|
| 127 | - array(T_DOUBLE_COLON), |
|
| 128 | - $doubleColonIndex + 1, |
|
| 129 | - $endFunctionIndex |
|
| 130 | - ); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - }//end processTokenWithinScope() |
|
| 74 | + if ($this->php5Constructors != '0') { |
|
| 75 | + if ($isPhp4Constructor) { |
|
| 76 | + $error = "PHP4 style constructors are not allowed; use \"__construct\" instead"; |
|
| 77 | + $phpcsFile->addError($error, $stackPtr); |
|
| 78 | + } |
|
| 79 | + } else { |
|
| 80 | + if ($isPhp5Constructor) { |
|
| 81 | + $error = "PHP5 style constructors are not allowed; use \"$className\" instead"; |
|
| 82 | + $phpcsFile->addError($error, $stackPtr); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + if ( ! $isPhp4Constructor && ! $isPhp5Constructor ) { |
|
| 86 | + return; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $tokens = $phpcsFile->getTokens(); |
|
| 90 | + |
|
| 91 | + $parentClassName = $phpcsFile->findExtendedClassName($currScope); |
|
| 92 | + $wrongConstructor = ''; |
|
| 93 | + // prepares the error message and wrong constructor |
|
| 94 | + if ($this->php5Constructors != '0') { |
|
| 95 | + $error = 'PHP4 style calls to parent constructors are not allowed.'; |
|
| 96 | + $error = "$error Please use \"parent::__construct\" instead."; |
|
| 97 | + if (false !== $parentClassName) { |
|
| 98 | + $wrongConstructor = $parentClassName; |
|
| 99 | + } |
|
| 100 | + // Else $wrongConstructor will be empty |
|
| 101 | + // and the test expression will always be false. |
|
| 102 | + // It doesn't check that no parent method should be called |
|
| 103 | + // when no parent class is defined. |
|
| 104 | + } else { |
|
| 105 | + $error = 'PHP5 style calls to parent constructors are not allowed.'; |
|
| 106 | + if (false !== $parentClassName) { |
|
| 107 | + $error = "$error Please use \"parent::$parentClassName\" instead."; |
|
| 108 | + } |
|
| 109 | + $wrongConstructor = '__construct'; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // looks for the use of a wrong constructor. |
|
| 113 | + $endFunctionIndex = $tokens[$stackPtr]['scope_closer']; |
|
| 114 | + $doubleColonIndex = $phpcsFile->findNext( |
|
| 115 | + array(T_DOUBLE_COLON), |
|
| 116 | + $stackPtr, |
|
| 117 | + $endFunctionIndex |
|
| 118 | + ); |
|
| 119 | + while ($doubleColonIndex) { |
|
| 120 | + if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING |
|
| 121 | + && $tokens[($doubleColonIndex + 1)]['content'] === $wrongConstructor |
|
| 122 | + ) { |
|
| 123 | + $phpcsFile->addError($error, ($doubleColonIndex + 1)); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $doubleColonIndex = $phpcsFile->findNext( |
|
| 127 | + array(T_DOUBLE_COLON), |
|
| 128 | + $doubleColonIndex + 1, |
|
| 129 | + $endFunctionIndex |
|
| 130 | + ); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + }//end processTokenWithinScope() |
|
| 134 | 134 | |
| 135 | 135 | protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) |
| 136 | 136 | { |
@@ -82,7 +82,7 @@ |
||
| 82 | 82 | $phpcsFile->addError($error, $stackPtr); |
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | - if ( ! $isPhp4Constructor && ! $isPhp5Constructor ) { |
|
| 85 | + if ( ! $isPhp4Constructor && ! $isPhp5Constructor) { |
|
| 86 | 86 | return; |
| 87 | 87 | } |
| 88 | 88 | |
@@ -32,8 +32,7 @@ discard block |
||
| 32 | 32 | * @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
| 33 | 33 | * @link http://pear.php.net/package/PHP_CodeSniffer |
| 34 | 34 | */ |
| 35 | -class ConstructorNameSniff extends AbstractScopeSniff |
|
| 36 | -{ |
|
| 35 | +class ConstructorNameSniff extends AbstractScopeSniff { |
|
| 37 | 36 | |
| 38 | 37 | |
| 39 | 38 | public $php5Constructors = '1'; |
@@ -71,18 +70,24 @@ discard block |
||
| 71 | 70 | |
| 72 | 71 | $isPhp4Constructor = strcasecmp($methodName, $className) === 0; |
| 73 | 72 | $isPhp5Constructor = strcasecmp($methodName, '__construct') === 0; |
| 74 | - if ($this->php5Constructors != '0') { |
|
| 75 | - if ($isPhp4Constructor) { |
|
| 73 | + if ($this->php5Constructors != '0') |
|
| 74 | + { |
|
| 75 | + if ($isPhp4Constructor) |
|
| 76 | + { |
|
| 76 | 77 | $error = "PHP4 style constructors are not allowed; use \"__construct\" instead"; |
| 77 | 78 | $phpcsFile->addError($error, $stackPtr); |
| 78 | 79 | } |
| 79 | - } else { |
|
| 80 | - if ($isPhp5Constructor) { |
|
| 80 | + } |
|
| 81 | + else |
|
| 82 | + { |
|
| 83 | + if ($isPhp5Constructor) |
|
| 84 | + { |
|
| 81 | 85 | $error = "PHP5 style constructors are not allowed; use \"$className\" instead"; |
| 82 | 86 | $phpcsFile->addError($error, $stackPtr); |
| 83 | 87 | } |
| 84 | 88 | } |
| 85 | - if ( ! $isPhp4Constructor && ! $isPhp5Constructor ) { |
|
| 89 | + if ( ! $isPhp4Constructor && ! $isPhp5Constructor ) |
|
| 90 | + { |
|
| 86 | 91 | return; |
| 87 | 92 | } |
| 88 | 93 | |
@@ -91,19 +96,24 @@ discard block |
||
| 91 | 96 | $parentClassName = $phpcsFile->findExtendedClassName($currScope); |
| 92 | 97 | $wrongConstructor = ''; |
| 93 | 98 | // prepares the error message and wrong constructor |
| 94 | - if ($this->php5Constructors != '0') { |
|
| 99 | + if ($this->php5Constructors != '0') |
|
| 100 | + { |
|
| 95 | 101 | $error = 'PHP4 style calls to parent constructors are not allowed.'; |
| 96 | 102 | $error = "$error Please use \"parent::__construct\" instead."; |
| 97 | - if (false !== $parentClassName) { |
|
| 103 | + if (false !== $parentClassName) |
|
| 104 | + { |
|
| 98 | 105 | $wrongConstructor = $parentClassName; |
| 99 | 106 | } |
| 100 | 107 | // Else $wrongConstructor will be empty |
| 101 | 108 | // and the test expression will always be false. |
| 102 | 109 | // It doesn't check that no parent method should be called |
| 103 | 110 | // when no parent class is defined. |
| 104 | - } else { |
|
| 111 | + } |
|
| 112 | + else |
|
| 113 | + { |
|
| 105 | 114 | $error = 'PHP5 style calls to parent constructors are not allowed.'; |
| 106 | - if (false !== $parentClassName) { |
|
| 115 | + if (false !== $parentClassName) |
|
| 116 | + { |
|
| 107 | 117 | $error = "$error Please use \"parent::$parentClassName\" instead."; |
| 108 | 118 | } |
| 109 | 119 | $wrongConstructor = '__construct'; |
@@ -116,7 +126,8 @@ discard block |
||
| 116 | 126 | $stackPtr, |
| 117 | 127 | $endFunctionIndex |
| 118 | 128 | ); |
| 119 | - while ($doubleColonIndex) { |
|
| 129 | + while ($doubleColonIndex) |
|
| 130 | + { |
|
| 120 | 131 | if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING |
| 121 | 132 | && $tokens[($doubleColonIndex + 1)]['content'] === $wrongConstructor |
| 122 | 133 | ) { |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | */ |
| 47 | 47 | public function __construct() |
| 48 | 48 | { |
| 49 | - parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true); |
|
| 49 | + parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), TRUE); |
|
| 50 | 50 | |
| 51 | 51 | }//end __construct() |
| 52 | 52 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | if ($this->php5Constructors != '0') { |
| 95 | 95 | $error = 'PHP4 style calls to parent constructors are not allowed.'; |
| 96 | 96 | $error = "$error Please use \"parent::__construct\" instead."; |
| 97 | - if (false !== $parentClassName) { |
|
| 97 | + if (FALSE !== $parentClassName) { |
|
| 98 | 98 | $wrongConstructor = $parentClassName; |
| 99 | 99 | } |
| 100 | 100 | // Else $wrongConstructor will be empty |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | // when no parent class is defined. |
| 104 | 104 | } else { |
| 105 | 105 | $error = 'PHP5 style calls to parent constructors are not allowed.'; |
| 106 | - if (false !== $parentClassName) { |
|
| 106 | + if (FALSE !== $parentClassName) { |
|
| 107 | 107 | $error = "$error Please use \"parent::$parentClassName\" instead."; |
| 108 | 108 | } |
| 109 | 109 | $wrongConstructor = '__construct'; |
@@ -39,45 +39,45 @@ |
||
| 39 | 39 | { |
| 40 | 40 | |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Returns an array of tokens this test wants to listen for. |
|
| 44 | - * |
|
| 45 | - * @return array |
|
| 46 | - */ |
|
| 47 | - public function register() |
|
| 48 | - { |
|
| 49 | - return array( |
|
| 50 | - T_CLASS, |
|
| 51 | - T_INTERFACE, |
|
| 52 | - ); |
|
| 42 | + /** |
|
| 43 | + * Returns an array of tokens this test wants to listen for. |
|
| 44 | + * |
|
| 45 | + * @return array |
|
| 46 | + */ |
|
| 47 | + public function register() |
|
| 48 | + { |
|
| 49 | + return array( |
|
| 50 | + T_CLASS, |
|
| 51 | + T_INTERFACE, |
|
| 52 | + ); |
|
| 53 | 53 | |
| 54 | - }//end register() |
|
| 54 | + }//end register() |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Processes this test, when one of its tokens is encountered. |
|
| 58 | - * |
|
| 59 | - * @param File $phpcsFile The current file being processed. |
|
| 60 | - * @param int $stackPtr The position of the current token |
|
| 61 | - * in the stack passed in $tokens. |
|
| 62 | - * |
|
| 63 | - * @return void |
|
| 64 | - */ |
|
| 65 | - public function process(File $phpcsFile, $stackPtr) |
|
| 66 | - { |
|
| 67 | - // get the class name |
|
| 68 | - $className = trim($phpcsFile->getDeclarationName($stackPtr)); |
|
| 69 | - // compute the expected class name |
|
| 70 | - // [^_] means "something different from _", but not "nothing or something different from _" |
|
| 71 | - $lcClassNameChunk = preg_replace('/([^_])([A-Z])/', '${1}_${2}', $className); |
|
| 72 | - $expectedClassName |
|
| 73 | - = strtoupper($className[0]) . strtolower(substr($lcClassNameChunk,1)); |
|
| 74 | - // ensures that the current class name |
|
| 75 | - // and the expected class name are identical |
|
| 76 | - if (0 !== strcmp($className, $expectedClassName)) { |
|
| 77 | - $error = 'Class names should always have their first letter uppercase. Multiple words should be separated with an underscore, and not CamelCased. Please consider ' . $expectedClassName . ' instead of ' . $className . '.'; |
|
| 78 | - $phpcsFile->addError($error, $stackPtr); |
|
| 79 | - } |
|
| 80 | - }//end process() |
|
| 56 | + /** |
|
| 57 | + * Processes this test, when one of its tokens is encountered. |
|
| 58 | + * |
|
| 59 | + * @param File $phpcsFile The current file being processed. |
|
| 60 | + * @param int $stackPtr The position of the current token |
|
| 61 | + * in the stack passed in $tokens. |
|
| 62 | + * |
|
| 63 | + * @return void |
|
| 64 | + */ |
|
| 65 | + public function process(File $phpcsFile, $stackPtr) |
|
| 66 | + { |
|
| 67 | + // get the class name |
|
| 68 | + $className = trim($phpcsFile->getDeclarationName($stackPtr)); |
|
| 69 | + // compute the expected class name |
|
| 70 | + // [^_] means "something different from _", but not "nothing or something different from _" |
|
| 71 | + $lcClassNameChunk = preg_replace('/([^_])([A-Z])/', '${1}_${2}', $className); |
|
| 72 | + $expectedClassName |
|
| 73 | + = strtoupper($className[0]) . strtolower(substr($lcClassNameChunk,1)); |
|
| 74 | + // ensures that the current class name |
|
| 75 | + // and the expected class name are identical |
|
| 76 | + if (0 !== strcmp($className, $expectedClassName)) { |
|
| 77 | + $error = 'Class names should always have their first letter uppercase. Multiple words should be separated with an underscore, and not CamelCased. Please consider ' . $expectedClassName . ' instead of ' . $className . '.'; |
|
| 78 | + $phpcsFile->addError($error, $stackPtr); |
|
| 79 | + } |
|
| 80 | + }//end process() |
|
| 81 | 81 | |
| 82 | 82 | }//end class |
| 83 | 83 | |
@@ -70,11 +70,11 @@ |
||
| 70 | 70 | // [^_] means "something different from _", but not "nothing or something different from _" |
| 71 | 71 | $lcClassNameChunk = preg_replace('/([^_])([A-Z])/', '${1}_${2}', $className); |
| 72 | 72 | $expectedClassName |
| 73 | - = strtoupper($className[0]) . strtolower(substr($lcClassNameChunk,1)); |
|
| 73 | + = strtoupper($className[0]) . strtolower(substr($lcClassNameChunk, 1)); |
|
| 74 | 74 | // ensures that the current class name |
| 75 | 75 | // and the expected class name are identical |
| 76 | 76 | if (0 !== strcmp($className, $expectedClassName)) { |
| 77 | - $error = 'Class names should always have their first letter uppercase. Multiple words should be separated with an underscore, and not CamelCased. Please consider ' . $expectedClassName . ' instead of ' . $className . '.'; |
|
| 77 | + $error = 'Class names should always have their first letter uppercase. Multiple words should be separated with an underscore, and not CamelCased. Please consider ' . $expectedClassName . ' instead of ' . $className . '.'; |
|
| 78 | 78 | $phpcsFile->addError($error, $stackPtr); |
| 79 | 79 | } |
| 80 | 80 | }//end process() |
@@ -35,8 +35,7 @@ discard block |
||
| 35 | 35 | use PHP_CodeSniffer\Sniffs\Sniff; |
| 36 | 36 | use PHP_CodeSniffer\Files\File; |
| 37 | 37 | |
| 38 | -class ValidClassNameSniff implements Sniff |
|
| 39 | -{ |
|
| 38 | +class ValidClassNameSniff implements Sniff { |
|
| 40 | 39 | |
| 41 | 40 | |
| 42 | 41 | /** |
@@ -73,7 +72,8 @@ discard block |
||
| 73 | 72 | = strtoupper($className[0]) . strtolower(substr($lcClassNameChunk,1)); |
| 74 | 73 | // ensures that the current class name |
| 75 | 74 | // and the expected class name are identical |
| 76 | - if (0 !== strcmp($className, $expectedClassName)) { |
|
| 75 | + if (0 !== strcmp($className, $expectedClassName)) |
|
| 76 | + { |
|
| 77 | 77 | $error = 'Class names should always have their first letter uppercase. Multiple words should be separated with an underscore, and not CamelCased. Please consider ' . $expectedClassName . ' instead of ' . $className . '.'; |
| 78 | 78 | $phpcsFile->addError($error, $stackPtr); |
| 79 | 79 | } |
@@ -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 | { |
@@ -44,519 +44,519 @@ |
||
| 44 | 44 | { |
| 45 | 45 | |
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Processes class member variables. |
|
| 49 | - * |
|
| 50 | - * @param 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 | - protected function processMemberVar(File $phpcsFile, $stackPtr) |
|
| 57 | - { |
|
| 58 | - // get variable name and properties |
|
| 59 | - $tokens = $phpcsFile->getTokens(); |
|
| 60 | - $varTk = $tokens[$stackPtr]; |
|
| 61 | - $varName = substr($varTk['content'], 1); |
|
| 62 | - $varProps = $phpcsFile->getMemberProperties($stackPtr); |
|
| 63 | - // check(s) |
|
| 64 | - if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName) ) { |
|
| 65 | - return; |
|
| 66 | - } |
|
| 67 | - if ( ! $this->checkVisibilityPrefix($phpcsFile, $stackPtr, $varName, $varProps)) { |
|
| 68 | - return; |
|
| 69 | - } |
|
| 70 | - if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) { |
|
| 71 | - return; |
|
| 72 | - } |
|
| 47 | + /** |
|
| 48 | + * Processes class member variables. |
|
| 49 | + * |
|
| 50 | + * @param 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 | + protected function processMemberVar(File $phpcsFile, $stackPtr) |
|
| 57 | + { |
|
| 58 | + // get variable name and properties |
|
| 59 | + $tokens = $phpcsFile->getTokens(); |
|
| 60 | + $varTk = $tokens[$stackPtr]; |
|
| 61 | + $varName = substr($varTk['content'], 1); |
|
| 62 | + $varProps = $phpcsFile->getMemberProperties($stackPtr); |
|
| 63 | + // check(s) |
|
| 64 | + if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName) ) { |
|
| 65 | + return; |
|
| 66 | + } |
|
| 67 | + if ( ! $this->checkVisibilityPrefix($phpcsFile, $stackPtr, $varName, $varProps)) { |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | + if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) { |
|
| 71 | + return; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - }//end processMemberVar() |
|
| 74 | + }//end processMemberVar() |
|
| 75 | 75 | |
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Processes normal variables. |
|
| 79 | - * |
|
| 80 | - * @param File $phpcsFile The file where this token was found. |
|
| 81 | - * @param int $stackPtr The position where the token was found. |
|
| 82 | - * |
|
| 83 | - * @return void |
|
| 84 | - */ |
|
| 85 | - protected function processVariable(File $phpcsFile, $stackPtr) |
|
| 86 | - { |
|
| 87 | - // get variable name |
|
| 88 | - $tokens = $phpcsFile->getTokens(); |
|
| 89 | - $varTk = $tokens[$stackPtr]; |
|
| 90 | - $varName = substr($varTk['content'], 1); |
|
| 91 | - // skip the current object variable, i.e. $this |
|
| 92 | - if (0 === strcmp($varName, 'this')) { |
|
| 93 | - return; |
|
| 94 | - } |
|
| 95 | - // check(s) |
|
| 96 | - if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) { |
|
| 97 | - return; |
|
| 98 | - } |
|
| 99 | - if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) { |
|
| 100 | - return; |
|
| 101 | - } |
|
| 77 | + /** |
|
| 78 | + * Processes normal variables. |
|
| 79 | + * |
|
| 80 | + * @param File $phpcsFile The file where this token was found. |
|
| 81 | + * @param int $stackPtr The position where the token was found. |
|
| 82 | + * |
|
| 83 | + * @return void |
|
| 84 | + */ |
|
| 85 | + protected function processVariable(File $phpcsFile, $stackPtr) |
|
| 86 | + { |
|
| 87 | + // get variable name |
|
| 88 | + $tokens = $phpcsFile->getTokens(); |
|
| 89 | + $varTk = $tokens[$stackPtr]; |
|
| 90 | + $varName = substr($varTk['content'], 1); |
|
| 91 | + // skip the current object variable, i.e. $this |
|
| 92 | + if (0 === strcmp($varName, 'this')) { |
|
| 93 | + return; |
|
| 94 | + } |
|
| 95 | + // check(s) |
|
| 96 | + if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) { |
|
| 97 | + return; |
|
| 98 | + } |
|
| 99 | + if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) { |
|
| 100 | + return; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - }//end processVariable() |
|
| 103 | + }//end processVariable() |
|
| 104 | 104 | |
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Processes variables in double quoted strings. |
|
| 108 | - * |
|
| 109 | - * @param File $phpcsFile The file where this token was found. |
|
| 110 | - * @param int $stackPtr The position where the token was found. |
|
| 111 | - * |
|
| 112 | - * @return void |
|
| 113 | - */ |
|
| 114 | - protected function processVariableInString(File $phpcsFile, $stackPtr) |
|
| 115 | - { |
|
| 116 | - $tokens = $phpcsFile->getTokens(); |
|
| 117 | - $stringTk = $tokens[$stackPtr]; |
|
| 118 | - $stringString = $stringTk['content']; |
|
| 119 | - $varAt = self::_getVariablePosition($stringString, 0); |
|
| 120 | - while (false !== $varAt) { |
|
| 121 | - // get variable name |
|
| 122 | - $matches = array(); |
|
| 123 | - preg_match('/^\$\{?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}?/', substr($stringString, $varAt), $matches); |
|
| 124 | - $varName = $matches[1]; |
|
| 125 | - // check(s) |
|
| 126 | - if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) { |
|
| 127 | - return; |
|
| 128 | - } |
|
| 129 | - if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) { |
|
| 130 | - return; |
|
| 131 | - } |
|
| 132 | - // prepare checking next variable |
|
| 133 | - $varAt = self::_getVariablePosition($stringString, $varAt + 1); |
|
| 134 | - } |
|
| 106 | + /** |
|
| 107 | + * Processes variables in double quoted strings. |
|
| 108 | + * |
|
| 109 | + * @param File $phpcsFile The file where this token was found. |
|
| 110 | + * @param int $stackPtr The position where the token was found. |
|
| 111 | + * |
|
| 112 | + * @return void |
|
| 113 | + */ |
|
| 114 | + protected function processVariableInString(File $phpcsFile, $stackPtr) |
|
| 115 | + { |
|
| 116 | + $tokens = $phpcsFile->getTokens(); |
|
| 117 | + $stringTk = $tokens[$stackPtr]; |
|
| 118 | + $stringString = $stringTk['content']; |
|
| 119 | + $varAt = self::_getVariablePosition($stringString, 0); |
|
| 120 | + while (false !== $varAt) { |
|
| 121 | + // get variable name |
|
| 122 | + $matches = array(); |
|
| 123 | + preg_match('/^\$\{?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}?/', substr($stringString, $varAt), $matches); |
|
| 124 | + $varName = $matches[1]; |
|
| 125 | + // check(s) |
|
| 126 | + if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) { |
|
| 127 | + return; |
|
| 128 | + } |
|
| 129 | + if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) { |
|
| 130 | + return; |
|
| 131 | + } |
|
| 132 | + // prepare checking next variable |
|
| 133 | + $varAt = self::_getVariablePosition($stringString, $varAt + 1); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - }//end processVariableInString() |
|
| 136 | + }//end processVariableInString() |
|
| 137 | 137 | |
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Checks that the variable name is all in lower case, else it add an error |
|
| 141 | - * to $phpcsFile. Returns true if variable name is all in lower case, false |
|
| 142 | - * otherwise. |
|
| 143 | - * |
|
| 144 | - * @param File $phpcsFile The current file being processed. |
|
| 145 | - * @param int $stackPtr The position of the current token |
|
| 146 | - * in the stack passed in $tokens. |
|
| 147 | - * @param string $varName The name of the variable to |
|
| 148 | - * procced without $, { nor }. |
|
| 149 | - * |
|
| 150 | - * @return bool true if variable name is all in lower case, false otherwise. |
|
| 151 | - */ |
|
| 152 | - protected function checkLowerCase(File $phpcsFile, $stackPtr, $varName) |
|
| 153 | - { |
|
| 154 | - $isInLowerCase = true; |
|
| 155 | - if (0 !== strcmp($varName, strtolower($varName))) { |
|
| 156 | - // get the expected variable name |
|
| 157 | - $varNameWithUnderscores = preg_replace('/([A-Z])/', '_${1}', $varName); |
|
| 158 | - $expectedVarName = strtolower(ltrim($varNameWithUnderscores, '_')); |
|
| 159 | - // adapts the error message to the error case |
|
| 160 | - if (strlen($varNameWithUnderscores) > strlen($varName)) { |
|
| 161 | - $error = 'Variables should not use CamelCasing or start with a Capital.'; |
|
| 162 | - } else { |
|
| 163 | - $error = 'Variables should be entirely lowercased.'; |
|
| 164 | - } |
|
| 165 | - $error = $error . 'Please consider "' . $expectedVarName |
|
| 166 | - . '" instead of "' . $varName . '".'; |
|
| 167 | - // adds the error and changes return value |
|
| 168 | - $phpcsFile->addError($error, $stackPtr); |
|
| 169 | - $isInLowerCase = false; |
|
| 170 | - } |
|
| 171 | - return $isInLowerCase; |
|
| 172 | - }//end checkLowerCase() |
|
| 139 | + /** |
|
| 140 | + * Checks that the variable name is all in lower case, else it add an error |
|
| 141 | + * to $phpcsFile. Returns true if variable name is all in lower case, false |
|
| 142 | + * otherwise. |
|
| 143 | + * |
|
| 144 | + * @param File $phpcsFile The current file being processed. |
|
| 145 | + * @param int $stackPtr The position of the current token |
|
| 146 | + * in the stack passed in $tokens. |
|
| 147 | + * @param string $varName The name of the variable to |
|
| 148 | + * procced without $, { nor }. |
|
| 149 | + * |
|
| 150 | + * @return bool true if variable name is all in lower case, false otherwise. |
|
| 151 | + */ |
|
| 152 | + protected function checkLowerCase(File $phpcsFile, $stackPtr, $varName) |
|
| 153 | + { |
|
| 154 | + $isInLowerCase = true; |
|
| 155 | + if (0 !== strcmp($varName, strtolower($varName))) { |
|
| 156 | + // get the expected variable name |
|
| 157 | + $varNameWithUnderscores = preg_replace('/([A-Z])/', '_${1}', $varName); |
|
| 158 | + $expectedVarName = strtolower(ltrim($varNameWithUnderscores, '_')); |
|
| 159 | + // adapts the error message to the error case |
|
| 160 | + if (strlen($varNameWithUnderscores) > strlen($varName)) { |
|
| 161 | + $error = 'Variables should not use CamelCasing or start with a Capital.'; |
|
| 162 | + } else { |
|
| 163 | + $error = 'Variables should be entirely lowercased.'; |
|
| 164 | + } |
|
| 165 | + $error = $error . 'Please consider "' . $expectedVarName |
|
| 166 | + . '" instead of "' . $varName . '".'; |
|
| 167 | + // adds the error and changes return value |
|
| 168 | + $phpcsFile->addError($error, $stackPtr); |
|
| 169 | + $isInLowerCase = false; |
|
| 170 | + } |
|
| 171 | + return $isInLowerCase; |
|
| 172 | + }//end checkLowerCase() |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * Checks that an underscore is used at the beginning of a variable only if |
|
| 176 | - * it is about a private variable. If it isn't a private variable, then it |
|
| 177 | - * must not be prefixed with an underscore. Returns true if $varName is |
|
| 178 | - * properly prefixed according to the variable visibility provided in |
|
| 179 | - * $varProps, false otherwise. |
|
| 180 | - * |
|
| 181 | - * @param File $phpcsFile The current file being processed. |
|
| 182 | - * @param int $stackPtr The position of the current token |
|
| 183 | - * in the stack passed in $tokens. |
|
| 184 | - * @param string $varName The name of the variable to |
|
| 185 | - * procced without $, { nor }. |
|
| 186 | - * @param array $varProps Member variable properties like |
|
| 187 | - * its visibility. |
|
| 188 | - * |
|
| 189 | - * @return bool true if variable name is prefixed with an underscore only |
|
| 190 | - * when it is about a private variable, false otherwise. |
|
| 191 | - */ |
|
| 192 | - protected function checkVisibilityPrefix(File $phpcsFile, $stackPtr, $varName, $varProps) |
|
| 193 | - { |
|
| 194 | - $isVisibilityPrefixRight = true; |
|
| 195 | - $scope = $varProps['scope']; |
|
| 196 | - // If it's a private variable, it must have an underscore on the front. |
|
| 197 | - if ($scope === 'private' && $varName{0} !== '_') { |
|
| 198 | - $error = "Private variable name \"$varName\" must be prefixed with an underscore"; |
|
| 199 | - $phpcsFile->addError($error, $stackPtr); |
|
| 200 | - $isVisibilityPrefixRight = false; |
|
| 201 | - } else if ($scope !== 'private' && $varName{0} === '_') { |
|
| 202 | - // If it's not a private variable, |
|
| 203 | - // then it must not start with an underscore. |
|
| 204 | - if (isset ($scopeSpecified) && true === $scopeSpecified) { |
|
| 205 | - $error = "Public variable name \"$varName\" must not be prefixed with an underscore"; |
|
| 206 | - } else { |
|
| 207 | - $error = ucfirst($scope) . " variable name \"$varName\" must not be prefixed with an underscore"; |
|
| 208 | - } |
|
| 209 | - $phpcsFile->addError($error, $stackPtr); |
|
| 210 | - $isVisibilityPrefixRight = false; |
|
| 211 | - } |
|
| 212 | - return $isVisibilityPrefixRight; |
|
| 213 | - }//end checkVisibilityPrefix() |
|
| 174 | + /** |
|
| 175 | + * Checks that an underscore is used at the beginning of a variable only if |
|
| 176 | + * it is about a private variable. If it isn't a private variable, then it |
|
| 177 | + * must not be prefixed with an underscore. Returns true if $varName is |
|
| 178 | + * properly prefixed according to the variable visibility provided in |
|
| 179 | + * $varProps, false otherwise. |
|
| 180 | + * |
|
| 181 | + * @param File $phpcsFile The current file being processed. |
|
| 182 | + * @param int $stackPtr The position of the current token |
|
| 183 | + * in the stack passed in $tokens. |
|
| 184 | + * @param string $varName The name of the variable to |
|
| 185 | + * procced without $, { nor }. |
|
| 186 | + * @param array $varProps Member variable properties like |
|
| 187 | + * its visibility. |
|
| 188 | + * |
|
| 189 | + * @return bool true if variable name is prefixed with an underscore only |
|
| 190 | + * when it is about a private variable, false otherwise. |
|
| 191 | + */ |
|
| 192 | + protected function checkVisibilityPrefix(File $phpcsFile, $stackPtr, $varName, $varProps) |
|
| 193 | + { |
|
| 194 | + $isVisibilityPrefixRight = true; |
|
| 195 | + $scope = $varProps['scope']; |
|
| 196 | + // If it's a private variable, it must have an underscore on the front. |
|
| 197 | + if ($scope === 'private' && $varName{0} !== '_') { |
|
| 198 | + $error = "Private variable name \"$varName\" must be prefixed with an underscore"; |
|
| 199 | + $phpcsFile->addError($error, $stackPtr); |
|
| 200 | + $isVisibilityPrefixRight = false; |
|
| 201 | + } else if ($scope !== 'private' && $varName{0} === '_') { |
|
| 202 | + // If it's not a private variable, |
|
| 203 | + // then it must not start with an underscore. |
|
| 204 | + if (isset ($scopeSpecified) && true === $scopeSpecified) { |
|
| 205 | + $error = "Public variable name \"$varName\" must not be prefixed with an underscore"; |
|
| 206 | + } else { |
|
| 207 | + $error = ucfirst($scope) . " variable name \"$varName\" must not be prefixed with an underscore"; |
|
| 208 | + } |
|
| 209 | + $phpcsFile->addError($error, $stackPtr); |
|
| 210 | + $isVisibilityPrefixRight = false; |
|
| 211 | + } |
|
| 212 | + return $isVisibilityPrefixRight; |
|
| 213 | + }//end checkVisibilityPrefix() |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * Checks that variable name length is not too short. Returns true, if it |
|
| 217 | - * meets minimum length requirement, false otherwise. |
|
| 218 | - * |
|
| 219 | - * A variable name is too short if it is shorter than the minimal |
|
| 220 | - * length and it isn't in the list of allowed short names nor declared in a |
|
| 221 | - * for loop (in which it would be nested). |
|
| 222 | - * The minimal length is defined in the function. It is 3 chars now. |
|
| 223 | - * The list of allowed short names is defined in the function. |
|
| 224 | - * It is case-sensitive. It contains only 'ci' now. |
|
| 225 | - * |
|
| 226 | - * @param File $phpcsFile The current file being processed. |
|
| 227 | - * @param int $stackPtr The position of the current token |
|
| 228 | - * in the stack passed in $tokens. |
|
| 229 | - * @param string $varName The name of the variable to |
|
| 230 | - * procced without $, { nor }. |
|
| 231 | - * |
|
| 232 | - * @return bool false if variable name $varName is shorter than the minimal |
|
| 233 | - * length and it isn't in the list of allowed short names nor declared in a |
|
| 234 | - * for loop (in which it would be nested), otherwise true. |
|
| 235 | - */ |
|
| 236 | - protected function checkLength(File $phpcsFile, $stackPtr, $varName) |
|
| 237 | - { |
|
| 238 | - $minLength = 3; |
|
| 239 | - $allowedShortName = array('ci'); |
|
| 215 | + /** |
|
| 216 | + * Checks that variable name length is not too short. Returns true, if it |
|
| 217 | + * meets minimum length requirement, false otherwise. |
|
| 218 | + * |
|
| 219 | + * A variable name is too short if it is shorter than the minimal |
|
| 220 | + * length and it isn't in the list of allowed short names nor declared in a |
|
| 221 | + * for loop (in which it would be nested). |
|
| 222 | + * The minimal length is defined in the function. It is 3 chars now. |
|
| 223 | + * The list of allowed short names is defined in the function. |
|
| 224 | + * It is case-sensitive. It contains only 'ci' now. |
|
| 225 | + * |
|
| 226 | + * @param File $phpcsFile The current file being processed. |
|
| 227 | + * @param int $stackPtr The position of the current token |
|
| 228 | + * in the stack passed in $tokens. |
|
| 229 | + * @param string $varName The name of the variable to |
|
| 230 | + * procced without $, { nor }. |
|
| 231 | + * |
|
| 232 | + * @return bool false if variable name $varName is shorter than the minimal |
|
| 233 | + * length and it isn't in the list of allowed short names nor declared in a |
|
| 234 | + * for loop (in which it would be nested), otherwise true. |
|
| 235 | + */ |
|
| 236 | + protected function checkLength(File $phpcsFile, $stackPtr, $varName) |
|
| 237 | + { |
|
| 238 | + $minLength = 3; |
|
| 239 | + $allowedShortName = array('ci'); |
|
| 240 | 240 | |
| 241 | - $isLengthRight = true; |
|
| 242 | - // cleans variable name |
|
| 243 | - $varName = ltrim($varName, '_'); |
|
| 244 | - if (strlen($varName) <= $minLength) { |
|
| 245 | - // skips adding an error, if it is a specific variable name |
|
| 246 | - if (in_array($varName, $allowedShortName)) { |
|
| 247 | - return $isLengthRight; |
|
| 248 | - } |
|
| 249 | - // skips adding an error, if the variable is in a for loop |
|
| 250 | - if (false !== self::_isInForLoop($phpcsFile, $stackPtr, $varName)) { |
|
| 251 | - return $isLengthRight; |
|
| 252 | - } |
|
| 253 | - // adds the error message finally |
|
| 254 | - $error = 'Very short' |
|
| 255 | - . ( |
|
| 256 | - $minLength > 0 ? |
|
| 257 | - ' (i.e. less than ' . ($minLength + 1) . ' chars)' |
|
| 258 | - : '' |
|
| 259 | - ) |
|
| 260 | - . ', non-word variables like "' . $varName |
|
| 261 | - . '" should only be used as iterators in for() loops.'; |
|
| 262 | - $phpcsFile->addError($error, $stackPtr); |
|
| 263 | - $isLengthRight = false; |
|
| 264 | - } |
|
| 265 | - return $isLengthRight; |
|
| 266 | - }//end checkLength() |
|
| 241 | + $isLengthRight = true; |
|
| 242 | + // cleans variable name |
|
| 243 | + $varName = ltrim($varName, '_'); |
|
| 244 | + if (strlen($varName) <= $minLength) { |
|
| 245 | + // skips adding an error, if it is a specific variable name |
|
| 246 | + if (in_array($varName, $allowedShortName)) { |
|
| 247 | + return $isLengthRight; |
|
| 248 | + } |
|
| 249 | + // skips adding an error, if the variable is in a for loop |
|
| 250 | + if (false !== self::_isInForLoop($phpcsFile, $stackPtr, $varName)) { |
|
| 251 | + return $isLengthRight; |
|
| 252 | + } |
|
| 253 | + // adds the error message finally |
|
| 254 | + $error = 'Very short' |
|
| 255 | + . ( |
|
| 256 | + $minLength > 0 ? |
|
| 257 | + ' (i.e. less than ' . ($minLength + 1) . ' chars)' |
|
| 258 | + : '' |
|
| 259 | + ) |
|
| 260 | + . ', non-word variables like "' . $varName |
|
| 261 | + . '" should only be used as iterators in for() loops.'; |
|
| 262 | + $phpcsFile->addError($error, $stackPtr); |
|
| 263 | + $isLengthRight = false; |
|
| 264 | + } |
|
| 265 | + return $isLengthRight; |
|
| 266 | + }//end checkLength() |
|
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * Returns the position of closest previous T_FOR, if token associated with |
|
| 270 | - * $stackPtr in $phpcsFile is in a for loop, otherwise false. |
|
| 271 | - * |
|
| 272 | - * @param File $phpcsFile The current file being processed. |
|
| 273 | - * @param int $stackPtr The position of the current token |
|
| 274 | - * in the stack passed in $tokens. |
|
| 275 | - * @param string $varName The name of the variable to |
|
| 276 | - * procced without $, { nor }. |
|
| 277 | - * |
|
| 278 | - * @return int|bool Position of T_FOR if token associated with $stackPtr in |
|
| 279 | - * $phpcsFile is in the head of a for loop, otherwise false. |
|
| 280 | - */ |
|
| 281 | - private static function _isInForLoop(File $phpcsFile, $stackPtr, $varName) |
|
| 282 | - { |
|
| 283 | - $keepLookingFromPtr = $stackPtr; |
|
| 284 | - while (false !== $keepLookingFromPtr) { |
|
| 285 | - // looks if it is in (head or body) of a for loop |
|
| 286 | - $forPtr = self::_isInForLoopHead($phpcsFile, $keepLookingFromPtr); |
|
| 287 | - if (false === $forPtr) { |
|
| 288 | - $forPtr = self::_isInForLoopBody($phpcsFile, $keepLookingFromPtr); |
|
| 289 | - } |
|
| 290 | - // checks if it is declared in here and prepares next step |
|
| 291 | - if (false !== $forPtr) { |
|
| 292 | - if (false !== self::_isDeclaredInForLoop($phpcsFile, $forPtr, $varName)) { |
|
| 293 | - return $forPtr; |
|
| 294 | - } |
|
| 295 | - $keepLookingFromPtr = $forPtr; |
|
| 296 | - } else { |
|
| 297 | - $keepLookingFromPtr = false; |
|
| 298 | - } |
|
| 299 | - } |
|
| 300 | - return false; |
|
| 301 | - }//end _isInForLoop() |
|
| 268 | + /** |
|
| 269 | + * Returns the position of closest previous T_FOR, if token associated with |
|
| 270 | + * $stackPtr in $phpcsFile is in a for loop, otherwise false. |
|
| 271 | + * |
|
| 272 | + * @param File $phpcsFile The current file being processed. |
|
| 273 | + * @param int $stackPtr The position of the current token |
|
| 274 | + * in the stack passed in $tokens. |
|
| 275 | + * @param string $varName The name of the variable to |
|
| 276 | + * procced without $, { nor }. |
|
| 277 | + * |
|
| 278 | + * @return int|bool Position of T_FOR if token associated with $stackPtr in |
|
| 279 | + * $phpcsFile is in the head of a for loop, otherwise false. |
|
| 280 | + */ |
|
| 281 | + private static function _isInForLoop(File $phpcsFile, $stackPtr, $varName) |
|
| 282 | + { |
|
| 283 | + $keepLookingFromPtr = $stackPtr; |
|
| 284 | + while (false !== $keepLookingFromPtr) { |
|
| 285 | + // looks if it is in (head or body) of a for loop |
|
| 286 | + $forPtr = self::_isInForLoopHead($phpcsFile, $keepLookingFromPtr); |
|
| 287 | + if (false === $forPtr) { |
|
| 288 | + $forPtr = self::_isInForLoopBody($phpcsFile, $keepLookingFromPtr); |
|
| 289 | + } |
|
| 290 | + // checks if it is declared in here and prepares next step |
|
| 291 | + if (false !== $forPtr) { |
|
| 292 | + if (false !== self::_isDeclaredInForLoop($phpcsFile, $forPtr, $varName)) { |
|
| 293 | + return $forPtr; |
|
| 294 | + } |
|
| 295 | + $keepLookingFromPtr = $forPtr; |
|
| 296 | + } else { |
|
| 297 | + $keepLookingFromPtr = false; |
|
| 298 | + } |
|
| 299 | + } |
|
| 300 | + return false; |
|
| 301 | + }//end _isInForLoop() |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * Returns the position of closest previous T_FOR, if token associated with |
|
| 305 | - * $stackPtr in $phpcsFile is in the head of a for loop, otherwise false. |
|
| 306 | - * The head is the code placed between parenthesis next to the key word |
|
| 307 | - * 'for' : for (<loop_head>) {<loop_body>}. |
|
| 308 | - * |
|
| 309 | - * @param File $phpcsFile The current file being processed. |
|
| 310 | - * @param int $stackPtr The position of the current token |
|
| 311 | - * in the stack passed in $tokens. |
|
| 312 | - * |
|
| 313 | - * @return int|bool Position of T_FOR if token associated with $stackPtr in |
|
| 314 | - * $phpcsFile is in the head of a for loop, otherwise false. |
|
| 315 | - */ |
|
| 316 | - private static function _isInForLoopHead(File $phpcsFile, $stackPtr) |
|
| 317 | - { |
|
| 318 | - $isInForLoop = false; |
|
| 319 | - $tokens = $phpcsFile->getTokens(); |
|
| 320 | - $currentTk = $tokens[$stackPtr]; |
|
| 321 | - if (array_key_exists('nested_parenthesis', $currentTk)) { |
|
| 322 | - $nestedParenthesis = $currentTk['nested_parenthesis']; |
|
| 323 | - foreach ( $nestedParenthesis as $openParPtr => $closeParPtr) { |
|
| 324 | - $nonWhitspacePtr = $phpcsFile->findPrevious( |
|
| 325 | - array(T_WHITESPACE), |
|
| 326 | - $openParPtr - 1, |
|
| 327 | - null, |
|
| 328 | - true, |
|
| 329 | - null, |
|
| 330 | - true |
|
| 331 | - ); |
|
| 332 | - if (false !== $nonWhitspacePtr) { |
|
| 333 | - $isFor = T_FOR === $tokens[$nonWhitspacePtr]['code']; |
|
| 334 | - if ($isFor) { |
|
| 335 | - $isInForLoop = $nonWhitspacePtr; |
|
| 336 | - break; |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - return $isInForLoop; |
|
| 342 | - }//end _isInForLoopHead() |
|
| 303 | + /** |
|
| 304 | + * Returns the position of closest previous T_FOR, if token associated with |
|
| 305 | + * $stackPtr in $phpcsFile is in the head of a for loop, otherwise false. |
|
| 306 | + * The head is the code placed between parenthesis next to the key word |
|
| 307 | + * 'for' : for (<loop_head>) {<loop_body>}. |
|
| 308 | + * |
|
| 309 | + * @param File $phpcsFile The current file being processed. |
|
| 310 | + * @param int $stackPtr The position of the current token |
|
| 311 | + * in the stack passed in $tokens. |
|
| 312 | + * |
|
| 313 | + * @return int|bool Position of T_FOR if token associated with $stackPtr in |
|
| 314 | + * $phpcsFile is in the head of a for loop, otherwise false. |
|
| 315 | + */ |
|
| 316 | + private static function _isInForLoopHead(File $phpcsFile, $stackPtr) |
|
| 317 | + { |
|
| 318 | + $isInForLoop = false; |
|
| 319 | + $tokens = $phpcsFile->getTokens(); |
|
| 320 | + $currentTk = $tokens[$stackPtr]; |
|
| 321 | + if (array_key_exists('nested_parenthesis', $currentTk)) { |
|
| 322 | + $nestedParenthesis = $currentTk['nested_parenthesis']; |
|
| 323 | + foreach ( $nestedParenthesis as $openParPtr => $closeParPtr) { |
|
| 324 | + $nonWhitspacePtr = $phpcsFile->findPrevious( |
|
| 325 | + array(T_WHITESPACE), |
|
| 326 | + $openParPtr - 1, |
|
| 327 | + null, |
|
| 328 | + true, |
|
| 329 | + null, |
|
| 330 | + true |
|
| 331 | + ); |
|
| 332 | + if (false !== $nonWhitspacePtr) { |
|
| 333 | + $isFor = T_FOR === $tokens[$nonWhitspacePtr]['code']; |
|
| 334 | + if ($isFor) { |
|
| 335 | + $isInForLoop = $nonWhitspacePtr; |
|
| 336 | + break; |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + return $isInForLoop; |
|
| 342 | + }//end _isInForLoopHead() |
|
| 343 | 343 | |
| 344 | - /** |
|
| 345 | - * Returns the position of closest previous T_FOR, if token associated with |
|
| 346 | - * $stackPtr in $phpcsFile is in the body of a for loop, otherwise false. |
|
| 347 | - * The body are the instructions placed after parenthesis of a 'for' |
|
| 348 | - * declaration, enclosed with curly brackets usually. |
|
| 349 | - * 'for' : for (<loop_head>) {<loop_body>}. |
|
| 350 | - * |
|
| 351 | - * @param File $phpcsFile The current file being processed. |
|
| 352 | - * @param int $stackPtr The position of the current token |
|
| 353 | - * in the stack passed in $tokens. |
|
| 354 | - * |
|
| 355 | - * @return int|bool Position of T_FOR if token associated with $stackPtr in |
|
| 356 | - * $phpcsFile is in the body of a for loop, otherwise false. |
|
| 357 | - */ |
|
| 358 | - private static function _isInForLoopBody(File $phpcsFile, $stackPtr) |
|
| 359 | - { |
|
| 360 | - $isInForLoop = false; |
|
| 361 | - $tokens = $phpcsFile->getTokens(); |
|
| 362 | - // get englobing hierarchy |
|
| 363 | - $parentPtrAndCode = $tokens[$stackPtr]['conditions']; |
|
| 364 | - krsort($parentPtrAndCode); |
|
| 344 | + /** |
|
| 345 | + * Returns the position of closest previous T_FOR, if token associated with |
|
| 346 | + * $stackPtr in $phpcsFile is in the body of a for loop, otherwise false. |
|
| 347 | + * The body are the instructions placed after parenthesis of a 'for' |
|
| 348 | + * declaration, enclosed with curly brackets usually. |
|
| 349 | + * 'for' : for (<loop_head>) {<loop_body>}. |
|
| 350 | + * |
|
| 351 | + * @param File $phpcsFile The current file being processed. |
|
| 352 | + * @param int $stackPtr The position of the current token |
|
| 353 | + * in the stack passed in $tokens. |
|
| 354 | + * |
|
| 355 | + * @return int|bool Position of T_FOR if token associated with $stackPtr in |
|
| 356 | + * $phpcsFile is in the body of a for loop, otherwise false. |
|
| 357 | + */ |
|
| 358 | + private static function _isInForLoopBody(File $phpcsFile, $stackPtr) |
|
| 359 | + { |
|
| 360 | + $isInForLoop = false; |
|
| 361 | + $tokens = $phpcsFile->getTokens(); |
|
| 362 | + // get englobing hierarchy |
|
| 363 | + $parentPtrAndCode = $tokens[$stackPtr]['conditions']; |
|
| 364 | + krsort($parentPtrAndCode); |
|
| 365 | 365 | |
| 366 | - // looks for a for loop having a body not enclosed with curly brackets, |
|
| 367 | - // which involves that its body contains only one instruction. |
|
| 368 | - if (is_array($parentPtrAndCode) && ! empty($parentPtrAndCode)) { |
|
| 369 | - $parentCode = reset($parentPtrAndCode); |
|
| 370 | - $parentPtr = key($parentPtrAndCode); |
|
| 371 | - $openBracketPtr = $tokens[$parentPtr]['scope_opener']; |
|
| 372 | - } else { |
|
| 373 | - $parentCode = 0; |
|
| 374 | - $parentPtr = 0; |
|
| 375 | - $openBracketPtr = 0; |
|
| 376 | - } |
|
| 377 | - $openResearchScopePtr = $stackPtr; |
|
| 378 | - // recursive search, since a for statement may englobe other inline |
|
| 379 | - // control statement or may be near to function calls, etc... |
|
| 380 | - while (false !== $openResearchScopePtr) { |
|
| 381 | - $closeParPtr = $phpcsFile->findPrevious( |
|
| 382 | - array(T_CLOSE_PARENTHESIS), |
|
| 383 | - $openResearchScopePtr, |
|
| 384 | - null, |
|
| 385 | - false, |
|
| 386 | - null, |
|
| 387 | - true |
|
| 388 | - ); |
|
| 389 | - // is there a closing parenthesis with a control statement before |
|
| 390 | - // the previous instruction ? |
|
| 391 | - if (false !== $closeParPtr) { |
|
| 392 | - // is there no opening curly bracket specific to |
|
| 393 | - // set of instructions, between the closing parenthesis |
|
| 394 | - // and the current token ? |
|
| 395 | - if ($openBracketPtr < $closeParPtr) { |
|
| 396 | - // starts the search from the token before the closing |
|
| 397 | - // parenthesis, if it isn't a for statement |
|
| 398 | - $openResearchScopePtr = $closeParPtr - 1; |
|
| 399 | - // is this parenthesis associated with a for statement ? |
|
| 400 | - $closeParenthesisTk = $tokens[$closeParPtr]; |
|
| 401 | - if (array_key_exists('parenthesis_owner', $closeParenthesisTk)) { |
|
| 402 | - $mayBeForPtr = $closeParenthesisTk['parenthesis_owner']; |
|
| 403 | - $mayBeForTk = $tokens[$mayBeForPtr]; |
|
| 404 | - if (T_FOR === $mayBeForTk['code']) { |
|
| 405 | - return $mayBeForPtr; |
|
| 406 | - } |
|
| 407 | - } |
|
| 408 | - } else { |
|
| 409 | - // if it is about a for loop, don't go further |
|
| 410 | - // and detect it after one more loop execution, do it now |
|
| 411 | - if (T_FOR === $parentCode) { |
|
| 412 | - return $parentPtr; |
|
| 413 | - } |
|
| 414 | - // starts the search from the token before the one |
|
| 415 | - // englobing the current statement |
|
| 416 | - $openResearchScopePtr = $parentPtr - 1; |
|
| 417 | - // re-initialize variables about the englobing structure |
|
| 418 | - if (is_array($parentPtrAndCode)) { |
|
| 419 | - $parentCode = next($parentPtrAndCode); |
|
| 420 | - $parentPtr = key($parentPtrAndCode); |
|
| 421 | - $openBracketPtr = $tokens[$parentPtr]['scope_opener']; |
|
| 422 | - } |
|
| 423 | - } |
|
| 424 | - } else { |
|
| 425 | - $openResearchScopePtr = false; |
|
| 426 | - } |
|
| 427 | - } |
|
| 428 | - // looks for a for loop having a body enclosed with curly brackets |
|
| 429 | - foreach ($parentPtrAndCode as $parentPtr => $parentCode) { |
|
| 430 | - if (T_FOR === $parentCode) { |
|
| 431 | - return $parentPtr; |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - return false; |
|
| 435 | - }//end _isInForLoopBody() |
|
| 366 | + // looks for a for loop having a body not enclosed with curly brackets, |
|
| 367 | + // which involves that its body contains only one instruction. |
|
| 368 | + if (is_array($parentPtrAndCode) && ! empty($parentPtrAndCode)) { |
|
| 369 | + $parentCode = reset($parentPtrAndCode); |
|
| 370 | + $parentPtr = key($parentPtrAndCode); |
|
| 371 | + $openBracketPtr = $tokens[$parentPtr]['scope_opener']; |
|
| 372 | + } else { |
|
| 373 | + $parentCode = 0; |
|
| 374 | + $parentPtr = 0; |
|
| 375 | + $openBracketPtr = 0; |
|
| 376 | + } |
|
| 377 | + $openResearchScopePtr = $stackPtr; |
|
| 378 | + // recursive search, since a for statement may englobe other inline |
|
| 379 | + // control statement or may be near to function calls, etc... |
|
| 380 | + while (false !== $openResearchScopePtr) { |
|
| 381 | + $closeParPtr = $phpcsFile->findPrevious( |
|
| 382 | + array(T_CLOSE_PARENTHESIS), |
|
| 383 | + $openResearchScopePtr, |
|
| 384 | + null, |
|
| 385 | + false, |
|
| 386 | + null, |
|
| 387 | + true |
|
| 388 | + ); |
|
| 389 | + // is there a closing parenthesis with a control statement before |
|
| 390 | + // the previous instruction ? |
|
| 391 | + if (false !== $closeParPtr) { |
|
| 392 | + // is there no opening curly bracket specific to |
|
| 393 | + // set of instructions, between the closing parenthesis |
|
| 394 | + // and the current token ? |
|
| 395 | + if ($openBracketPtr < $closeParPtr) { |
|
| 396 | + // starts the search from the token before the closing |
|
| 397 | + // parenthesis, if it isn't a for statement |
|
| 398 | + $openResearchScopePtr = $closeParPtr - 1; |
|
| 399 | + // is this parenthesis associated with a for statement ? |
|
| 400 | + $closeParenthesisTk = $tokens[$closeParPtr]; |
|
| 401 | + if (array_key_exists('parenthesis_owner', $closeParenthesisTk)) { |
|
| 402 | + $mayBeForPtr = $closeParenthesisTk['parenthesis_owner']; |
|
| 403 | + $mayBeForTk = $tokens[$mayBeForPtr]; |
|
| 404 | + if (T_FOR === $mayBeForTk['code']) { |
|
| 405 | + return $mayBeForPtr; |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + } else { |
|
| 409 | + // if it is about a for loop, don't go further |
|
| 410 | + // and detect it after one more loop execution, do it now |
|
| 411 | + if (T_FOR === $parentCode) { |
|
| 412 | + return $parentPtr; |
|
| 413 | + } |
|
| 414 | + // starts the search from the token before the one |
|
| 415 | + // englobing the current statement |
|
| 416 | + $openResearchScopePtr = $parentPtr - 1; |
|
| 417 | + // re-initialize variables about the englobing structure |
|
| 418 | + if (is_array($parentPtrAndCode)) { |
|
| 419 | + $parentCode = next($parentPtrAndCode); |
|
| 420 | + $parentPtr = key($parentPtrAndCode); |
|
| 421 | + $openBracketPtr = $tokens[$parentPtr]['scope_opener']; |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | + } else { |
|
| 425 | + $openResearchScopePtr = false; |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | + // looks for a for loop having a body enclosed with curly brackets |
|
| 429 | + foreach ($parentPtrAndCode as $parentPtr => $parentCode) { |
|
| 430 | + if (T_FOR === $parentCode) { |
|
| 431 | + return $parentPtr; |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + return false; |
|
| 435 | + }//end _isInForLoopBody() |
|
| 436 | 436 | |
| 437 | - /** |
|
| 438 | - * Returns true if a variable declared in the head of the for loop pointed |
|
| 439 | - * by $forPtr in file $phpcsFile has the name $varName. |
|
| 440 | - * |
|
| 441 | - * @param File $phpcsFile The current file being processed. |
|
| 442 | - * @param int $forPtr The position of the 'for' token |
|
| 443 | - * in the stack passed in $tokens. |
|
| 444 | - * @param string $varName The name of the variable to |
|
| 445 | - * procced without $, { nor }. |
|
| 446 | - * |
|
| 447 | - * @return int|bool true if a variable declared in the head of the for loop |
|
| 448 | - * pointed by $forPtr in file $phpcsFile has the name $varName. |
|
| 449 | - */ |
|
| 450 | - private static function _isDeclaredInForLoop(File $phpcsFile, $forPtr, $varName) |
|
| 451 | - { |
|
| 452 | - $isDeclaredInFor = false; |
|
| 453 | - $tokens = $phpcsFile->getTokens(); |
|
| 454 | - $forVarPtrs = self::_getVarDeclaredInFor($phpcsFile, $forPtr); |
|
| 455 | - foreach ($forVarPtrs as $forVarPtr) { |
|
| 456 | - $forVarTk = $tokens[$forVarPtr]; |
|
| 457 | - // get variable name |
|
| 458 | - $matches = array(); |
|
| 459 | - preg_match('/^\$\{?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}?/', $forVarTk['content'], $matches); |
|
| 460 | - $forVarName = $matches[1]; |
|
| 461 | - if (0 === strcmp($forVarName, $varName)) { |
|
| 462 | - $isDeclaredInFor = $forVarPtr; |
|
| 463 | - break; |
|
| 464 | - } |
|
| 465 | - } |
|
| 466 | - return $isDeclaredInFor; |
|
| 467 | - }//end _isDeclaredInForLoop() |
|
| 437 | + /** |
|
| 438 | + * Returns true if a variable declared in the head of the for loop pointed |
|
| 439 | + * by $forPtr in file $phpcsFile has the name $varName. |
|
| 440 | + * |
|
| 441 | + * @param File $phpcsFile The current file being processed. |
|
| 442 | + * @param int $forPtr The position of the 'for' token |
|
| 443 | + * in the stack passed in $tokens. |
|
| 444 | + * @param string $varName The name of the variable to |
|
| 445 | + * procced without $, { nor }. |
|
| 446 | + * |
|
| 447 | + * @return int|bool true if a variable declared in the head of the for loop |
|
| 448 | + * pointed by $forPtr in file $phpcsFile has the name $varName. |
|
| 449 | + */ |
|
| 450 | + private static function _isDeclaredInForLoop(File $phpcsFile, $forPtr, $varName) |
|
| 451 | + { |
|
| 452 | + $isDeclaredInFor = false; |
|
| 453 | + $tokens = $phpcsFile->getTokens(); |
|
| 454 | + $forVarPtrs = self::_getVarDeclaredInFor($phpcsFile, $forPtr); |
|
| 455 | + foreach ($forVarPtrs as $forVarPtr) { |
|
| 456 | + $forVarTk = $tokens[$forVarPtr]; |
|
| 457 | + // get variable name |
|
| 458 | + $matches = array(); |
|
| 459 | + preg_match('/^\$\{?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}?/', $forVarTk['content'], $matches); |
|
| 460 | + $forVarName = $matches[1]; |
|
| 461 | + if (0 === strcmp($forVarName, $varName)) { |
|
| 462 | + $isDeclaredInFor = $forVarPtr; |
|
| 463 | + break; |
|
| 464 | + } |
|
| 465 | + } |
|
| 466 | + return $isDeclaredInFor; |
|
| 467 | + }//end _isDeclaredInForLoop() |
|
| 468 | 468 | |
| 469 | - /** |
|
| 470 | - * Returns list of pointers to variables declared in for loop associated to |
|
| 471 | - * $forPtr in file $phpcsFile. |
|
| 472 | - * |
|
| 473 | - * All pointers in the result list are pointing to token with code |
|
| 474 | - * T_VARIABLE. An exception is raised, if $forPtr doesn't point a token with |
|
| 475 | - * code T_FOR. |
|
| 476 | - * |
|
| 477 | - * @param File $phpcsFile The current file being processed. |
|
| 478 | - * @param int $forPtr The position of the current token |
|
| 479 | - * in the stack passed in $tokens. |
|
| 480 | - * |
|
| 481 | - * @return array List of pointers to variables declared in for loop $forPtr. |
|
| 482 | - */ |
|
| 483 | - private static function _getVarDeclaredInFor(File $phpcsFile, $forPtr) |
|
| 484 | - { |
|
| 485 | - $tokens = $phpcsFile->getTokens(); |
|
| 486 | - $forTk = $tokens[$forPtr]; |
|
| 487 | - if (T_FOR !== $forTk['code']) { |
|
| 488 | - throw new PHP_CodeSniffer_Exception('$forPtr must be of type T_FOR'); |
|
| 489 | - } |
|
| 490 | - $openParPtr = $forTk['parenthesis_opener']; |
|
| 491 | - $openParenthesisTk = $tokens[$openParPtr]; |
|
| 492 | - $endOfDeclPtr = $phpcsFile->findNext(array(T_SEMICOLON), $openParPtr); |
|
| 493 | - $forVarPtrs = array(); |
|
| 494 | - $varPtr = $phpcsFile->findNext( |
|
| 495 | - array(T_VARIABLE), |
|
| 496 | - $openParPtr + 1, |
|
| 497 | - $endOfDeclPtr |
|
| 498 | - ); |
|
| 499 | - while (false !== $varPtr) { |
|
| 500 | - $forVarPtrs [] = $varPtr; |
|
| 501 | - $varPtr = $phpcsFile->findNext( |
|
| 502 | - array(T_VARIABLE), |
|
| 503 | - $varPtr + 1, |
|
| 504 | - $endOfDeclPtr |
|
| 505 | - ); |
|
| 506 | - } |
|
| 507 | - return $forVarPtrs; |
|
| 508 | - }//end _getVarDeclaredInFor() |
|
| 469 | + /** |
|
| 470 | + * Returns list of pointers to variables declared in for loop associated to |
|
| 471 | + * $forPtr in file $phpcsFile. |
|
| 472 | + * |
|
| 473 | + * All pointers in the result list are pointing to token with code |
|
| 474 | + * T_VARIABLE. An exception is raised, if $forPtr doesn't point a token with |
|
| 475 | + * code T_FOR. |
|
| 476 | + * |
|
| 477 | + * @param File $phpcsFile The current file being processed. |
|
| 478 | + * @param int $forPtr The position of the current token |
|
| 479 | + * in the stack passed in $tokens. |
|
| 480 | + * |
|
| 481 | + * @return array List of pointers to variables declared in for loop $forPtr. |
|
| 482 | + */ |
|
| 483 | + private static function _getVarDeclaredInFor(File $phpcsFile, $forPtr) |
|
| 484 | + { |
|
| 485 | + $tokens = $phpcsFile->getTokens(); |
|
| 486 | + $forTk = $tokens[$forPtr]; |
|
| 487 | + if (T_FOR !== $forTk['code']) { |
|
| 488 | + throw new PHP_CodeSniffer_Exception('$forPtr must be of type T_FOR'); |
|
| 489 | + } |
|
| 490 | + $openParPtr = $forTk['parenthesis_opener']; |
|
| 491 | + $openParenthesisTk = $tokens[$openParPtr]; |
|
| 492 | + $endOfDeclPtr = $phpcsFile->findNext(array(T_SEMICOLON), $openParPtr); |
|
| 493 | + $forVarPtrs = array(); |
|
| 494 | + $varPtr = $phpcsFile->findNext( |
|
| 495 | + array(T_VARIABLE), |
|
| 496 | + $openParPtr + 1, |
|
| 497 | + $endOfDeclPtr |
|
| 498 | + ); |
|
| 499 | + while (false !== $varPtr) { |
|
| 500 | + $forVarPtrs [] = $varPtr; |
|
| 501 | + $varPtr = $phpcsFile->findNext( |
|
| 502 | + array(T_VARIABLE), |
|
| 503 | + $varPtr + 1, |
|
| 504 | + $endOfDeclPtr |
|
| 505 | + ); |
|
| 506 | + } |
|
| 507 | + return $forVarPtrs; |
|
| 508 | + }//end _getVarDeclaredInFor() |
|
| 509 | 509 | |
| 510 | - /** |
|
| 511 | - * Returns the position of first occurrence of a PHP variable starting with |
|
| 512 | - * $ in $haystack from $offset. |
|
| 513 | - * |
|
| 514 | - * @param string $haystack The string to search in. |
|
| 515 | - * @param int $offset The optional offset parameter allows you to |
|
| 516 | - * specify which character in haystack to start |
|
| 517 | - * searching. The returned position is still |
|
| 518 | - * relative to the beginning of haystack. |
|
| 519 | - * |
|
| 520 | - * @return mixed The position as an integer |
|
| 521 | - * or the boolean false, if no variable is found. |
|
| 522 | - */ |
|
| 523 | - private static function _getVariablePosition($haystack, $offset = 0) |
|
| 524 | - { |
|
| 525 | - $var_starts_at = strpos($haystack, '$', $offset); |
|
| 526 | - $is_a_var = false; |
|
| 527 | - while (false !== $var_starts_at && ! $is_a_var) { |
|
| 528 | - // makes sure that $ is used for a variable and not as a symbol, |
|
| 529 | - // if $ is protected with the escape char, then it is a symbol. |
|
| 530 | - if (0 !== strcmp($haystack[$var_starts_at - 1], '\\')) { |
|
| 531 | - if (0 === strcmp($haystack[$var_starts_at + 1], '{')) { |
|
| 532 | - // there is an opening brace in the right place |
|
| 533 | - // so it looks for the closing brace in the right place |
|
| 534 | - $hsChunk2 = substr($haystack, $var_starts_at + 2); |
|
| 535 | - if (1 === preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\}/', $hsChunk2)) { |
|
| 536 | - $is_a_var = true; |
|
| 537 | - } |
|
| 538 | - } else { |
|
| 539 | - $hsChunk1 = substr($haystack, $var_starts_at + 1); |
|
| 540 | - if (1 === preg_match('/^[a-zA-Z_\x7f-\xff]/', $hsChunk1)) { |
|
| 541 | - // $ is used for a variable and not as a symbol, |
|
| 542 | - // since what follows $ matchs the definition of |
|
| 543 | - // a variable label for PHP. |
|
| 544 | - $is_a_var = true; |
|
| 545 | - } |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - // update $var_starts_at for the next variable |
|
| 549 | - // only if no variable was found, since it is returned otherwise. |
|
| 550 | - if ( ! $is_a_var) { |
|
| 551 | - $var_starts_at = strpos($haystack, '$', $var_starts_at + 1); |
|
| 552 | - } |
|
| 553 | - } |
|
| 554 | - if ($is_a_var) { |
|
| 555 | - return $var_starts_at; |
|
| 556 | - } else { |
|
| 557 | - return false; |
|
| 558 | - } |
|
| 559 | - }//end _getVariablePosition() |
|
| 510 | + /** |
|
| 511 | + * Returns the position of first occurrence of a PHP variable starting with |
|
| 512 | + * $ in $haystack from $offset. |
|
| 513 | + * |
|
| 514 | + * @param string $haystack The string to search in. |
|
| 515 | + * @param int $offset The optional offset parameter allows you to |
|
| 516 | + * specify which character in haystack to start |
|
| 517 | + * searching. The returned position is still |
|
| 518 | + * relative to the beginning of haystack. |
|
| 519 | + * |
|
| 520 | + * @return mixed The position as an integer |
|
| 521 | + * or the boolean false, if no variable is found. |
|
| 522 | + */ |
|
| 523 | + private static function _getVariablePosition($haystack, $offset = 0) |
|
| 524 | + { |
|
| 525 | + $var_starts_at = strpos($haystack, '$', $offset); |
|
| 526 | + $is_a_var = false; |
|
| 527 | + while (false !== $var_starts_at && ! $is_a_var) { |
|
| 528 | + // makes sure that $ is used for a variable and not as a symbol, |
|
| 529 | + // if $ is protected with the escape char, then it is a symbol. |
|
| 530 | + if (0 !== strcmp($haystack[$var_starts_at - 1], '\\')) { |
|
| 531 | + if (0 === strcmp($haystack[$var_starts_at + 1], '{')) { |
|
| 532 | + // there is an opening brace in the right place |
|
| 533 | + // so it looks for the closing brace in the right place |
|
| 534 | + $hsChunk2 = substr($haystack, $var_starts_at + 2); |
|
| 535 | + if (1 === preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\}/', $hsChunk2)) { |
|
| 536 | + $is_a_var = true; |
|
| 537 | + } |
|
| 538 | + } else { |
|
| 539 | + $hsChunk1 = substr($haystack, $var_starts_at + 1); |
|
| 540 | + if (1 === preg_match('/^[a-zA-Z_\x7f-\xff]/', $hsChunk1)) { |
|
| 541 | + // $ is used for a variable and not as a symbol, |
|
| 542 | + // since what follows $ matchs the definition of |
|
| 543 | + // a variable label for PHP. |
|
| 544 | + $is_a_var = true; |
|
| 545 | + } |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + // update $var_starts_at for the next variable |
|
| 549 | + // only if no variable was found, since it is returned otherwise. |
|
| 550 | + if ( ! $is_a_var) { |
|
| 551 | + $var_starts_at = strpos($haystack, '$', $var_starts_at + 1); |
|
| 552 | + } |
|
| 553 | + } |
|
| 554 | + if ($is_a_var) { |
|
| 555 | + return $var_starts_at; |
|
| 556 | + } else { |
|
| 557 | + return false; |
|
| 558 | + } |
|
| 559 | + }//end _getVariablePosition() |
|
| 560 | 560 | }//end class |
| 561 | 561 | |
| 562 | 562 | ?> |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | $varName = substr($varTk['content'], 1); |
| 62 | 62 | $varProps = $phpcsFile->getMemberProperties($stackPtr); |
| 63 | 63 | // check(s) |
| 64 | - if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName) ) { |
|
| 64 | + if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) { |
|
| 65 | 65 | return; |
| 66 | 66 | } |
| 67 | 67 | if ( ! $this->checkVisibilityPrefix($phpcsFile, $stackPtr, $varName, $varProps)) { |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | $currentTk = $tokens[$stackPtr]; |
| 321 | 321 | if (array_key_exists('nested_parenthesis', $currentTk)) { |
| 322 | 322 | $nestedParenthesis = $currentTk['nested_parenthesis']; |
| 323 | - foreach ( $nestedParenthesis as $openParPtr => $closeParPtr) { |
|
| 323 | + foreach ($nestedParenthesis as $openParPtr => $closeParPtr) { |
|
| 324 | 324 | $nonWhitspacePtr = $phpcsFile->findPrevious( |
| 325 | 325 | array(T_WHITESPACE), |
| 326 | 326 | $openParPtr - 1, |
@@ -40,8 +40,7 @@ discard block |
||
| 40 | 40 | use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; |
| 41 | 41 | use PHP_CodeSniffer\Files\File; |
| 42 | 42 | |
| 43 | -class ValidVariableNameSniff extends AbstractVariableSniff |
|
| 44 | -{ |
|
| 43 | +class ValidVariableNameSniff extends AbstractVariableSniff { |
|
| 45 | 44 | |
| 46 | 45 | |
| 47 | 46 | /** |
@@ -61,13 +60,16 @@ discard block |
||
| 61 | 60 | $varName = substr($varTk['content'], 1); |
| 62 | 61 | $varProps = $phpcsFile->getMemberProperties($stackPtr); |
| 63 | 62 | // check(s) |
| 64 | - if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName) ) { |
|
| 63 | + if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName) ) |
|
| 64 | + { |
|
| 65 | 65 | return; |
| 66 | 66 | } |
| 67 | - if ( ! $this->checkVisibilityPrefix($phpcsFile, $stackPtr, $varName, $varProps)) { |
|
| 67 | + if ( ! $this->checkVisibilityPrefix($phpcsFile, $stackPtr, $varName, $varProps)) |
|
| 68 | + { |
|
| 68 | 69 | return; |
| 69 | 70 | } |
| 70 | - if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) { |
|
| 71 | + if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) |
|
| 72 | + { |
|
| 71 | 73 | return; |
| 72 | 74 | } |
| 73 | 75 | |
@@ -89,14 +91,17 @@ discard block |
||
| 89 | 91 | $varTk = $tokens[$stackPtr]; |
| 90 | 92 | $varName = substr($varTk['content'], 1); |
| 91 | 93 | // skip the current object variable, i.e. $this |
| 92 | - if (0 === strcmp($varName, 'this')) { |
|
| 94 | + if (0 === strcmp($varName, 'this')) |
|
| 95 | + { |
|
| 93 | 96 | return; |
| 94 | 97 | } |
| 95 | 98 | // check(s) |
| 96 | - if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) { |
|
| 99 | + if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) |
|
| 100 | + { |
|
| 97 | 101 | return; |
| 98 | 102 | } |
| 99 | - if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) { |
|
| 103 | + if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) |
|
| 104 | + { |
|
| 100 | 105 | return; |
| 101 | 106 | } |
| 102 | 107 | |
@@ -117,16 +122,19 @@ discard block |
||
| 117 | 122 | $stringTk = $tokens[$stackPtr]; |
| 118 | 123 | $stringString = $stringTk['content']; |
| 119 | 124 | $varAt = self::_getVariablePosition($stringString, 0); |
| 120 | - while (false !== $varAt) { |
|
| 125 | + while (false !== $varAt) |
|
| 126 | + { |
|
| 121 | 127 | // get variable name |
| 122 | 128 | $matches = array(); |
| 123 | 129 | preg_match('/^\$\{?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}?/', substr($stringString, $varAt), $matches); |
| 124 | 130 | $varName = $matches[1]; |
| 125 | 131 | // check(s) |
| 126 | - if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) { |
|
| 132 | + if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) |
|
| 133 | + { |
|
| 127 | 134 | return; |
| 128 | 135 | } |
| 129 | - if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) { |
|
| 136 | + if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) |
|
| 137 | + { |
|
| 130 | 138 | return; |
| 131 | 139 | } |
| 132 | 140 | // prepare checking next variable |
@@ -152,14 +160,18 @@ discard block |
||
| 152 | 160 | protected function checkLowerCase(File $phpcsFile, $stackPtr, $varName) |
| 153 | 161 | { |
| 154 | 162 | $isInLowerCase = true; |
| 155 | - if (0 !== strcmp($varName, strtolower($varName))) { |
|
| 163 | + if (0 !== strcmp($varName, strtolower($varName))) |
|
| 164 | + { |
|
| 156 | 165 | // get the expected variable name |
| 157 | 166 | $varNameWithUnderscores = preg_replace('/([A-Z])/', '_${1}', $varName); |
| 158 | 167 | $expectedVarName = strtolower(ltrim($varNameWithUnderscores, '_')); |
| 159 | 168 | // adapts the error message to the error case |
| 160 | - if (strlen($varNameWithUnderscores) > strlen($varName)) { |
|
| 169 | + if (strlen($varNameWithUnderscores) > strlen($varName)) |
|
| 170 | + { |
|
| 161 | 171 | $error = 'Variables should not use CamelCasing or start with a Capital.'; |
| 162 | - } else { |
|
| 172 | + } |
|
| 173 | + else |
|
| 174 | + { |
|
| 163 | 175 | $error = 'Variables should be entirely lowercased.'; |
| 164 | 176 | } |
| 165 | 177 | $error = $error . 'Please consider "' . $expectedVarName |
@@ -194,16 +206,22 @@ discard block |
||
| 194 | 206 | $isVisibilityPrefixRight = true; |
| 195 | 207 | $scope = $varProps['scope']; |
| 196 | 208 | // If it's a private variable, it must have an underscore on the front. |
| 197 | - if ($scope === 'private' && $varName{0} !== '_') { |
|
| 209 | + if ($scope === 'private' && $varName{0} !== '_') |
|
| 210 | + { |
|
| 198 | 211 | $error = "Private variable name \"$varName\" must be prefixed with an underscore"; |
| 199 | 212 | $phpcsFile->addError($error, $stackPtr); |
| 200 | 213 | $isVisibilityPrefixRight = false; |
| 201 | - } else if ($scope !== 'private' && $varName{0} === '_') { |
|
| 214 | + } |
|
| 215 | + else if ($scope !== 'private' && $varName{0} === '_') |
|
| 216 | + { |
|
| 202 | 217 | // If it's not a private variable, |
| 203 | 218 | // then it must not start with an underscore. |
| 204 | - if (isset ($scopeSpecified) && true === $scopeSpecified) { |
|
| 219 | + if (isset ($scopeSpecified) && true === $scopeSpecified) |
|
| 220 | + { |
|
| 205 | 221 | $error = "Public variable name \"$varName\" must not be prefixed with an underscore"; |
| 206 | - } else { |
|
| 222 | + } |
|
| 223 | + else |
|
| 224 | + { |
|
| 207 | 225 | $error = ucfirst($scope) . " variable name \"$varName\" must not be prefixed with an underscore"; |
| 208 | 226 | } |
| 209 | 227 | $phpcsFile->addError($error, $stackPtr); |
@@ -241,13 +259,16 @@ discard block |
||
| 241 | 259 | $isLengthRight = true; |
| 242 | 260 | // cleans variable name |
| 243 | 261 | $varName = ltrim($varName, '_'); |
| 244 | - if (strlen($varName) <= $minLength) { |
|
| 262 | + if (strlen($varName) <= $minLength) |
|
| 263 | + { |
|
| 245 | 264 | // skips adding an error, if it is a specific variable name |
| 246 | - if (in_array($varName, $allowedShortName)) { |
|
| 265 | + if (in_array($varName, $allowedShortName)) |
|
| 266 | + { |
|
| 247 | 267 | return $isLengthRight; |
| 248 | 268 | } |
| 249 | 269 | // skips adding an error, if the variable is in a for loop |
| 250 | - if (false !== self::_isInForLoop($phpcsFile, $stackPtr, $varName)) { |
|
| 270 | + if (false !== self::_isInForLoop($phpcsFile, $stackPtr, $varName)) |
|
| 271 | + { |
|
| 251 | 272 | return $isLengthRight; |
| 252 | 273 | } |
| 253 | 274 | // adds the error message finally |
@@ -281,19 +302,25 @@ discard block |
||
| 281 | 302 | private static function _isInForLoop(File $phpcsFile, $stackPtr, $varName) |
| 282 | 303 | { |
| 283 | 304 | $keepLookingFromPtr = $stackPtr; |
| 284 | - while (false !== $keepLookingFromPtr) { |
|
| 305 | + while (false !== $keepLookingFromPtr) |
|
| 306 | + { |
|
| 285 | 307 | // looks if it is in (head or body) of a for loop |
| 286 | 308 | $forPtr = self::_isInForLoopHead($phpcsFile, $keepLookingFromPtr); |
| 287 | - if (false === $forPtr) { |
|
| 309 | + if (false === $forPtr) |
|
| 310 | + { |
|
| 288 | 311 | $forPtr = self::_isInForLoopBody($phpcsFile, $keepLookingFromPtr); |
| 289 | 312 | } |
| 290 | 313 | // checks if it is declared in here and prepares next step |
| 291 | - if (false !== $forPtr) { |
|
| 292 | - if (false !== self::_isDeclaredInForLoop($phpcsFile, $forPtr, $varName)) { |
|
| 314 | + if (false !== $forPtr) |
|
| 315 | + { |
|
| 316 | + if (false !== self::_isDeclaredInForLoop($phpcsFile, $forPtr, $varName)) |
|
| 317 | + { |
|
| 293 | 318 | return $forPtr; |
| 294 | 319 | } |
| 295 | 320 | $keepLookingFromPtr = $forPtr; |
| 296 | - } else { |
|
| 321 | + } |
|
| 322 | + else |
|
| 323 | + { |
|
| 297 | 324 | $keepLookingFromPtr = false; |
| 298 | 325 | } |
| 299 | 326 | } |
@@ -318,9 +345,11 @@ discard block |
||
| 318 | 345 | $isInForLoop = false; |
| 319 | 346 | $tokens = $phpcsFile->getTokens(); |
| 320 | 347 | $currentTk = $tokens[$stackPtr]; |
| 321 | - if (array_key_exists('nested_parenthesis', $currentTk)) { |
|
| 348 | + if (array_key_exists('nested_parenthesis', $currentTk)) |
|
| 349 | + { |
|
| 322 | 350 | $nestedParenthesis = $currentTk['nested_parenthesis']; |
| 323 | - foreach ( $nestedParenthesis as $openParPtr => $closeParPtr) { |
|
| 351 | + foreach ( $nestedParenthesis as $openParPtr => $closeParPtr) |
|
| 352 | + { |
|
| 324 | 353 | $nonWhitspacePtr = $phpcsFile->findPrevious( |
| 325 | 354 | array(T_WHITESPACE), |
| 326 | 355 | $openParPtr - 1, |
@@ -329,9 +358,11 @@ discard block |
||
| 329 | 358 | null, |
| 330 | 359 | true |
| 331 | 360 | ); |
| 332 | - if (false !== $nonWhitspacePtr) { |
|
| 361 | + if (false !== $nonWhitspacePtr) |
|
| 362 | + { |
|
| 333 | 363 | $isFor = T_FOR === $tokens[$nonWhitspacePtr]['code']; |
| 334 | - if ($isFor) { |
|
| 364 | + if ($isFor) |
|
| 365 | + { |
|
| 335 | 366 | $isInForLoop = $nonWhitspacePtr; |
| 336 | 367 | break; |
| 337 | 368 | } |
@@ -365,11 +396,14 @@ discard block |
||
| 365 | 396 | |
| 366 | 397 | // looks for a for loop having a body not enclosed with curly brackets, |
| 367 | 398 | // which involves that its body contains only one instruction. |
| 368 | - if (is_array($parentPtrAndCode) && ! empty($parentPtrAndCode)) { |
|
| 399 | + if (is_array($parentPtrAndCode) && ! empty($parentPtrAndCode)) |
|
| 400 | + { |
|
| 369 | 401 | $parentCode = reset($parentPtrAndCode); |
| 370 | 402 | $parentPtr = key($parentPtrAndCode); |
| 371 | 403 | $openBracketPtr = $tokens[$parentPtr]['scope_opener']; |
| 372 | - } else { |
|
| 404 | + } |
|
| 405 | + else |
|
| 406 | + { |
|
| 373 | 407 | $parentCode = 0; |
| 374 | 408 | $parentPtr = 0; |
| 375 | 409 | $openBracketPtr = 0; |
@@ -377,7 +411,8 @@ discard block |
||
| 377 | 411 | $openResearchScopePtr = $stackPtr; |
| 378 | 412 | // recursive search, since a for statement may englobe other inline |
| 379 | 413 | // control statement or may be near to function calls, etc... |
| 380 | - while (false !== $openResearchScopePtr) { |
|
| 414 | + while (false !== $openResearchScopePtr) |
|
| 415 | + { |
|
| 381 | 416 | $closeParPtr = $phpcsFile->findPrevious( |
| 382 | 417 | array(T_CLOSE_PARENTHESIS), |
| 383 | 418 | $openResearchScopePtr, |
@@ -388,46 +423,58 @@ discard block |
||
| 388 | 423 | ); |
| 389 | 424 | // is there a closing parenthesis with a control statement before |
| 390 | 425 | // the previous instruction ? |
| 391 | - if (false !== $closeParPtr) { |
|
| 426 | + if (false !== $closeParPtr) |
|
| 427 | + { |
|
| 392 | 428 | // is there no opening curly bracket specific to |
| 393 | 429 | // set of instructions, between the closing parenthesis |
| 394 | 430 | // and the current token ? |
| 395 | - if ($openBracketPtr < $closeParPtr) { |
|
| 431 | + if ($openBracketPtr < $closeParPtr) |
|
| 432 | + { |
|
| 396 | 433 | // starts the search from the token before the closing |
| 397 | 434 | // parenthesis, if it isn't a for statement |
| 398 | 435 | $openResearchScopePtr = $closeParPtr - 1; |
| 399 | 436 | // is this parenthesis associated with a for statement ? |
| 400 | 437 | $closeParenthesisTk = $tokens[$closeParPtr]; |
| 401 | - if (array_key_exists('parenthesis_owner', $closeParenthesisTk)) { |
|
| 438 | + if (array_key_exists('parenthesis_owner', $closeParenthesisTk)) |
|
| 439 | + { |
|
| 402 | 440 | $mayBeForPtr = $closeParenthesisTk['parenthesis_owner']; |
| 403 | 441 | $mayBeForTk = $tokens[$mayBeForPtr]; |
| 404 | - if (T_FOR === $mayBeForTk['code']) { |
|
| 442 | + if (T_FOR === $mayBeForTk['code']) |
|
| 443 | + { |
|
| 405 | 444 | return $mayBeForPtr; |
| 406 | 445 | } |
| 407 | 446 | } |
| 408 | - } else { |
|
| 447 | + } |
|
| 448 | + else |
|
| 449 | + { |
|
| 409 | 450 | // if it is about a for loop, don't go further |
| 410 | 451 | // and detect it after one more loop execution, do it now |
| 411 | - if (T_FOR === $parentCode) { |
|
| 452 | + if (T_FOR === $parentCode) |
|
| 453 | + { |
|
| 412 | 454 | return $parentPtr; |
| 413 | 455 | } |
| 414 | 456 | // starts the search from the token before the one |
| 415 | 457 | // englobing the current statement |
| 416 | 458 | $openResearchScopePtr = $parentPtr - 1; |
| 417 | 459 | // re-initialize variables about the englobing structure |
| 418 | - if (is_array($parentPtrAndCode)) { |
|
| 460 | + if (is_array($parentPtrAndCode)) |
|
| 461 | + { |
|
| 419 | 462 | $parentCode = next($parentPtrAndCode); |
| 420 | 463 | $parentPtr = key($parentPtrAndCode); |
| 421 | 464 | $openBracketPtr = $tokens[$parentPtr]['scope_opener']; |
| 422 | 465 | } |
| 423 | 466 | } |
| 424 | - } else { |
|
| 467 | + } |
|
| 468 | + else |
|
| 469 | + { |
|
| 425 | 470 | $openResearchScopePtr = false; |
| 426 | 471 | } |
| 427 | 472 | } |
| 428 | 473 | // looks for a for loop having a body enclosed with curly brackets |
| 429 | - foreach ($parentPtrAndCode as $parentPtr => $parentCode) { |
|
| 430 | - if (T_FOR === $parentCode) { |
|
| 474 | + foreach ($parentPtrAndCode as $parentPtr => $parentCode) |
|
| 475 | + { |
|
| 476 | + if (T_FOR === $parentCode) |
|
| 477 | + { |
|
| 431 | 478 | return $parentPtr; |
| 432 | 479 | } |
| 433 | 480 | } |
@@ -452,13 +499,15 @@ discard block |
||
| 452 | 499 | $isDeclaredInFor = false; |
| 453 | 500 | $tokens = $phpcsFile->getTokens(); |
| 454 | 501 | $forVarPtrs = self::_getVarDeclaredInFor($phpcsFile, $forPtr); |
| 455 | - foreach ($forVarPtrs as $forVarPtr) { |
|
| 502 | + foreach ($forVarPtrs as $forVarPtr) |
|
| 503 | + { |
|
| 456 | 504 | $forVarTk = $tokens[$forVarPtr]; |
| 457 | 505 | // get variable name |
| 458 | 506 | $matches = array(); |
| 459 | 507 | preg_match('/^\$\{?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}?/', $forVarTk['content'], $matches); |
| 460 | 508 | $forVarName = $matches[1]; |
| 461 | - if (0 === strcmp($forVarName, $varName)) { |
|
| 509 | + if (0 === strcmp($forVarName, $varName)) |
|
| 510 | + { |
|
| 462 | 511 | $isDeclaredInFor = $forVarPtr; |
| 463 | 512 | break; |
| 464 | 513 | } |
@@ -484,7 +533,8 @@ discard block |
||
| 484 | 533 | { |
| 485 | 534 | $tokens = $phpcsFile->getTokens(); |
| 486 | 535 | $forTk = $tokens[$forPtr]; |
| 487 | - if (T_FOR !== $forTk['code']) { |
|
| 536 | + if (T_FOR !== $forTk['code']) |
|
| 537 | + { |
|
| 488 | 538 | throw new PHP_CodeSniffer_Exception('$forPtr must be of type T_FOR'); |
| 489 | 539 | } |
| 490 | 540 | $openParPtr = $forTk['parenthesis_opener']; |
@@ -496,7 +546,8 @@ discard block |
||
| 496 | 546 | $openParPtr + 1, |
| 497 | 547 | $endOfDeclPtr |
| 498 | 548 | ); |
| 499 | - while (false !== $varPtr) { |
|
| 549 | + while (false !== $varPtr) |
|
| 550 | + { |
|
| 500 | 551 | $forVarPtrs [] = $varPtr; |
| 501 | 552 | $varPtr = $phpcsFile->findNext( |
| 502 | 553 | array(T_VARIABLE), |
@@ -524,20 +575,27 @@ discard block |
||
| 524 | 575 | { |
| 525 | 576 | $var_starts_at = strpos($haystack, '$', $offset); |
| 526 | 577 | $is_a_var = false; |
| 527 | - while (false !== $var_starts_at && ! $is_a_var) { |
|
| 578 | + while (false !== $var_starts_at && ! $is_a_var) |
|
| 579 | + { |
|
| 528 | 580 | // makes sure that $ is used for a variable and not as a symbol, |
| 529 | 581 | // if $ is protected with the escape char, then it is a symbol. |
| 530 | - if (0 !== strcmp($haystack[$var_starts_at - 1], '\\')) { |
|
| 531 | - if (0 === strcmp($haystack[$var_starts_at + 1], '{')) { |
|
| 582 | + if (0 !== strcmp($haystack[$var_starts_at - 1], '\\')) |
|
| 583 | + { |
|
| 584 | + if (0 === strcmp($haystack[$var_starts_at + 1], '{')) |
|
| 585 | + { |
|
| 532 | 586 | // there is an opening brace in the right place |
| 533 | 587 | // so it looks for the closing brace in the right place |
| 534 | 588 | $hsChunk2 = substr($haystack, $var_starts_at + 2); |
| 535 | - if (1 === preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\}/', $hsChunk2)) { |
|
| 589 | + if (1 === preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\}/', $hsChunk2)) |
|
| 590 | + { |
|
| 536 | 591 | $is_a_var = true; |
| 537 | 592 | } |
| 538 | - } else { |
|
| 593 | + } |
|
| 594 | + else |
|
| 595 | + { |
|
| 539 | 596 | $hsChunk1 = substr($haystack, $var_starts_at + 1); |
| 540 | - if (1 === preg_match('/^[a-zA-Z_\x7f-\xff]/', $hsChunk1)) { |
|
| 597 | + if (1 === preg_match('/^[a-zA-Z_\x7f-\xff]/', $hsChunk1)) |
|
| 598 | + { |
|
| 541 | 599 | // $ is used for a variable and not as a symbol, |
| 542 | 600 | // since what follows $ matchs the definition of |
| 543 | 601 | // a variable label for PHP. |
@@ -547,13 +605,17 @@ discard block |
||
| 547 | 605 | } |
| 548 | 606 | // update $var_starts_at for the next variable |
| 549 | 607 | // only if no variable was found, since it is returned otherwise. |
| 550 | - if ( ! $is_a_var) { |
|
| 608 | + if ( ! $is_a_var) |
|
| 609 | + { |
|
| 551 | 610 | $var_starts_at = strpos($haystack, '$', $var_starts_at + 1); |
| 552 | 611 | } |
| 553 | 612 | } |
| 554 | - if ($is_a_var) { |
|
| 613 | + if ($is_a_var) |
|
| 614 | + { |
|
| 555 | 615 | return $var_starts_at; |
| 556 | - } else { |
|
| 616 | + } |
|
| 617 | + else |
|
| 618 | + { |
|
| 557 | 619 | return false; |
| 558 | 620 | } |
| 559 | 621 | }//end _getVariablePosition() |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | $stringTk = $tokens[$stackPtr]; |
| 118 | 118 | $stringString = $stringTk['content']; |
| 119 | 119 | $varAt = self::_getVariablePosition($stringString, 0); |
| 120 | - while (false !== $varAt) { |
|
| 120 | + while (FALSE !== $varAt) { |
|
| 121 | 121 | // get variable name |
| 122 | 122 | $matches = array(); |
| 123 | 123 | preg_match('/^\$\{?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}?/', substr($stringString, $varAt), $matches); |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | protected function checkLowerCase(File $phpcsFile, $stackPtr, $varName) |
| 153 | 153 | { |
| 154 | - $isInLowerCase = true; |
|
| 154 | + $isInLowerCase = TRUE; |
|
| 155 | 155 | if (0 !== strcmp($varName, strtolower($varName))) { |
| 156 | 156 | // get the expected variable name |
| 157 | 157 | $varNameWithUnderscores = preg_replace('/([A-Z])/', '_${1}', $varName); |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | . '" instead of "' . $varName . '".'; |
| 167 | 167 | // adds the error and changes return value |
| 168 | 168 | $phpcsFile->addError($error, $stackPtr); |
| 169 | - $isInLowerCase = false; |
|
| 169 | + $isInLowerCase = FALSE; |
|
| 170 | 170 | } |
| 171 | 171 | return $isInLowerCase; |
| 172 | 172 | }//end checkLowerCase() |
@@ -191,23 +191,23 @@ discard block |
||
| 191 | 191 | */ |
| 192 | 192 | protected function checkVisibilityPrefix(File $phpcsFile, $stackPtr, $varName, $varProps) |
| 193 | 193 | { |
| 194 | - $isVisibilityPrefixRight = true; |
|
| 194 | + $isVisibilityPrefixRight = TRUE; |
|
| 195 | 195 | $scope = $varProps['scope']; |
| 196 | 196 | // If it's a private variable, it must have an underscore on the front. |
| 197 | 197 | if ($scope === 'private' && $varName{0} !== '_') { |
| 198 | 198 | $error = "Private variable name \"$varName\" must be prefixed with an underscore"; |
| 199 | 199 | $phpcsFile->addError($error, $stackPtr); |
| 200 | - $isVisibilityPrefixRight = false; |
|
| 200 | + $isVisibilityPrefixRight = FALSE; |
|
| 201 | 201 | } else if ($scope !== 'private' && $varName{0} === '_') { |
| 202 | 202 | // If it's not a private variable, |
| 203 | 203 | // then it must not start with an underscore. |
| 204 | - if (isset ($scopeSpecified) && true === $scopeSpecified) { |
|
| 204 | + if (isset ($scopeSpecified) && TRUE === $scopeSpecified) { |
|
| 205 | 205 | $error = "Public variable name \"$varName\" must not be prefixed with an underscore"; |
| 206 | 206 | } else { |
| 207 | 207 | $error = ucfirst($scope) . " variable name \"$varName\" must not be prefixed with an underscore"; |
| 208 | 208 | } |
| 209 | 209 | $phpcsFile->addError($error, $stackPtr); |
| 210 | - $isVisibilityPrefixRight = false; |
|
| 210 | + $isVisibilityPrefixRight = FALSE; |
|
| 211 | 211 | } |
| 212 | 212 | return $isVisibilityPrefixRight; |
| 213 | 213 | }//end checkVisibilityPrefix() |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | $minLength = 3; |
| 239 | 239 | $allowedShortName = array('ci'); |
| 240 | 240 | |
| 241 | - $isLengthRight = true; |
|
| 241 | + $isLengthRight = TRUE; |
|
| 242 | 242 | // cleans variable name |
| 243 | 243 | $varName = ltrim($varName, '_'); |
| 244 | 244 | if (strlen($varName) <= $minLength) { |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | return $isLengthRight; |
| 248 | 248 | } |
| 249 | 249 | // skips adding an error, if the variable is in a for loop |
| 250 | - if (false !== self::_isInForLoop($phpcsFile, $stackPtr, $varName)) { |
|
| 250 | + if (FALSE !== self::_isInForLoop($phpcsFile, $stackPtr, $varName)) { |
|
| 251 | 251 | return $isLengthRight; |
| 252 | 252 | } |
| 253 | 253 | // adds the error message finally |
@@ -260,7 +260,7 @@ discard block |
||
| 260 | 260 | . ', non-word variables like "' . $varName |
| 261 | 261 | . '" should only be used as iterators in for() loops.'; |
| 262 | 262 | $phpcsFile->addError($error, $stackPtr); |
| 263 | - $isLengthRight = false; |
|
| 263 | + $isLengthRight = FALSE; |
|
| 264 | 264 | } |
| 265 | 265 | return $isLengthRight; |
| 266 | 266 | }//end checkLength() |
@@ -281,23 +281,23 @@ discard block |
||
| 281 | 281 | private static function _isInForLoop(File $phpcsFile, $stackPtr, $varName) |
| 282 | 282 | { |
| 283 | 283 | $keepLookingFromPtr = $stackPtr; |
| 284 | - while (false !== $keepLookingFromPtr) { |
|
| 284 | + while (FALSE !== $keepLookingFromPtr) { |
|
| 285 | 285 | // looks if it is in (head or body) of a for loop |
| 286 | 286 | $forPtr = self::_isInForLoopHead($phpcsFile, $keepLookingFromPtr); |
| 287 | - if (false === $forPtr) { |
|
| 287 | + if (FALSE === $forPtr) { |
|
| 288 | 288 | $forPtr = self::_isInForLoopBody($phpcsFile, $keepLookingFromPtr); |
| 289 | 289 | } |
| 290 | 290 | // checks if it is declared in here and prepares next step |
| 291 | - if (false !== $forPtr) { |
|
| 292 | - if (false !== self::_isDeclaredInForLoop($phpcsFile, $forPtr, $varName)) { |
|
| 291 | + if (FALSE !== $forPtr) { |
|
| 292 | + if (FALSE !== self::_isDeclaredInForLoop($phpcsFile, $forPtr, $varName)) { |
|
| 293 | 293 | return $forPtr; |
| 294 | 294 | } |
| 295 | 295 | $keepLookingFromPtr = $forPtr; |
| 296 | 296 | } else { |
| 297 | - $keepLookingFromPtr = false; |
|
| 297 | + $keepLookingFromPtr = FALSE; |
|
| 298 | 298 | } |
| 299 | 299 | } |
| 300 | - return false; |
|
| 300 | + return FALSE; |
|
| 301 | 301 | }//end _isInForLoop() |
| 302 | 302 | |
| 303 | 303 | /** |
@@ -315,7 +315,7 @@ discard block |
||
| 315 | 315 | */ |
| 316 | 316 | private static function _isInForLoopHead(File $phpcsFile, $stackPtr) |
| 317 | 317 | { |
| 318 | - $isInForLoop = false; |
|
| 318 | + $isInForLoop = FALSE; |
|
| 319 | 319 | $tokens = $phpcsFile->getTokens(); |
| 320 | 320 | $currentTk = $tokens[$stackPtr]; |
| 321 | 321 | if (array_key_exists('nested_parenthesis', $currentTk)) { |
@@ -324,12 +324,12 @@ discard block |
||
| 324 | 324 | $nonWhitspacePtr = $phpcsFile->findPrevious( |
| 325 | 325 | array(T_WHITESPACE), |
| 326 | 326 | $openParPtr - 1, |
| 327 | - null, |
|
| 328 | - true, |
|
| 329 | - null, |
|
| 330 | - true |
|
| 327 | + NULL, |
|
| 328 | + TRUE, |
|
| 329 | + NULL, |
|
| 330 | + TRUE |
|
| 331 | 331 | ); |
| 332 | - if (false !== $nonWhitspacePtr) { |
|
| 332 | + if (FALSE !== $nonWhitspacePtr) { |
|
| 333 | 333 | $isFor = T_FOR === $tokens[$nonWhitspacePtr]['code']; |
| 334 | 334 | if ($isFor) { |
| 335 | 335 | $isInForLoop = $nonWhitspacePtr; |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | */ |
| 358 | 358 | private static function _isInForLoopBody(File $phpcsFile, $stackPtr) |
| 359 | 359 | { |
| 360 | - $isInForLoop = false; |
|
| 360 | + $isInForLoop = FALSE; |
|
| 361 | 361 | $tokens = $phpcsFile->getTokens(); |
| 362 | 362 | // get englobing hierarchy |
| 363 | 363 | $parentPtrAndCode = $tokens[$stackPtr]['conditions']; |
@@ -377,18 +377,18 @@ discard block |
||
| 377 | 377 | $openResearchScopePtr = $stackPtr; |
| 378 | 378 | // recursive search, since a for statement may englobe other inline |
| 379 | 379 | // control statement or may be near to function calls, etc... |
| 380 | - while (false !== $openResearchScopePtr) { |
|
| 380 | + while (FALSE !== $openResearchScopePtr) { |
|
| 381 | 381 | $closeParPtr = $phpcsFile->findPrevious( |
| 382 | 382 | array(T_CLOSE_PARENTHESIS), |
| 383 | 383 | $openResearchScopePtr, |
| 384 | - null, |
|
| 385 | - false, |
|
| 386 | - null, |
|
| 387 | - true |
|
| 384 | + NULL, |
|
| 385 | + FALSE, |
|
| 386 | + NULL, |
|
| 387 | + TRUE |
|
| 388 | 388 | ); |
| 389 | 389 | // is there a closing parenthesis with a control statement before |
| 390 | 390 | // the previous instruction ? |
| 391 | - if (false !== $closeParPtr) { |
|
| 391 | + if (FALSE !== $closeParPtr) { |
|
| 392 | 392 | // is there no opening curly bracket specific to |
| 393 | 393 | // set of instructions, between the closing parenthesis |
| 394 | 394 | // and the current token ? |
@@ -422,7 +422,7 @@ discard block |
||
| 422 | 422 | } |
| 423 | 423 | } |
| 424 | 424 | } else { |
| 425 | - $openResearchScopePtr = false; |
|
| 425 | + $openResearchScopePtr = FALSE; |
|
| 426 | 426 | } |
| 427 | 427 | } |
| 428 | 428 | // looks for a for loop having a body enclosed with curly brackets |
@@ -431,7 +431,7 @@ discard block |
||
| 431 | 431 | return $parentPtr; |
| 432 | 432 | } |
| 433 | 433 | } |
| 434 | - return false; |
|
| 434 | + return FALSE; |
|
| 435 | 435 | }//end _isInForLoopBody() |
| 436 | 436 | |
| 437 | 437 | /** |
@@ -449,7 +449,7 @@ discard block |
||
| 449 | 449 | */ |
| 450 | 450 | private static function _isDeclaredInForLoop(File $phpcsFile, $forPtr, $varName) |
| 451 | 451 | { |
| 452 | - $isDeclaredInFor = false; |
|
| 452 | + $isDeclaredInFor = FALSE; |
|
| 453 | 453 | $tokens = $phpcsFile->getTokens(); |
| 454 | 454 | $forVarPtrs = self::_getVarDeclaredInFor($phpcsFile, $forPtr); |
| 455 | 455 | foreach ($forVarPtrs as $forVarPtr) { |
@@ -496,7 +496,7 @@ discard block |
||
| 496 | 496 | $openParPtr + 1, |
| 497 | 497 | $endOfDeclPtr |
| 498 | 498 | ); |
| 499 | - while (false !== $varPtr) { |
|
| 499 | + while (FALSE !== $varPtr) { |
|
| 500 | 500 | $forVarPtrs [] = $varPtr; |
| 501 | 501 | $varPtr = $phpcsFile->findNext( |
| 502 | 502 | array(T_VARIABLE), |
@@ -523,8 +523,8 @@ discard block |
||
| 523 | 523 | private static function _getVariablePosition($haystack, $offset = 0) |
| 524 | 524 | { |
| 525 | 525 | $var_starts_at = strpos($haystack, '$', $offset); |
| 526 | - $is_a_var = false; |
|
| 527 | - while (false !== $var_starts_at && ! $is_a_var) { |
|
| 526 | + $is_a_var = FALSE; |
|
| 527 | + while (FALSE !== $var_starts_at && ! $is_a_var) { |
|
| 528 | 528 | // makes sure that $ is used for a variable and not as a symbol, |
| 529 | 529 | // if $ is protected with the escape char, then it is a symbol. |
| 530 | 530 | if (0 !== strcmp($haystack[$var_starts_at - 1], '\\')) { |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | // so it looks for the closing brace in the right place |
| 534 | 534 | $hsChunk2 = substr($haystack, $var_starts_at + 2); |
| 535 | 535 | if (1 === preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\}/', $hsChunk2)) { |
| 536 | - $is_a_var = true; |
|
| 536 | + $is_a_var = TRUE; |
|
| 537 | 537 | } |
| 538 | 538 | } else { |
| 539 | 539 | $hsChunk1 = substr($haystack, $var_starts_at + 1); |
@@ -541,7 +541,7 @@ discard block |
||
| 541 | 541 | // $ is used for a variable and not as a symbol, |
| 542 | 542 | // since what follows $ matchs the definition of |
| 543 | 543 | // a variable label for PHP. |
| 544 | - $is_a_var = true; |
|
| 544 | + $is_a_var = TRUE; |
|
| 545 | 545 | } |
| 546 | 546 | } |
| 547 | 547 | } |
@@ -554,7 +554,7 @@ discard block |
||
| 554 | 554 | if ($is_a_var) { |
| 555 | 555 | return $var_starts_at; |
| 556 | 556 | } else { |
| 557 | - return false; |
|
| 557 | + return FALSE; |
|
| 558 | 558 | } |
| 559 | 559 | }//end _getVariablePosition() |
| 560 | 560 | }//end class |
@@ -40,121 +40,121 @@ |
||
| 40 | 40 | |
| 41 | 41 | class ValidMethodNameSniff extends AbstractScopeSniff |
| 42 | 42 | { |
| 43 | - /** |
|
| 44 | - * A list of all PHP magic methods. |
|
| 45 | - * |
|
| 46 | - * @var array |
|
| 47 | - */ |
|
| 48 | - protected static $magicMethods = array( |
|
| 49 | - 'construct', |
|
| 50 | - 'destruct', |
|
| 51 | - 'call', |
|
| 52 | - 'callStatic', |
|
| 53 | - 'get', |
|
| 54 | - 'set', |
|
| 55 | - 'isset', |
|
| 56 | - 'unset', |
|
| 57 | - 'sleep', |
|
| 58 | - 'wakeup', |
|
| 59 | - 'toString', |
|
| 60 | - 'set_state', |
|
| 61 | - 'clone', |
|
| 62 | - ); |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Defines which token(s) in which scope(s) will be proceed. |
|
| 66 | - */ |
|
| 67 | - public function __construct() |
|
| 68 | - { |
|
| 69 | - parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true); |
|
| 70 | - |
|
| 71 | - }//end __construct() |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Processes the tokens within the scope. |
|
| 76 | - * |
|
| 77 | - * @param File $phpcsFile The file being processed. |
|
| 78 | - * @param int $stackPtr The position where this token was |
|
| 79 | - * found. |
|
| 80 | - * @param int $currScope The position of the current scope. |
|
| 81 | - * |
|
| 82 | - * @return void |
|
| 83 | - */ |
|
| 84 | - protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) |
|
| 85 | - { |
|
| 86 | - $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
| 87 | - if ($methodName === null) { |
|
| 88 | - // Ignore closures. |
|
| 89 | - return; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $className = $phpcsFile->getDeclarationName($currScope); |
|
| 93 | - |
|
| 94 | - // Is this a magic method i.e. is prefixed with "__". |
|
| 95 | - if (0 === strcmp(substr($methodName, 0, 2), '__')) { |
|
| 96 | - $magicPart = substr($methodName, 2); |
|
| 97 | - if (in_array($magicPart, self::$magicMethods) === false) { |
|
| 98 | - $error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore"; |
|
| 99 | - $phpcsFile->addError($error, $stackPtr); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - return; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // PHP4 constructors are allowed to break our rules. |
|
| 106 | - if ($methodName === $className) { |
|
| 107 | - return; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // PHP4 destructors are allowed to break our rules. |
|
| 111 | - if ($methodName === '_'.$className) { |
|
| 112 | - return; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - if (0 !== strcmp($methodName, strtolower($methodName))) { |
|
| 116 | - $uscrdMethodName = preg_replace('/([A-Z])/', '_${1}', $methodName); |
|
| 117 | - $expectedMethodName = strtolower($uscrdMethodName); |
|
| 118 | - $error = "Class methods should be entirely lowercased. Please consider \"$expectedMethodName\" instead of \"$methodName\"."; |
|
| 119 | - $phpcsFile->addError($error, $stackPtr); |
|
| 120 | - return; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
| 124 | - $scope = $methodProps['scope']; |
|
| 125 | - $scopeSpecified = $methodProps['scope_specified']; |
|
| 126 | - |
|
| 127 | - // If it's a private method, it must have an underscore on the front. |
|
| 128 | - if ($scope === 'private' && $methodName{0} !== '_') { |
|
| 129 | - $error = "Private method name \"$className::$methodName\" must be prefixed with an underscore"; |
|
| 130 | - $phpcsFile->addError($error, $stackPtr); |
|
| 131 | - return; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // If it's not a private method, it must not have an underscore on the front. |
|
| 135 | - if ($scope !== 'private' && $methodName{0} === '_') { |
|
| 136 | - if (true === $scopeSpecified) { |
|
| 137 | - $error = "Public method name \"$className::$methodName\" must not be prefixed with an underscore"; |
|
| 138 | - } else { |
|
| 139 | - $error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore"; |
|
| 140 | - } |
|
| 141 | - $phpcsFile->addError($error, $stackPtr); |
|
| 142 | - return; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // If name is too verbose, |
|
| 146 | - // then either an error or a warning is displayed. |
|
| 147 | - $error_limit = 50; |
|
| 148 | - $warning_limit = 35; |
|
| 149 | - if (strlen($methodName) > $error_limit) { |
|
| 150 | - $error = "Overly long and verbose names are prohibited. Please find a name shorter than $error_limit chars."; |
|
| 151 | - $phpcsFile->addError($error, $stackPtr); |
|
| 152 | - return; |
|
| 153 | - } else if (strlen($methodName) > $warning_limit) { |
|
| 154 | - $warning = "Try to avoid overly long and verbose names in finding a name shorter than $warning_limit chars."; |
|
| 155 | - $phpcsFile->addWarning($warning, $stackPtr); |
|
| 156 | - } |
|
| 157 | - }//end processTokenWithinScope() |
|
| 43 | + /** |
|
| 44 | + * A list of all PHP magic methods. |
|
| 45 | + * |
|
| 46 | + * @var array |
|
| 47 | + */ |
|
| 48 | + protected static $magicMethods = array( |
|
| 49 | + 'construct', |
|
| 50 | + 'destruct', |
|
| 51 | + 'call', |
|
| 52 | + 'callStatic', |
|
| 53 | + 'get', |
|
| 54 | + 'set', |
|
| 55 | + 'isset', |
|
| 56 | + 'unset', |
|
| 57 | + 'sleep', |
|
| 58 | + 'wakeup', |
|
| 59 | + 'toString', |
|
| 60 | + 'set_state', |
|
| 61 | + 'clone', |
|
| 62 | + ); |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Defines which token(s) in which scope(s) will be proceed. |
|
| 66 | + */ |
|
| 67 | + public function __construct() |
|
| 68 | + { |
|
| 69 | + parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true); |
|
| 70 | + |
|
| 71 | + }//end __construct() |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Processes the tokens within the scope. |
|
| 76 | + * |
|
| 77 | + * @param File $phpcsFile The file being processed. |
|
| 78 | + * @param int $stackPtr The position where this token was |
|
| 79 | + * found. |
|
| 80 | + * @param int $currScope The position of the current scope. |
|
| 81 | + * |
|
| 82 | + * @return void |
|
| 83 | + */ |
|
| 84 | + protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) |
|
| 85 | + { |
|
| 86 | + $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
| 87 | + if ($methodName === null) { |
|
| 88 | + // Ignore closures. |
|
| 89 | + return; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $className = $phpcsFile->getDeclarationName($currScope); |
|
| 93 | + |
|
| 94 | + // Is this a magic method i.e. is prefixed with "__". |
|
| 95 | + if (0 === strcmp(substr($methodName, 0, 2), '__')) { |
|
| 96 | + $magicPart = substr($methodName, 2); |
|
| 97 | + if (in_array($magicPart, self::$magicMethods) === false) { |
|
| 98 | + $error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore"; |
|
| 99 | + $phpcsFile->addError($error, $stackPtr); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + return; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // PHP4 constructors are allowed to break our rules. |
|
| 106 | + if ($methodName === $className) { |
|
| 107 | + return; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // PHP4 destructors are allowed to break our rules. |
|
| 111 | + if ($methodName === '_'.$className) { |
|
| 112 | + return; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + if (0 !== strcmp($methodName, strtolower($methodName))) { |
|
| 116 | + $uscrdMethodName = preg_replace('/([A-Z])/', '_${1}', $methodName); |
|
| 117 | + $expectedMethodName = strtolower($uscrdMethodName); |
|
| 118 | + $error = "Class methods should be entirely lowercased. Please consider \"$expectedMethodName\" instead of \"$methodName\"."; |
|
| 119 | + $phpcsFile->addError($error, $stackPtr); |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
| 124 | + $scope = $methodProps['scope']; |
|
| 125 | + $scopeSpecified = $methodProps['scope_specified']; |
|
| 126 | + |
|
| 127 | + // If it's a private method, it must have an underscore on the front. |
|
| 128 | + if ($scope === 'private' && $methodName{0} !== '_') { |
|
| 129 | + $error = "Private method name \"$className::$methodName\" must be prefixed with an underscore"; |
|
| 130 | + $phpcsFile->addError($error, $stackPtr); |
|
| 131 | + return; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // If it's not a private method, it must not have an underscore on the front. |
|
| 135 | + if ($scope !== 'private' && $methodName{0} === '_') { |
|
| 136 | + if (true === $scopeSpecified) { |
|
| 137 | + $error = "Public method name \"$className::$methodName\" must not be prefixed with an underscore"; |
|
| 138 | + } else { |
|
| 139 | + $error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore"; |
|
| 140 | + } |
|
| 141 | + $phpcsFile->addError($error, $stackPtr); |
|
| 142 | + return; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // If name is too verbose, |
|
| 146 | + // then either an error or a warning is displayed. |
|
| 147 | + $error_limit = 50; |
|
| 148 | + $warning_limit = 35; |
|
| 149 | + if (strlen($methodName) > $error_limit) { |
|
| 150 | + $error = "Overly long and verbose names are prohibited. Please find a name shorter than $error_limit chars."; |
|
| 151 | + $phpcsFile->addError($error, $stackPtr); |
|
| 152 | + return; |
|
| 153 | + } else if (strlen($methodName) > $warning_limit) { |
|
| 154 | + $warning = "Try to avoid overly long and verbose names in finding a name shorter than $warning_limit chars."; |
|
| 155 | + $phpcsFile->addWarning($warning, $stackPtr); |
|
| 156 | + } |
|
| 157 | + }//end processTokenWithinScope() |
|
| 158 | 158 | |
| 159 | 159 | }//end class |
| 160 | 160 | |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | return; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - $className = $phpcsFile->getDeclarationName($currScope); |
|
| 92 | + $className = $phpcsFile->getDeclarationName($currScope); |
|
| 93 | 93 | |
| 94 | 94 | // Is this a magic method i.e. is prefixed with "__". |
| 95 | 95 | if (0 === strcmp(substr($methodName, 0, 2), '__')) { |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // PHP4 destructors are allowed to break our rules. |
| 111 | - if ($methodName === '_'.$className) { |
|
| 111 | + if ($methodName === '_' . $className) { |
|
| 112 | 112 | return; |
| 113 | 113 | } |
| 114 | 114 | |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | if (true === $scopeSpecified) { |
| 137 | 137 | $error = "Public method name \"$className::$methodName\" must not be prefixed with an underscore"; |
| 138 | 138 | } else { |
| 139 | - $error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore"; |
|
| 139 | + $error = ucfirst($scope) . " method name \"$className::$methodName\" must not be prefixed with an underscore"; |
|
| 140 | 140 | } |
| 141 | 141 | $phpcsFile->addError($error, $stackPtr); |
| 142 | 142 | return; |
@@ -38,8 +38,7 @@ discard block |
||
| 38 | 38 | use PHP_CodeSniffer\Sniffs\AbstactScopeSniff; |
| 39 | 39 | use PHP_CodeSniffer\Files\File; |
| 40 | 40 | |
| 41 | -class ValidMethodNameSniff extends AbstractScopeSniff |
|
| 42 | -{ |
|
| 41 | +class ValidMethodNameSniff extends AbstractScopeSniff { |
|
| 43 | 42 | /** |
| 44 | 43 | * A list of all PHP magic methods. |
| 45 | 44 | * |
@@ -84,7 +83,8 @@ discard block |
||
| 84 | 83 | protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) |
| 85 | 84 | { |
| 86 | 85 | $methodName = $phpcsFile->getDeclarationName($stackPtr); |
| 87 | - if ($methodName === null) { |
|
| 86 | + if ($methodName === null) |
|
| 87 | + { |
|
| 88 | 88 | // Ignore closures. |
| 89 | 89 | return; |
| 90 | 90 | } |
@@ -92,9 +92,11 @@ discard block |
||
| 92 | 92 | $className = $phpcsFile->getDeclarationName($currScope); |
| 93 | 93 | |
| 94 | 94 | // Is this a magic method i.e. is prefixed with "__". |
| 95 | - if (0 === strcmp(substr($methodName, 0, 2), '__')) { |
|
| 95 | + if (0 === strcmp(substr($methodName, 0, 2), '__')) |
|
| 96 | + { |
|
| 96 | 97 | $magicPart = substr($methodName, 2); |
| 97 | - if (in_array($magicPart, self::$magicMethods) === false) { |
|
| 98 | + if (in_array($magicPart, self::$magicMethods) === false) |
|
| 99 | + { |
|
| 98 | 100 | $error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore"; |
| 99 | 101 | $phpcsFile->addError($error, $stackPtr); |
| 100 | 102 | } |
@@ -103,16 +105,19 @@ discard block |
||
| 103 | 105 | } |
| 104 | 106 | |
| 105 | 107 | // PHP4 constructors are allowed to break our rules. |
| 106 | - if ($methodName === $className) { |
|
| 108 | + if ($methodName === $className) |
|
| 109 | + { |
|
| 107 | 110 | return; |
| 108 | 111 | } |
| 109 | 112 | |
| 110 | 113 | // PHP4 destructors are allowed to break our rules. |
| 111 | - if ($methodName === '_'.$className) { |
|
| 114 | + if ($methodName === '_'.$className) |
|
| 115 | + { |
|
| 112 | 116 | return; |
| 113 | 117 | } |
| 114 | 118 | |
| 115 | - if (0 !== strcmp($methodName, strtolower($methodName))) { |
|
| 119 | + if (0 !== strcmp($methodName, strtolower($methodName))) |
|
| 120 | + { |
|
| 116 | 121 | $uscrdMethodName = preg_replace('/([A-Z])/', '_${1}', $methodName); |
| 117 | 122 | $expectedMethodName = strtolower($uscrdMethodName); |
| 118 | 123 | $error = "Class methods should be entirely lowercased. Please consider \"$expectedMethodName\" instead of \"$methodName\"."; |
@@ -125,17 +130,22 @@ discard block |
||
| 125 | 130 | $scopeSpecified = $methodProps['scope_specified']; |
| 126 | 131 | |
| 127 | 132 | // If it's a private method, it must have an underscore on the front. |
| 128 | - if ($scope === 'private' && $methodName{0} !== '_') { |
|
| 133 | + if ($scope === 'private' && $methodName{0} !== '_') |
|
| 134 | + { |
|
| 129 | 135 | $error = "Private method name \"$className::$methodName\" must be prefixed with an underscore"; |
| 130 | 136 | $phpcsFile->addError($error, $stackPtr); |
| 131 | 137 | return; |
| 132 | 138 | } |
| 133 | 139 | |
| 134 | 140 | // If it's not a private method, it must not have an underscore on the front. |
| 135 | - if ($scope !== 'private' && $methodName{0} === '_') { |
|
| 136 | - if (true === $scopeSpecified) { |
|
| 141 | + if ($scope !== 'private' && $methodName{0} === '_') |
|
| 142 | + { |
|
| 143 | + if (true === $scopeSpecified) |
|
| 144 | + { |
|
| 137 | 145 | $error = "Public method name \"$className::$methodName\" must not be prefixed with an underscore"; |
| 138 | - } else { |
|
| 146 | + } |
|
| 147 | + else |
|
| 148 | + { |
|
| 139 | 149 | $error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore"; |
| 140 | 150 | } |
| 141 | 151 | $phpcsFile->addError($error, $stackPtr); |
@@ -146,11 +156,14 @@ discard block |
||
| 146 | 156 | // then either an error or a warning is displayed. |
| 147 | 157 | $error_limit = 50; |
| 148 | 158 | $warning_limit = 35; |
| 149 | - if (strlen($methodName) > $error_limit) { |
|
| 159 | + if (strlen($methodName) > $error_limit) |
|
| 160 | + { |
|
| 150 | 161 | $error = "Overly long and verbose names are prohibited. Please find a name shorter than $error_limit chars."; |
| 151 | 162 | $phpcsFile->addError($error, $stackPtr); |
| 152 | 163 | return; |
| 153 | - } else if (strlen($methodName) > $warning_limit) { |
|
| 164 | + } |
|
| 165 | + else if (strlen($methodName) > $warning_limit) |
|
| 166 | + { |
|
| 154 | 167 | $warning = "Try to avoid overly long and verbose names in finding a name shorter than $warning_limit chars."; |
| 155 | 168 | $phpcsFile->addWarning($warning, $stackPtr); |
| 156 | 169 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | */ |
| 67 | 67 | public function __construct() |
| 68 | 68 | { |
| 69 | - parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true); |
|
| 69 | + parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), TRUE); |
|
| 70 | 70 | |
| 71 | 71 | }//end __construct() |
| 72 | 72 | |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) |
| 85 | 85 | { |
| 86 | 86 | $methodName = $phpcsFile->getDeclarationName($stackPtr); |
| 87 | - if ($methodName === null) { |
|
| 87 | + if ($methodName === NULL) { |
|
| 88 | 88 | // Ignore closures. |
| 89 | 89 | return; |
| 90 | 90 | } |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | // Is this a magic method i.e. is prefixed with "__". |
| 95 | 95 | if (0 === strcmp(substr($methodName, 0, 2), '__')) { |
| 96 | 96 | $magicPart = substr($methodName, 2); |
| 97 | - if (in_array($magicPart, self::$magicMethods) === false) { |
|
| 97 | + if (in_array($magicPart, self::$magicMethods) === FALSE) { |
|
| 98 | 98 | $error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore"; |
| 99 | 99 | $phpcsFile->addError($error, $stackPtr); |
| 100 | 100 | } |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | // If it's not a private method, it must not have an underscore on the front. |
| 135 | 135 | if ($scope !== 'private' && $methodName{0} === '_') { |
| 136 | - if (true === $scopeSpecified) { |
|
| 136 | + if (TRUE === $scopeSpecified) { |
|
| 137 | 137 | $error = "Public method name \"$className::$methodName\" must not be prefixed with an underscore"; |
| 138 | 138 | } else { |
| 139 | 139 | $error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore"; |