Conditions | 26 |
Paths | 60 |
Total Lines | 92 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 apply(Context $ctx, array $symbols, \Iterator $tokens, int $n, array $attribute): \Generator |
||
40 | { |
||
41 | $type = null; |
||
|
|||
42 | for ($tokens->rewind(); $tokens->valid(); $tokens->next()) { |
||
43 | /** @var Token $token */ |
||
44 | $token = $tokens->current(); |
||
45 | switch ($token->getType()) { |
||
46 | case Token::NAME: |
||
47 | $type = null; |
||
48 | $v = -1; |
||
49 | |||
50 | for ($i = 0; $i <= $n; $i++) { |
||
51 | if ($symbols[$i]->name === $token->getValue()) { |
||
52 | if ($v < 0) { |
||
53 | $v = $i; |
||
54 | } else { |
||
55 | throw new ParseException("Ambiguous semantic value reference for $token"); |
||
56 | } |
||
57 | } |
||
58 | } |
||
59 | |||
60 | if ($v < 0) { |
||
61 | for ($i = 0; $i <= $n; $i++) { |
||
62 | if ($attribute[$i] === $token->getValue()) { |
||
63 | $v = $i; |
||
64 | break; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | if ($token->getValue() === $attribute[$n + 1]) { |
||
69 | $v = 0; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | if ($v >= 0) { |
||
74 | $token = new Token($v === 0 ? Token::DOLLAR : 0, $token->getValue(), $token->getLine(), $token->getFilename()); |
||
75 | goto semval; |
||
76 | } |
||
77 | break; |
||
78 | |||
79 | case Token::DOLLAR: |
||
80 | $type = null; |
||
81 | $token = self::next($tokens); |
||
82 | if ($token->getId() === '<') { |
||
83 | $token = self::next($tokens); |
||
84 | if ($token->getId() !== Token::NAME) { |
||
85 | throw ParseException::unexpected($token, Token::NAME); |
||
86 | } |
||
87 | $type = $ctx->intern($token->getValue()); |
||
88 | $dump = self::next($tokens); |
||
89 | if ($dump->getId() !== '>') { |
||
90 | throw ParseException::unexpected($dump, '>'); |
||
91 | } |
||
92 | $token = self::next($tokens); |
||
93 | } |
||
94 | |||
95 | if ($token->getType() === Token::DOLLAR) { |
||
96 | $v = 0; |
||
97 | } elseif ($token->getValue()[0] === '-') { |
||
98 | $token = self::next($tokens); |
||
99 | if ($token->getId() !== Token::NUMBER) { |
||
100 | throw ParseException::unexpected($token, Token::NUMBER); |
||
101 | } |
||
102 | $v = -1 * ((int) $token->getValue()); |
||
103 | } else { |
||
104 | if ($token->getId() !== Token::NUMBER) { |
||
105 | throw new \RuntimeException('Number expected'); |
||
106 | } |
||
107 | $v = (int) $token->getValue(); |
||
108 | if ($v > $n) { |
||
109 | throw new \RuntimeException('N is too big'); |
||
110 | } |
||
111 | } |
||
112 | semval: |
||
113 | if ($type === null) { |
||
114 | $type = $symbols[$v]->type; |
||
115 | } |
||
116 | |||
117 | if ($type === null /* && $ctx->unioned */ && false) { |
||
118 | throw new ParseException('Type not defined for '.$symbols[$v]->name); |
||
119 | } |
||
120 | |||
121 | foreach ($this->parseDollar($ctx, $token, $v, $n, $type ? $type->name : null) as $token) { |
||
122 | yield $token; |
||
123 | } |
||
124 | |||
125 | continue 2; |
||
126 | } |
||
127 | |||
128 | yield $token; |
||
129 | } |
||
130 | } |
||
131 | |||
182 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.