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 Respect\Validation\Exceptions\ComponentException; |
||
17 | use Respect\Validation\Helpers\CanValidateDateTime; |
||
18 | use function date; |
||
0 ignored issues
–
show
|
|||
19 | use function is_scalar; |
||
20 | use function preg_match; |
||
21 | use function sprintf; |
||
22 | use function strtotime; |
||
23 | |||
24 | /** |
||
25 | * Validates if input is a date. |
||
26 | * |
||
27 | * @author Bruno Luiz da Silva <[email protected]> |
||
28 | * @author Henrique Moody <[email protected]> |
||
29 | */ |
||
30 | final class Date extends AbstractRule |
||
31 | { |
||
32 | use CanValidateDateTime; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $format; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $sample; |
||
43 | |||
44 | /** |
||
45 | * Initializes the rule. |
||
46 | * |
||
47 | * @throws ComponentException |
||
48 | */ |
||
49 | 4 | public function __construct(string $format = 'Y-m-d') |
|
50 | { |
||
51 | 4 | if (!preg_match('/^[djSFmMnYy\W]+$/', $format)) { |
|
52 | 2 | throw new ComponentException(sprintf('"%s" is not a valid date format', $format)); |
|
53 | } |
||
54 | |||
55 | 2 | $this->format = $format; |
|
56 | 2 | $this->sample = date($format, strtotime('2005-12-30')); |
|
57 | 2 | } |
|
58 | |||
59 | /** |
||
60 | * {@inheritDoc} |
||
61 | */ |
||
62 | 24 | public function validate($input): bool |
|
63 | { |
||
64 | 24 | if (!is_scalar($input)) { |
|
65 | 4 | return false; |
|
66 | } |
||
67 | |||
68 | 20 | return $this->isDateTime($this->format, (string) $input); |
|
69 | } |
||
70 | } |
||
71 |
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: