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