Conditions | 5 |
Paths | 1 |
Total Lines | 53 |
Code Lines | 33 |
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 |
||
97 | private function defaultOptions(): array |
||
98 | { |
||
99 | return [ |
||
100 | 'concurrency' => 5, |
||
101 | 'timeout' => 2.0, |
||
102 | 'on_reject' => static::ON_REJECT_EXCEPTION, |
||
103 | 'base_uri' => '', |
||
104 | 'cache_prefix' => 'esi:include', |
||
105 | 'cache_ttl' => 3600, |
||
106 | 'request_options' => [], |
||
107 | 'fulfilled' => function (string &$data, array $esiRequests) |
||
108 | { |
||
109 | return (function (Response $response, int $index) use (&$data, $esiRequests) |
||
110 | { |
||
111 | /** @var EsiRequest $esiRequest */ |
||
112 | $esiRequest = $esiRequests[$index]; |
||
113 | $needle = $esiRequest->getEsiTag(); |
||
114 | $pos = strpos($data, $needle); |
||
115 | if ($pos !== false) { |
||
116 | |||
117 | if ($this->cachePool instanceof CacheItemPoolInterface && !$esiRequest->isNoCache()) { |
||
118 | $cacheKey = $this->options['cache_prefix'].':'.base64_encode($esiRequest->getSrc()); |
||
119 | |||
120 | $this->cachePool->save( |
||
121 | $this->cachePool |
||
122 | ->getItem($cacheKey) |
||
123 | ->set($response->getBody()->getContents()) |
||
124 | ->expiresAfter($this->options['cache_ttl']) |
||
125 | ); |
||
126 | } |
||
127 | |||
128 | $data = substr_replace($data, $response->getBody()->getContents(), $pos, strlen($needle)); |
||
129 | } else { |
||
130 | $this->logger->error('This should not happen. Could not replace previously found esi tag.'); |
||
131 | } |
||
132 | }); |
||
133 | }, |
||
134 | 'rejected' => function (string &$data, array $esiRequests) { |
||
135 | return (function (\Exception $reason, int $index) use (&$data, $esiRequests) { |
||
136 | |||
137 | $this->logger->error( |
||
138 | 'Could not fetch ['.$esiRequests[$index]->getSrc().']. Reason: '.$reason->getMessage() |
||
139 | ); |
||
140 | |||
141 | if ($this->options['on_reject'] == static::ON_REJECT_EMPTY) { |
||
142 | $data = str_replace($esiRequests[$index]->getEsiTag(), '', $data); |
||
143 | } else { |
||
144 | throw $reason; |
||
145 | } |
||
146 | }); |
||
147 | } |
||
148 | ]; |
||
149 | } |
||
150 | |||
180 | } |