Conditions | 5 |
Paths | 5 |
Total Lines | 55 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
25 | public function holidays(int $year = null): ?array |
||
26 | { |
||
27 | $year = $year ?? date('Y'); |
||
28 | |||
29 | $baseUrl = $this->baseUrl(); |
||
30 | |||
31 | $url = "{$baseUrl}/{$year}/calendario-colombia-{$year}.html"; |
||
32 | |||
33 | try { |
||
34 | $html = file_get_contents($url); |
||
35 | } catch (\Throwable $th) { |
||
36 | throw HolidaysPhpException::notFound(); |
||
37 | } |
||
38 | |||
39 | $page = new HtmlPage($html); |
||
40 | |||
41 | $pList = $page->filter('.group-three > p'); |
||
42 | |||
43 | if ($pList->count() === 0) { |
||
44 | throw HolidaysPhpException::unrecognizedStructure(); |
||
45 | } |
||
46 | |||
47 | $holidays = []; |
||
48 | |||
49 | $country = $this->country(); |
||
50 | |||
51 | /** @var \DOMElement */ |
||
52 | foreach ($pList as $pItem) { |
||
53 | $pChildren = $pItem->childNodes; |
||
54 | |||
55 | // It must contain 2 nodes, else, it must be something different than a date |
||
56 | if ($pChildren->count() !== 2) { |
||
57 | continue; |
||
58 | } |
||
59 | |||
60 | // The text is like "Viernes 1 de Enero", so it has to be separated |
||
61 | [$day, $spanishDate] = explode(' ', trim($pChildren->item(0)->textContent), 2); |
||
62 | |||
63 | // The ending : has to be removed |
||
64 | $spanishDate = rtrim($spanishDate, ':'); |
||
65 | |||
66 | Date::setLocale($this->getLanguage()); |
||
67 | |||
68 | // The date looks this way "1 de Enero 2021" |
||
69 | $date = Date::createFromFormat('j \d\e F Y', "{$spanishDate} {$year}"); |
||
70 | |||
71 | $holidays[] = new Holiday( |
||
72 | $country, |
||
73 | $date->setTime(0, 0), |
||
|
|||
74 | trim($pChildren->item(1)->textContent), // title |
||
75 | $this->getLanguage() |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | return $holidays; |
||
80 | } |
||
82 |