Factory   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 117
Duplicated Lines 20.51 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 64.81%

Importance

Changes 0
Metric Value
dl 24
loc 117
ccs 35
cts 54
cp 0.6481
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A create() 0 14 1
A getTranslator() 0 12 3
A getCalendar() 0 8 2
A guardThatTheseAreCalendars() 12 15 4
C guardThatTheseAreTranslators() 12 26 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace HansOtt\Holiday;
4
5
use InvalidArgumentException;
6
use HansOtt\Holiday\Calendar\Belgium;
7
use HansOtt\Holiday\Translator\Belgium\Dutch;
8
9
final class Factory
10
{
11
    private $calendars;
12
13
    private $translators;
14
15
    /**
16
     * @param Calendar[] $calendars
17
     * @param array[] $translators
18
     */
19 12
    public function __construct(array $calendars, array $translators)
20
    {
21 12
        $this->guardThatTheseAreCalendars($calendars);
22 12
        $this->guardThatTheseAreTranslators($translators);
23 12
        $this->calendars = $calendars;
24 12
        $this->translators = $translators;
25 12
    }
26
27 12
    public static function create()
28
    {
29
        $calendars = [
30 12
            'BEL' => new Belgium(),
31 12
        ];
32
33
        $translators = [
34
            'BEL' => [
35 12
                'nl-be' => new Dutch(),
36
            ]
37 12
        ];
38
39 12
        return new static($calendars, $translators);
40
    }
41
42
    /**
43
     * Get a translator for a country and locale.
44
     *
45
     * @param string $alpha3 https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
46
     * @param string $locale https://msdn.microsoft.com/en-us/library/windows/desktop/dd757512(v=vs.85).aspx
47
     *
48
     * @throws NoTranslatorFound
49
     *
50
     * @return Translator
51
     */
52 6
    public function getTranslator($alpha3, $locale)
53
    {
54 6
        if (isset($this->translators[$alpha3]) === false) {
55 2
            throw NoTranslatorFound::forCountry($alpha3);
56
        }
57
58 4
        if (isset($this->translators[$alpha3][$locale]) === false) {
59 2
            throw NoTranslatorFound::forLocale($locale);
60
        }
61
62 2
        return $this->translators[$alpha3][$locale];
63
    }
64
65
    /**
66
     * Get a calendar for a country.
67
     *
68
     * @param string $alpha3 https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
69
     *
70
     * @throws NoCalendarFound
71
     *
72
     * @return Calendar
73
     */
74 4
    public function getCalendar($alpha3)
75
    {
76 4
        if (isset($this->calendars[$alpha3]) === false) {
77 2
            throw NoCalendarFound::forCountry($alpha3);
78
        }
79
80 2
        return $this->calendars[$alpha3];
81
    }
82
83 12
    private function guardThatTheseAreCalendars(array $calendars)
84
    {
85 12 View Code Duplication
        foreach ($calendars as $index => $calendar) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86 12
            if (!$calendar instanceof Calendar) {
87
                throw new InvalidArgumentException(
88
                    sprintf(
89
                        'Expected an array of "%s" but instead got "%s" at "%s"',
90
                        Calendar::class,
91
                        is_object($calendar) ? get_class($calendar) : gettype($calendar),
92
                        $index
93
                    )
94
                );
95
            }
96 12
        }
97 12
    }
98
99 12
    private function guardThatTheseAreTranslators(array $translators)
100
    {
101 12
        foreach ($translators as $alpha3 => $locales) {
102 12
            if (is_array($locales) === false) {
103
                throw new InvalidArgumentException(
104
                    'Expected an array of locale => "%s", but instead got "%s" at "%s"',
105
                    Translator::class,
106
                    is_object($locales) ? get_class($locales) : gettype($locales),
107
                    $alpha3
108
                );
109
            }
110
111 12 View Code Duplication
            foreach ($locales as $locale => $translator) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112 12
                if (!$translator instanceof Translator) {
113
                    throw new InvalidArgumentException(
114
                        sprintf(
115
                            'Expected an array of "%s" but instead got "%s" at "%s"',
116
                            Calendar::class,
117
                            is_object($translator) ? get_class($translator) : gettype($translator),
118
                            $locale
119
                        )
120
                    );
121
                }
122 12
            }
123 12
        }
124 12
    }
125
}
126