|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is checked by PhpStan as production code and confirms correctness of PhpDocs |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use PetrKnap\Optional\Optional; |
| 10 | +use PetrKnap\Optional\OptionalInt; |
| 11 | +use PetrKnap\Optional\OptionalObject; |
| 12 | +use PetrKnap\Optional\OptionalString; |
| 13 | + |
| 14 | +$functionWithGenericInputOptions = fn (Optional $string = null, Optional $int = null) => null; |
| 15 | +$functionWithNonGenericInputOptions = fn (OptionalString $string = null, OptionalInt $int = null) => null; |
| 16 | +$functionWithNonGenericInputs = fn (string $string = '', int $int = 0) => null; |
| 17 | + |
| 18 | +// --------------------------------------------------------------------------------------------------------------------- |
| 19 | + |
| 20 | +// Call all factories |
| 21 | +OptionalString::of(''); |
| 22 | +OptionalString::of(null); // @phpstan-ignore argument.type |
| 23 | +OptionalString::of(false); // @phpstan-ignore argument.type |
| 24 | +OptionalString::ofFalsable(''); |
| 25 | +OptionalString::ofFalsable(null); // @phpstan-ignore argument.type |
| 26 | +OptionalString::ofFalsable(false); |
| 27 | +OptionalString::ofNullable(''); |
| 28 | +OptionalString::ofNullable(null); |
| 29 | +OptionalString::ofNullable(false); // @phpstan-ignore argument.type |
| 30 | +OptionalString::ofSingle(['']); |
| 31 | +OptionalString::ofSingle([null]); // @phpstan-ignore argument.type |
| 32 | +OptionalString::ofSingle([false]); // @phpstan-ignore argument.type |
| 33 | +OptionalString::ofSingle([]); |
| 34 | + |
| 35 | +// Check sub-typed optional factory |
| 36 | +OptionalObject::of(new stdClass()); |
| 37 | +OptionalObject::of(new class { |
| 38 | +}); |
| 39 | +OptionalObject::of(''); // @phpstan-ignore argument.type, argument.templateType |
| 40 | +OptionalObject\OptionalStdClass::of(new stdClass()); |
| 41 | +OptionalObject\OptionalStdClass::of(new class { // @phpstan-ignore argument.type |
| 42 | +}); |
| 43 | +OptionalObject\OptionalStdClass::of(''); // @phpstan-ignore argument.type |
| 44 | + |
| 45 | +// --------------------------------------------------------------------------------------------------------------------- |
| 46 | + |
| 47 | +// Create generic option |
| 48 | +$stringOption = Optional::of(''); |
| 49 | + |
| 50 | +// Call all methods with generic arguments |
| 51 | +$stringOption->filter(static fn (string $value): bool => true); |
| 52 | +$stringOption->filter(static fn (int $value): bool => true); // @phpstan-ignore argument.type |
| 53 | +$stringOption->flatMap(static fn (string $value): Optional => $stringOption); |
| 54 | +$stringOption->flatMap(static fn (int $value): Optional => $stringOption); // @phpstan-ignore argument.type |
| 55 | +$stringOption->ifPresent(static fn (string $value): string => $value); |
| 56 | +$stringOption->ifPresent(static fn (int $value): int => $value); // @phpstan-ignore argument.type |
| 57 | +$stringOption->map(static fn (string $value): string => $value); |
| 58 | +$stringOption->map(static fn (int $value): int => $value); // @phpstan-ignore argument.type |
| 59 | +$stringOptionOrElse = $stringOption->orElse(''); |
| 60 | +$stringOptionOrElseNull = $stringOption->orElse(null); |
| 61 | +$stringOption->orElse(0); // @phpstan-ignore argument.type |
| 62 | +$stringOptionOrElseGet = $stringOption->orElseGet(static fn (): string => ''); |
| 63 | +$stringOption->orElseGet(static fn (): int => 0); // @phpstan-ignore argument.type |
| 64 | + |
| 65 | +// Use generic option as input for functions |
| 66 | +$functionWithGenericInputOptions(string: $stringOption); |
| 67 | +$functionWithNonGenericInputOptions(string: $stringOption); // @phpstan-ignore argument.type |
| 68 | +$functionWithNonGenericInputs(string: $stringOption->get()); |
| 69 | +$functionWithNonGenericInputs(string: $stringOption->orElseThrow()); |
| 70 | +$functionWithNonGenericInputs(string: $stringOptionOrElse); |
| 71 | +$functionWithNonGenericInputs(string: $stringOptionOrElseNull); // @phpstan-ignore argument.type |
| 72 | +$functionWithNonGenericInputs(string: $stringOptionOrElseNull ?? ''); |
| 73 | +$functionWithNonGenericInputs(string: $stringOptionOrElseGet); |
| 74 | + |
| 75 | +// Re-map generic option & call filter on it to check new generic |
| 76 | +$intOptionMapped = $stringOption->map(static fn (string $value): int => 0); |
| 77 | +$intOptionMappedFiltered = $intOptionMapped->filter(static fn (int $value): bool => true); |
| 78 | +$intOptionMapped->filter(static fn (string $value): bool => true); // @phpstan-ignore argument.type |
| 79 | +$intOptionFlatMapped = $stringOption->flatMap(static fn (string $value): Optional => Optional::of(0)); |
| 80 | +$intOptionFlatMappedFiltered = $intOptionFlatMapped->filter(static fn (int $value): bool => true); |
| 81 | +$intOptionFlatMapped->filter(static fn (string $value): bool => true); // @phpstan-ignore argument.type |
| 82 | + |
| 83 | +// Use re-mapped filtered options as input for functions |
| 84 | +$functionWithGenericInputOptions(int: $intOptionMappedFiltered); |
| 85 | +$functionWithNonGenericInputOptions(int: $intOptionMappedFiltered); // @phpstan-ignore argument.type |
| 86 | +$functionWithNonGenericInputs(int: $intOptionMappedFiltered->get()); |
| 87 | +$functionWithGenericInputOptions(int: $intOptionFlatMappedFiltered); |
| 88 | +$functionWithNonGenericInputOptions(int: $intOptionFlatMappedFiltered); // @phpstan-ignore argument.type |
| 89 | +$functionWithNonGenericInputs(int: $intOptionFlatMappedFiltered->get()); |
| 90 | + |
| 91 | +// --------------------------------------------------------------------------------------------------------------------- |
| 92 | + |
| 93 | +// Create non-generic option |
| 94 | +$stringOption = OptionalString::of(''); |
| 95 | + |
| 96 | +// Call all methods with generic arguments |
| 97 | +$stringOption->filter(static fn (string $value): bool => true); |
| 98 | +$stringOption->filter(static fn (int $value): bool => true); // @phpstan-ignore argument.type |
| 99 | +$stringOption->flatMap(static fn (string $value): OptionalString => $stringOption); |
| 100 | +$stringOption->flatMap(static fn (int $value): OptionalString => $stringOption); // @phpstan-ignore argument.type |
| 101 | +$stringOption->ifPresent(static fn (string $value): string => $value); |
| 102 | +$stringOption->ifPresent(static fn (int $value): int => $value); // @phpstan-ignore argument.type |
| 103 | +$stringOption->map(static fn (string $value): string => $value); |
| 104 | +$stringOption->map(static fn (int $value): int => $value); // @phpstan-ignore argument.type |
| 105 | +$stringOptionOrElse = $stringOption->orElse(''); |
| 106 | +$stringOptionOrElseNull = $stringOption->orElse(null); |
| 107 | +$stringOption->orElse(0); // @phpstan-ignore argument.type |
| 108 | +$stringOptionOrElseGet = $stringOption->orElseGet(static fn (): string => ''); |
| 109 | +$stringOption->orElseGet(static fn (): int => 0); // @phpstan-ignore argument.type |
| 110 | + |
| 111 | +// Use non-generic option as input for functions |
| 112 | +$functionWithGenericInputOptions(string: $stringOption); |
| 113 | +$functionWithNonGenericInputOptions(string: $stringOption); |
| 114 | +$functionWithNonGenericInputs(string: $stringOption->get()); |
| 115 | +$functionWithNonGenericInputs(string: $stringOption->orElseThrow()); |
| 116 | +$functionWithNonGenericInputs(string: $stringOptionOrElse); |
| 117 | +$functionWithNonGenericInputs(string: $stringOptionOrElseNull); // @phpstan-ignore argument.type |
| 118 | +$functionWithNonGenericInputs(string: $stringOptionOrElseNull ?? ''); |
| 119 | +$functionWithNonGenericInputs(string: $stringOptionOrElseGet); |
| 120 | + |
| 121 | +// Re-map typed option & call filter on it to check new generic |
| 122 | +$intOptionMapped = $stringOption->map(static fn (string $value): int => 0); |
| 123 | +$intOptionMappedFiltered = $intOptionMapped->filter(static fn (int $value): bool => true); |
| 124 | +$intOptionMapped->filter(static fn (string $value): bool => true); // @phpstan-ignore argument.type |
| 125 | +$intOptionFlatMapped = $stringOption->flatMap(static fn (string $value): OptionalInt => OptionalInt::of(0)); |
| 126 | +$intOptionFlatMappedFiltered = $intOptionFlatMapped->filter(static fn (int $value): bool => true); |
| 127 | +$intOptionFlatMapped->filter(static fn (string $value): bool => true); // @phpstan-ignore argument.type |
| 128 | + |
| 129 | +// Use re-mapped filtered options as input for functions |
| 130 | +$functionWithGenericInputOptions(int: $intOptionMappedFiltered); |
| 131 | +$functionWithNonGenericInputOptions(int: $intOptionMappedFiltered); // @phpstan-ignore argument.type |
| 132 | +$functionWithNonGenericInputs(int: $intOptionMappedFiltered->get()); |
| 133 | +$functionWithGenericInputOptions(int: $intOptionFlatMappedFiltered); |
| 134 | +$functionWithNonGenericInputOptions(int: $intOptionFlatMappedFiltered); // @phpstan-ignore argument.type |
| 135 | +$functionWithNonGenericInputs(int: $intOptionFlatMappedFiltered->get()); |
| 136 | + |
| 137 | +// --------------------------------------------------------------------------------------------------------------------- |
0 commit comments