-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractOptional.php
More file actions
53 lines (47 loc) · 1.42 KB
/
Copy pathAbstractOptional.php
File metadata and controls
53 lines (47 loc) · 1.42 KB
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
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
namespace PetrKnap\Optional;
/**
* Provides internal logic for abstract {@see Optional}
*
* @template-covariant T of mixed type of non-null value
*
* @phpstan-require-extends Optional
*/
trait AbstractOptional
{
/** @use GenericOptional<T> */
use GenericOptional;
/**
* @template U of T
*
* @param U|null $value
*
* @return self<U>
*/
public static function ofNullable(mixed $value): self // @phpstan-ignore generics.notSubtype
{
if (static::class === self::class) {
if ($value !== null) {
try {
/** @var self<U> */ // @phpstan-ignore generics.notSubtype
return TypedOptional::of($value, static::class);
} catch (Exception\CouldNotFindTypedOptionalForValue) {
}
}
/** @var self<U> */
return self::createInstance($value); // @phpstan-ignore generics.notSubtype, argument.type, argument.templateType
}
/** @var self<U> */
return parent::ofNullable($value); // @phpstan-ignore generics.notSubtype, argument.type, argument.templateType
}
/**
* @template U of T
*
* @param U|null $value
*
* @return self<U>
*/
abstract protected static function createInstance(mixed $value): self;
abstract protected static function isInstanceOfStatic(object $obj): bool;
}