@@ -45,138 +45,138 @@ |
||
| 45 | 45 | |
| 46 | 46 | class ClosingLocationCommentSniff extends AbstractClosingCommentSniff |
| 47 | 47 | { |
| 48 | - public $applicationRoot = '/application/'; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Returns an array of tokens this test wants to listen for. |
|
| 52 | - * |
|
| 53 | - * @return array |
|
| 54 | - */ |
|
| 55 | - public function register() |
|
| 56 | - { |
|
| 57 | - return array( |
|
| 58 | - T_OPEN_TAG |
|
| 59 | - ); |
|
| 60 | - |
|
| 61 | - }//end register() |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Processes this test, when one of its tokens is encountered. |
|
| 66 | - * |
|
| 67 | - * @param File $phpcsFile The current file being scanned. |
|
| 68 | - * @param int $stackPtr The position of the current token |
|
| 69 | - * in the stack passed in $tokens. |
|
| 70 | - * |
|
| 71 | - * @return void |
|
| 72 | - */ |
|
| 73 | - public function process(File $phpcsFile, $stackPtr) |
|
| 74 | - { |
|
| 75 | - // We are only interested if this is the first open tag. |
|
| 76 | - if ($stackPtr !== 0) { |
|
| 77 | - if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 78 | - return; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - $filePath = $phpcsFile->getFilename(); |
|
| 83 | - $tokens = $phpcsFile->getTokens(); |
|
| 84 | - // removes the application root from the beginning of the file path |
|
| 85 | - $locationPath = self::_getLocationPath($filePath, $this->_getAppRoot()); |
|
| 86 | - // add an error, if application root doesn't exist in current file path |
|
| 87 | - if (false === $locationPath) { |
|
| 88 | - $error = 'Unable to find "' . $this->_getAppRoot() . '" in file path "' . $filePath . '". Please set your project\'s application root.'; |
|
| 89 | - $phpcsFile->addError($error, count($tokens) - 1); |
|
| 90 | - return; |
|
| 91 | - } |
|
| 92 | - // generates the expected comment |
|
| 93 | - $commentTemplate = "Location: $locationPath"; |
|
| 94 | - |
|
| 95 | - $currentToken = count($tokens) - 1; |
|
| 96 | - $hasClosingLocationComment = false; |
|
| 97 | - $isNotAWhitespaceOrAComment = false; |
|
| 98 | - while ($currentToken >= 0 |
|
| 99 | - && ! $isNotAWhitespaceOrAComment |
|
| 100 | - && ! $hasClosingLocationComment |
|
| 101 | - ) { |
|
| 102 | - $token = $tokens[$currentToken]; |
|
| 103 | - $tokenCode = $token['code']; |
|
| 104 | - if (T_COMMENT === $tokenCode) { |
|
| 105 | - $commentString = self::_getCommentContent($token['content']); |
|
| 106 | - if (0 === strcmp($commentString, $commentTemplate)) { |
|
| 107 | - $hasClosingLocationComment = true; |
|
| 108 | - } |
|
| 109 | - } else if (T_WHITESPACE === $tokenCode) { |
|
| 110 | - // Whitespaces are allowed between the closing file comment, |
|
| 111 | - //other comments and end of file |
|
| 112 | - } else { |
|
| 113 | - $isNotAWhitespaceOrAComment = true; |
|
| 114 | - } |
|
| 115 | - $currentToken--; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - if ( ! $hasClosingLocationComment) { |
|
| 119 | - $error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".'; |
|
| 120 | - $phpcsFile->addError($error, $currentToken); |
|
| 121 | - } |
|
| 122 | - }//end process() |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Returns the relative path from $appRoot to $filePath, or false if |
|
| 127 | - * $appRoot cannot be found in $filePath, because $appRoot is not a parent |
|
| 128 | - * of $filePath. |
|
| 129 | - * |
|
| 130 | - * @param string $filePath Full path to the file being proceed. |
|
| 131 | - * @param string $appRoot Partial or full path to the CodeIgniter |
|
| 132 | - * application root of the file being proceed. It must not contain the |
|
| 133 | - * full path to the application root, but at least the name of the |
|
| 134 | - * application root. Parent directory of the application root are allowed |
|
| 135 | - * but not mandatory. |
|
| 136 | - * |
|
| 137 | - * @return string|bool The relative path from $appRoot to $filePath, or |
|
| 138 | - * false if $appRoot cannot be found in $filePath. |
|
| 139 | - */ |
|
| 140 | - private static function _getLocationPath ($filePath, $appRoot) |
|
| 141 | - { |
|
| 142 | - // removes the path to application root |
|
| 143 | - // from the beginning of the file path |
|
| 144 | - $appRootAt = strpos($filePath, $appRoot); |
|
| 145 | - if (false === $appRootAt) { |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - $localPath = substr($filePath, $appRootAt + strlen($appRoot)); |
|
| 149 | - // ensures the location path to be a relative path starting with "./". |
|
| 150 | - if ( ! self::_stringStartsWith($localPath, './')) { |
|
| 151 | - $localPath = './' . $localPath; |
|
| 152 | - } else if ( ! self::_stringStartsWith($localPath, '.') |
|
| 153 | - && self::_stringStartsWith($localPath, '/') |
|
| 154 | - ) { |
|
| 155 | - $localPath = '.' . $localPath; |
|
| 156 | - } |
|
| 157 | - return $localPath; |
|
| 158 | - }//end _getLocationPath() |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Returns the application root that should be used first. |
|
| 163 | - * |
|
| 164 | - * There are several ways to configure the application root. |
|
| 165 | - * In order of priority : |
|
| 166 | - * - Configuration variable ci_application_root. |
|
| 167 | - * - Rule property applicationRoot. |
|
| 168 | - * - Default value '/application/' |
|
| 169 | - * |
|
| 170 | - * @return string Path to your project application root. |
|
| 171 | - */ |
|
| 172 | - private function _getAppRoot() |
|
| 173 | - { |
|
| 174 | - $appRoot = Common::getConfigData('ci_application_root'); |
|
| 175 | - if (null === $appRoot) { |
|
| 176 | - $appRoot = $this->applicationRoot; |
|
| 177 | - } |
|
| 178 | - return $appRoot; |
|
| 179 | - }//end _getAppRoot() |
|
| 48 | + public $applicationRoot = '/application/'; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Returns an array of tokens this test wants to listen for. |
|
| 52 | + * |
|
| 53 | + * @return array |
|
| 54 | + */ |
|
| 55 | + public function register() |
|
| 56 | + { |
|
| 57 | + return array( |
|
| 58 | + T_OPEN_TAG |
|
| 59 | + ); |
|
| 60 | + |
|
| 61 | + }//end register() |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Processes this test, when one of its tokens is encountered. |
|
| 66 | + * |
|
| 67 | + * @param File $phpcsFile The current file being scanned. |
|
| 68 | + * @param int $stackPtr The position of the current token |
|
| 69 | + * in the stack passed in $tokens. |
|
| 70 | + * |
|
| 71 | + * @return void |
|
| 72 | + */ |
|
| 73 | + public function process(File $phpcsFile, $stackPtr) |
|
| 74 | + { |
|
| 75 | + // We are only interested if this is the first open tag. |
|
| 76 | + if ($stackPtr !== 0) { |
|
| 77 | + if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 78 | + return; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + $filePath = $phpcsFile->getFilename(); |
|
| 83 | + $tokens = $phpcsFile->getTokens(); |
|
| 84 | + // removes the application root from the beginning of the file path |
|
| 85 | + $locationPath = self::_getLocationPath($filePath, $this->_getAppRoot()); |
|
| 86 | + // add an error, if application root doesn't exist in current file path |
|
| 87 | + if (false === $locationPath) { |
|
| 88 | + $error = 'Unable to find "' . $this->_getAppRoot() . '" in file path "' . $filePath . '". Please set your project\'s application root.'; |
|
| 89 | + $phpcsFile->addError($error, count($tokens) - 1); |
|
| 90 | + return; |
|
| 91 | + } |
|
| 92 | + // generates the expected comment |
|
| 93 | + $commentTemplate = "Location: $locationPath"; |
|
| 94 | + |
|
| 95 | + $currentToken = count($tokens) - 1; |
|
| 96 | + $hasClosingLocationComment = false; |
|
| 97 | + $isNotAWhitespaceOrAComment = false; |
|
| 98 | + while ($currentToken >= 0 |
|
| 99 | + && ! $isNotAWhitespaceOrAComment |
|
| 100 | + && ! $hasClosingLocationComment |
|
| 101 | + ) { |
|
| 102 | + $token = $tokens[$currentToken]; |
|
| 103 | + $tokenCode = $token['code']; |
|
| 104 | + if (T_COMMENT === $tokenCode) { |
|
| 105 | + $commentString = self::_getCommentContent($token['content']); |
|
| 106 | + if (0 === strcmp($commentString, $commentTemplate)) { |
|
| 107 | + $hasClosingLocationComment = true; |
|
| 108 | + } |
|
| 109 | + } else if (T_WHITESPACE === $tokenCode) { |
|
| 110 | + // Whitespaces are allowed between the closing file comment, |
|
| 111 | + //other comments and end of file |
|
| 112 | + } else { |
|
| 113 | + $isNotAWhitespaceOrAComment = true; |
|
| 114 | + } |
|
| 115 | + $currentToken--; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + if ( ! $hasClosingLocationComment) { |
|
| 119 | + $error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".'; |
|
| 120 | + $phpcsFile->addError($error, $currentToken); |
|
| 121 | + } |
|
| 122 | + }//end process() |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Returns the relative path from $appRoot to $filePath, or false if |
|
| 127 | + * $appRoot cannot be found in $filePath, because $appRoot is not a parent |
|
| 128 | + * of $filePath. |
|
| 129 | + * |
|
| 130 | + * @param string $filePath Full path to the file being proceed. |
|
| 131 | + * @param string $appRoot Partial or full path to the CodeIgniter |
|
| 132 | + * application root of the file being proceed. It must not contain the |
|
| 133 | + * full path to the application root, but at least the name of the |
|
| 134 | + * application root. Parent directory of the application root are allowed |
|
| 135 | + * but not mandatory. |
|
| 136 | + * |
|
| 137 | + * @return string|bool The relative path from $appRoot to $filePath, or |
|
| 138 | + * false if $appRoot cannot be found in $filePath. |
|
| 139 | + */ |
|
| 140 | + private static function _getLocationPath ($filePath, $appRoot) |
|
| 141 | + { |
|
| 142 | + // removes the path to application root |
|
| 143 | + // from the beginning of the file path |
|
| 144 | + $appRootAt = strpos($filePath, $appRoot); |
|
| 145 | + if (false === $appRootAt) { |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + $localPath = substr($filePath, $appRootAt + strlen($appRoot)); |
|
| 149 | + // ensures the location path to be a relative path starting with "./". |
|
| 150 | + if ( ! self::_stringStartsWith($localPath, './')) { |
|
| 151 | + $localPath = './' . $localPath; |
|
| 152 | + } else if ( ! self::_stringStartsWith($localPath, '.') |
|
| 153 | + && self::_stringStartsWith($localPath, '/') |
|
| 154 | + ) { |
|
| 155 | + $localPath = '.' . $localPath; |
|
| 156 | + } |
|
| 157 | + return $localPath; |
|
| 158 | + }//end _getLocationPath() |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Returns the application root that should be used first. |
|
| 163 | + * |
|
| 164 | + * There are several ways to configure the application root. |
|
| 165 | + * In order of priority : |
|
| 166 | + * - Configuration variable ci_application_root. |
|
| 167 | + * - Rule property applicationRoot. |
|
| 168 | + * - Default value '/application/' |
|
| 169 | + * |
|
| 170 | + * @return string Path to your project application root. |
|
| 171 | + */ |
|
| 172 | + private function _getAppRoot() |
|
| 173 | + { |
|
| 174 | + $appRoot = Common::getConfigData('ci_application_root'); |
|
| 175 | + if (null === $appRoot) { |
|
| 176 | + $appRoot = $this->applicationRoot; |
|
| 177 | + } |
|
| 178 | + return $appRoot; |
|
| 179 | + }//end _getAppRoot() |
|
| 180 | 180 | }//end class |
| 181 | 181 | |
| 182 | 182 | ?> |
@@ -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 | ?> |
@@ -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,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 | { |
@@ -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 | |
@@ -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 | ?> |
@@ -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 | |
@@ -32,53 +32,53 @@ |
||
| 32 | 32 | |
| 33 | 33 | class ValidFileNameSniff implements Sniff |
| 34 | 34 | { |
| 35 | - /** |
|
| 36 | - * Returns an array of tokens this test wants to listen for. |
|
| 37 | - * |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function register() |
|
| 41 | - { |
|
| 42 | - return array( |
|
| 43 | - T_CLASS, |
|
| 44 | - T_INTERFACE, |
|
| 45 | - ); |
|
| 46 | - }//end register() |
|
| 35 | + /** |
|
| 36 | + * Returns an array of tokens this test wants to listen for. |
|
| 37 | + * |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function register() |
|
| 41 | + { |
|
| 42 | + return array( |
|
| 43 | + T_CLASS, |
|
| 44 | + T_INTERFACE, |
|
| 45 | + ); |
|
| 46 | + }//end register() |
|
| 47 | 47 | |
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Processes this test, when one of its tokens is encountered. |
|
| 51 | - * |
|
| 52 | - * @param File $phpcsFile The file being scanned. |
|
| 53 | - * @param int $stackPtr The position of the current token in the |
|
| 54 | - * stack passed in $tokens. |
|
| 55 | - * |
|
| 56 | - * @return void |
|
| 57 | - */ |
|
| 58 | - public function process(File $phpcsFile, $stackPtr) |
|
| 59 | - { |
|
| 60 | - $tokens = $phpcsFile->getTokens(); |
|
| 61 | - // computes the expected filename based on the name of the class or interface that it contains. |
|
| 62 | - $decNamePtr = $phpcsFile->findNext(T_STRING, $stackPtr); |
|
| 63 | - $decName = $tokens[$decNamePtr]['content']; |
|
| 64 | - $expectedFileName = strtolower($decName); |
|
| 65 | - // extracts filename without extension from its path. |
|
| 66 | - $fullPath = $phpcsFile->getFilename(); |
|
| 67 | - $fileNameAndExt = basename($fullPath); |
|
| 68 | - $fileName = substr($fileNameAndExt, 0, strrpos($fileNameAndExt, '.')); |
|
| 49 | + /** |
|
| 50 | + * Processes this test, when one of its tokens is encountered. |
|
| 51 | + * |
|
| 52 | + * @param File $phpcsFile The file being scanned. |
|
| 53 | + * @param int $stackPtr The position of the current token in the |
|
| 54 | + * stack passed in $tokens. |
|
| 55 | + * |
|
| 56 | + * @return void |
|
| 57 | + */ |
|
| 58 | + public function process(File $phpcsFile, $stackPtr) |
|
| 59 | + { |
|
| 60 | + $tokens = $phpcsFile->getTokens(); |
|
| 61 | + // computes the expected filename based on the name of the class or interface that it contains. |
|
| 62 | + $decNamePtr = $phpcsFile->findNext(T_STRING, $stackPtr); |
|
| 63 | + $decName = $tokens[$decNamePtr]['content']; |
|
| 64 | + $expectedFileName = strtolower($decName); |
|
| 65 | + // extracts filename without extension from its path. |
|
| 66 | + $fullPath = $phpcsFile->getFilename(); |
|
| 67 | + $fileNameAndExt = basename($fullPath); |
|
| 68 | + $fileName = substr($fileNameAndExt, 0, strrpos($fileNameAndExt, '.')); |
|
| 69 | 69 | |
| 70 | - if ($expectedFileName !== $fileName) { |
|
| 71 | - $errorTemplate = 'Filename "%s" doesn\'t match the name of the %s that it contains "%s" in lower case. "%s" was expected.'; |
|
| 72 | - $errorMessage = sprintf( |
|
| 73 | - $errorTemplate, |
|
| 74 | - $fileName, |
|
| 75 | - strtolower($tokens[$stackPtr]['content']), // class or interface |
|
| 76 | - $decName, |
|
| 77 | - $expectedFileName |
|
| 78 | - ); |
|
| 79 | - $phpcsFile->addError($errorMessage, 0); |
|
| 80 | - } |
|
| 81 | - }//end process() |
|
| 70 | + if ($expectedFileName !== $fileName) { |
|
| 71 | + $errorTemplate = 'Filename "%s" doesn\'t match the name of the %s that it contains "%s" in lower case. "%s" was expected.'; |
|
| 72 | + $errorMessage = sprintf( |
|
| 73 | + $errorTemplate, |
|
| 74 | + $fileName, |
|
| 75 | + strtolower($tokens[$stackPtr]['content']), // class or interface |
|
| 76 | + $decName, |
|
| 77 | + $expectedFileName |
|
| 78 | + ); |
|
| 79 | + $phpcsFile->addError($errorMessage, 0); |
|
| 80 | + } |
|
| 81 | + }//end process() |
|
| 82 | 82 | }//end class |
| 83 | 83 | |
| 84 | 84 | ?> |
@@ -33,74 +33,74 @@ discard block |
||
| 33 | 33 | class Utf8EncodingSniff implements Sniff |
| 34 | 34 | { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Returns an array of tokens this test wants to listen for. |
|
| 38 | - * |
|
| 39 | - * @return array |
|
| 40 | - */ |
|
| 41 | - public function register() |
|
| 42 | - { |
|
| 43 | - return array( |
|
| 44 | - T_OPEN_TAG |
|
| 45 | - ); |
|
| 46 | - |
|
| 47 | - }//end register() |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Processes this test, when one of its tokens is encountered. |
|
| 52 | - * |
|
| 53 | - * @param File $phpcsFile The current file being scanned. |
|
| 54 | - * @param int $stackPtr The position of the current token |
|
| 55 | - * in the stack passed in $tokens. |
|
| 56 | - * |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function process(File $phpcsFile, $stackPtr) |
|
| 60 | - { |
|
| 61 | - // We are only interested if this is the first open tag. |
|
| 62 | - if ($stackPtr !== 0) { |
|
| 63 | - if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 64 | - return; |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $file_path = $phpcsFile->getFilename(); |
|
| 69 | - $file_name = basename($file_path); |
|
| 70 | - $file_content = file_get_contents($file_path); |
|
| 71 | - if (false === mb_check_encoding($file_content, 'UTF-8')) { |
|
| 72 | - $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding.'; |
|
| 73 | - $phpcsFile->addError($error, 0); |
|
| 74 | - } |
|
| 75 | - if ( ! self::_checkUtf8W3c($file_content)) { |
|
| 76 | - $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not successfully pass the W3C test.'; |
|
| 77 | - $phpcsFile->addError($error, 0); |
|
| 78 | - } |
|
| 79 | - if ( ! self::_checkUtf8Rfc3629($file_content)) { |
|
| 80 | - $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not meet RFC3629 requirements.'; |
|
| 81 | - $phpcsFile->addError($error, 0); |
|
| 82 | - } |
|
| 83 | - }//end process() |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Checks that the string $content contains only valid UTF-8 chars |
|
| 88 | - * using W3C's method. |
|
| 89 | - * Returns true if $content contains only UTF-8 chars, false otherwise. |
|
| 90 | - * |
|
| 91 | - * @param string $content String to check. |
|
| 92 | - * |
|
| 93 | - * @return bool true if $content contains only UTF-8 chars, false otherwise. |
|
| 94 | - * |
|
| 95 | - * @see http://w3.org/International/questions/qa-forms-utf-8.html |
|
| 96 | - */ |
|
| 97 | - private static function _checkUtf8W3c($content) |
|
| 98 | - { |
|
| 99 | - $content_chunks=self::mb_chunk_split($content, 4096, ''); |
|
| 100 | - foreach($content_chunks as $content_chunk) |
|
| 36 | + /** |
|
| 37 | + * Returns an array of tokens this test wants to listen for. |
|
| 38 | + * |
|
| 39 | + * @return array |
|
| 40 | + */ |
|
| 41 | + public function register() |
|
| 42 | + { |
|
| 43 | + return array( |
|
| 44 | + T_OPEN_TAG |
|
| 45 | + ); |
|
| 46 | + |
|
| 47 | + }//end register() |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Processes this test, when one of its tokens is encountered. |
|
| 52 | + * |
|
| 53 | + * @param File $phpcsFile The current file being scanned. |
|
| 54 | + * @param int $stackPtr The position of the current token |
|
| 55 | + * in the stack passed in $tokens. |
|
| 56 | + * |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function process(File $phpcsFile, $stackPtr) |
|
| 60 | + { |
|
| 61 | + // We are only interested if this is the first open tag. |
|
| 62 | + if ($stackPtr !== 0) { |
|
| 63 | + if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
| 64 | + return; |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $file_path = $phpcsFile->getFilename(); |
|
| 69 | + $file_name = basename($file_path); |
|
| 70 | + $file_content = file_get_contents($file_path); |
|
| 71 | + if (false === mb_check_encoding($file_content, 'UTF-8')) { |
|
| 72 | + $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding.'; |
|
| 73 | + $phpcsFile->addError($error, 0); |
|
| 74 | + } |
|
| 75 | + if ( ! self::_checkUtf8W3c($file_content)) { |
|
| 76 | + $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not successfully pass the W3C test.'; |
|
| 77 | + $phpcsFile->addError($error, 0); |
|
| 78 | + } |
|
| 79 | + if ( ! self::_checkUtf8Rfc3629($file_content)) { |
|
| 80 | + $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not meet RFC3629 requirements.'; |
|
| 81 | + $phpcsFile->addError($error, 0); |
|
| 82 | + } |
|
| 83 | + }//end process() |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Checks that the string $content contains only valid UTF-8 chars |
|
| 88 | + * using W3C's method. |
|
| 89 | + * Returns true if $content contains only UTF-8 chars, false otherwise. |
|
| 90 | + * |
|
| 91 | + * @param string $content String to check. |
|
| 92 | + * |
|
| 93 | + * @return bool true if $content contains only UTF-8 chars, false otherwise. |
|
| 94 | + * |
|
| 95 | + * @see http://w3.org/International/questions/qa-forms-utf-8.html |
|
| 96 | + */ |
|
| 97 | + private static function _checkUtf8W3c($content) |
|
| 98 | + { |
|
| 99 | + $content_chunks=self::mb_chunk_split($content, 4096, ''); |
|
| 100 | + foreach($content_chunks as $content_chunk) |
|
| 101 | 101 | { |
| 102 | 102 | $preg_result= preg_match( |
| 103 | - '%^(?: |
|
| 103 | + '%^(?: |
|
| 104 | 104 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 105 | 105 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
| 106 | 106 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 111 | 111 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 112 | 112 | )*$%xs', |
| 113 | - $content_chunk |
|
| 113 | + $content_chunk |
|
| 114 | 114 | ); |
| 115 | 115 | if($preg_result!==1) |
| 116 | 116 | { |
@@ -119,66 +119,66 @@ discard block |
||
| 119 | 119 | |
| 120 | 120 | } |
| 121 | 121 | return true; |
| 122 | - }//end _checkUtf8W3c() |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Checks that the string $content contains only valid UTF-8 chars |
|
| 126 | - * using the method described in RFC 3629. |
|
| 127 | - * Returns true if $content contains only UTF-8 chars, false otherwise. |
|
| 128 | - * |
|
| 129 | - * @param string $content String to check. |
|
| 130 | - * |
|
| 131 | - * @return bool true if $content contains only UTF-8 chars, false otherwise. |
|
| 132 | - * |
|
| 133 | - * @see http://www.php.net/manual/en/function.mb-detect-encoding.php#85294 |
|
| 134 | - */ |
|
| 135 | - private static function _checkUtf8Rfc3629($content) |
|
| 136 | - { |
|
| 137 | - $len = strlen($content); |
|
| 138 | - for ($i = 0; $i < $len; $i++) { |
|
| 139 | - $c = ord($content[$i]); |
|
| 140 | - if ($c > 128) { |
|
| 141 | - if (($c >= 254)) { |
|
| 142 | - return false; |
|
| 143 | - } elseif ($c >= 252) { |
|
| 144 | - $bits=6; |
|
| 145 | - } elseif ($c >= 248) { |
|
| 146 | - $bits=5; |
|
| 147 | - } elseif ($c >= 240) { |
|
| 148 | - $bytes = 4; |
|
| 149 | - } elseif ($c >= 224) { |
|
| 150 | - $bytes = 3; |
|
| 151 | - } elseif ($c >= 192) { |
|
| 152 | - $bytes = 2; |
|
| 153 | - } else { |
|
| 154 | - return false; |
|
| 155 | - } if (($i + $bytes) > $len) { |
|
| 156 | - return false; |
|
| 157 | - } while ($bytes > 1) { |
|
| 158 | - $i++; |
|
| 159 | - $b = ord($content[$i]); |
|
| 160 | - if ($b < 128 || $b > 191) { |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 163 | - $bytes--; |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - return true; |
|
| 168 | - }//_checkUtf8Rfc3629() |
|
| 122 | + }//end _checkUtf8W3c() |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Checks that the string $content contains only valid UTF-8 chars |
|
| 126 | + * using the method described in RFC 3629. |
|
| 127 | + * Returns true if $content contains only UTF-8 chars, false otherwise. |
|
| 128 | + * |
|
| 129 | + * @param string $content String to check. |
|
| 130 | + * |
|
| 131 | + * @return bool true if $content contains only UTF-8 chars, false otherwise. |
|
| 132 | + * |
|
| 133 | + * @see http://www.php.net/manual/en/function.mb-detect-encoding.php#85294 |
|
| 134 | + */ |
|
| 135 | + private static function _checkUtf8Rfc3629($content) |
|
| 136 | + { |
|
| 137 | + $len = strlen($content); |
|
| 138 | + for ($i = 0; $i < $len; $i++) { |
|
| 139 | + $c = ord($content[$i]); |
|
| 140 | + if ($c > 128) { |
|
| 141 | + if (($c >= 254)) { |
|
| 142 | + return false; |
|
| 143 | + } elseif ($c >= 252) { |
|
| 144 | + $bits=6; |
|
| 145 | + } elseif ($c >= 248) { |
|
| 146 | + $bits=5; |
|
| 147 | + } elseif ($c >= 240) { |
|
| 148 | + $bytes = 4; |
|
| 149 | + } elseif ($c >= 224) { |
|
| 150 | + $bytes = 3; |
|
| 151 | + } elseif ($c >= 192) { |
|
| 152 | + $bytes = 2; |
|
| 153 | + } else { |
|
| 154 | + return false; |
|
| 155 | + } if (($i + $bytes) > $len) { |
|
| 156 | + return false; |
|
| 157 | + } while ($bytes > 1) { |
|
| 158 | + $i++; |
|
| 159 | + $b = ord($content[$i]); |
|
| 160 | + if ($b < 128 || $b > 191) { |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | + $bytes--; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + return true; |
|
| 168 | + }//_checkUtf8Rfc3629() |
|
| 169 | 169 | |
| 170 | 170 | /** |
| 171 | - * Splits a string to chunks of given size |
|
| 172 | - * This helps to avoid segmentation fault errors when large text is given |
|
| 173 | - * Returns array of strings after splitting |
|
| 174 | - * |
|
| 175 | - * @param string $str String to split. |
|
| 176 | - * @param int $len number of characters per chunk |
|
| 177 | - * |
|
| 178 | - * @return array string array after splitting |
|
| 179 | - * |
|
| 180 | - * @see http://php.net/manual/en/function.chunk-split.php |
|
| 181 | - */ |
|
| 171 | + * Splits a string to chunks of given size |
|
| 172 | + * This helps to avoid segmentation fault errors when large text is given |
|
| 173 | + * Returns array of strings after splitting |
|
| 174 | + * |
|
| 175 | + * @param string $str String to split. |
|
| 176 | + * @param int $len number of characters per chunk |
|
| 177 | + * |
|
| 178 | + * @return array string array after splitting |
|
| 179 | + * |
|
| 180 | + * @see http://php.net/manual/en/function.chunk-split.php |
|
| 181 | + */ |
|
| 182 | 182 | private static function mb_chunk_split($str, $len, $glue) |
| 183 | 183 | { |
| 184 | 184 | if (empty($str)) return false; |
@@ -196,14 +196,14 @@ discard block |
||
| 196 | 196 | return $new; |
| 197 | 197 | }//mb_chunk_split |
| 198 | 198 | /** |
| 199 | - * Supporting function for mb_chunk_split |
|
| 200 | - * |
|
| 201 | - * @param string $str |
|
| 199 | + * Supporting function for mb_chunk_split |
|
| 200 | + * |
|
| 201 | + * @param string $str |
|
| 202 | + * |
|
| 203 | + * @return array |
|
| 202 | 204 | * |
| 203 | - * @return array |
|
| 204 | - * |
|
| 205 | - * @see http://php.net/manual/en/function.chunk-split.php |
|
| 206 | - */ |
|
| 205 | + * @see http://php.net/manual/en/function.chunk-split.php |
|
| 206 | + */ |
|
| 207 | 207 | private static function mbStringToArray ($str) |
| 208 | 208 | { |
| 209 | 209 | if (empty($str)) return false; |