-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpdoc_check.php
More file actions
199 lines (171 loc) · 10.2 KB
/
Copy pathphpdoc_check.php
File metadata and controls
199 lines (171 loc) · 10.2 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* This file is checked by PhpStan as production code and confirms correctness of PhpDocs
*/
declare(strict_types=1);
use PetrKnap\Optional\Optional;
use PetrKnap\Optional\Some\DataObject;
use PetrKnap\Optional\Some\OptionalArray;
use PetrKnap\Optional\Some\OptionalInt;
use PetrKnap\Optional\Some\OptionalObject;
use PetrKnap\Optional\Some\OptionalString;
$check = (new class {
/**
* @param Optional<object> $object
*/
public function covariantInputOption(Optional $object): void {}
/**
* @param Optional<string>|null $string
* @param Optional<object>|null $object
* @param Optional<array<object|string>>|null $array
*/
public function genericInputOptions(Optional $string = null, Optional $object = null, Optional $array = null): void {}
/**
* @param OptionalObject<object>|null $object
* @param OptionalArray<array<object|string>>|null $array
*/
public function nonGenericInputOptions(OptionalString $string = null, OptionalObject $object = null, OptionalArray $array = null): void {}
public function nonGenericInputs(string $string = '', object $object = new stdClass()): void {}
});
// ---------------------------------------------------------------------------------------------------------------------
// Call all factories
Optional::of('');
try {
Optional::of(null);
null; // @phpstan-ignore deadCode.unreachable
} catch(Throwable) {}
Optional::ofFalsable('');
try {
Optional::ofFalsable(null);
null; // @phpstan-ignore deadCode.unreachable
} catch(Throwable) {}
Optional::ofFalsable(false);
Optional::ofNullable('');
Optional::ofNullable(null);
Optional::ofNullable(false);
Optional::ofSingle(['']);
try {
Optional::ofSingle([null]);
null; // @phpstan-ignore deadCode.unreachable
} catch(Throwable) {}
Optional::ofSingle([]);
// Call main typed factories
OptionalArray::of([]);
OptionalArray::of(null); // @phpstan-ignore argument.type, argument.templateType
OptionalArray::of(false); // @phpstan-ignore argument.type, argument.templateType
OptionalArray::ofNullable([]);
OptionalArray::ofNullable(null); // @phpstan-ignore argument.templateType
OptionalArray::ofNullable(false); // @phpstan-ignore argument.type, argument.templateType
OptionalString::of('');
OptionalString::of(null); // @phpstan-ignore argument.type
OptionalString::of(false); // @phpstan-ignore argument.type
OptionalString::ofNullable('');
OptionalString::ofNullable(null);
OptionalString::ofNullable(false); // @phpstan-ignore argument.type
// Check return types of factory `of`
$stringOption = OptionalString::of('');
$objectOption = OptionalObject::of(new stdClass());
$arrayOption = OptionalArray::of(['', new stdClass()]);
$check->genericInputOptions($stringOption, $objectOption, $arrayOption);
$check->nonGenericInputOptions($stringOption, $objectOption, $arrayOption);
// ---------------------------------------------------------------------------------------------------------------------
// Create generic option
$stringOption = Optional::of('');
// Call all methods with generic arguments
$stringOption->filter(static fn (string $value): bool => true);
$stringOption->filter(static fn (int $value): bool => true); // @phpstan-ignore argument.type
$stringOption->flatMap(static fn (string $value): Optional => $stringOption);
$stringOption->flatMap(static fn (int $value): Optional => $stringOption); // @phpstan-ignore argument.type
$stringOption->ifPresent(static fn (string $value): string => $value);
$stringOption->ifPresent(static fn (int $value): int => $value); // @phpstan-ignore argument.type
$stringOption->map(static fn (string $value): string => $value);
$stringOption->map(static fn (int $value): int => $value); // @phpstan-ignore argument.type
$stringOptionOrElse = $stringOption->orElse('');
$stringOptionOrElseNull = $stringOption->orElse(null);
$stringOption->orElse(0); // @phpstan-ignore argument.type
$stringOptionOrElseGet = $stringOption->orElseGet(static fn (): string => '');
$stringOption->orElseGet(static fn (): int => 0); // @phpstan-ignore argument.type
// Use generic option as input for functions
$check->genericInputOptions(string: $stringOption);
$check->nonGenericInputOptions(string: $stringOption); // @phpstan-ignore argument.type
$check->nonGenericInputs(string: $stringOption->get());
$check->nonGenericInputs(string: $stringOption->orElseThrow());
$check->nonGenericInputs(string: $stringOptionOrElse);
$check->nonGenericInputs(string: $stringOptionOrElseNull); // @phpstan-ignore argument.type
$check->nonGenericInputs(string: $stringOptionOrElseNull ?? '');
$check->nonGenericInputs(string: $stringOptionOrElseGet);
// Re-map generic option & call filter on it to check new generic
$objectOptionMapped = $stringOption->map(static fn (string $value): object => new DataObject($value));
$objectOptionMappedFiltered = $objectOptionMapped->filter(static fn (object $value): bool => true);
$objectOptionMapped->filter(static fn (string $value): bool => true); // @phpstan-ignore argument.type
$objectOptionFlatMapped = $stringOption->flatMap(static fn (string $value): Optional => Optional::of(new DataObject($value)));
$objectOptionFlatMappedFiltered = $objectOptionFlatMapped->filter(static fn (object $value): bool => true);
$objectOptionFlatMapped->filter(static fn (string $value): bool => true); // @phpstan-ignore argument.type
// Use re-mapped filtered options as input for functions
$check->genericInputOptions(object: $objectOptionMappedFiltered);
$check->nonGenericInputOptions(object: $objectOptionMappedFiltered); // @phpstan-ignore argument.type
$check->nonGenericInputs(object: $objectOptionMappedFiltered->get());
$check->genericInputOptions(object: $objectOptionFlatMappedFiltered);
$check->nonGenericInputOptions(object: $objectOptionFlatMappedFiltered); // @phpstan-ignore argument.type
$check->nonGenericInputs(object: $objectOptionFlatMappedFiltered->get());
// ---------------------------------------------------------------------------------------------------------------------
// Create non-generic option
$stringOption = OptionalString::of('');
// Call all methods with generic arguments
$stringOption->filter(static fn (string $value): bool => true);
$stringOption->filter(static fn (int $value): bool => true); // @phpstan-ignore argument.type
$stringOption->flatMap(static fn (string $value): OptionalString => $stringOption);
$stringOption->flatMap(static fn (int $value): OptionalString => $stringOption); // @phpstan-ignore argument.type
$stringOption->ifPresent(static fn (string $value): string => $value);
$stringOption->ifPresent(static fn (int $value): int => $value); // @phpstan-ignore argument.type
$stringOption->map(static fn (string $value): string => $value);
$stringOption->map(static fn (int $value): int => $value); // @phpstan-ignore argument.type
$stringOptionOrElse = $stringOption->orElse('');
$stringOptionOrElseNull = $stringOption->orElse(null);
$stringOption->orElse(0); // @phpstan-ignore argument.type
$stringOptionOrElseGet = $stringOption->orElseGet(static fn (): string => '');
$stringOption->orElseGet(static fn (): int => 0); // @phpstan-ignore argument.type
// Use non-generic option as input for functions
$check->genericInputOptions(string: $stringOption);
$check->nonGenericInputOptions(string: $stringOption);
$check->nonGenericInputs(string: $stringOption->get());
$check->nonGenericInputs(string: $stringOption->orElseThrow());
$check->nonGenericInputs(string: $stringOptionOrElse);
$check->nonGenericInputs(string: $stringOptionOrElseNull); // @phpstan-ignore argument.type
$check->nonGenericInputs(string: $stringOptionOrElseNull ?? '');
$check->nonGenericInputs(string: $stringOptionOrElseGet);
// Re-map typed option & call filter on it to check new generic
$objectOptionMapped = $stringOption->map(static fn (string $value): object => new DataObject($value));
$objectOptionMappedFiltered = $objectOptionMapped->filter(static fn (object $value): bool => true);
$objectOptionMapped->filter(static fn (string $value): bool => true); // @phpstan-ignore argument.type
$objectOptionFlatMapped = $stringOption->flatMap(static fn (string $value): OptionalObject => OptionalObject::of(new DataObject($value)), empty: OptionalObject::empty());
$objectOptionFlatMappedFiltered = $objectOptionFlatMapped->filter(static fn (object $value): bool => true);
$objectOptionFlatMapped->filter(static fn (string $value): bool => true); // @phpstan-ignore argument.type
// Use re-mapped filtered options as input for functions
$check->genericInputOptions(object: $objectOptionMappedFiltered);
$check->nonGenericInputOptions(object: $objectOptionMappedFiltered); // @phpstan-ignore argument.type
$check->nonGenericInputs(object: $objectOptionMappedFiltered->get());
$check->genericInputOptions(object: $objectOptionFlatMappedFiltered);
$check->nonGenericInputOptions(object: $objectOptionFlatMappedFiltered);
$check->nonGenericInputs(object: $objectOptionFlatMappedFiltered->get());
// ---------------------------------------------------------------------------------------------------------------------
// Create complexly generic option
/** @var array{string, object} $array */
$array = ['', new DataObject()];
$arrayOption = OptionalArray::of($array);
// Call some methods with generic arguments
$arrayOptionFiltered = $arrayOption->filter(static fn (array $value): bool => true);
$arrayOption->filter(static fn (string $value): bool => true); // @phpstan-ignore argument.type
$arrayOption->orElse(['1', new stdClass()]);
$arrayOption->orElse([new stdClass(), '1']); // @phpstan-ignore argument.type
// Use filtered complexly generic option as input for function
$check->nonGenericInputs(string: $arrayOptionFiltered->get()[0]);
$check->nonGenericInputs(string: $arrayOptionFiltered->get()[1]); // @phpstan-ignore argument.type
$check->nonGenericInputs(object: $arrayOptionFiltered->get()[0]); // @phpstan-ignore argument.type
$check->nonGenericInputs(object: $arrayOptionFiltered->get()[1]);
// ---------------------------------------------------------------------------------------------------------------------
// Create covariant option
$stdClassOption = Optional::of(new stdClass());
// Pass covariant option as input argument
$check->covariantInputOption(object: $stdClassOption);
// ---------------------------------------------------------------------------------------------------------------------