We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of Respect/Validation. |
||
5 | * |
||
6 | * (c) Alexandre Gomes Gaigalas <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE file |
||
9 | * that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Respect\Validation\Rules; |
||
15 | |||
16 | use DateTimeInterface; |
||
17 | use Respect\Validation\Helpers\CanValidateDateTime; |
||
18 | use function date; |
||
0 ignored issues
–
show
|
|||
19 | use function is_scalar; |
||
20 | use function strtotime; |
||
21 | |||
22 | /** |
||
23 | * @author Alexandre Gomes Gaigalas <[email protected]> |
||
24 | * @author Emmerson Siqueira <[email protected]> |
||
25 | * @author Henrique Moody <[email protected]> |
||
26 | */ |
||
27 | final class DateTime extends AbstractRule |
||
28 | { |
||
29 | use CanValidateDateTime; |
||
30 | |||
31 | /** |
||
32 | * @var string|null |
||
33 | */ |
||
34 | private $format; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $sample; |
||
40 | |||
41 | /** |
||
42 | * Initializes the rule. |
||
43 | */ |
||
44 | 15 | public function __construct(?string $format = null) |
|
45 | { |
||
46 | 15 | $this->format = $format; |
|
47 | 15 | $this->sample = date($format ?: 'c', strtotime('2005-12-30 01:02:03')); |
|
48 | 15 | } |
|
49 | |||
50 | /** |
||
51 | * {@inheritDoc} |
||
52 | */ |
||
53 | 43 | public function validate($input): bool |
|
54 | { |
||
55 | 43 | if ($input instanceof DateTimeInterface) { |
|
56 | 4 | return $this->format === null; |
|
57 | } |
||
58 | |||
59 | 39 | if (!is_scalar($input)) { |
|
60 | 2 | return false; |
|
61 | } |
||
62 | |||
63 | 37 | if ($this->format === null) { |
|
64 | 18 | return strtotime((string) $input) !== false; |
|
65 | } |
||
66 | |||
67 | 20 | return $this->isDateTime($this->format, (string) $input); |
|
68 | } |
||
69 | } |
||
70 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: