Passed
Pull Request — master (#7)
by Andrés
02:29
created

CalendarioDeColombiaCom::holidays()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 52
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5.0812

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 27
c 3
b 0
f 0
nc 5
nop 1
dl 0
loc 52
ccs 23
cts 27
cp 0.8519
crap 5.0812
rs 9.1768

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 CalendarioDeColombiaCom extends BaseProvider
13
{
14 1
    protected function baseUrl(): string
15
    {
16 1
        return 'https://calendariodecolombia.com';
17
    }
18
19 1
    public function countryTranslations(): array
20
    {
21
        return [
22 1
            'es' => 'Colombia',
23
        ];
24
    }
25
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
Bug introduced by
It seems like $titleContainer->getAttribute('title') can also be of type null; however, parameter $title of Andreshg112\HolidaysPhp\Holiday::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
                /** @scrutinizer ignore-type */ $titleContainer->getAttribute('title'), // title
Loading history...
73 1
                $this->getLanguage()
74
            );
75
        }
76
77 1
        return $holidays;
78
    }
79
}
80