Conditions | 5 |
Paths | 5 |
Total Lines | 55 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
25 | public function holidays(int $year = null): ?array |
||
26 | { |
||
27 | $year = $year ?? date('Y'); |
||
28 | |||
29 | $baseUrl = $this->baseUrl(); |
||
30 | |||
31 | $url = "{$baseUrl}/calendario-{$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 | $rows = $page->filter( |
||
42 | '#cuadro_festivos > div > .tabla_festivos1 > .formato_fechas, #cuadro_festivos > div > .tabla_festivos2 > .formato_fechas' |
||
43 | ); |
||
44 | |||
45 | if ($rows->count() === 0) { |
||
46 | throw HolidaysPhpException::unrecognizedStructure(); |
||
47 | } |
||
48 | |||
49 | $holidays = []; |
||
50 | |||
51 | $country = $this->country(); |
||
52 | |||
53 | /** @var \DOMElement */ |
||
54 | foreach ($rows as $row) { |
||
55 | $nodeList = $row->childNodes; |
||
56 | |||
57 | // It must contain this amount, else, it must be something different than a holiday |
||
58 | if ($nodeList->count() !== 7) { |
||
59 | continue; |
||
60 | } |
||
61 | |||
62 | /** @var \DOMElement */ |
||
63 | $timeElement = $nodeList->item(3); |
||
64 | |||
65 | // The date is already in a field time |
||
66 | $date = Date::parse($timeElement->getAttribute('datetime')); |
||
67 | |||
68 | /** @var \DOMElement */ |
||
69 | $aElement = $nodeList->item(2); |
||
70 | |||
71 | $holidays[] = new Holiday( |
||
72 | $country, |
||
73 | $date->setTime(0, 0), |
||
74 | trim($aElement->getAttribute('title')), // title |
||
75 | $this->getLanguage() |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | return $holidays; |
||
80 | } |
||
82 |