Failed Conditions
Pull Request — master (#75)
by
unknown
01:29
created

DateTime::filter()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 7.7777
c 0
b 0
f 0
cc 8
eloc 10
nc 6
nop 3
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use DateTimeInterface;
6
use DateTimeZone;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, TraderInteractive\Filter\DateTimeZone.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use DateTime as DateTimeStandard;
8
9
/**
10
 * A collection of filters for filtering strings into \DateTime objects.
11
 */
12
class DateTime
13
{
14
    /**
15
     * Filters the given value into a \DateTime object.
16
     *
17
     * @param mixed        $value      The value to be filtered.
18
     * @param boolean      $allowNull  True to allow nulls through, and false (default) if nulls should not be allowed.
19
     * @param DateTimeZone $timezone   A \DateTimeZone object representing the timezone of $value.
20
     *                                 If $timezone is omitted, the current timezone will be used.
21
     *
22
     * @return DateTimeStandard|null
23
     *
24
     * @throws Exception if the value did not pass validation.
25
     */
26
    public static function filter($value, bool $allowNull = false, DateTimeZone $timezone = null)
27
    {
28
        if ($value === null && $allowNull) {
29
            return null;
30
        }
31
32
        if ($value instanceof \DateTime) {
33
            return $value;
34
        }
35
36
        if (is_int($value) || ctype_digit($value)) {
37
            $value = "@{$value}";
38
        }
39
40
        if (!is_string($value) || trim($value) == '') {
41
            throw new Exception('$value is not a non-empty string');
42
        }
43
44
        return new \DateTime($value, $timezone);
45
    }
46
47
    /**
48
     * Filters the give \DateTime object to a formatted string.
49
     *
50
     * @param DateTimeInterface $dateTime The date to be formatted.
51
     * @param string             $format   The format of the outputted date string.
52
     *
53
     * @return string
54
     *
55
     * @throws \InvalidArgumentException Thrown if $format is not a string
56
     */
57
    public static function format(DateTimeInterface $dateTime, string $format = 'c') : string
58
    {
59
        if (!is_string($format) || trim($format) === '') {
60
            throw new \InvalidArgumentException('$format is not a non-empty string');
61
        }
62
63
        return $dateTime->format($format);
64
    }
65
}
66