Conditions | 4 |
Paths | 6 |
Total Lines | 20 |
Code Lines | 11 |
Lines | 4 |
Ratio | 20 % |
Changes | 0 |
1 | <?php |
||
30 | public function validateTimeDifference($dates, int $maxDiff) |
||
31 | { |
||
32 | View Code Duplication | if (! is_array($dates)) { |
|
|
|||
33 | $dates = str_replace('\'', '', $dates); // Remove escaping quotes |
||
34 | $dates = explode(',', $dates); |
||
35 | } |
||
36 | |||
37 | if (! is_array) { |
||
38 | throw new InvalidArgumentException('Somehow validateTimeDifference couldn\'t make a date array.'); |
||
39 | } |
||
40 | |||
41 | $date1 = \DateTime::createFromFormat('Y-m-d', $dates[0]); |
||
42 | $date2 = \DateTime::createFromFormat('Y-m-d', $dates[1]); |
||
43 | |||
44 | $diff = $date2->diff($date1, true); |
||
45 | |||
46 | if ($diff->days > $maxDiff) { |
||
47 | throw new InvalidArgumentException("Number of days attempting to access is more than maximum allowed ($maxDiff)"); |
||
48 | } |
||
49 | } |
||
50 | } |
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.