Conditions | 18 |
Paths | 1315 |
Total Lines | 74 |
Code Lines | 47 |
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 |
||
205 | $wdOuvrage = (new WikidataSearchHandler($bnfOuvrage, $this->wikidataAdapter, $this->page))->handle(); |
||
206 | $this->completeOuvrage($wdOuvrage); |
||
207 | } |
||
208 | |||
209 | if ((new GoogleRequestValidator($this->ouvrage, $bnfOuvrage))->validate()) { |
||
210 | $googleOuvrage = (new GoogleBooksHandler($isbn, $this->logger))->handle(); |
||
211 | $this->completeOuvrage($googleOuvrage); |
||
212 | } |
||
213 | |||
214 | if (!isset($bnfOuvrage) && !isset($googleOuvrage)) { |
||
215 | $openLibraryOuvrage = (new OpenLibraryHandler($isbn, $this->logger))->handle(); |
||
216 | $this->completeOuvrage($openLibraryOuvrage); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | protected function completeOuvrage(?OuvrageTemplate $onlineOuvrage): void |
||
221 | { |
||
222 | if (!$onlineOuvrage instanceof OuvrageTemplate) { |
||
223 | return; |
||
224 | } |
||
225 | $this->logger->info($onlineOuvrage->serialize(true)); |
||
226 | $optimizer = OptimizerFactory::fromTemplate($onlineOuvrage, $this->page, $this->logger); |
||
227 | $onlineOptimized = ($optimizer)->doTasks()->getOptiTemplate(); |
||
228 | |||
229 | $completer = new OuvrageComplete($this->ouvrage, $onlineOptimized, $this->logger); |
||
230 | $this->ouvrage = $completer->getResult(); |
||
231 | |||
232 | // todo move that optimizing in OuvrageComplete ? |
||
233 | $optimizer = OptimizerFactory::fromTemplate($this->ouvrage, $this->page, $this->logger); |
||
234 | $this->ouvrage = $optimizer->doTasks()->getOptiTemplate(); |
||
235 | |||
236 | $this->logger->info('Summary', $completer->getSummaryLog()); |
||
237 | |||
238 | if ($completer->major) { |
||
239 | $this->major = true; |
||
240 | } |
||
241 | $this->notCosmetic = ($completer->notCosmetic || $this->notCosmetic); |
||
242 | $this->summaryLog = array_merge($this->getSummaryLog(), $completer->getSummaryLog()); |
||
243 | unset($optimizer); |
||
244 | unset($completer); |
||
245 | } |
||
246 | |||
247 | protected function sendCompleted() |
||
248 | { |
||
249 | $this->pageOuvrage |
||
250 | ->setOpti($this->serializeFinalOpti()) |
||
251 | ->setOptidate(new DateTime()) |
||
252 | ->setModifs(mb_substr(implode(',', $this->getSummaryLog()), 0, 250)) |
||
253 | ->setNotcosmetic(($this->notCosmetic) ? 1 : 0) |
||
254 | ->setMajor(($this->major) ? 1 : 0) |
||
255 | ->setIsbn(substr($this->ouvrage->getParam('isbn'), 0, 19)) |
||
256 | ->setVersion(WikiBotConfig::VERSION ?? null); |
||
257 | |||
258 | $result = $this->queueAdapter->sendCompletedData($this->pageOuvrage); |
||
259 | $this->logger->debug($result ? 'OK DB' : 'erreur sendCompletedData()'); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Final serialization of the completed OuvrageTemplate. |
||
264 | */ |
||
265 | protected function serializeFinalOpti(): string |
||
266 | { |
||
267 | // // Améliore style compact : plus espacé |
||
268 | // if ('|' === $this->ouvrage->userSeparator) { |
||
269 | // $this->ouvrage->userSeparator = ' |'; |
||
270 | // } |
||
271 | $finalOpti = $this->ouvrage->serialize(true); |
||
272 | $finalOpti = Normalizer::normalize($finalOpti); |
||
273 | if (empty($finalOpti) || !is_string($finalOpti)) { |
||
274 | throw new Exception('normalized $finalOpti serialize in OuvrageComplete is not a string'); |
||
275 | } |
||
276 | |||
277 | return $finalOpti; |
||
278 | } |
||
279 | } |
||
280 |