| Conditions | 11 |
| Paths | 10 |
| Total Lines | 40 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 97 | public function matchOnPath() |
||
| 98 | { |
||
| 99 | if (!is_string($this->operand) || !$this->jsonText->isJson($this->operand)) { |
||
| 100 | $msg = 'Invalid JSON passed as operand on RHS.'; |
||
| 101 | throw new JSONTextException($msg); |
||
| 102 | } |
||
| 103 | |||
| 104 | $operandAsArray = $this->jsonText->toArray($this->operand); |
||
| 105 | |||
| 106 | // Empty is OK..could've been accidental... |
||
| 107 | if (!count($operandAsArray)) { |
||
| 108 | return []; |
||
| 109 | } |
||
| 110 | |||
| 111 | $keys = array_keys($operandAsArray); |
||
| 112 | if (count($keys) >1) { |
||
| 113 | $msg = 'Sorry. I can\'t handle complex operands.'; |
||
| 114 | throw new JSONTextException($msg); |
||
| 115 | } |
||
| 116 | |||
| 117 | $vals = array_values($operandAsArray); |
||
| 118 | if (count($vals) >1) { |
||
| 119 | $msg = 'Sorry. I can\'t handle complex operands.'; |
||
| 120 | throw new JSONTextException($msg); |
||
| 121 | } |
||
| 122 | |||
| 123 | // TODO Move this back to JSONText no use in iterating twice over source data |
||
| 124 | $data = []; |
||
| 125 | foreach ($this->jsonText->getValueAsIterable() as $sourceKey => $sourceVal) { |
||
| 126 | if ($keys[0] === $sourceKey && is_array($sourceVal) && !empty($sourceVal[$vals[0]])) { |
||
| 127 | $data[] = $sourceVal[$vals[0]]; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if (count($data) === 1) { |
||
| 132 | return $data[0]; |
||
| 133 | } |
||
| 134 | |||
| 135 | return $data; |
||
| 136 | } |
||
| 137 | |||
| 139 |