| Conditions | 9 |
| Paths | 144 |
| Total Lines | 87 |
| Code Lines | 66 |
| 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 |
||
| 39 | public function parse(Token $token) |
||
| 40 | { |
||
| 41 | $lineno = $token->getLine(); |
||
| 42 | /** @var Parser $parser */ |
||
| 43 | $parser = $this->parser; |
||
| 44 | $stream = $parser->getStream(); |
||
| 45 | |||
| 46 | $nodes = []; |
||
| 47 | |||
| 48 | $attributes = [ |
||
| 49 | 'global' => false, |
||
| 50 | 'durationNum' => null, |
||
| 51 | 'durationUnit' => null, |
||
| 52 | 'elements' => false, |
||
| 53 | ]; |
||
| 54 | |||
| 55 | if ($stream->test(Token::NAME_TYPE, 'flagged')) { |
||
| 56 | $stream->next(); |
||
| 57 | $nodes['flags'] = $parser->getExpressionParser()->parseExpression(); |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($stream->test(Token::NAME_TYPE, 'with')) { |
||
| 61 | $stream->next(); |
||
| 62 | $stream->expect(Token::NAME_TYPE, 'elements'); |
||
| 63 | $attributes['elements'] = true; |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($stream->test(Token::NAME_TYPE, 'globally')) { |
||
| 67 | $attributes['global'] = true; |
||
| 68 | $stream->next(); |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($stream->test(Token::NAME_TYPE, 'using')) { |
||
| 72 | $stream->next(); |
||
| 73 | $stream->expect(Token::NAME_TYPE, 'key'); |
||
| 74 | $nodes['key'] = $parser->getExpressionParser()->parseExpression(); |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($stream->test(Token::NAME_TYPE, 'for')) { |
||
| 78 | $stream->next(); |
||
| 79 | $attributes['durationNum'] = $stream->expect(Token::NUMBER_TYPE)->getValue(); |
||
| 80 | $attributes['durationUnit'] = $stream->expect(Token::NAME_TYPE, |
||
| 81 | [ |
||
| 82 | 'sec', |
||
| 83 | 'secs', |
||
| 84 | 'second', |
||
| 85 | 'seconds', |
||
| 86 | 'min', |
||
| 87 | 'mins', |
||
| 88 | 'minute', |
||
| 89 | 'minutes', |
||
| 90 | 'hour', |
||
| 91 | 'hours', |
||
| 92 | 'day', |
||
| 93 | 'days', |
||
| 94 | 'fortnight', |
||
| 95 | 'fortnights', |
||
| 96 | 'forthnight', |
||
| 97 | 'forthnights', |
||
| 98 | 'month', |
||
| 99 | 'months', |
||
| 100 | 'year', |
||
| 101 | 'years', |
||
| 102 | 'week', |
||
| 103 | 'weeks' |
||
| 104 | ])->getValue(); |
||
| 105 | } else if ($stream->test(Token::NAME_TYPE, 'until')) { |
||
| 106 | $stream->next(); |
||
| 107 | $nodes['expiration'] = $parser->getExpressionParser()->parseExpression(); |
||
| 108 | } |
||
| 109 | |||
| 110 | if ($stream->test(Token::NAME_TYPE, 'if')) { |
||
| 111 | $stream->next(); |
||
| 112 | $nodes['conditions'] = $parser->getExpressionParser()->parseExpression(); |
||
| 113 | } else if ($stream->test(Token::NAME_TYPE, 'unless')) { |
||
| 114 | $stream->next(); |
||
| 115 | $nodes['ignoreConditions'] = $parser->getExpressionParser()->parseExpression(); |
||
| 116 | } |
||
| 117 | |||
| 118 | $stream->expect(Token::BLOCK_END_TYPE); |
||
| 119 | $nodes['body'] = $parser->subparse([ |
||
| 120 | $this, |
||
| 121 | 'decideCacheEnd' |
||
| 122 | ], true); |
||
| 123 | $stream->expect(Token::BLOCK_END_TYPE); |
||
| 124 | |||
| 125 | return new CacheFlagNode($nodes, $attributes, $lineno, $this->getTag()); |
||
| 126 | } |
||
| 137 |