DateTime::createFromMultipleFormats()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 6
nop 2
dl 0
loc 18
ccs 0
cts 11
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace IproSync\Ipro;
4
5
use Carbon\Carbon;
6
use Carbon\Exceptions\InvalidFormatException;
7
8
class DateTime
9
{
10
    public static function createFromMultipleFormats(array $formats, string $dateString): Carbon
11
    {
12
        $dateObj   = null;
13
        $lastError = null;
14
        foreach ($formats as $format) {
15
            try {
16
                if ($dateObj = Carbon::createFromFormat($format, $dateString)) {
17
                    break;
18
                }
19
            } catch (InvalidFormatException $e) {
20
                $lastError = $e;
21
            }
22
        }
23
        if (!$dateObj && $lastError) {
24
            throw $lastError;
25
        }
26
27
        return $dateObj;
28
    }
29
}
30