| Total Complexity | 44 |
| Total Lines | 271 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DocumentationNodeParser 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 DocumentationNodeParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class DocumentationNodeParser |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var Lexer $lexer The lexer to use. |
||
| 26 | */ |
||
| 27 | private $lexer; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var AnnotationFactory $annotationFactory The annotation factory to use. |
||
| 31 | */ |
||
| 32 | private $annotationFactory; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var AbstractAnnotation[] $parsedAnnotations The parsed annotation list. |
||
| 36 | */ |
||
| 37 | private $parsedAnnotations; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int $currentLine The current line number. |
||
| 41 | */ |
||
| 42 | private $currentLine; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var AbstractAnnotation $currentAnnotation The current parsed annotation, null if none. |
||
| 46 | */ |
||
| 47 | private $currentAnnotation; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string|null $currentAnnotationContent The current parsed annotation content, null if none. |
||
| 51 | */ |
||
| 52 | private $currentAnnotationContent; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int|null $openedStringToken The opened string opening token identifier, null if no string opened. |
||
| 56 | */ |
||
| 57 | private $openedStringToken; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool $currentlyEscaping Tells if last parsed token was an escape token. |
||
| 61 | */ |
||
| 62 | private $currentlyEscaping; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int $openedParenthesis The opened parenthesis number for an annotation content. |
||
| 66 | */ |
||
| 67 | private $openedParenthesis; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * DocumentationNodeParser constructor. |
||
| 71 | * |
||
| 72 | * @param Lexer $lexer The lexer to use. |
||
| 73 | * @param AnnotationFactory $annotationFactory The annotation factory to use. |
||
| 74 | */ |
||
| 75 | public function __construct(Lexer $lexer, AnnotationFactory $annotationFactory) |
||
| 76 | { |
||
| 77 | $this->lexer = $lexer; |
||
| 78 | $this->annotationFactory = $annotationFactory; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Parse a node to update the parent node model. |
||
| 83 | * |
||
| 84 | * @param Doc $node The node to parse. |
||
| 85 | * @param FunctionModelInterface $parent The parent node. |
||
| 86 | * |
||
| 87 | * @return FunctionModelInterface The updated parent. |
||
| 88 | * |
||
| 89 | * @throws AnnotationParseException If an annotation is invalid. |
||
| 90 | */ |
||
| 91 | public function invoke(Doc $node, FunctionModelInterface $parent): FunctionModelInterface |
||
| 92 | { |
||
| 93 | $documentation = $node->getText(); |
||
| 94 | $parent->setDocumentation($documentation); |
||
| 95 | |||
| 96 | try { |
||
| 97 | $this->initialize(); |
||
| 98 | |||
| 99 | $this->lexer->setInput($documentation); |
||
| 100 | $this->lexer->moveNext(); |
||
| 101 | $this->lexer->moveNext(); |
||
| 102 | |||
| 103 | while ($this->lexer->token) { |
||
| 104 | $this->parse( |
||
| 105 | $this->lexer->token['type'], |
||
| 106 | $this->lexer->token['value'] |
||
| 107 | ); |
||
| 108 | $this->lexer->moveNext(); |
||
| 109 | $this->lexer->moveNext(); |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($this->currentAnnotation !== null) { |
||
| 113 | if ($this->currentAnnotationContent === null) { |
||
| 114 | $this->parsedAnnotations[] = $this->currentAnnotation; |
||
| 115 | } else { |
||
| 116 | throw new AnnotationParseException( |
||
| 117 | 'An annotation content is not closed (you probably forget to close a parenthesis or a quote)' |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | foreach ($this->parsedAnnotations as $annotation) { |
||
| 123 | $annotation->compile(); |
||
| 124 | /** @todo add to parent */ |
||
| 125 | } |
||
| 126 | } catch (AnnotationParseException $exception) { |
||
| 127 | throw new AnnotationParseException( |
||
| 128 | sprintf('On function "%s": %s', $parent->getName(), $exception->getMessage()) |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $parent; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Initialize the documentation parsing process. |
||
| 137 | */ |
||
| 138 | private function initialize(): void |
||
| 139 | { |
||
| 140 | $this->parsedAnnotations = []; |
||
| 141 | $this->currentLine = 0; |
||
| 142 | $this->reset(); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Reset an annotation parsing. |
||
| 147 | */ |
||
| 148 | private function reset(): void |
||
| 149 | { |
||
| 150 | $this->currentAnnotation = null; |
||
| 151 | $this->currentAnnotationContent = null; |
||
| 152 | $this->openedStringToken = null; |
||
| 153 | $this->currentlyEscaping = false; |
||
| 154 | |||
| 155 | $this->openedParenthesis = 0; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Parse a token with a value and type, and consume it depending on type. |
||
| 160 | * |
||
| 161 | * @param int $type The token type (an integer from the Lexer class constant). |
||
| 162 | * @param string $value The token value. |
||
| 163 | * |
||
| 164 | * @throws AnnotationParseException If the token type is invalid. |
||
| 165 | */ |
||
| 166 | private function parse(int $type, string $value): void |
||
| 167 | { |
||
| 168 | switch ($type) { |
||
| 169 | case Lexer::T_ANNOTATION: |
||
| 170 | $this->consumeAnnotationToken($value); |
||
| 171 | break; |
||
| 172 | case Lexer::T_O_PARENTHESIS: |
||
| 173 | $this->addTokenToContent($value); |
||
| 174 | $this->consumeOpeningParenthesisToken(); |
||
| 175 | break; |
||
| 176 | case Lexer::T_C_PARENTHESIS: |
||
| 177 | $this->consumeClosingParenthesisToken(); |
||
| 178 | $this->addTokenToContent($value); |
||
| 179 | break; |
||
| 180 | case Lexer::T_SINGLE_QUOTE: |
||
| 181 | case Lexer::T_DOUBLE_QUOTE: |
||
| 182 | $this->addTokenToContent($value); |
||
| 183 | $this->consumeQuoteToken($type); |
||
| 184 | break; |
||
| 185 | case Lexer::T_ASTERISK: |
||
| 186 | if ($this->openedStringToken !== null) { |
||
| 187 | // We are in a string, save this token value. |
||
| 188 | $this->addTokenToContent($value); |
||
| 189 | } |
||
| 190 | break; |
||
| 191 | case Lexer::T_LINE_BREAK: |
||
| 192 | $this->addTokenToContent($value); |
||
| 193 | $this->currentLine++; |
||
| 194 | break; |
||
| 195 | case Lexer::T_BACKSLASH: |
||
| 196 | case Lexer::T_WHITESPACE: |
||
| 197 | case Lexer::T_OTHER: |
||
| 198 | $this->addTokenToContent($value); |
||
| 199 | break; |
||
| 200 | default: |
||
| 201 | throw new AnnotationParseException(sprintf('A token of value "%s" has an invalid type', $value)); |
||
| 202 | } |
||
| 203 | $this->afterConsume($type); |
||
| 204 | } |
||
| 205 | |||
| 206 | private function addTokenToContent(string $value) |
||
| 207 | { |
||
| 208 | if ($this->currentAnnotationContent !== null) { |
||
| 209 | $this->currentAnnotationContent .= $value; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | private function consumeAnnotationToken(string $value): void |
||
| 214 | { |
||
| 215 | if ($this->currentAnnotation === null) { |
||
| 216 | // We are not in an annotation, build a new one. |
||
| 217 | $this->currentAnnotation = $this->annotationFactory |
||
| 218 | ->invoke($value, $this->currentLine); |
||
| 219 | } else { |
||
| 220 | if ($this->currentAnnotationContent === null) { |
||
| 221 | // It is an annotation without content, save it and create the new one. |
||
| 222 | $this->parsedAnnotations[] = $this->currentAnnotation; |
||
| 223 | $this->reset(); |
||
| 224 | $this->currentAnnotation = $this->annotationFactory |
||
| 225 | ->invoke($value, $this->currentLine); |
||
| 226 | } else { |
||
| 227 | // An annotation content parsing is not finished. |
||
| 228 | throw new AnnotationParseException( |
||
| 229 | 'An annotation content is not closed (you probably forget to close a parenthesis or a quote)' |
||
| 230 | ); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | private function consumeOpeningParenthesisToken(): void |
||
| 236 | { |
||
| 237 | if ($this->currentAnnotation !== null && $this->openedStringToken === null) { |
||
| 238 | // We are in an annotation but not in a string, lets do something. |
||
| 239 | if ($this->currentAnnotationContent === null) { |
||
| 240 | if ($this->currentAnnotation->getLine() === $this->currentLine) { |
||
| 241 | // Begin content parsing only if it is on the same line. |
||
| 242 | $this->currentAnnotationContent = ''; |
||
| 243 | $this->openedParenthesis++; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | private function consumeClosingParenthesisToken(): void |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | private function consumeQuoteToken(int $type): void |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * A method that is executed after consuming a token. |
||
| 283 | * |
||
| 284 | * @param int $type The token type (an integer from the Lexer class constant). |
||
| 285 | */ |
||
| 286 | private function afterConsume(int $type): void |
||
| 296 |