Conditions | 6 |
Paths | 6 |
Total Lines | 24 |
Code Lines | 12 |
Lines | 12 |
Ratio | 50 % |
Tests | 11 |
CRAP Score | 6.0208 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
37 | 6 | public static function ensureValidValue($value) |
|
38 | { |
||
39 | 6 | if ($value instanceof DateTimeImmutable) { |
|
40 | 4 | return $value; |
|
41 | } |
||
42 | |||
43 | 2 | View Code Duplication | if (preg_match('/^(\d{4})-(\d{2})$/', $value, $ms)) { |
|
|||
44 | 1 | return new DateTimeImmutable("$ms[1]-$ms[2]-01"); |
|
45 | } |
||
46 | |||
47 | 2 | View Code Duplication | if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value, $ms)) { |
48 | 2 | return new DateTimeImmutable("$ms[1]-$ms[2]-$ms[3]"); |
|
49 | } |
||
50 | |||
51 | 1 | View Code Duplication | if (preg_match('/^(\d{2})\.(\d{4})$/', $value, $ms)) { |
52 | 1 | return new DateTimeImmutable("$ms[2]-$ms[1]-01"); |
|
53 | } |
||
54 | |||
55 | 1 | View Code Duplication | if (preg_match('/^(\d{1,2})\.(\d{1,2})\.(\d{4})$/', $value, $ms)) { |
56 | 1 | return new DateTimeImmutable("$ms[3]-$ms[2]-$ms[1]"); |
|
57 | } |
||
58 | |||
59 | throw new \Exception('invalid date given'); |
||
60 | } |
||
61 | } |
||
62 |
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.