PublicHolidaysCo::holidays()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 54
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 13
Bugs 2 Features 0
Metric Value
cc 7
eloc 29
c 13
b 2
f 0
nc 12
nop 1
dl 0
loc 54
ccs 0
cts 29
cp 0
crap 56
rs 8.5226

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Andreshg112\HolidaysPhp\Providers\Colombia;
4
5
use Andreshg112\HolidaysPhp\Holiday;
6
use Andreshg112\HolidaysPhp\HolidaysPhpException;
7
use Andreshg112\HolidaysPhp\Providers\BaseProvider;
8
use Jenssegers\Date\Date;
9
use Wa72\HtmlPageDom\HtmlPage;
10
use Wa72\HtmlPageDom\HtmlPageCrawler;
11
12
class PublicHolidaysCo extends BaseProvider
13
{
14
    protected function baseUrl(): string
15
    {
16
        return 'https://publicholidays.co';
17
    }
18
19
    public function countryTranslations(): array
20
    {
21
        return [
22
            'en' => 'Colombia',
23
            'es' => 'Colombia',
24
        ];
25
    }
26
27
    public function holidays(int $year = null): array
28
    {
29
        $year = $year ?? date('Y');
30
31
        $baseUrl = $this->baseUrl();
32
33
        $url = $this->getLanguage() === 'en' ? "{$baseUrl}/{$year}-dates" : "{$baseUrl}/es/{$year}-dates";
34
35
        try {
36
            $html = file_get_contents($url);
37
        } catch (\Throwable $th) {
38
            throw HolidaysPhpException::notFound();
39
        }
40
41
        $page = new HtmlPage($html);
42
43
        $rows = $page->filter('.publicholidays > tbody > tr');
44
45
        if ($rows->count() === 0) {
46
            throw HolidaysPhpException::unrecognizedStructure();
47
        }
48
49
        $holidays = [];
50
51
        $country = $this->country();
52
53
        foreach ($rows as $row) {
54
            $crawler = new HtmlPageCrawler($row);
55
56
            if ($crawler->children()->count() !== 3) {
57
                continue;
58
            }
59
60
            $spanishDate = trim($crawler->children()->getNode(0)->textContent);
61
62
            Date::setLocale($this->getLanguage());
63
64
            $date = Date::createFromFormat('j F Y', "{$spanishDate} {$year}");
65
66
            if (!$date) {
67
                throw HolidaysPhpException::unrecognizedDate();
68
            }
69
70
            $holiday = new Holiday(
71
                $country,
72
                $date,
73
                $crawler->children()->getNode(2)->textContent, // title,
74
                $this->getLanguage()
75
            );
76
77
            $holidays[] = $holiday;
78
        }
79
80
        return $holidays;
81
    }
82
}
83