Conditions | 7 |
Paths | 1 |
Total Lines | 69 |
Code Lines | 45 |
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 |
||
101 | private function defaultOptions(): array |
||
102 | { |
||
103 | return [ |
||
104 | 'concurrency' => 5, |
||
105 | 'timeout' => 2.0, |
||
106 | 'on_reject' => static::ON_REJECT_EXCEPTION, |
||
107 | 'on_timeout' => static::ON_TIMEOUT_EXCEPTION, |
||
108 | 'base_uri' => '', |
||
109 | 'cache_prefix' => 'esi:include', |
||
110 | 'cache_ttl' => 3600, |
||
111 | 'request_options' => [], |
||
112 | 'recurecny_lever' => 1, |
||
113 | 'cache_poo;' => null, |
||
114 | 'logger' => new VoidLogger(), |
||
115 | 'fulfilled' => function (string &$data, array $esiRequests) |
||
116 | { |
||
117 | return (function (Response $response, int $index) use (&$data, $esiRequests) |
||
118 | { |
||
119 | /** @var EsiRequest $esiRequest */ |
||
120 | $esiRequest = $esiRequests[$index]; |
||
121 | $needle = $esiRequest->getEsiTag(); |
||
122 | $pos = strpos($data, $needle); |
||
123 | if ($pos !== false) { |
||
124 | |||
125 | if ($this->cachePool instanceof CacheItemPoolInterface && !$esiRequest->isNoCache()) { |
||
126 | $cacheKey = $this->options['cache_prefix'].':'.base64_encode($esiRequest->getSrc()); |
||
127 | |||
128 | $this->cachePool->save( |
||
129 | $this->cachePool |
||
130 | ->getItem($cacheKey) |
||
131 | ->set($response->getBody()->getContents()) |
||
132 | ->expiresAfter($this->options['cache_ttl']) |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | $data = substr_replace($data, $response->getBody()->getContents(), $pos, strlen($needle)); |
||
137 | } else { |
||
138 | $this->logger->error('This should not happen. Could not replace previously found esi tag.'); |
||
139 | } |
||
140 | }); |
||
141 | }, |
||
142 | 'rejected' => function (string &$data, array $esiRequests) { |
||
143 | return (function (\Exception $reason, int $index) use (&$data, $esiRequests) { |
||
144 | /** @var EsiRequest $esiRequest */ |
||
145 | $esiRequest = $esiRequests[$index]; |
||
146 | |||
147 | $this->logger->error( |
||
148 | 'Could not fetch ['.$esiRequest->getSrc().']. Reason: '.$reason->getMessage() |
||
149 | ); |
||
150 | if ($reason instanceof ConnectException && ( |
||
151 | $this->options['on_timeout'] == static::ON_TIMEOUT_H_INCLUDE |
||
152 | )) { |
||
153 | $data = str_replace( |
||
154 | $esiRequest->getEsiTag(), |
||
155 | '<hx:include src="'.$this->options['base_uri'].$esiRequest->getSrc().'"></hx:include>', |
||
156 | $data |
||
157 | ); |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | if ($this->options['on_reject'] == static::ON_REJECT_EMPTY) { |
||
162 | $data = str_replace($esiRequest->getEsiTag(), '', $data); |
||
163 | } else { |
||
164 | throw $reason; |
||
165 | } |
||
166 | }); |
||
167 | } |
||
168 | ]; |
||
169 | } |
||
170 | |||
200 | } |