Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
6 / 6 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
ResultData | |
100.00% |
6 / 6 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
createSuccess | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createFailure | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isFailureOrFailValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
isFailureOrFalse | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core; |
6 | |
7 | /** |
8 | * 結果データ。 |
9 | * |
10 | * @template TValue |
11 | */ |
12 | final readonly class ResultData |
13 | { |
14 | #region variable |
15 | |
16 | /** |
17 | * 成功状態。 |
18 | */ |
19 | public bool $success; |
20 | /** |
21 | * 成功時のデータ。 |
22 | * |
23 | * @phpstan-var ($success is true ? TValue : mixed) |
24 | */ |
25 | public mixed $value; |
26 | |
27 | #endregion |
28 | |
29 | private function __construct(bool $success, mixed $value) |
30 | { |
31 | $this->success = $success; |
32 | $this->value = $value; |
33 | } |
34 | |
35 | #region function |
36 | |
37 | /** |
38 | * 成功データの生成。 |
39 | * |
40 | * @template TResultValue |
41 | * @param mixed $value 成功データ。 |
42 | * @phpstan-param TResultValue $value |
43 | * @return ResultData |
44 | * @phpstan-return ResultData<TResultValue> |
45 | */ |
46 | public static function createSuccess(mixed $value): ResultData |
47 | { |
48 | return new ResultData(true, $value); |
49 | } |
50 | |
51 | /** |
52 | * 失敗データの生成。 |
53 | * |
54 | * @return ResultData |
55 | * @phpstan-return ResultData<mixed> |
56 | */ |
57 | public static function createFailure(): ResultData |
58 | { |
59 | return new ResultData(false, null); |
60 | } |
61 | |
62 | /** |
63 | * 結果が失敗か失敗対象の値か。 |
64 | * @return bool |
65 | */ |
66 | public function isFailureOrFailValue(mixed $failValue): bool |
67 | { |
68 | return !$this->success || $this->value === $failValue; |
69 | } |
70 | |
71 | /** |
72 | * 結果が失敗か `false` か。 |
73 | * @return bool |
74 | */ |
75 | public function isFailureOrFalse(): bool |
76 | { |
77 | return $this->isFailureOrFailValue(false); |
78 | } |
79 | |
80 | |
81 | #endregion |
82 | } |