Conditions | 26 |
Paths | 60 |
Total Lines | 98 |
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::T_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( |
||
75 | $v === 0 ? Token::T_DOLLAR : 0, |
||
76 | $token->getValue(), |
||
77 | $token->getLine(), |
||
78 | $token->getFilename() |
||
79 | ); |
||
80 | goto semval; |
||
81 | } |
||
82 | |||
83 | break; |
||
84 | |||
85 | case Token::T_DOLLAR: |
||
86 | $type = null; |
||
87 | $token = self::next($tokens); |
||
88 | if ($token->getValue()[0] === '<') { |
||
89 | $token = self::next($tokens); |
||
90 | if ($token->getType() !== Token::T_NAME) { |
||
91 | throw ParseException::unexpected($token, Token::T_NAME); |
||
92 | } |
||
93 | $type = $ctx->intern($token->getValue()); |
||
94 | $dump = self::next($tokens); |
||
95 | if ($dump->getValue()[0] !== '>') { |
||
96 | throw ParseException::unexpected($dump, '>'); |
||
97 | } |
||
98 | $token = self::next($tokens); |
||
99 | } |
||
100 | |||
101 | if ($token->getType() === Token::T_DOLLAR) { |
||
102 | $v = 0; |
||
103 | } elseif ($token->getValue()[0] === '-') { |
||
104 | $token = self::next($tokens); |
||
105 | if ($token->getType() !== Token::T_NUMBER) { |
||
106 | throw ParseException::unexpected($token, Token::T_NUMBER); |
||
107 | } |
||
108 | $v = -1 * ((int) $token->getValue()); |
||
109 | } else { |
||
110 | if ($token->getType() !== Token::T_NUMBER) { |
||
111 | throw new \RuntimeException('Number expected'); |
||
112 | } |
||
113 | $v = (int) $token->getValue(); |
||
114 | if ($v > $n) { |
||
115 | throw new \RuntimeException('N is too big'); |
||
116 | } |
||
117 | } |
||
118 | semval: |
||
119 | if ($type === null) { |
||
120 | $type = $symbols[$v]->type; |
||
121 | } |
||
122 | |||
123 | if ($type === null /* && $ctx->unioned */ && false) { |
||
124 | throw new ParseException('Type not defined for '.$symbols[$v]->name); |
||
125 | } |
||
126 | |||
127 | foreach ($this->parseDollar($ctx, $token, $v, $n, $type ? $type->name : null) as $token) { |
||
128 | yield $token; |
||
129 | } |
||
130 | |||
131 | continue 2; |
||
132 | } |
||
133 | |||
134 | yield $token; |
||
135 | } |
||
136 | } |
||
137 | |||
188 |
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.