Skip to content

Commit

Permalink
Merge pull request #17 from yemilgr/returns-type-hints
Browse files Browse the repository at this point in the history
chore: add common functions return types
  • Loading branch information
drupol committed May 12, 2024
2 parents e11e36c + 637bb50 commit 00a3d1d
Show file tree
Hide file tree
Showing 22 changed files with 57 additions and 57 deletions.
14 changes: 7 additions & 7 deletions src/Combinatorics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -61,7 +61,7 @@ public function count()
* @return mixed[]
* The dataset
*/
public function getDataset()
public function getDataset(): array
{
return $this->dataset;
}
Expand All @@ -72,7 +72,7 @@ public function getDataset()
* @return int
* The length
*/
public function getLength()
public function getLength(): int
{
return (int) $this->length;
}
Expand All @@ -85,7 +85,7 @@ public function getLength()
*
* @return $this
*/
public function setDataset(array $dataset = [])
public function setDataset(array $dataset = []): Combinatorics
{
$this->dataset = $dataset;

Expand All @@ -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;
Expand All @@ -111,7 +111,7 @@ public function setLength($length = null)
/**
* @return array<int, mixed>
*/
public function toArray()
public function toArray(): array
{
return [];
}
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/GeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ interface GeneratorInterface
* @return Generator<int, mixed>
* The generator
*/
public function generator();
public function generator(): Generator;

/**
* Convert the generator into an array.
*
* @return array<int, mixed>
*/
public function toArray();
public function toArray(): array;
}
4 changes: 2 additions & 2 deletions src/Generators/Combinations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -36,7 +36,7 @@ public function generator()
*
* @return Generator<array>
*/
protected function get(array $dataset, $length)
protected function get(array $dataset, $length): Generator
{
$originalLength = count($dataset);
$remainingLength = $originalLength - $length + 1;
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/Fibonacci.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Fibonacci extends FibonacciIterator implements GeneratorInterface
/**
* {@inheritdoc}
*/
public function generator()
public function generator(): Generator
{
return $this->get();
}
Expand All @@ -23,7 +23,7 @@ public function generator()
*
* @return Generator<int>
*/
protected function get()
protected function get(): Generator
{
$a = 0;
$b = 1;
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/FiniteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FiniteGroup extends FiniteGroupIterator implements GeneratorInterface
/**
* {@inheritdoc}
*/
public function generator()
public function generator(): Generator
{
return $this->get();
}
Expand All @@ -29,7 +29,7 @@ public function generator()
* @return Generator<int>
* The finite group generator
*/
protected function get()
protected function get(): Generator
{
foreach ($this->group as $number) {
yield $number;
Expand Down
6 changes: 3 additions & 3 deletions src/Generators/NGrams.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class NGrams extends NGramsIterator implements GeneratorInterface
/**
* {@inheritdoc}
*/
public function generator()
public function generator(): Generator
{
return $this->get();
}

/**
* {@inheritdoc}
*/
public function toArray()
public function toArray(): array
{
return [];
}
Expand All @@ -32,7 +32,7 @@ public function toArray()
* @return Generator<int>
* The generator
*/
protected function get()
protected function get(): Generator
{
while (true) {
$this->next();
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Perfect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 4 additions & 4 deletions src/Generators/Permutations.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public function __construct(array $dataset = [], $length = null)
/**
* {@inheritdoc}
*/
public function count()
public function count(): int
{
return $this->combinations->count() * $this->fact($this->getLength());
}

/**
* {@inheritdoc}
*/
public function generator()
public function generator(): Generator
{
foreach ($this->combinations->generator() as $combination) {
foreach ($this->get($combination) as $current) {
Expand All @@ -56,7 +56,7 @@ public function generator()
/**
* {@inheritdoc}
*/
public function toArray()
public function toArray(): array
{
$data = [];

Expand All @@ -75,7 +75,7 @@ public function toArray()
*
* @return Generator<array>
*/
protected function get(array $dataset)
protected function get(array $dataset): Generator
{
foreach ($dataset as $key => $firstItem) {
$remaining = $dataset;
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Prime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/PrimeFactors.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PrimeFactors extends PrimeFactorsIterator implements GeneratorInterface
/**
* {@inheritdoc}
*/
public function generator()
public function generator(): \Generator
{
$number = $this->getNumber();

Expand Down
4 changes: 2 additions & 2 deletions src/Generators/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Product extends ProductIterator implements GeneratorInterface
/**
* {@inheritdoc}
*/
public function generator()
public function generator(): Generator
{
return $this->get($this->getDataset());
}
Expand All @@ -28,7 +28,7 @@ public function generator()
*
* @return Generator<array>
*/
protected function get(array $data)
protected function get(array $data): Generator
{
if (!empty($data)) {
if ($u = array_pop($data)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/Shift.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Shift extends ShiftIterator implements GeneratorInterface
/**
* {@inheritdoc}
*/
public function generator()
public function generator(): \Generator
{
while (true) {
$this->next();
Expand All @@ -24,7 +24,7 @@ public function generator()
/**
* {@inheritdoc}
*/
public function toArray()
public function toArray(): array
{
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Iterators.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function rewind()
* @return array<int, mixed>
* The elements
*/
public function toArray()
public function toArray(): array
{
$data = [];

Expand Down
4 changes: 2 additions & 2 deletions src/Iterators/Cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Cycle extends Iterators
/**
* {@inheritdoc}
*/
public function count()
public function count(): int
{
return count($this->getDataset());
}
Expand Down Expand Up @@ -49,7 +49,7 @@ public function rewind()
*
* @return bool
*/
public function valid()
public function valid(): bool
{
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Iterators/Fibonacci.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct()
* @return int
* The limit
*/
public function getMaxLimit()
public function getMaxLimit(): int
{
return (int) $this->max;
}
Expand Down Expand Up @@ -88,7 +88,7 @@ public function setMaxLimit($max)
*
* @return bool
*/
public function valid()
public function valid(): bool
{
return $this->getMaxLimit() > $this->current;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Iterators/FiniteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -54,7 +54,7 @@ public function current()
* @return int
* The size
*/
public function getSize()
public function getSize(): int
{
return (int) $this->size;
}
Expand All @@ -79,7 +79,7 @@ public function next()
* @return int
* The order
*/
public function order($generator)
public function order($generator): int
{
$result = [];

Expand Down Expand Up @@ -110,7 +110,7 @@ public function setSize($size)
*
* @return bool
*/
public function valid()
public function valid(): bool
{
return isset($this->group[$this->key()]);
}
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Iterators/Perfect.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function current()
* @return int
* The limit
*/
public function getMaxLimit()
public function getMaxLimit(): int
{
return (int) $this->max;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -121,7 +121,7 @@ public function setMinLimit($min)
*
* @return bool
*/
public function valid()
public function valid(): bool
{
return $this->current() < $this->getMaxLimit();
}
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 00a3d1d

Please sign in to comment.