Completed
Push — master ( c91fb3...77e88f )
by Ariel
05:48
created

Timetable   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 307
Duplicated Lines 1.3 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 27
c 2
b 0
f 1
lcom 1
cbo 1
dl 4
loc 307
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A from() 0 6 1
A future() 0 6 1
A startAt() 0 6 1
A finishAt() 0 6 1
A interval() 0 6 1
A services() 0 6 1
A init() 0 18 4
A inflateServices() 0 4 1
A inflateDates() 4 11 2
A inflateTimes() 0 15 2
A get() 0 8 2
A capacity() 0 8 2
A dimensions() 0 8 1
A format() 0 12 3
A array_set() 0 4 1
A array_get() 0 4 1
A array_substitute() 0 6 2

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\Booking;
4
5
use Illuminate\Support\Arr;
6
7
class Timetable
8
{
9
    /**
10
     * Timetable matrix.
11
     *
12
     * @var array
13
     */
14
    protected $timetable = null;
15
16
    /**
17
     * DateTime keyword for the begining of the timetable.
18
     *
19
     * @var string
20
     */
21
    protected $from = 'today';
22
23
    /**
24
     * Number of days to build the timetable forward.
25
     *
26
     * @var int
27
     */
28
    protected $future = 1;
29
30
    /**
31
     * Starting time for each day.
32
     *
33
     * @var string
34
     */
35
    protected $startAt = '09:00:00';
36
37
    /**
38
     * Finishing time for each day.
39
     *
40
     * @var string
41
     */
42
    protected $finishAt = '18:00:00';
43
44
    /**
45
     * Interval between slots in minutes.
46
     *
47
     * @var string
48
     */
49
    protected $interval = 30;
50
51
    /**
52
     * Services.
53
     *
54
     * @var array
55
     */
56
    protected $services = ['default'];
57
58
    /**
59
     * Dimensions format of the timetable matrix.
60
     *
61
     * @var string
62
     */
63
    protected $dimensions = ['date', 'service', 'time'];
64
65
    /**
66
     * Setter for beginning date.
67
     *
68
     * @param  string $relative
69
     *
70
     * @return $this
71
     */
72
    public function from($relative)
73
    {
74
        $this->from = $relative;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Setter for number of days forward.
81
     *
82
     * @param  int $days
83
     *
84
     * @return $this
85
     */
86
    public function future($days)
87
    {
88
        $this->future = $days;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Setter for startAt time.
95
     *
96
     * @param  string $time
97
     *
98
     * @return $this
99
     */
100
    public function startAt($time)
101
    {
102
        $this->startAt = $time;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Setter for finishAt time.
109
     *
110
     * @param  string $time
111
     *
112
     * @return $this
113
     */
114
    public function finishAt($time)
115
    {
116
        $this->finishAt = $time;
117
118
        return $this;
119
    }
120
121
    /**
122
     * Setter for interval.
123
     *
124
     * @param  int $minutes
0 ignored issues
show
Bug introduced by
There is no parameter named $minutes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
125
     *
126
     * @return $this
127
     */
128
    public function interval($interval = 30)
129
    {
130
        $this->interval = $interval;
0 ignored issues
show
Documentation Bug introduced by
The property $interval was declared of type string, but $interval is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
131
132
        return $this;
133
    }
134
135
    /**
136
     * Setter for services.
137
     *
138
     * @param  array $services
139
     *
140
     * @return $this
141
     */
142
    public function services($services)
143
    {
144
        $this->services = $services;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Initialize Timetable.
151
     *
152
     * @return $this
153
     */
154
    public function init()
155
    {
156
        $this->timetable = [];
157
158
        $dimensions['service'] = $this->inflateServices();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dimensions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dimensions = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
159
        $dimensions['date'] = $this->inflateDates();
160
        $dimensions['time'] = $this->inflateTimes();
161
162
        foreach ($dimensions['service'] as $service) {
163
            foreach ($dimensions['date'] as $date) {
164
                foreach ($dimensions['time'] as $time) {
165
                    $this->capacity($date, $time, $service, 0);
166
                }
167
            }
168
        }
169
170
        return $this;
171
    }
172
173
    public function inflateServices()
174
    {
175
        return $this->services;
176
    }
177
178
    public function inflateDates()
179
    {
180
        $starting = $this->from;
181
182 View Code Duplication
        for ($i = 0; $i < $this->future; $i++) {
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...
183
            $date = date('Y-m-d', strtotime("$starting +$i days"));
184
            $dates[$date] = $date;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dates was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dates = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
185
        }
186
187
        return $dates;
0 ignored issues
show
Bug introduced by
The variable $dates does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
188
    }
189
190
    public function inflateTimes()
191
    {
192
        $interval = $this->interval;
193
194
        $start = strtotime('today '.$this->startAt);
195
        $finish = strtotime('today '.$this->finishAt);
196
197
        $times = [];
198
        for ($i = $start; $i < $finish; $i += $interval * 60) {
199
            $time = date('H:i:s', $i);
200
            $times[$time] = $time;
201
        }
202
203
        return $times;
204
    }
205
206
    /**
207
     * Get the Timetable matrix.
208
     *
209
     * @return array
210
     */
211
    public function get()
212
    {
213
        if ($this->timetable === null) {
214
            $this->init();
215
        }
216
217
        return $this->timetable;
218
    }
219
220
    /**
221
     * Set the capacity for a slot.
222
     *
223
     * @param  string $date
224
     * @param  string $time
225
     * @param  string $service
226
     * @param  int $capacity
227
     *
228
     * @return int
229
     */
230
    public function capacity($date, $time, $service, $capacity = null)
231
    {
232
        $path = $this->dimensions(compact('date', 'service', 'time'));
233
234
        return $capacity === null
235
            ? $this->array_get($this->timetable, $path)
236
            : $this->array_set($this->timetable, $path, $capacity);
237
    }
238
239
    /**
240
     * Get a concrete Timetable path.
241
     *
242
     * @param  array $segments
243
     *
244
     * @return string
245
     */
246
    private function dimensions(array $segments)
247
    {
248
        $translatedDimensions = $this->dimensions;
249
250
        $this->array_substitute($translatedDimensions, $segments);
251
252
        return implode('.', $translatedDimensions);
253
    }
254
255
    /**
256
     * Setter for the dimensions format.
257
     *
258
     * @param  string $dimensions
259
     *
260
     * @return $this
261
     */
262
    public function format($dimensions)
263
    {
264
        if (is_array($dimensions)) {
265
            $this->dimensions = $dimensions;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dimensions of type array is incompatible with the declared type string of property $dimensions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
266
        }
267
268
        if (is_string($dimensions)) {
269
            $this->dimensions = explode('.', $dimensions);
0 ignored issues
show
Documentation Bug introduced by
It seems like explode('.', $dimensions) of type array is incompatible with the declared type string of property $dimensions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
270
        }
271
272
        return $this;
273
    }
274
275
    /////////////
276
    // Helpers //
277
    /////////////
278
279
    /**
280
     * Helper method for Arr::set
281
     *
282
     * @param  array &$array
283
     * @param  mixed $key
284
     * @param  mixed $value
285
     *
286
     * @return mixed
287
     */
288
    private function array_set(&$array, $key, $value)
289
    {
290
        return Arr::set($array, $key, $value);
291
    }
292
293
    /**
294
     * Helper method for Arr::get
295
     *
296
     * @param  array &$array
297
     * @param  mixed $key
298
     * @param  mixed $default
299
     *
300
     * @return mixed
301
     */
302
    private function array_get($array, $key, $default = null)
303
    {
304
        return Arr::get($array, $key, $default);
305
    }
306
307
    private function array_substitute(&$array1, $array2)
308
    {
309
        foreach ($array1 as $key => $value) {
310
            $array1[$key] = $array2[$value];
311
        }
312
    }
313
}
314