Conditions | 7 |
Paths | 9 |
Total Lines | 52 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
51 | private function process(): ?ArticleOrLienBriseInterface |
||
52 | { |
||
53 | $mapper = PublisherMapperFactory::fromURL($this->url); |
||
54 | if (!$mapper) { |
||
55 | return null; |
||
56 | } |
||
57 | sleep(10); |
||
58 | $arrayLD = []; |
||
59 | try { |
||
60 | $html = $this->publisherAction->getHTMLSource(); |
||
61 | $htmlData = $this->publisherAction->extractWebData($html); |
||
62 | } catch (Throwable $e) { |
||
63 | if (strpos($e->getMessage(), '404') !== false) { |
||
64 | dump('****** lien brisé !!!!'); |
||
65 | $lienBrise = WikiTemplateFactory::create('lien brisé'); |
||
66 | $lienBrise->hydrate(['url' => $this->url, 'titre' => 'Article de presse', 'brisé le' => date('d-m-Y')]); |
||
67 | |||
68 | return $lienBrise; // ok |
||
69 | } |
||
70 | echo "*** Erreur ".$e->getMessage()."\n"; |
||
71 | |||
72 | return null; |
||
73 | } |
||
74 | |||
75 | if (empty($htmlData)) { |
||
76 | echo "*** Pas de data Json-LD ou meta\n"; |
||
77 | |||
78 | return null; |
||
79 | } |
||
80 | $htmlData['url'] = $this->url; |
||
81 | |||
82 | try { |
||
83 | $articleData = $mapper->process($htmlData); |
||
84 | } catch (Throwable $e) { |
||
85 | echo sprintf( |
||
86 | "SKIP : %s %s:%s \n", |
||
87 | $e->getMessage(), |
||
88 | $e->getFile(), |
||
89 | $e->getLine() |
||
90 | ); |
||
91 | |||
92 | return null; |
||
93 | } |
||
94 | |||
95 | if (!empty($articleData)) { |
||
96 | $article = WikiTemplateFactory::create('article'); |
||
97 | $article->hydrate($articleData); |
||
98 | |||
99 | return $article; // ok |
||
100 | } |
||
101 | |||
102 | return null; |
||
103 | } |
||
111 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.