forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTimeDynamicReturnTypes.php
43 lines (33 loc) · 1.71 KB
/
DateTimeDynamicReturnTypes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php declare(strict_types=1);
namespace DateTimeDynamicReturnTypes;
use DateTime;
use DateTimeImmutable;
use function date_create_from_format;
use function PHPStan\Testing\assertType;
class Foo
{
public function createDynamic(string $format, string $datetime): void {
assertType('DateTime|false', date_create_from_format($format, $datetime));
assertType('DateTimeImmutable|false', date_create_immutable_from_format($format, $datetime));
assertType('DateTime|false', DateTime::createFromFormat($format, $datetime));
assertType('DateTimeImmutable|false', DateTimeImmutable::createFromFormat($format, $datetime));
}
public function staticInvalidFormat(): void {
assertType('false', date_create_from_format('Foobar', '2022-02-20'));
assertType('false', date_create_immutable_from_format('Foobar', '2022-02-20'));
assertType('false', DateTime::createFromFormat('Foobar', '2022-02-20'));
assertType('false', DateTimeImmutable::createFromFormat('Foobar', '2022-02-20'));
}
public function staticInvalidDatetime(): void {
assertType('false', date_create_from_format('Y-m-d', '2022/02/20'));
assertType('false', date_create_immutable_from_format('Y-m-d', '2022/02/20'));
assertType('false', DateTime::createFromFormat('Y-m-d', '2022/02/20'));
assertType('false', DateTimeImmutable::createFromFormat('Y-m-d', '2022/02/20'));
}
public function staticValidStrings(): void {
assertType('DateTime', date_create_from_format('Y-m-d', '2020-10-12'));
assertType('DateTimeImmutable', date_create_immutable_from_format('Y-m-d', '2020-10-12'));
assertType('DateTime', DateTime::createFromFormat('Y-m-d', '2020-10-12'));
assertType('DateTimeImmutable', DateTimeImmutable::createFromFormat('Y-m-d', '2020-10-12'));
}
}