From 637bb507e78c2dedeb1aa8a5c27a4a581fdd8a49 Mon Sep 17 00:00:00 2001 From: Yemil Godinez Date: Sun, 12 May 2024 12:44:06 +0200 Subject: [PATCH] chore: add common functions return types --- src/Combinatorics.php | 14 +++++++------- src/GeneratorInterface.php | 4 ++-- src/Generators/Combinations.php | 4 ++-- src/Generators/Fibonacci.php | 4 ++-- src/Generators/FiniteGroup.php | 4 ++-- src/Generators/NGrams.php | 6 +++--- src/Generators/Perfect.php | 2 +- src/Generators/Permutations.php | 8 ++++---- src/Generators/Prime.php | 2 +- src/Generators/PrimeFactors.php | 2 +- src/Generators/Product.php | 4 ++-- src/Generators/Shift.php | 4 ++-- src/Iterators.php | 2 +- src/Iterators/Cycle.php | 4 ++-- src/Iterators/Fibonacci.php | 4 ++-- src/Iterators/FiniteGroup.php | 10 +++++----- src/Iterators/Perfect.php | 8 ++++---- src/Iterators/Prime.php | 8 ++++---- src/Iterators/PrimeFactors.php | 8 ++++---- src/Iterators/Product.php | 4 ++-- src/Iterators/Rotation.php | 4 ++-- src/Iterators/Shift.php | 4 ++-- 22 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/Combinatorics.php b/src/Combinatorics.php index 23156a2..dafe832 100644 --- a/src/Combinatorics.php +++ b/src/Combinatorics.php @@ -50,7 +50,7 @@ public function __construct(array $dataset = [], $length = null) * @return int * The number of element */ - public function count() + public function count(): int { return count($this->toArray()); } @@ -61,7 +61,7 @@ public function count() * @return mixed[] * The dataset */ - public function getDataset() + public function getDataset(): array { return $this->dataset; } @@ -72,7 +72,7 @@ public function getDataset() * @return int * The length */ - public function getLength() + public function getLength(): int { return (int) $this->length; } @@ -85,7 +85,7 @@ public function getLength() * * @return $this */ - public function setDataset(array $dataset = []) + public function setDataset(array $dataset = []): Combinatorics { $this->dataset = $dataset; @@ -100,7 +100,7 @@ public function setDataset(array $dataset = []) * * @return $this */ - public function setLength($length = null) + public function setLength($length = null): Combinatorics { $length = $length ?? $this->datasetCount; $this->length = (abs($length) > $this->datasetCount) ? $this->datasetCount : $length; @@ -111,7 +111,7 @@ public function setLength($length = null) /** * @return array */ - public function toArray() + public function toArray(): array { return []; } @@ -127,7 +127,7 @@ public function toArray() * @return int * The factorial of $n */ - protected function fact($n, $total = 1) + protected function fact($n, $total = 1): int { return (2 > $n) ? $total : $this->fact($n - 1, $total * $n); } diff --git a/src/GeneratorInterface.php b/src/GeneratorInterface.php index 5cceca3..7a4fee8 100644 --- a/src/GeneratorInterface.php +++ b/src/GeneratorInterface.php @@ -14,12 +14,12 @@ interface GeneratorInterface * @return Generator * The generator */ - public function generator(); + public function generator(): Generator; /** * Convert the generator into an array. * * @return array */ - public function toArray(); + public function toArray(): array; } diff --git a/src/Generators/Combinations.php b/src/Generators/Combinations.php index e2d84b2..d9af7d4 100644 --- a/src/Generators/Combinations.php +++ b/src/Generators/Combinations.php @@ -21,7 +21,7 @@ class Combinations extends CombinationsIterator implements GeneratorInterface /** * {@inheritdoc} */ - public function generator() + public function generator(): Generator { return $this->get($this->getDataset(), $this->getLength()); } @@ -36,7 +36,7 @@ public function generator() * * @return Generator */ - protected function get(array $dataset, $length) + protected function get(array $dataset, $length): Generator { $originalLength = count($dataset); $remainingLength = $originalLength - $length + 1; diff --git a/src/Generators/Fibonacci.php b/src/Generators/Fibonacci.php index ba81d89..a3afb86 100644 --- a/src/Generators/Fibonacci.php +++ b/src/Generators/Fibonacci.php @@ -13,7 +13,7 @@ class Fibonacci extends FibonacciIterator implements GeneratorInterface /** * {@inheritdoc} */ - public function generator() + public function generator(): Generator { return $this->get(); } @@ -23,7 +23,7 @@ public function generator() * * @return Generator */ - protected function get() + protected function get(): Generator { $a = 0; $b = 1; diff --git a/src/Generators/FiniteGroup.php b/src/Generators/FiniteGroup.php index 03e688a..b6caa7c 100644 --- a/src/Generators/FiniteGroup.php +++ b/src/Generators/FiniteGroup.php @@ -18,7 +18,7 @@ class FiniteGroup extends FiniteGroupIterator implements GeneratorInterface /** * {@inheritdoc} */ - public function generator() + public function generator(): Generator { return $this->get(); } @@ -29,7 +29,7 @@ public function generator() * @return Generator * The finite group generator */ - protected function get() + protected function get(): Generator { foreach ($this->group as $number) { yield $number; diff --git a/src/Generators/NGrams.php b/src/Generators/NGrams.php index 9b92f31..9380e60 100644 --- a/src/Generators/NGrams.php +++ b/src/Generators/NGrams.php @@ -13,7 +13,7 @@ class NGrams extends NGramsIterator implements GeneratorInterface /** * {@inheritdoc} */ - public function generator() + public function generator(): Generator { return $this->get(); } @@ -21,7 +21,7 @@ public function generator() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { return []; } @@ -32,7 +32,7 @@ public function toArray() * @return Generator * The generator */ - protected function get() + protected function get(): Generator { while (true) { $this->next(); diff --git a/src/Generators/Perfect.php b/src/Generators/Perfect.php index 171536d..937851b 100644 --- a/src/Generators/Perfect.php +++ b/src/Generators/Perfect.php @@ -12,7 +12,7 @@ class Perfect extends PerfectIterator implements GeneratorInterface /** * {@inheritdoc} */ - public function generator() + public function generator(): \Generator { for ($j = 2; $this->getMaxLimit() >= $j; ++$j) { if ($this->isPerfectNumber($j)) { diff --git a/src/Generators/Permutations.php b/src/Generators/Permutations.php index 4e63971..7b27a02 100644 --- a/src/Generators/Permutations.php +++ b/src/Generators/Permutations.php @@ -36,7 +36,7 @@ public function __construct(array $dataset = [], $length = null) /** * {@inheritdoc} */ - public function count() + public function count(): int { return $this->combinations->count() * $this->fact($this->getLength()); } @@ -44,7 +44,7 @@ public function count() /** * {@inheritdoc} */ - public function generator() + public function generator(): Generator { foreach ($this->combinations->generator() as $combination) { foreach ($this->get($combination) as $current) { @@ -56,7 +56,7 @@ public function generator() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $data = []; @@ -75,7 +75,7 @@ public function toArray() * * @return Generator */ - protected function get(array $dataset) + protected function get(array $dataset): Generator { foreach ($dataset as $key => $firstItem) { $remaining = $dataset; diff --git a/src/Generators/Prime.php b/src/Generators/Prime.php index cc91f32..1cf436d 100644 --- a/src/Generators/Prime.php +++ b/src/Generators/Prime.php @@ -12,7 +12,7 @@ class Prime extends PrimeIterator implements GeneratorInterface /** * {@inheritdoc} */ - public function generator() + public function generator(): \Generator { for ($j = 2; $this->getMaxLimit() >= $j; ++$j) { if ($this->isPrimeNumber($j)) { diff --git a/src/Generators/PrimeFactors.php b/src/Generators/PrimeFactors.php index 80eae06..410e14a 100644 --- a/src/Generators/PrimeFactors.php +++ b/src/Generators/PrimeFactors.php @@ -12,7 +12,7 @@ class PrimeFactors extends PrimeFactorsIterator implements GeneratorInterface /** * {@inheritdoc} */ - public function generator() + public function generator(): \Generator { $number = $this->getNumber(); diff --git a/src/Generators/Product.php b/src/Generators/Product.php index fd52f3c..4aa446a 100644 --- a/src/Generators/Product.php +++ b/src/Generators/Product.php @@ -15,7 +15,7 @@ class Product extends ProductIterator implements GeneratorInterface /** * {@inheritdoc} */ - public function generator() + public function generator(): Generator { return $this->get($this->getDataset()); } @@ -28,7 +28,7 @@ public function generator() * * @return Generator */ - protected function get(array $data) + protected function get(array $data): Generator { if (!empty($data)) { if ($u = array_pop($data)) { diff --git a/src/Generators/Shift.php b/src/Generators/Shift.php index ef1f584..3f9a7c5 100644 --- a/src/Generators/Shift.php +++ b/src/Generators/Shift.php @@ -12,7 +12,7 @@ class Shift extends ShiftIterator implements GeneratorInterface /** * {@inheritdoc} */ - public function generator() + public function generator(): \Generator { while (true) { $this->next(); @@ -24,7 +24,7 @@ public function generator() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { return []; } diff --git a/src/Iterators.php b/src/Iterators.php index 093606f..5e70867 100644 --- a/src/Iterators.php +++ b/src/Iterators.php @@ -52,7 +52,7 @@ public function rewind() * @return array * The elements */ - public function toArray() + public function toArray(): array { $data = []; diff --git a/src/Iterators/Cycle.php b/src/Iterators/Cycle.php index f933f11..49ead06 100644 --- a/src/Iterators/Cycle.php +++ b/src/Iterators/Cycle.php @@ -13,7 +13,7 @@ class Cycle extends Iterators /** * {@inheritdoc} */ - public function count() + public function count(): int { return count($this->getDataset()); } @@ -49,7 +49,7 @@ public function rewind() * * @return bool */ - public function valid() + public function valid(): bool { return true; } diff --git a/src/Iterators/Fibonacci.php b/src/Iterators/Fibonacci.php index c382e82..6e30f2e 100644 --- a/src/Iterators/Fibonacci.php +++ b/src/Iterators/Fibonacci.php @@ -44,7 +44,7 @@ public function __construct() * @return int * The limit */ - public function getMaxLimit() + public function getMaxLimit(): int { return (int) $this->max; } @@ -88,7 +88,7 @@ public function setMaxLimit($max) * * @return bool */ - public function valid() + public function valid(): bool { return $this->getMaxLimit() > $this->current; } diff --git a/src/Iterators/FiniteGroup.php b/src/Iterators/FiniteGroup.php index 45a9c89..6c2e6c2 100644 --- a/src/Iterators/FiniteGroup.php +++ b/src/Iterators/FiniteGroup.php @@ -35,7 +35,7 @@ class FiniteGroup extends Iterators * @return int * The number of element */ - public function count() + public function count(): int { return count($this->group); } @@ -54,7 +54,7 @@ public function current() * @return int * The size */ - public function getSize() + public function getSize(): int { return (int) $this->size; } @@ -79,7 +79,7 @@ public function next() * @return int * The order */ - public function order($generator) + public function order($generator): int { $result = []; @@ -110,7 +110,7 @@ public function setSize($size) * * @return bool */ - public function valid() + public function valid(): bool { return isset($this->group[$this->key()]); } @@ -142,7 +142,7 @@ private function computeGroup() * @return int * The greater common divisor between $a and $b */ - private function gcd($a, $b) + private function gcd($a, $b): int { return $b ? $this->gcd($b, $a % $b) : $a; } diff --git a/src/Iterators/Perfect.php b/src/Iterators/Perfect.php index 5140002..25308be 100644 --- a/src/Iterators/Perfect.php +++ b/src/Iterators/Perfect.php @@ -56,7 +56,7 @@ public function current() * @return int * The limit */ - public function getMaxLimit() + public function getMaxLimit(): int { return (int) $this->max; } @@ -67,7 +67,7 @@ public function getMaxLimit() * @return int * The limit */ - public function getMinLimit() + public function getMinLimit(): int { return 2 > $this->min ? 2 : $this->min; } @@ -121,7 +121,7 @@ public function setMinLimit($min) * * @return bool */ - public function valid() + public function valid(): bool { return $this->current() < $this->getMaxLimit(); } @@ -137,7 +137,7 @@ public function valid() * @return bool * The true if the number is perfect, false otherwise */ - protected function isPerfectNumber($number) + protected function isPerfectNumber($number): bool { $d = 0; $max = sqrt($number); diff --git a/src/Iterators/Prime.php b/src/Iterators/Prime.php index e0ef377..e3ac4d9 100644 --- a/src/Iterators/Prime.php +++ b/src/Iterators/Prime.php @@ -56,7 +56,7 @@ public function current() * @return int * The limit */ - public function getMaxLimit() + public function getMaxLimit(): int { return (int) $this->max; } @@ -67,7 +67,7 @@ public function getMaxLimit() * @return int * The limit */ - public function getMinLimit() + public function getMinLimit(): int { return 2 >= $this->min ? 2 : (int) $this->min; } @@ -121,7 +121,7 @@ public function setMinLimit($min) * * @return bool */ - public function valid() + public function valid(): bool { return $this->current() < $this->getMaxLimit(); } @@ -135,7 +135,7 @@ public function valid() * @return bool * The true if the number is prime, false otherwise */ - protected function isPrimeNumber($number) + protected function isPrimeNumber($number): bool { $number = abs($number); diff --git a/src/Iterators/PrimeFactors.php b/src/Iterators/PrimeFactors.php index 22f8635..ef37338 100644 --- a/src/Iterators/PrimeFactors.php +++ b/src/Iterators/PrimeFactors.php @@ -30,7 +30,7 @@ class PrimeFactors extends Iterators * @return int * The number of element */ - public function count() + public function count(): int { return count($this->factors); } @@ -49,7 +49,7 @@ public function current() * @return int * The number */ - public function getNumber() + public function getNumber(): int { return (int) $this->number; } @@ -92,7 +92,7 @@ public function setNumber($number) * * @return bool */ - public function valid() + public function valid(): bool { return isset($this->factors[$this->key()]); } @@ -105,7 +105,7 @@ public function valid() * @return int[] * The factors */ - private function getFactors($number) + private function getFactors($number): array { $factors = []; diff --git a/src/Iterators/Product.php b/src/Iterators/Product.php index e1741bc..4e33465 100644 --- a/src/Iterators/Product.php +++ b/src/Iterators/Product.php @@ -39,7 +39,7 @@ public function __construct(array $datasetArray) /** * {@inheritdoc} */ - public function count() + public function count(): int { $product = 1; @@ -109,7 +109,7 @@ public function rewind() * * @return bool */ - public function valid() + public function valid(): bool { $isUnlessOneValid = false; diff --git a/src/Iterators/Rotation.php b/src/Iterators/Rotation.php index e688b63..22e8f11 100644 --- a/src/Iterators/Rotation.php +++ b/src/Iterators/Rotation.php @@ -33,7 +33,7 @@ public function __construct(array $dataset = []) /** * {@inheritdoc} */ - public function count() + public function count(): int { return count($this->getDataset()); } @@ -83,7 +83,7 @@ public function rewind() * * @return bool */ - public function valid() + public function valid(): bool { return true; } diff --git a/src/Iterators/Shift.php b/src/Iterators/Shift.php index f82a7eb..478f277 100644 --- a/src/Iterators/Shift.php +++ b/src/Iterators/Shift.php @@ -28,7 +28,7 @@ public function __construct(array $dataset = [], $length = 1) /** * {@inheritdoc} */ - public function count() + public function count(): int { return count($this->getDataset()); } @@ -56,7 +56,7 @@ public function rewind() * * @return bool */ - public function valid() + public function valid(): bool { return true; }