| Conditions | 17 |
| Paths | 41 |
| Total Lines | 66 |
| Code Lines | 40 |
| 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 |
||
| 124 | public function evaluateUsingStack(ScriptInterface $script, Stack $mainStack) |
||
| 125 | { |
||
| 126 | $vfStack = new Stack(); |
||
| 127 | $parser = $script->getScriptParser(); |
||
| 128 | $tracer = new PathTracer(); |
||
| 129 | |||
| 130 | foreach ($parser as $i => $operation) { |
||
| 131 | $opCode = $operation->getOp(); |
||
| 132 | $fExec = !$this->checkExec($vfStack, false); |
||
| 133 | |||
| 134 | if (in_array($opCode, $this->disabledOps, true)) { |
||
| 135 | throw new \RuntimeException('Disabled Opcode'); |
||
| 136 | } |
||
| 137 | |||
| 138 | if (Opcodes::OP_IF <= $opCode && $opCode <= Opcodes::OP_ENDIF) { |
||
| 139 | switch ($opCode) { |
||
| 140 | case Opcodes::OP_IF: |
||
| 141 | case Opcodes::OP_NOTIF: |
||
| 142 | // <expression> if [statements] [else [statements]] endif |
||
| 143 | $value = false; |
||
| 144 | if ($fExec) { |
||
| 145 | if ($mainStack->isEmpty()) { |
||
| 146 | throw new \RuntimeException('Unbalanced conditional'); |
||
| 147 | } |
||
| 148 | |||
| 149 | $value = $mainStack->pop(); |
||
| 150 | if ($opCode === Opcodes::OP_NOTIF) { |
||
| 151 | $value = !$value; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | $vfStack->push($value); |
||
| 155 | break; |
||
| 156 | |||
| 157 | case Opcodes::OP_ELSE: |
||
| 158 | if ($vfStack->isEmpty()) { |
||
| 159 | throw new \RuntimeException('Unbalanced conditional'); |
||
| 160 | } |
||
| 161 | $vfStack->push(!$vfStack->pop()); |
||
| 162 | break; |
||
| 163 | |||
| 164 | case Opcodes::OP_ENDIF: |
||
| 165 | if ($vfStack->isEmpty()) { |
||
| 166 | throw new \RuntimeException('Unbalanced conditional'); |
||
| 167 | } |
||
| 168 | $vfStack->pop(); |
||
| 169 | |||
| 170 | break; |
||
| 171 | } |
||
| 172 | |||
| 173 | $tracer->operation($operation); |
||
| 174 | } else if ($fExec) { |
||
| 175 | // Fill up trace with executed opcodes |
||
| 176 | $tracer->operation($operation); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | if (count($vfStack) !== 0) { |
||
| 181 | throw new \RuntimeException('Unbalanced conditional at script end'); |
||
| 182 | } |
||
| 183 | |||
| 184 | if (count($mainStack) !== 0) { |
||
| 185 | throw new \RuntimeException('Values remaining after script execution - invalid branch data'); |
||
| 186 | } |
||
| 187 | |||
| 188 | return $tracer->done(); |
||
| 189 | } |
||
| 190 | } |
||
| 191 |