Timetable::array_get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Timegridio\Concierge\Timetable;
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 = [];
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 13
    public function from($relative)
73
    {
74 13
        $this->from = $relative;
75
76 13
        return $this;
77
    }
78
79
    /**
80
     * Setter for number of days forward.
81
     *
82
     * @param  int $days
83
     *
84
     * @return $this
85
     */
86 13
    public function future($days)
87
    {
88 13
        $this->future = $days;
89
90 13
        return $this;
91
    }
92
93
    /**
94
     * Setter for startAt time.
95
     *
96
     * @param  string $time
97
     *
98
     * @return $this
99
     */
100 2
    public function startAt($time)
101
    {
102 2
        $this->startAt = $time;
103
104 2
        return $this;
105
    }
106
107
    /**
108
     * Setter for finishAt time.
109
     *
110
     * @param  string $time
111
     *
112
     * @return $this
113
     */
114 2
    public function finishAt($time)
115
    {
116 2
        $this->finishAt = $time;
117
118 2
        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 6
    public function interval($interval = 30)
129
    {
130 6
        $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 6
        return $this;
133
    }
134
135
    /**
136
     * Setter for services.
137
     *
138
     * @param  array $services
139
     *
140
     * @return $this
141
     */
142 2
    public function services($services)
143
    {
144 2
        $this->services = $services;
145
146 2
        return $this;
147
    }
148
149
    /**
150
     * Initialize Timetable.
151
     *
152
     * @return $this
153
     */
154 9
    public function init()
155
    {
156 9
        $this->timetable = [];
157
158 9
        $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 9
        $dimensions['date'] = $this->inflateDates();
160 9
        $dimensions['time'] = $this->inflateTimes();
161
162 9
        foreach ($dimensions['service'] as $service) {
163 2
            foreach ($dimensions['date'] as $date) {
164 2
                foreach ($dimensions['time'] as $time) {
165 2
                    $this->capacity($date, $time, $service, 0);
166
                }
167
            }
168
        }
169
170 9
        return $this;
171
    }
172
173 9
    public function inflateServices()
174
    {
175 9
        return $this->services;
176
    }
177
178 10
    public function inflateDates()
179
    {
180 10
        $starting = $this->from;
181
182 10 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 10
            $date = date('Y-m-d', strtotime("$starting +$i days"));
184 10
            $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 10
        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 10
    public function inflateTimes()
191
    {
192 10
        $interval = $this->interval;
193
194 10
        $start = strtotime('today '.$this->startAt);
195 10
        $finish = strtotime('today '.$this->finishAt);
196
197 10
        $times = [];
198 10
        for ($i = $start; $i < $finish; $i += $interval * 60) {
199 10
            $time = date('H:i:s', $i);
200 10
            $times[$time] = $time;
201
        }
202
203 10
        return $times;
204
    }
205
206
    /**
207
     * Get the Timetable matrix.
208
     *
209
     * @return array
210
     */
211 12
    public function get()
212
    {
213 12
        if ($this->timetable === null) {
214 3
            $this->init();
215
        }
216
217 12
        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 12
    public function capacity($date, $time, $service, $capacity = null)
231
    {
232 12
        $path = $this->dimensions(compact('date', 'service', 'time'));
233
234 12
        return $capacity === null
235 1
            ? $this->array_get($this->timetable, $path)
236 12
            : $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 12
    private function dimensions(array $segments)
247
    {
248 12
        $translatedDimensions = $this->dimensions;
249
250 12
        $this->array_substitute($translatedDimensions, $segments);
251
252 12
        return implode('.', $translatedDimensions);
253
    }
254
255
    /**
256
     * Setter for the dimensions format.
257
     *
258
     * @param  string $dimensions
259
     *
260
     * @return $this
261
     */
262 8
    public function format($dimensions)
263
    {
264 8
        if (is_array($dimensions)) {
265 1
            $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 8
        if (is_string($dimensions)) {
269 7
            $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 8
        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 12
    private function array_set(&$array, $key, $value)
289
    {
290 12
        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 1
    private function array_get($array, $key, $default = null)
303
    {
304 1
        return Arr::get($array, $key, $default);
305
    }
306
307 12
    private function array_substitute(&$array1, $array2)
308
    {
309 12
        foreach ($array1 as $key => $value) {
310 12
            $array1[$key] = $array2[$value];
311
        }
312 12
    }
313
}
314