Skip to content

Commit

Permalink
improve and fix tuple return types (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored Jun 1, 2024
1 parent 56c1cd5 commit 80e71d0
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ public function append(PhpNamespace $namespace, ClassType $class, int $tupleSize

$method = $class->addMethod($this->methodName());
$method->addParameter('value');
$method->addComment('@template T');
$method->addComment(self::EMPTY_COMMENT_LINE);
$method->addComment('@param T $value');
$method->addComment(self::EMPTY_COMMENT_LINE);

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

return;
}

$method->setReturnType($this->getMethodReturnType($namespace, $resultTupleSize));
$method->addComment('@template T');
$method->addComment(self::EMPTY_COMMENT_LINE);
$method->addComment('@param T $value');
$method->addComment(self::EMPTY_COMMENT_LINE);
$method->addComment($this->getReturnTypeComment($resultTupleSize, $tupleSize));
$method->setBody($this->getMethodBody($resultTupleSize, $tupleSize));
}
Expand All @@ -49,7 +50,7 @@ private function getMethodReturnType(PhpNamespace $namespace, int $resultTupleSi

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

private function getMethodBody(int $resultTupleSize, int $tupleSize): string
Expand Down
9 changes: 7 additions & 2 deletions generators/Tuple/FragmentGenerator/ConcatMethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ private function generateConcatTupleNMethod(int $n, int $tupleSize, ClassType $c

$concatTupleN->addComment(sprintf('@param Tuple%s%s $tuple', $n, $paramTupleGenerics));
$concatTupleN->addComment(self::EMPTY_COMMENT_LINE);
$concatTupleN->addComment(sprintf('@returns Tuple%s%s', $returnTupleSize, $returnTupleGenerics));
$concatTupleN->addBody('return $this->concat($tuple);');
$concatTupleN->addComment(sprintf('@return Tuple%s%s', $returnTupleSize, $returnTupleGenerics));

if ($this->isTupleZero($tupleSize) && $n === 0) {
$concatTupleN->addBody('return new Tuple0();');
} else {
$concatTupleN->addBody('return $this->concat($tuple);');
}
}
}
5 changes: 5 additions & 0 deletions generators/Tuple/FragmentGenerator/ToArrayMethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class ToArrayMethodGenerator extends FragmentGenerator
public function append(PhpNamespace $namespace, ClassType $class, int $tupleSize, int $maxTupleSize): void
{
$toArray = $class->addMethod('toArray');
if ($this->isTupleZero($tupleSize)) {
$toArray->addComment('@return array{}');
} else {
$toArray->addComment('@return array{'.join(', ', array_map(fn ($int) => 'T'.$int, range(1, $tupleSize))).'}');
}
$toArray->setReturnType('array');

if ($this->isTupleZero($tupleSize)) {
Expand Down
9 changes: 4 additions & 5 deletions src/Collection/Stream/Collectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Munus\Collection\Map;
use Munus\Collection\Set;
use Munus\Collection\Stream\Collector\GenericCollector;
use Munus\Tuple;
use Munus\Tuple\Tuple2;

final class Collectors
Expand Down Expand Up @@ -94,17 +93,17 @@ public static function counting(): Collector
}

/**
* @return Collector<mixed,int|float>
* @return Collector<mixed,int|float|Tuple2>
*/
public static function averaging(): Collector
{
return new GenericCollector(Tuple::of(0, 0), /** @param T $value */ function (Tuple2 $acc, $value): Tuple2 {
return new GenericCollector(new Tuple2(0, 0), /** @param T $value */ function (Tuple2 $acc, $value): Tuple2 {
if (!is_numeric($value)) {
throw new \InvalidArgumentException(sprintf('Could not convert %s to number', (string) $value));
}

return Tuple::of($acc[0] + $value, $acc[1] + 1);
}, /** @return int|float */ function (Tuple2 $acc) {
return new Tuple2($acc[0] + $value, $acc[1] + 1);
}, function (Tuple2 $acc): int|float {
if ($acc[1] === 0) {
return 0;
}
Expand Down
24 changes: 22 additions & 2 deletions src/Tuple.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
use Munus\Tuple\Tuple8;
use Munus\Value\Comparator;

/**
* @implements \ArrayAccess<int, mixed>
*/
abstract class Tuple implements \ArrayAccess
{
public const TUPLE_MAX_SIZE = 8;

/**
* @param mixed ...$values
*/
public static function of(...$values)
public static function of(...$values): self
{
return match (count($values)) {
0 => new Tuple0(),
Expand All @@ -41,9 +44,26 @@ public static function of(...$values)

abstract public function arity(): int;

/**
* @return mixed[]
*/
abstract public function toArray(): array;

public function concat(Tuple0|Tuple1|Tuple2|Tuple3|Tuple4|Tuple5|Tuple6|Tuple7|Tuple8 $tuple): Tuple0|Tuple1|Tuple2|Tuple3|Tuple4|Tuple5|Tuple6|Tuple7|Tuple8
/**
* @template T
*
* @param T $value
*/
abstract public function append($value): self;

/**
* @template T
*
* @param T $value
*/
abstract public function prepend($value): self;

public function concat(self $tuple): self
{
return Tuple::of(...$this->toArray(), ...$tuple->toArray());
}
Expand Down
27 changes: 15 additions & 12 deletions src/Tuple/Tuple0.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function arity(): int
return 0;
}

/**
* @return array{}
*/
public function toArray(): array
{
return [];
Expand All @@ -32,7 +35,7 @@ public function toArray(): array
*
* @param T $value
*
* @returns Tuple1<T>
* @return Tuple1<T>
*/
public function prepend($value): Tuple1
{
Expand All @@ -44,7 +47,7 @@ public function prepend($value): Tuple1
*
* @param T $value
*
* @returns Tuple1<T>
* @return Tuple1<T>
*/
public function append($value): Tuple1
{
Expand All @@ -54,19 +57,19 @@ public function append($value): Tuple1
/**
* @param Tuple0 $tuple
*
* @returns Tuple0
* @return Tuple0
*/
public function concatTuple0($tuple)
{
return $this->concat($tuple);
return new Tuple0();
}

/**
* @template U1
*
* @param Tuple1<U1> $tuple
*
* @returns Tuple1<U1>
* @return Tuple1<U1>
*/
public function concatTuple1($tuple)
{
Expand All @@ -79,7 +82,7 @@ public function concatTuple1($tuple)
*
* @param Tuple2<U1, U2> $tuple
*
* @returns Tuple2<U1, U2>
* @return Tuple2<U1, U2>
*/
public function concatTuple2($tuple)
{
Expand All @@ -93,7 +96,7 @@ public function concatTuple2($tuple)
*
* @param Tuple3<U1, U2, U3> $tuple
*
* @returns Tuple3<U1, U2, U3>
* @return Tuple3<U1, U2, U3>
*/
public function concatTuple3($tuple)
{
Expand All @@ -108,7 +111,7 @@ public function concatTuple3($tuple)
*
* @param Tuple4<U1, U2, U3, U4> $tuple
*
* @returns Tuple4<U1, U2, U3, U4>
* @return Tuple4<U1, U2, U3, U4>
*/
public function concatTuple4($tuple)
{
Expand All @@ -124,7 +127,7 @@ public function concatTuple4($tuple)
*
* @param Tuple5<U1, U2, U3, U4, U5> $tuple
*
* @returns Tuple5<U1, U2, U3, U4, U5>
* @return Tuple5<U1, U2, U3, U4, U5>
*/
public function concatTuple5($tuple)
{
Expand All @@ -141,7 +144,7 @@ public function concatTuple5($tuple)
*
* @param Tuple6<U1, U2, U3, U4, U5, U6> $tuple
*
* @returns Tuple6<U1, U2, U3, U4, U5, U6>
* @return Tuple6<U1, U2, U3, U4, U5, U6>
*/
public function concatTuple6($tuple)
{
Expand All @@ -159,7 +162,7 @@ public function concatTuple6($tuple)
*
* @param Tuple7<U1, U2, U3, U4, U5, U6, U7> $tuple
*
* @returns Tuple7<U1, U2, U3, U4, U5, U6, U7>
* @return Tuple7<U1, U2, U3, U4, U5, U6, U7>
*/
public function concatTuple7($tuple)
{
Expand All @@ -178,7 +181,7 @@ public function concatTuple7($tuple)
*
* @param Tuple8<U1, U2, U3, U4, U5, U6, U7, U8> $tuple
*
* @returns Tuple8<U1, U2, U3, U4, U5, U6, U7, U8>
* @return Tuple8<U1, U2, U3, U4, U5, U6, U7, U8>
*/
public function concatTuple8($tuple)
{
Expand Down
23 changes: 13 additions & 10 deletions src/Tuple/Tuple1.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public function arity(): int
return 1;
}

/**
* @return array{T1}
*/
public function toArray(): array
{
return [
Expand All @@ -41,7 +44,7 @@ public function toArray(): array
*
* @param T $value
*
* @returns Tuple2<T, T1>
* @return Tuple2<T, T1>
*/
public function prepend($value): Tuple2
{
Expand All @@ -53,7 +56,7 @@ public function prepend($value): Tuple2
*
* @param T $value
*
* @returns Tuple2<T1, T>
* @return Tuple2<T1, T>
*/
public function append($value): Tuple2
{
Expand All @@ -63,7 +66,7 @@ public function append($value): Tuple2
/**
* @param Tuple0 $tuple
*
* @returns Tuple1<T1>
* @return Tuple1<T1>
*/
public function concatTuple0($tuple)
{
Expand All @@ -75,7 +78,7 @@ public function concatTuple0($tuple)
*
* @param Tuple1<U1> $tuple
*
* @returns Tuple2<T1, U1>
* @return Tuple2<T1, U1>
*/
public function concatTuple1($tuple)
{
Expand All @@ -88,7 +91,7 @@ public function concatTuple1($tuple)
*
* @param Tuple2<U1, U2> $tuple
*
* @returns Tuple3<T1, U1, U2>
* @return Tuple3<T1, U1, U2>
*/
public function concatTuple2($tuple)
{
Expand All @@ -102,7 +105,7 @@ public function concatTuple2($tuple)
*
* @param Tuple3<U1, U2, U3> $tuple
*
* @returns Tuple4<T1, U1, U2, U3>
* @return Tuple4<T1, U1, U2, U3>
*/
public function concatTuple3($tuple)
{
Expand All @@ -117,7 +120,7 @@ public function concatTuple3($tuple)
*
* @param Tuple4<U1, U2, U3, U4> $tuple
*
* @returns Tuple5<T1, U1, U2, U3, U4>
* @return Tuple5<T1, U1, U2, U3, U4>
*/
public function concatTuple4($tuple)
{
Expand All @@ -133,7 +136,7 @@ public function concatTuple4($tuple)
*
* @param Tuple5<U1, U2, U3, U4, U5> $tuple
*
* @returns Tuple6<T1, U1, U2, U3, U4, U5>
* @return Tuple6<T1, U1, U2, U3, U4, U5>
*/
public function concatTuple5($tuple)
{
Expand All @@ -150,7 +153,7 @@ public function concatTuple5($tuple)
*
* @param Tuple6<U1, U2, U3, U4, U5, U6> $tuple
*
* @returns Tuple7<T1, U1, U2, U3, U4, U5, U6>
* @return Tuple7<T1, U1, U2, U3, U4, U5, U6>
*/
public function concatTuple6($tuple)
{
Expand All @@ -168,7 +171,7 @@ public function concatTuple6($tuple)
*
* @param Tuple7<U1, U2, U3, U4, U5, U6, U7> $tuple
*
* @returns Tuple8<T1, U1, U2, U3, U4, U5, U6, U7>
* @return Tuple8<T1, U1, U2, U3, U4, U5, U6, U7>
*/
public function concatTuple7($tuple)
{
Expand Down
Loading

0 comments on commit 80e71d0

Please sign in to comment.