| Conditions | 9 |
| Paths | 7 |
| Total Lines | 30 |
| Code Lines | 15 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 8 |
| CRAP Score | 9.111 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 35 | public function check($value): bool |
||
| 36 | { |
||
| 37 | if ((! is_string($value) && ! is_numeric($value) && ! ($value instanceof DateTimeInterface))) { |
||
| 38 | 2 | return false; |
|
| 39 | } |
||
| 40 | |||
| 41 | if (! empty($format = $this->parameter('format'))) { |
||
| 42 | 4 | $this->message .= ' format'; |
|
| 43 | |||
| 44 | 4 | $date = DateTime::createFromFormat($format, $value); |
|
| 45 | |||
| 46 | 4 | return $date && $date->format($format) === $value; |
|
| 47 | // return date_create_from_format($format, $value) !== false; |
||
| 48 | } |
||
| 49 | |||
| 50 | if ($value instanceof DateTimeInterface) { |
||
| 51 | 2 | return true; |
|
| 52 | } |
||
| 53 | |||
| 54 | try { |
||
| 55 | if (strtotime($value) === false) { |
||
| 56 | 2 | return false; |
|
| 57 | } |
||
| 58 | } catch (Exception $e) { |
||
| 59 | return false; |
||
| 60 | } |
||
| 61 | |||
| 62 | 2 | $date = date_parse($value); |
|
| 63 | |||
| 64 | 2 | return checkdate($date['month'], $date['day'], $date['year']); |
|
| 65 | } |
||
| 67 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: