VacancyParser   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 182
Duplicated Lines 4.4 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 0
dl 8
loc 182
ccs 82
cts 82
cp 1
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A readVacancies() 0 6 1
A buildVacancies() 0 26 4
A parseStatements() 0 11 2
A services() 0 6 1
A splitServices() 0 4 1
A getServicesCapacity() 0 9 2
A getServiceCapacity() 0 9 2
A dates() 0 6 1
A splitDates() 0 4 1
A convertDaysToDate() 0 9 2
A dayToDate() 0 4 1
A hours() 0 6 1
A normalizeRanges() 0 14 2
A splitRanges() 0 4 1
B milTimeToStandard() 8 29 6

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 Timegridio\Concierge\Vacancy;
4
5
class VacancyParser
6
{
7
    const REGEX_PATTERN_VACANCY = '/(?P<services>.*)\n\ (?P<days>.*)\n\ \ (?<hours>.*)/im';
8
9
    /////////////////////
10
    // VACANCY PARSING //
11
    /////////////////////
12
13 6
    public function readVacancies($vacancyString)
14
    {
15 6
        preg_match_all(self::REGEX_PATTERN_VACANCY, $vacancyString, $matches, PREG_SET_ORDER);
16
17 6
        return $matches;
18
    }
19
20 6
    public function buildVacancies(array $vacancyParameters)
21
    {
22 6
        $services = $this->services($vacancyParameters['services']);
23 6
        $days = $this->dates($vacancyParameters['days']);
24 6
        $hourRanges = $this->hours($vacancyParameters['hours']);
25
26 6
        $builtVacancies = [];
27 6
        foreach ($services as $service) {
28 6
            foreach ($days as $day) {
29 6
                foreach ($hourRanges as $hourRange) {
30
                    $data = [
31 6
                        'service'  => $service['slug'],
32 6
                        'date'     => $day,
33 6
                        'startAt'  => $hourRange['startAt'],
34 6
                        'finishAt' => $hourRange['finishAt'],
35 6
                        'capacity' => $service['capacity'],
36
                    ];
37
                    // Avoid repeated sets
38 6
                    $key = md5(implode('.', $data));
39 6
                    $builtVacancies[$key] = $data;
40
                }
41
            }
42
        }
43
44 6
        return $builtVacancies;
45
    }
46
47 6
    public function parseStatements($stringStatements)
48
    {
49 6
        $vacancyStatements = $this->readVacancies($stringStatements);
50
51 6
        $builtVacancies = [];
52 6
        foreach ($vacancyStatements as $vacancyStatement) {
0 ignored issues
show
Bug introduced by
The expression $vacancyStatements of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
53 6
            $builtVacancies = array_merge($builtVacancies, $this->buildVacancies($vacancyStatement));
54
        }
55
56 6
        return $builtVacancies;
57
    }
58
59
    /////////////////////
60
    // SERVICE PARSING //
61
    /////////////////////
62
63 8
    public function services($services)
64
    {
65 8
        $services = $this->splitServices($services);
66
67 8
        return $this->getServicesCapacity($services);
68
    }
69
70 9
    public function splitServices($services)
71
    {
72 9
        return preg_split('/\ *\,\ */', $services);
73
    }
74
75 8
    public function getServicesCapacity(array $services)
76
    {
77 8
        $converted = [];
78 8
        foreach ($services as $service) {
79 8
            $converted[] = $this->getServiceCapacity($service);
80
        }
81
82 8
        return $converted;
83
    }
84
85 8
    public function getServiceCapacity($service)
86
    {
87 8
        $capacity = 1;
88 8
        if (strpos($service, ':') !== false) {
89 8
            list($service, $capacity) = explode(':', $service);
90
        }
91
92 8
        return ['slug' => $service, 'capacity' => trim($capacity)];
93
    }
94
95
    //////////////////
96
    // DATE PARSING //
97
    //////////////////
98
99 8
    public function dates($days)
100
    {
101 8
        $days = $this->splitDates($days);
102
103 8
        return $this->convertDaysToDate($days);
104
    }
105
106 9
    public function splitDates($days)
107
    {
108 9
        return preg_split('/\ *\,\ */', $days);
109
    }
110
111 8
    public function convertDaysToDate(array $days)
112
    {
113 8
        $converted = [];
114 8
        foreach ($days as $day) {
115 8
            $converted[] = $this->dayToDate($day);
116
        }
117
118 8
        return $converted;
119
    }
120
121 8
    public function dayToDate($day)
122
    {
123 8
        return date('Y-m-d', strtotime($day));
124
    }
125
126
    //////////////////
127
    // TIME PARSING //
128
    //////////////////
129
130 8
    public function hours($string)
131
    {
132 8
        $ranges = $this->splitRanges($string);
133
134 8
        return $this->normalizeRanges($ranges);
135
    }
136
137 8
    public function normalizeRanges($ranges)
138
    {
139 8
        $normalizedRanges = [];
140
141 8
        foreach ($ranges as $range) {
142 8
            list($startAt, $finishAt) = preg_split('/\ *\-\ */', $range);
143 8
            $normalizedRanges[] = [
144 8
                'startAt'  => $this->milTimeToStandard($startAt),
145 8
                'finishAt' => $this->milTimeToStandard($finishAt),
146
            ];
147
        }
148
149 8
        return $normalizedRanges;
150
    }
151
152 8
    public function splitRanges($string)
153
    {
154 8
        return preg_split('/,\ */', $string);
155
    }
156
157 11
    public function milTimeToStandard($militaryTime)
158
    {
159 11
        $militaryTime = trim($militaryTime);
160
161 11
        if ($militaryTime == '') {
162 1
            return '';
163
        }
164
165 10
        if (strpos($militaryTime, ':') !== false) {
166 2
            return $militaryTime;
167
        }
168
169 8
        $parts = [];
170
171 8
        if (strlen($militaryTime) <= 2) {
172 7
            $parts[] = intval($militaryTime);
173 7
            $parts[] = '00';
174
        }
175 8 View Code Duplication
        if (strlen($militaryTime) == 3) {
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...
176 3
            $parts[0] = substr($militaryTime, 0, 1);
177 3
            $parts[1] = substr($militaryTime, 1, 2);
178
        }
179 8 View Code Duplication
        if (strlen($militaryTime) == 4) {
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...
180 2
            $parts[0] = substr($militaryTime, 0, 2);
181 2
            $parts[1] = substr($militaryTime, 2, 2);
182
        }
183
184 8
        return implode(':', $parts);
185
    }
186
}
187