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

CalendarioHispanohablanteCom::holidays()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 61
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 5.0073

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 30
c 2
b 1
f 0
nc 5
nop 1
dl 0
loc 61
ccs 28
cts 30
cp 0.9333
crap 5.0073
rs 9.1288

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 CalendarioHispanohablanteCom extends BaseProvider
13
{
14 2
    protected function baseUrl(): string
15
    {
16 2
        return 'https://calendariohispanohablante.com';
17
    }
18
19 3
    public function countryTranslations(): array
20
    {
21
        return [
22 3
            'es' => 'Colombia',
23
        ];
24
    }
25
26 2
    public function holidays(int $year = null): array
27
    {
28 2
        $year = $year ?? date('Y');
29
30 2
        $baseUrl = $this->baseUrl();
31
32 2
        $url = "{$baseUrl}/{$year}/calendario-colombia-{$year}.html";
33
34
        try {
35 2
            $html = file_get_contents($url);
36 1
        } catch (\Throwable $th) {
37 1
            throw HolidaysPhpException::notFound();
38
        }
39
40 1
        $page = new HtmlPage($html);
41
42 1
        $rows = $page->filter('.group-three > p');
43
44 1
        if ($rows->count() === 0) {
45
            throw HolidaysPhpException::unrecognizedStructure();
46
        }
47
48 1
        $holidays = [];
49
50 1
        $country = $this->country();
51
52 1
        foreach ($rows as $row) {
53 1
            $crawler = new HtmlPageCrawler($row);
54
55 1
            $dateContainer = $crawler->filter('b')->first();
56
57
            // This element must exist
58 1
            if ($dateContainer->count() === 0) {
59
                continue;
60
            }
61
62
            // The text is like "Viernes 1 de Enero", so it has to be separated
63 1
            [, $spanishDate] = explode(' ', $dateContainer->text(), 2);
64
65
            // The ending : has to be removed
66 1
            $spanishDate = rtrim($spanishDate, ':');
67
68 1
            Date::setLocale($this->getLanguage());
69
70
            // The date looks this way "1 de Enero 2021"
71 1
            $date = Date::createFromFormat('j \d\e F Y', "{$spanishDate} {$year}");
72
73
            // Remove it so the text inside the $crawler is only the title of the holiday
74 1
            $dateContainer->remove();
75
76 1
            $titleContainer = $crawler->first();
77
78 1
            $holidays[] = new Holiday(
79 1
                $country,
80 1
                $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

80
                /** @scrutinizer ignore-type */ $date->setTime(0, 0),
Loading history...
81 1
                $titleContainer->text(),
82 1
                $this->getLanguage()
83
            );
84
        }
85
86 1
        return $holidays;
87
    }
88
}
89