Test Failed
Push — master ( 7cb433...d93f46 )
by Andrés
03:27
created

CalendarioHispanohablanteCom::holidays()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 55
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 27
c 1
b 1
f 0
nc 5
nop 1
dl 0
loc 55
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
11
class CalendarioHispanohablanteCom extends BaseProvider
12
{
13
    protected function baseUrl(): string
14
    {
15
        return 'https://calendariohispanohablante.com';
16
    }
17
18
    public function countryTranslations(): array
19
    {
20
        return [
21
            'es' => 'Colombia',
22
        ];
23
    }
24
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),
0 ignored issues
show
Bug introduced by
It seems like $date->setTime(0, 0) can also be of type false; however, parameter $date of Andreshg112\HolidaysPhp\Holiday::__construct() does only seem to accept Jenssegers\Date\Date, 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

73
                /** @scrutinizer ignore-type */ $date->setTime(0, 0),
Loading history...
74
                trim($pChildren->item(1)->textContent), // title
75
                $this->getLanguage()
76
            );
77
        }
78
79
        return $holidays;
80
    }
81
}
82