| Conditions | 5 |
| Paths | 5 |
| Total Lines | 52 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 23 |
| CRAP Score | 5.0812 |
| Changes | 3 | ||
| 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 |
||
| 26 | 1 | public function holidays(int $year = null): array |
|
| 27 | { |
||
| 28 | 1 | $year = $year ?? date('Y'); |
|
| 29 | |||
| 30 | 1 | $baseUrl = $this->baseUrl(); |
|
| 31 | |||
| 32 | 1 | $url = "{$baseUrl}/calendario-{$year}.html"; |
|
| 33 | |||
| 34 | try { |
||
| 35 | 1 | $html = file_get_contents($url); |
|
| 36 | } catch (\Throwable $th) { |
||
| 37 | throw HolidaysPhpException::notFound(); |
||
| 38 | } |
||
| 39 | |||
| 40 | 1 | $page = new HtmlPage($html); |
|
| 41 | |||
| 42 | 1 | $rows = $page->filter( |
|
| 43 | 1 | '#cuadro_festivos > div > .tabla_festivos1 > .formato_fechas, #cuadro_festivos > div > .tabla_festivos2 > .formato_fechas' |
|
| 44 | ); |
||
| 45 | |||
| 46 | 1 | if ($rows->count() === 0) { |
|
| 47 | throw HolidaysPhpException::unrecognizedStructure(); |
||
| 48 | } |
||
| 49 | |||
| 50 | 1 | $holidays = []; |
|
| 51 | |||
| 52 | 1 | $country = $this->country(); |
|
| 53 | |||
| 54 | 1 | foreach ($rows as $row) { |
|
| 55 | 1 | $crawler = new HtmlPageCrawler($row); |
|
| 56 | |||
| 57 | 1 | $dateContainer = $crawler->filter('time')->first(); |
|
| 58 | |||
| 59 | // This element must exist |
||
| 60 | 1 | if ($dateContainer->count() === 0) { |
|
| 61 | continue; |
||
| 62 | } |
||
| 63 | |||
| 64 | // The date is already in a field time |
||
| 65 | 1 | $date = Date::parse($dateContainer->getAttribute('datetime')); |
|
| 66 | |||
| 67 | 1 | $titleContainer = $crawler->filter('a')->first(); |
|
| 68 | |||
| 69 | 1 | $holidays[] = new Holiday( |
|
| 70 | 1 | $country, |
|
| 71 | 1 | $date->setTime(0, 0), |
|
| 72 | 1 | $titleContainer->getAttribute('title'), // title |
|
|
1 ignored issue
–
show
|
|||
| 73 | 1 | $this->getLanguage() |
|
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | 1 | return $holidays; |
|
| 78 | } |
||
| 80 |