| Conditions | 10 |
| Paths | 64 |
| Total Lines | 43 |
| Code Lines | 22 |
| 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 declare(strict_types=1); |
||
| 32 | public function getItem($segmentId): ?array |
||
| 33 | { |
||
| 34 | // Endpoint URI |
||
| 35 | $uri = sprintf('v2/segments/%s/audience/totals', $segmentId); |
||
| 36 | |||
| 37 | try { |
||
| 38 | $data = null; |
||
| 39 | |||
| 40 | // try to get from cache |
||
| 41 | if ($this->cache) { |
||
| 42 | $data = $this->cache->get($this->cachePrefix, $uri, []); |
||
| 43 | } |
||
| 44 | |||
| 45 | // load from API |
||
| 46 | if (!$data) { |
||
| 47 | $data = $this->httpClient->get($uri)->getBody()->getContents(); |
||
| 48 | |||
| 49 | if ($this->cache && $data) { |
||
| 50 | $this->cache->put($this->cachePrefix, $uri, [], $data); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | $dataDecoded = \json_decode($data); |
||
| 55 | |||
| 56 | $audiences = []; |
||
| 57 | |||
| 58 | if (\is_array($dataDecoded) && \count($dataDecoded) > 0) { |
||
| 59 | foreach ($dataDecoded as $item) { |
||
| 60 | $audiences[] = AudienceHydrator::fromStdClass($item); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | return $audiences; |
||
| 65 | |||
| 66 | } catch (ClientException $e) { |
||
| 67 | $response = $e->getResponse(); |
||
| 68 | if ($response === null) { |
||
| 69 | throw new RuntimeException('Null response'); |
||
| 70 | } |
||
| 71 | $responseBody = $response->getBody()->getContents(); |
||
| 72 | $responseCode = $response->getStatusCode(); |
||
| 73 | |||
| 74 | throw EntityNotFoundException::translate($segmentId, $responseBody, $responseCode); |
||
| 75 | } |
||
| 79 |