| Total Complexity | 50 |
| Total Lines | 226 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like TokenParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TokenParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class TokenParser |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The token list. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private $tokens; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The number of tokens. |
||
| 22 | * |
||
| 23 | * @var int |
||
| 24 | */ |
||
| 25 | private $numTokens; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The current array pointer. |
||
| 29 | * |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | private $pointer = 0; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param string $contents |
||
| 36 | */ |
||
| 37 | public function __construct($contents) |
||
| 38 | { |
||
| 39 | $this->tokens = token_get_all($contents); |
||
| 40 | |||
| 41 | // The PHP parser sets internal compiler globals for certain things. Annoyingly, the last docblock comment it |
||
| 42 | // saw gets stored in doc_comment. When it comes to compile the next thing to be include()d this stored |
||
| 43 | // doc_comment becomes owned by the first thing the compiler sees in the file that it considers might have a |
||
| 44 | // docblock. If the first thing in the file is a class without a doc block this would cause calls to |
||
| 45 | // getDocBlock() on said class to return our long lost doc_comment. Argh. |
||
| 46 | // To workaround, cause the parser to parse an empty docblock. Sure getDocBlock() will return this, but at least |
||
| 47 | // it's harmless to us. |
||
| 48 | token_get_all("<?php\n/**\n *\n */"); |
||
| 49 | |||
| 50 | $this->numTokens = count($this->tokens); |
||
| 51 | |||
| 52 | // Compatibility defines for PHP < 8.0. |
||
| 53 | if (!defined('T_NAME_QUALIFIED')) { |
||
| 54 | \define('T_NAME_QUALIFIED', -2); |
||
| 55 | } |
||
| 56 | if (!defined('T_NAME_FULLY_QUALIFIED')) { |
||
| 57 | \define('T_NAME_FULLY_QUALIFIED', -3); |
||
| 58 | } |
||
| 59 | if (!defined('T_NAME_RELATIVE')) { |
||
| 60 | \define('T_NAME_RELATIVE', -4); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Gets the next non whitespace and non comment token. |
||
| 66 | * |
||
| 67 | * @param boolean $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped. |
||
| 68 | * If FALSE then only whitespace and normal comments are skipped. |
||
| 69 | * |
||
| 70 | * @return array|null The token if exists, null otherwise. |
||
| 71 | */ |
||
| 72 | public function next($docCommentIsComment = TRUE) |
||
| 73 | { |
||
| 74 | for ($i = $this->pointer; $i < $this->numTokens; $i++) { |
||
| 75 | $this->pointer++; |
||
| 76 | if ($this->tokens[$i][0] === T_WHITESPACE || |
||
| 77 | $this->tokens[$i][0] === T_COMMENT || |
||
| 78 | ($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) { |
||
| 79 | |||
| 80 | continue; |
||
| 81 | } |
||
| 82 | |||
| 83 | return $this->tokens[$i]; |
||
| 84 | } |
||
| 85 | |||
| 86 | return null; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Parses a single use statement. |
||
| 91 | * |
||
| 92 | * @return array A list with all found class names for a use statement. |
||
| 93 | */ |
||
| 94 | public function parseUseStatement() |
||
| 95 | { |
||
| 96 | return PHP_VERSION_ID >=80000 ? $this->parseUseStatementV8() : $this->parseUseStatementV7(); |
||
| 97 | } |
||
| 98 | |||
| 99 | private function parseUseStatementV7(): array |
||
| 100 | { |
||
| 101 | $groupRoot = ''; |
||
| 102 | $class = ''; |
||
| 103 | $alias = ''; |
||
| 104 | $statements = []; |
||
| 105 | $explicitAlias = false; |
||
| 106 | while (($token = $this->next())) { |
||
| 107 | $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR; |
||
| 108 | if (!$explicitAlias && $isNameToken) { |
||
| 109 | $class .= $token[1]; |
||
| 110 | $alias = $token[1]; |
||
| 111 | } else if ($explicitAlias && $isNameToken) { |
||
| 112 | $alias .= $token[1]; |
||
| 113 | } else if ($token[0] === T_AS) { |
||
| 114 | $explicitAlias = true; |
||
| 115 | $alias = ''; |
||
| 116 | } else if ($token === ',') { |
||
| 117 | $statements[strtolower($alias)] = $groupRoot . $class; |
||
| 118 | $class = ''; |
||
| 119 | $alias = ''; |
||
| 120 | $explicitAlias = false; |
||
| 121 | } else if ($token === ';') { |
||
| 122 | $statements[strtolower($alias)] = $groupRoot . $class; |
||
| 123 | break; |
||
| 124 | } else if ($token === '{' ) { |
||
| 125 | $groupRoot = $class; |
||
| 126 | $class = ''; |
||
| 127 | } else if ($token === '}' ) { |
||
| 128 | continue; |
||
| 129 | } else { |
||
| 130 | break; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | return $statements; |
||
| 135 | } |
||
| 136 | |||
| 137 | private function parseUseStatementV8(): array |
||
| 138 | { |
||
| 139 | $groupRoot = ''; |
||
| 140 | $class = ''; |
||
| 141 | $alias = ''; |
||
| 142 | $statements = []; |
||
| 143 | $explicitAlias = false; |
||
| 144 | while (($token = $this->next())) { |
||
| 145 | $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR; |
||
| 146 | |||
| 147 | if ($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED) { |
||
| 148 | $class .= $token[1]; |
||
| 149 | |||
| 150 | $classSplit = explode('\\', $token[1]); |
||
| 151 | $alias = $classSplit[count($classSplit) - 1]; |
||
| 152 | } else if($token[0] === T_NS_SEPARATOR) { |
||
| 153 | $class .= '\\'; |
||
| 154 | } else if ($token[0] === T_AS) { |
||
| 155 | $explicitAlias = true; |
||
| 156 | $alias = ''; |
||
| 157 | } else if ($isNameToken && !$explicitAlias) { |
||
| 158 | $class = $token[1]; |
||
| 159 | $alias = $token[1]; |
||
| 160 | } else if ($isNameToken && $explicitAlias) { |
||
| 161 | $alias = $token[1]; |
||
| 162 | } else if ($token === ',') { |
||
| 163 | $statements[strtolower($alias)] = $groupRoot . $class; |
||
| 164 | $class = ''; |
||
| 165 | $alias = ''; |
||
| 166 | $explicitAlias = false; |
||
| 167 | } else if ($token === ';') { |
||
| 168 | $statements[strtolower($alias)] = $groupRoot . $class; |
||
| 169 | break; |
||
| 170 | } else if ($token === '{' ) { |
||
| 171 | $groupRoot = $class; |
||
| 172 | $class = ''; |
||
| 173 | } else if ($token === '}' ) { |
||
| 174 | continue; |
||
| 175 | } else { |
||
| 176 | break; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | return $statements; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Gets all use statements. |
||
| 185 | * |
||
| 186 | * @param string $namespaceName The namespace name of the reflected class. |
||
| 187 | * |
||
| 188 | * @return array A list with all found use statements. |
||
| 189 | */ |
||
| 190 | public function parseUseStatements($namespaceName) |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Gets the namespace. |
||
| 213 | * |
||
| 214 | * @return string The found namespace. |
||
| 215 | */ |
||
| 216 | public function parseNamespace() |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Gets the class name. |
||
| 228 | * |
||
| 229 | * @return string The found class name. |
||
| 230 | */ |
||
| 231 | public function parseClass() |
||
| 237 | } |
||
| 238 | } |
||
| 239 |