Conditions | 11 |
Paths | 26 |
Total Lines | 47 |
Code Lines | 25 |
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 |
||
49 | public function urls(?array &$response): void |
||
50 | { |
||
51 | if (empty($response) || empty($response['data'])) { |
||
52 | return; |
||
53 | } |
||
54 | |||
55 | // extract ids of objects |
||
56 | $types = $this->getConfig('objectTypes', []); |
||
57 | $ids = (array)Hash::extract($response, sprintf('data.{n}[type=/%s/].id', join('|', $types))); |
||
58 | if (empty($ids)) { |
||
59 | return; |
||
60 | } |
||
61 | |||
62 | $thumbs = $this->getThumbs($ids); |
||
63 | if ($thumbs === null) { |
||
64 | // An error happened: let's try again by generating one thumbnail at a time. |
||
65 | $thumbs = []; |
||
66 | foreach ($ids as $id) { |
||
67 | $thumbs += (array)$this->getThumbs([$id]); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | foreach ($response['data'] as &$object) { |
||
72 | $thumbnail = Hash::get($object, 'attributes.provider_thumbnail'); |
||
73 | // if provider_thumbnail is found there's no need to extract it from thumbsResponse |
||
74 | if ($thumbnail) { |
||
75 | $object['meta']['thumb_url'] = $thumbnail; |
||
76 | continue; |
||
77 | } |
||
78 | |||
79 | // extract url of the matching objectid's thumb |
||
80 | $thumbnail = Hash::get($thumbs, sprintf('%d.url', $object['id'])); |
||
81 | if ($thumbnail !== null) { |
||
82 | $object['meta']['thumb_url'] = $thumbnail; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | // Extract possible errors in creation of thumbnail(s) |
||
87 | $errors = (array)Hash::extract($thumbs, '{*}[acceptable=false].message'); |
||
88 | |||
89 | if (!empty($errors)) { |
||
90 | $message = __('There were errors creating the thumbnail(s)'); |
||
91 | if (count($errors) === 1) { |
||
92 | $message = array_shift($errors); |
||
93 | } |
||
94 | |||
95 | $this->Flash->error($message, ['params' => $errors]); |
||
96 | } |
||
124 |