forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-11430.php
More file actions
98 lines (84 loc) · 1.72 KB
/
bug-11430.php
File metadata and controls
98 lines (84 loc) · 1.72 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php declare(strict_types = 1);
namespace Bug11430Methods;
use function PHPStan\Testing\assertType;
/**
* @template T
*
* @implements \IteratorAggregate<T>
*/
abstract class Option implements \IteratorAggregate
{
/**
* @template S
* @template U
*
* @param S $value The actual return value.
* @param U $noneValue The value which should be considered "None"; null by
* default.
*
* @return (S is U ? None : Option<S>)
*/
public static function fromValue($value, $noneValue = null)
{
if ($value === $noneValue) {
return None::create();
}
return new Some($value);
}
}
/**
* @extends Option<mixed>
*/
final class None extends Option
{
/** @var None|null */
private static $instance;
/**
* @return None
*/
public static function create(): self
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function getIterator(): \EmptyIterator
{
return new \EmptyIterator();
}
}
/**
* @template T
*
* @extends Option<T>
*/
final class Some extends Option
{
/** @var T */
private $value;
/**
* @param T $value
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* @return \ArrayIterator<int, T>
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator([$this->value]);
}
}
class Test
{
/** @var Option<string> */
public Option $name;
}
$test = new Test();
/** @var ?string $foo */
$foo = null;
$test->name = Option::fromValue($foo);
assertType('Bug11430Methods\Option<string>', $test->name);