Completed
Pull Request — master (#1)
by Artem
01:03
created

DateTimeHelper::convertDateTimeToImmutable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of the DateTime library
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\DateTime;
14
15
use Fresh\DateTime\Exception\InvalidArgumentException;
16
17
/**
18
 * DateTimeHelper.
19
 *
20
 * @author Artem Henvald <[email protected]>
21
 */
22
class DateTimeHelper
23
{
24
    /**
25
     * @param \DateTimeInterface $date
26
     *
27
     * @throws InvalidArgumentException
28
     *
29
     * @return \DateTimeImmutable
30
     */
31
    public static function convertDateTimeToImmutable(\DateTimeInterface $date): \DateTimeImmutable
32
    {
33
        if (!$date instanceof \DateTimeImmutable) {
34
            if ($date instanceof \DateTime) {
35
                return \DateTimeImmutable::createFromMutable($date);
36
            }
37
38
            throw new InvalidArgumentException(\sprintf('Date object is not instance of %s', \DateTime::class));
39
        }
40
41
        return $date;
42
    }
43
}
44