| Conditions | 24 |
| Paths | 19 |
| Total Lines | 100 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 70 | public function handle(Parser $parser, Assembler $asm, Token $token): void |
||
| 71 | { |
||
| 72 | switch ($token->type) { |
||
| 73 | case HTMLGrammar::TYPE_OPEN: |
||
| 74 | case HTMLGrammar::TYPE_OPEN_SHORT: |
||
| 75 | $this->node = new Tag(new Parser\Context($token, $parser->getPath())); |
||
| 76 | $this->token = $token; |
||
| 77 | |||
| 78 | break; |
||
| 79 | |||
| 80 | case HTMLGrammar::TYPE_KEYWORD: |
||
| 81 | if ($this->node->name === null) { |
||
| 82 | $this->node->name = $this->parseToken($parser, $token); |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($this->attr !== null && !$this->attr->value instanceof Nil) { |
||
| 87 | $this->attr->value = $this->parseToken($parser, $token); |
||
| 88 | $this->attr = null; |
||
| 89 | break; |
||
| 90 | } |
||
| 91 | |||
| 92 | $this->attr = new Attr( |
||
| 93 | $this->parseToken($parser, $token), |
||
| 94 | new Nil(), |
||
| 95 | new Parser\Context($token, $parser->getPath()) |
||
| 96 | ); |
||
| 97 | |||
| 98 | $this->node->attrs[] = $this->attr; |
||
| 99 | break; |
||
| 100 | |||
| 101 | case HTMLGrammar::TYPE_EQUAL: |
||
| 102 | if ($this->attr === null) { |
||
| 103 | throw new SyntaxException('unexpected attribute token', $token); |
||
| 104 | } |
||
| 105 | |||
| 106 | // expect the value |
||
| 107 | $this->attr->value = null; |
||
| 108 | break; |
||
| 109 | |||
| 110 | case HTMLGrammar::TYPE_ATTRIBUTE: |
||
| 111 | if ($this->attr === null) { |
||
| 112 | throw new SyntaxException('unexpected attribute token', $token); |
||
| 113 | } |
||
| 114 | |||
| 115 | if ( |
||
| 116 | is_string($this->attr->name) |
||
| 117 | && ( |
||
| 118 | strpos($this->attr->name, 'on') === 0 |
||
| 119 | || in_array($this->attr->name, self::VERBATIM_ATTRIBUTES, true) |
||
| 120 | ) |
||
| 121 | ) { |
||
| 122 | $this->attr->value = $this->parseVerbatim($parser, $token); |
||
| 123 | } else { |
||
| 124 | $this->attr->value = $this->parseToken($parser, $token); |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->attr = null; |
||
| 128 | break; |
||
| 129 | |||
| 130 | case HTMLGrammar::TYPE_CLOSE_SHORT: |
||
| 131 | $this->node->void = true; |
||
| 132 | $asm->push($this->node); |
||
| 133 | $this->flush(); |
||
| 134 | break; |
||
| 135 | |||
| 136 | case HTMLGrammar::TYPE_CLOSE: |
||
| 137 | if ($this->token->type == HTMLGrammar::TYPE_OPEN_SHORT) { |
||
| 138 | if (!$asm->getNode() instanceof Tag || $asm->getNode()->name !== $this->node->name) { |
||
| 139 | throw new SyntaxException( |
||
| 140 | "Invalid closing tag `{$this->node->name}`, expected `{$asm->getNode()->name}`", |
||
| 141 | $this->token |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | $asm->close(); |
||
| 146 | } else { |
||
| 147 | if (in_array($this->node->name, self::VOID_TAGS)) { |
||
| 148 | $this->node->void = true; |
||
| 149 | $asm->push($this->node); |
||
| 150 | } else { |
||
| 151 | $asm->open($this->node, 'nodes'); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | $this->flush(); |
||
| 155 | |||
| 156 | break; |
||
| 157 | |||
| 158 | case HTMLGrammar::TYPE_VERBATIM: |
||
| 159 | $asm->push($this->parseVerbatim($parser, $token)); |
||
| 160 | break; |
||
| 161 | |||
| 162 | default: |
||
| 163 | if ($asm->getNode() instanceof Mixin || $asm->getNode() instanceof Verbatim) { |
||
| 164 | $node = $this->parseToken($parser, $token); |
||
| 165 | if (is_string($node)) { |
||
| 166 | $node = new Raw($node, new Parser\Context($token, $parser->getPath())); |
||
| 167 | } |
||
| 168 | |||
| 169 | $asm->push($node); |
||
| 170 | } |
||
| 207 |