| Conditions | 4 |
| Paths | 4 |
| Total Lines | 23 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 1 |
| CRAP Score | 6 |
| Changes | 0 | ||
| 1 | <?php declare(strict_types=1); |
||
| 24 | 6 | function datetimeinterface_to_datetimeimmutable(\DateTimeInterface $dateTime): \DateTimeImmutable |
|
| 25 | { |
||
| 26 | static $utcTimeZone; |
||
| 27 | |||
| 28 | if ($dateTime instanceof \DateTimeImmutable) { |
||
| 29 | return $dateTime; |
||
| 30 | } |
||
| 31 | |||
| 32 | if ($dateTime instanceof \DateTime) { |
||
| 33 | return \DateTimeImmutable::createFromMutable($dateTime); |
||
| 34 | } |
||
| 35 | |||
| 36 | $result = \DateTimeImmutable::createFromFormat( |
||
| 37 | 'u U', |
||
| 38 | $dateTime->format('u U'), |
||
| 39 | $utcTimeZone ?? ($utcTimeZone = new \DateTimeZone('UTC')) |
||
| 40 | ); |
||
| 41 | |||
| 42 | if ($result === false) { |
||
| 43 | throw new InvalidValueException('Could not create DateTimeImmutable instance from DateTimeInterface instance'); |
||
| 44 | } |
||
| 45 | |||
| 46 | return $result; |
||
| 47 | } |
||
| 48 |