| Conditions | 14 |
| Paths | 69 |
| Total Lines | 41 |
| Code Lines | 26 |
| 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 |
||
| 39 | public function run() |
||
| 40 | { |
||
| 41 | try { |
||
| 42 | $data = $this->getData(); |
||
| 43 | $multiple = is_array($data); |
||
| 44 | if (is_object($data)) { |
||
| 45 | $data = [$data]; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (empty($data)) { |
||
| 49 | throw new Exception(Response::INVALID_REQUEST); |
||
| 50 | } elseif ($this->limit !== 0 && $this->limit < count($data)) { |
||
| 51 | throw new Exception(Response::LIMIT_EXCEEDED); |
||
| 52 | } |
||
| 53 | |||
| 54 | $responses = []; |
||
| 55 | foreach ((array)$data as $datum) { |
||
| 56 | $request = (new Request)->setProperties($datum); |
||
| 57 | $response = $this->getResponse($request); |
||
| 58 | if ($response->isNotification() && !$response->hasError()) { |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | $responses[] = $response->getData(); |
||
| 62 | } |
||
| 63 | |||
| 64 | // Check if ID already exists |
||
| 65 | $ids = []; |
||
| 66 | foreach ($responses as $response) { |
||
| 67 | if ($response->id === null) continue; |
||
| 68 | if (in_array($response->id, $ids, true)) { |
||
| 69 | throw new Exception(Response::DUPLICATED_ID); |
||
| 70 | } |
||
| 71 | array_push($ids, $response->id); |
||
| 72 | } |
||
| 73 | |||
| 74 | } catch (Exception $exception) { |
||
| 75 | return (string)$exception; |
||
| 76 | } |
||
| 77 | |||
| 78 | if (empty($responses)) return ''; |
||
| 79 | return json_encode($multiple ? $responses : current($responses)); |
||
| 80 | } |
||
| 153 |