Skip to content

Commit 80e71d0

Browse files
authored
improve and fix tuple return types (#109)
1 parent 56c1cd5 commit 80e71d0

File tree

15 files changed

+146
-79
lines changed

15 files changed

+146
-79
lines changed

generators/Tuple/FragmentGenerator/AppendPrependMethodAbstractGenerator.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@ public function append(PhpNamespace $namespace, ClassType $class, int $tupleSize
1717

1818
$method = $class->addMethod($this->methodName());
1919
$method->addParameter('value');
20+
$method->addComment('@template T');
21+
$method->addComment(self::EMPTY_COMMENT_LINE);
22+
$method->addComment('@param T $value');
23+
$method->addComment(self::EMPTY_COMMENT_LINE);
2024

2125
if ($this->isMaxSizeTuple($tupleSize, $maxTupleSize)) {
2226
$namespace->addUse(UnsupportedOperationException::class);
2327
$method->setBody($this->getMaxTupleSizeExceptionThrowBody());
28+
$method->setReturnType('never');
2429

2530
return;
2631
}
2732

2833
$method->setReturnType($this->getMethodReturnType($namespace, $resultTupleSize));
29-
$method->addComment('@template T');
30-
$method->addComment(self::EMPTY_COMMENT_LINE);
31-
$method->addComment('@param T $value');
32-
$method->addComment(self::EMPTY_COMMENT_LINE);
3334
$method->addComment($this->getReturnTypeComment($resultTupleSize, $tupleSize));
3435
$method->setBody($this->getMethodBody($resultTupleSize, $tupleSize));
3536
}
@@ -49,7 +50,7 @@ private function getMethodReturnType(PhpNamespace $namespace, int $resultTupleSi
4950

5051
private function getReturnTypeComment(int $resultTupleSize, int $tupleSize): string
5152
{
52-
return sprintf('@returns Tuple%s<%s>', $resultTupleSize, $this->listOfTypes($tupleSize));
53+
return sprintf('@return Tuple%s<%s>', $resultTupleSize, $this->listOfTypes($tupleSize));
5354
}
5455

5556
private function getMethodBody(int $resultTupleSize, int $tupleSize): string

generators/Tuple/FragmentGenerator/ConcatMethodGenerator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ private function generateConcatTupleNMethod(int $n, int $tupleSize, ClassType $c
4747

4848
$concatTupleN->addComment(sprintf('@param Tuple%s%s $tuple', $n, $paramTupleGenerics));
4949
$concatTupleN->addComment(self::EMPTY_COMMENT_LINE);
50-
$concatTupleN->addComment(sprintf('@returns Tuple%s%s', $returnTupleSize, $returnTupleGenerics));
51-
$concatTupleN->addBody('return $this->concat($tuple);');
50+
$concatTupleN->addComment(sprintf('@return Tuple%s%s', $returnTupleSize, $returnTupleGenerics));
51+
52+
if ($this->isTupleZero($tupleSize) && $n === 0) {
53+
$concatTupleN->addBody('return new Tuple0();');
54+
} else {
55+
$concatTupleN->addBody('return $this->concat($tuple);');
56+
}
5257
}
5358
}

generators/Tuple/FragmentGenerator/ToArrayMethodGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class ToArrayMethodGenerator extends FragmentGenerator
1313
public function append(PhpNamespace $namespace, ClassType $class, int $tupleSize, int $maxTupleSize): void
1414
{
1515
$toArray = $class->addMethod('toArray');
16+
if ($this->isTupleZero($tupleSize)) {
17+
$toArray->addComment('@return array{}');
18+
} else {
19+
$toArray->addComment('@return array{'.join(', ', array_map(fn ($int) => 'T'.$int, range(1, $tupleSize))).'}');
20+
}
1621
$toArray->setReturnType('array');
1722

1823
if ($this->isTupleZero($tupleSize)) {

src/Collection/Stream/Collectors.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Munus\Collection\Map;
99
use Munus\Collection\Set;
1010
use Munus\Collection\Stream\Collector\GenericCollector;
11-
use Munus\Tuple;
1211
use Munus\Tuple\Tuple2;
1312

1413
final class Collectors
@@ -94,17 +93,17 @@ public static function counting(): Collector
9493
}
9594

9695
/**
97-
* @return Collector<mixed,int|float>
96+
* @return Collector<mixed,int|float|Tuple2>
9897
*/
9998
public static function averaging(): Collector
10099
{
101-
return new GenericCollector(Tuple::of(0, 0), /** @param T $value */ function (Tuple2 $acc, $value): Tuple2 {
100+
return new GenericCollector(new Tuple2(0, 0), /** @param T $value */ function (Tuple2 $acc, $value): Tuple2 {
102101
if (!is_numeric($value)) {
103102
throw new \InvalidArgumentException(sprintf('Could not convert %s to number', (string) $value));
104103
}
105104

106-
return Tuple::of($acc[0] + $value, $acc[1] + 1);
107-
}, /** @return int|float */ function (Tuple2 $acc) {
105+
return new Tuple2($acc[0] + $value, $acc[1] + 1);
106+
}, function (Tuple2 $acc): int|float {
108107
if ($acc[1] === 0) {
109108
return 0;
110109
}

src/Tuple.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616
use Munus\Tuple\Tuple8;
1717
use Munus\Value\Comparator;
1818

19+
/**
20+
* @implements \ArrayAccess<int, mixed>
21+
*/
1922
abstract class Tuple implements \ArrayAccess
2023
{
2124
public const TUPLE_MAX_SIZE = 8;
2225

2326
/**
2427
* @param mixed ...$values
2528
*/
26-
public static function of(...$values)
29+
public static function of(...$values): self
2730
{
2831
return match (count($values)) {
2932
0 => new Tuple0(),
@@ -41,9 +44,26 @@ public static function of(...$values)
4144

4245
abstract public function arity(): int;
4346

47+
/**
48+
* @return mixed[]
49+
*/
4450
abstract public function toArray(): array;
4551

46-
public function concat(Tuple0|Tuple1|Tuple2|Tuple3|Tuple4|Tuple5|Tuple6|Tuple7|Tuple8 $tuple): Tuple0|Tuple1|Tuple2|Tuple3|Tuple4|Tuple5|Tuple6|Tuple7|Tuple8
52+
/**
53+
* @template T
54+
*
55+
* @param T $value
56+
*/
57+
abstract public function append($value): self;
58+
59+
/**
60+
* @template T
61+
*
62+
* @param T $value
63+
*/
64+
abstract public function prepend($value): self;
65+
66+
public function concat(self $tuple): self
4767
{
4868
return Tuple::of(...$this->toArray(), ...$tuple->toArray());
4969
}

src/Tuple/Tuple0.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public function arity(): int
2222
return 0;
2323
}
2424

25+
/**
26+
* @return array{}
27+
*/
2528
public function toArray(): array
2629
{
2730
return [];
@@ -32,7 +35,7 @@ public function toArray(): array
3235
*
3336
* @param T $value
3437
*
35-
* @returns Tuple1<T>
38+
* @return Tuple1<T>
3639
*/
3740
public function prepend($value): Tuple1
3841
{
@@ -44,7 +47,7 @@ public function prepend($value): Tuple1
4447
*
4548
* @param T $value
4649
*
47-
* @returns Tuple1<T>
50+
* @return Tuple1<T>
4851
*/
4952
public function append($value): Tuple1
5053
{
@@ -54,19 +57,19 @@ public function append($value): Tuple1
5457
/**
5558
* @param Tuple0 $tuple
5659
*
57-
* @returns Tuple0
60+
* @return Tuple0
5861
*/
5962
public function concatTuple0($tuple)
6063
{
61-
return $this->concat($tuple);
64+
return new Tuple0();
6265
}
6366

6467
/**
6568
* @template U1
6669
*
6770
* @param Tuple1<U1> $tuple
6871
*
69-
* @returns Tuple1<U1>
72+
* @return Tuple1<U1>
7073
*/
7174
public function concatTuple1($tuple)
7275
{
@@ -79,7 +82,7 @@ public function concatTuple1($tuple)
7982
*
8083
* @param Tuple2<U1, U2> $tuple
8184
*
82-
* @returns Tuple2<U1, U2>
85+
* @return Tuple2<U1, U2>
8386
*/
8487
public function concatTuple2($tuple)
8588
{
@@ -93,7 +96,7 @@ public function concatTuple2($tuple)
9396
*
9497
* @param Tuple3<U1, U2, U3> $tuple
9598
*
96-
* @returns Tuple3<U1, U2, U3>
99+
* @return Tuple3<U1, U2, U3>
97100
*/
98101
public function concatTuple3($tuple)
99102
{
@@ -108,7 +111,7 @@ public function concatTuple3($tuple)
108111
*
109112
* @param Tuple4<U1, U2, U3, U4> $tuple
110113
*
111-
* @returns Tuple4<U1, U2, U3, U4>
114+
* @return Tuple4<U1, U2, U3, U4>
112115
*/
113116
public function concatTuple4($tuple)
114117
{
@@ -124,7 +127,7 @@ public function concatTuple4($tuple)
124127
*
125128
* @param Tuple5<U1, U2, U3, U4, U5> $tuple
126129
*
127-
* @returns Tuple5<U1, U2, U3, U4, U5>
130+
* @return Tuple5<U1, U2, U3, U4, U5>
128131
*/
129132
public function concatTuple5($tuple)
130133
{
@@ -141,7 +144,7 @@ public function concatTuple5($tuple)
141144
*
142145
* @param Tuple6<U1, U2, U3, U4, U5, U6> $tuple
143146
*
144-
* @returns Tuple6<U1, U2, U3, U4, U5, U6>
147+
* @return Tuple6<U1, U2, U3, U4, U5, U6>
145148
*/
146149
public function concatTuple6($tuple)
147150
{
@@ -159,7 +162,7 @@ public function concatTuple6($tuple)
159162
*
160163
* @param Tuple7<U1, U2, U3, U4, U5, U6, U7> $tuple
161164
*
162-
* @returns Tuple7<U1, U2, U3, U4, U5, U6, U7>
165+
* @return Tuple7<U1, U2, U3, U4, U5, U6, U7>
163166
*/
164167
public function concatTuple7($tuple)
165168
{
@@ -178,7 +181,7 @@ public function concatTuple7($tuple)
178181
*
179182
* @param Tuple8<U1, U2, U3, U4, U5, U6, U7, U8> $tuple
180183
*
181-
* @returns Tuple8<U1, U2, U3, U4, U5, U6, U7, U8>
184+
* @return Tuple8<U1, U2, U3, U4, U5, U6, U7, U8>
182185
*/
183186
public function concatTuple8($tuple)
184187
{

src/Tuple/Tuple1.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public function arity(): int
2929
return 1;
3030
}
3131

32+
/**
33+
* @return array{T1}
34+
*/
3235
public function toArray(): array
3336
{
3437
return [
@@ -41,7 +44,7 @@ public function toArray(): array
4144
*
4245
* @param T $value
4346
*
44-
* @returns Tuple2<T, T1>
47+
* @return Tuple2<T, T1>
4548
*/
4649
public function prepend($value): Tuple2
4750
{
@@ -53,7 +56,7 @@ public function prepend($value): Tuple2
5356
*
5457
* @param T $value
5558
*
56-
* @returns Tuple2<T1, T>
59+
* @return Tuple2<T1, T>
5760
*/
5861
public function append($value): Tuple2
5962
{
@@ -63,7 +66,7 @@ public function append($value): Tuple2
6366
/**
6467
* @param Tuple0 $tuple
6568
*
66-
* @returns Tuple1<T1>
69+
* @return Tuple1<T1>
6770
*/
6871
public function concatTuple0($tuple)
6972
{
@@ -75,7 +78,7 @@ public function concatTuple0($tuple)
7578
*
7679
* @param Tuple1<U1> $tuple
7780
*
78-
* @returns Tuple2<T1, U1>
81+
* @return Tuple2<T1, U1>
7982
*/
8083
public function concatTuple1($tuple)
8184
{
@@ -88,7 +91,7 @@ public function concatTuple1($tuple)
8891
*
8992
* @param Tuple2<U1, U2> $tuple
9093
*
91-
* @returns Tuple3<T1, U1, U2>
94+
* @return Tuple3<T1, U1, U2>
9295
*/
9396
public function concatTuple2($tuple)
9497
{
@@ -102,7 +105,7 @@ public function concatTuple2($tuple)
102105
*
103106
* @param Tuple3<U1, U2, U3> $tuple
104107
*
105-
* @returns Tuple4<T1, U1, U2, U3>
108+
* @return Tuple4<T1, U1, U2, U3>
106109
*/
107110
public function concatTuple3($tuple)
108111
{
@@ -117,7 +120,7 @@ public function concatTuple3($tuple)
117120
*
118121
* @param Tuple4<U1, U2, U3, U4> $tuple
119122
*
120-
* @returns Tuple5<T1, U1, U2, U3, U4>
123+
* @return Tuple5<T1, U1, U2, U3, U4>
121124
*/
122125
public function concatTuple4($tuple)
123126
{
@@ -133,7 +136,7 @@ public function concatTuple4($tuple)
133136
*
134137
* @param Tuple5<U1, U2, U3, U4, U5> $tuple
135138
*
136-
* @returns Tuple6<T1, U1, U2, U3, U4, U5>
139+
* @return Tuple6<T1, U1, U2, U3, U4, U5>
137140
*/
138141
public function concatTuple5($tuple)
139142
{
@@ -150,7 +153,7 @@ public function concatTuple5($tuple)
150153
*
151154
* @param Tuple6<U1, U2, U3, U4, U5, U6> $tuple
152155
*
153-
* @returns Tuple7<T1, U1, U2, U3, U4, U5, U6>
156+
* @return Tuple7<T1, U1, U2, U3, U4, U5, U6>
154157
*/
155158
public function concatTuple6($tuple)
156159
{
@@ -168,7 +171,7 @@ public function concatTuple6($tuple)
168171
*
169172
* @param Tuple7<U1, U2, U3, U4, U5, U6, U7> $tuple
170173
*
171-
* @returns Tuple8<T1, U1, U2, U3, U4, U5, U6, U7>
174+
* @return Tuple8<T1, U1, U2, U3, U4, U5, U6, U7>
172175
*/
173176
public function concatTuple7($tuple)
174177
{

0 commit comments

Comments
 (0)