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