From 017f1837b9cd412b7d50492706312164dee35e80 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Fri, 20 Apr 2018 10:40:13 +0200 Subject: [PATCH 1/5] Add interfaces. --- src/GeneratorInterface.php | 21 +++++++++++++++++++++ src/Generators/Combinations.php | 3 ++- src/Generators/Fibonacci.php | 3 ++- src/Generators/FiniteGroup.php | 3 ++- src/Generators/Perfect.php | 3 ++- src/Generators/Permutations.php | 3 ++- src/Generators/Prime.php | 3 ++- src/Generators/PrimeFactors.php | 3 ++- src/Generators/Product.php | 3 ++- src/Generators/Shift.php | 11 ++++++++++- src/IteratorInterface.php | 7 +++++++ src/Iterators/Combinations.php | 3 ++- src/Iterators/Cycle.php | 3 ++- src/Iterators/Fibonacci.php | 3 ++- src/Iterators/FiniteGroup.php | 3 ++- src/Iterators/NGrams.php | 2 +- src/Iterators/Perfect.php | 3 ++- src/Iterators/Prime.php | 3 ++- src/Iterators/PrimeFactors.php | 3 ++- src/Iterators/Product.php | 3 ++- src/Iterators/Rotation.php | 3 ++- src/Iterators/Shift.php | 3 ++- 22 files changed, 75 insertions(+), 20 deletions(-) create mode 100644 src/GeneratorInterface.php create mode 100644 src/IteratorInterface.php diff --git a/src/GeneratorInterface.php b/src/GeneratorInterface.php new file mode 100644 index 0000000..9fa62be --- /dev/null +++ b/src/GeneratorInterface.php @@ -0,0 +1,21 @@ + */ -class Combinations extends CombinationsIterator +class Combinations extends CombinationsIterator implements GeneratorInterface { /** * Alias of the get() method. diff --git a/src/Generators/Fibonacci.php b/src/Generators/Fibonacci.php index 0489225..a331c2d 100644 --- a/src/Generators/Fibonacci.php +++ b/src/Generators/Fibonacci.php @@ -2,12 +2,13 @@ namespace drupol\phpermutations\Generators; +use drupol\phpermutations\GeneratorInterface; use drupol\phpermutations\Iterators\Fibonacci as FibonacciIterator; /** * Class Fibonacci. */ -class Fibonacci extends FibonacciIterator +class Fibonacci extends FibonacciIterator implements GeneratorInterface { /** * The maximum value. diff --git a/src/Generators/FiniteGroup.php b/src/Generators/FiniteGroup.php index 6e6fba5..c9e9ce2 100644 --- a/src/Generators/FiniteGroup.php +++ b/src/Generators/FiniteGroup.php @@ -2,6 +2,7 @@ namespace drupol\phpermutations\Generators; +use drupol\phpermutations\GeneratorInterface; use drupol\phpermutations\Iterators\FiniteGroup as FiniteGroupIterator; /** @@ -9,7 +10,7 @@ * * The finite group is an abelian finite cyclic group. */ -class FiniteGroup extends FiniteGroupIterator +class FiniteGroup extends FiniteGroupIterator implements GeneratorInterface { /** * Alias of the get() method. diff --git a/src/Generators/Perfect.php b/src/Generators/Perfect.php index d87cfcb..0466764 100644 --- a/src/Generators/Perfect.php +++ b/src/Generators/Perfect.php @@ -2,12 +2,13 @@ namespace drupol\phpermutations\Generators; +use drupol\phpermutations\GeneratorInterface; use drupol\phpermutations\Iterators\Perfect as PerfectIterator; /** * Class Perfect. */ -class Perfect extends PerfectIterator +class Perfect extends PerfectIterator implements GeneratorInterface { /** * The maximum value. diff --git a/src/Generators/Permutations.php b/src/Generators/Permutations.php index 52f0a66..765cb0a 100644 --- a/src/Generators/Permutations.php +++ b/src/Generators/Permutations.php @@ -3,11 +3,12 @@ namespace drupol\phpermutations\Generators; use drupol\phpermutations\Combinatorics; +use drupol\phpermutations\GeneratorInterface; /** * Class Permutations. */ -class Permutations extends Combinatorics +class Permutations extends Combinatorics implements GeneratorInterface { /** * The combinations generator. diff --git a/src/Generators/Prime.php b/src/Generators/Prime.php index c038be1..162f9ad 100644 --- a/src/Generators/Prime.php +++ b/src/Generators/Prime.php @@ -2,12 +2,13 @@ namespace drupol\phpermutations\Generators; +use drupol\phpermutations\GeneratorInterface; use drupol\phpermutations\Iterators\Prime as PrimeIterator; /** * Class Prime. */ -class Prime extends PrimeIterator +class Prime extends PrimeIterator implements GeneratorInterface { /** * Alias of the get() method. diff --git a/src/Generators/PrimeFactors.php b/src/Generators/PrimeFactors.php index adb1cb1..3b34ff7 100644 --- a/src/Generators/PrimeFactors.php +++ b/src/Generators/PrimeFactors.php @@ -2,12 +2,13 @@ namespace drupol\phpermutations\Generators; +use drupol\phpermutations\GeneratorInterface; use drupol\phpermutations\Iterators\PrimeFactors as PrimeFactorsIterator; /** * Class PrimeFactors. */ -class PrimeFactors extends PrimeFactorsIterator +class PrimeFactors extends PrimeFactorsIterator implements GeneratorInterface { /** * Alias of the get() method. diff --git a/src/Generators/Product.php b/src/Generators/Product.php index 4d846f5..80f524a 100644 --- a/src/Generators/Product.php +++ b/src/Generators/Product.php @@ -2,12 +2,13 @@ namespace drupol\phpermutations\Generators; +use drupol\phpermutations\GeneratorInterface; use drupol\phpermutations\Iterators\Product as ProductIterator; /** * Class Product. */ -class Product extends ProductIterator +class Product extends ProductIterator implements GeneratorInterface { /** * Get the generator. diff --git a/src/Generators/Shift.php b/src/Generators/Shift.php index 9d71579..3df3931 100644 --- a/src/Generators/Shift.php +++ b/src/Generators/Shift.php @@ -2,12 +2,13 @@ namespace drupol\phpermutations\Generators; +use drupol\phpermutations\GeneratorInterface; use drupol\phpermutations\Iterators\Shift as ShiftIterator; /** * Class Shift. */ -class Shift extends ShiftIterator +class Shift extends ShiftIterator implements GeneratorInterface { /** * Get the generator. @@ -20,6 +21,14 @@ public function generator() return $this->get(); } + /** + * {@inheritdoc} + */ + public function toArray() + { + return []; + } + /** * Get the generator. * diff --git a/src/IteratorInterface.php b/src/IteratorInterface.php new file mode 100644 index 0000000..8dbe32a --- /dev/null +++ b/src/IteratorInterface.php @@ -0,0 +1,7 @@ + Date: Fri, 20 Apr 2018 10:42:06 +0200 Subject: [PATCH 2/5] Update function documentation headers. --- src/Generators/Combinations.php | 10 ++-------- src/Generators/Fibonacci.php | 5 +---- src/Generators/FiniteGroup.php | 5 +---- src/Generators/Perfect.php | 12 +----------- src/Generators/Permutations.php | 12 ++---------- src/Generators/Prime.php | 5 +---- src/Generators/PrimeFactors.php | 5 +---- src/Generators/Product.php | 5 +---- src/Generators/Shift.php | 5 +---- 9 files changed, 11 insertions(+), 53 deletions(-) diff --git a/src/Generators/Combinations.php b/src/Generators/Combinations.php index 5d67257..3d40e74 100644 --- a/src/Generators/Combinations.php +++ b/src/Generators/Combinations.php @@ -14,10 +14,7 @@ class Combinations extends CombinationsIterator implements GeneratorInterface { /** - * Alias of the get() method. - * - * @return \Generator - * The prime generator + * {@inheritdoc} */ public function generator() { @@ -25,10 +22,7 @@ public function generator() } /** - * Convert the generator into an array. - * - * @return array - * The elements + * {@inheritdoc} */ public function toArray() { diff --git a/src/Generators/Fibonacci.php b/src/Generators/Fibonacci.php index a331c2d..2686cf4 100644 --- a/src/Generators/Fibonacci.php +++ b/src/Generators/Fibonacci.php @@ -18,10 +18,7 @@ class Fibonacci extends FibonacciIterator implements GeneratorInterface protected $max; /** - * Alias of the get() method. - * - * @return \Generator - * The prime generator + * {@inheritdoc} */ public function generator() { diff --git a/src/Generators/FiniteGroup.php b/src/Generators/FiniteGroup.php index c9e9ce2..0a258aa 100644 --- a/src/Generators/FiniteGroup.php +++ b/src/Generators/FiniteGroup.php @@ -13,10 +13,7 @@ class FiniteGroup extends FiniteGroupIterator implements GeneratorInterface { /** - * Alias of the get() method. - * - * @return \Generator - * The finite group generator + * {@inheritdoc} */ public function generator() { diff --git a/src/Generators/Perfect.php b/src/Generators/Perfect.php index 0466764..dc193dd 100644 --- a/src/Generators/Perfect.php +++ b/src/Generators/Perfect.php @@ -11,17 +11,7 @@ class Perfect extends PerfectIterator implements GeneratorInterface { /** - * The maximum value. - * - * @var int - */ - protected $max; - - /** - * Alias of the get() method. - * - * @return \Generator - * The prime generator + * {@inheritdoc} */ public function generator() { diff --git a/src/Generators/Permutations.php b/src/Generators/Permutations.php index 765cb0a..519ebd5 100644 --- a/src/Generators/Permutations.php +++ b/src/Generators/Permutations.php @@ -32,12 +32,7 @@ public function __construct(array $dataset = [], $length = null) } /** - * Alias of the get() method. - * - * @codingStandardsIgnoreStart - * - * @return \Generator - * @codingStandardsIgnoreEnd + * {@inheritdoc} */ public function generator() { @@ -49,10 +44,7 @@ public function generator() } /** - * Convert the generator into an array. - * - * @return array - * The elements + * {@inheritdoc} */ public function toArray() { diff --git a/src/Generators/Prime.php b/src/Generators/Prime.php index 162f9ad..760715f 100644 --- a/src/Generators/Prime.php +++ b/src/Generators/Prime.php @@ -11,10 +11,7 @@ class Prime extends PrimeIterator implements GeneratorInterface { /** - * Alias of the get() method. - * - * @return \Generator - * The prime generator + * {@inheritdoc} */ public function generator() { diff --git a/src/Generators/PrimeFactors.php b/src/Generators/PrimeFactors.php index 3b34ff7..46d33db 100644 --- a/src/Generators/PrimeFactors.php +++ b/src/Generators/PrimeFactors.php @@ -11,10 +11,7 @@ class PrimeFactors extends PrimeFactorsIterator implements GeneratorInterface { /** - * Alias of the get() method. - * - * @return \Generator - * The prime factors generator + * {@inheritdoc} */ public function generator() { diff --git a/src/Generators/Product.php b/src/Generators/Product.php index 80f524a..b1c7e57 100644 --- a/src/Generators/Product.php +++ b/src/Generators/Product.php @@ -11,10 +11,7 @@ class Product extends ProductIterator implements GeneratorInterface { /** - * Get the generator. - * - * @return \Generator - * The generator + * {@inheritdoc} */ public function generator() { diff --git a/src/Generators/Shift.php b/src/Generators/Shift.php index 3df3931..1c9aee9 100644 --- a/src/Generators/Shift.php +++ b/src/Generators/Shift.php @@ -11,10 +11,7 @@ class Shift extends ShiftIterator implements GeneratorInterface { /** - * Get the generator. - * - * @return \Generator - * The generator + * {@inheritdoc} */ public function generator() { From d4647c9672a8d5366bb60d0cb8101eaaaa316a14 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Fri, 20 Apr 2018 10:44:47 +0200 Subject: [PATCH 3/5] Move methods count() and toArray() into the parent class. --- src/Combinatorics.php | 36 ++++++++++++++++++++++++++++++++++ src/Generators/Fibonacci.php | 7 ------- src/Iterators/Combinations.php | 25 ----------------------- src/Iterators/Cycle.php | 8 -------- src/Iterators/Fibonacci.php | 33 ------------------------------- src/Iterators/FiniteGroup.php | 25 ----------------------- src/Iterators/Perfect.php | 36 ---------------------------------- src/Iterators/Prime.php | 36 ---------------------------------- src/Iterators/PrimeFactors.php | 25 ----------------------- src/Iterators/Product.php | 8 -------- src/Iterators/Rotation.php | 8 -------- src/Iterators/Shift.php | 8 -------- 12 files changed, 36 insertions(+), 219 deletions(-) diff --git a/src/Combinatorics.php b/src/Combinatorics.php index 1ab62e8..7414beb 100644 --- a/src/Combinatorics.php +++ b/src/Combinatorics.php @@ -96,6 +96,42 @@ public function getDataset() return $this->dataset; } + /** + * Count elements of an object. + * + * @return int + * The number of element + */ + public function count() + { + return count($this->toArray()); + } + + /** + * Convert the iterator into an array. + * + * @return array + * The elements + */ + public function toArray() + { + $data = []; + + for ($this->rewind(); $this->valid(); $this->next()) { + $data[] = $this->current(); + } + + return $data; + } + + /** + * {@inheritdoc} + */ + public function key() + { + return $this->key; + } + /** * Compute the factorial of an integer. * diff --git a/src/Generators/Fibonacci.php b/src/Generators/Fibonacci.php index 2686cf4..2dba1fb 100644 --- a/src/Generators/Fibonacci.php +++ b/src/Generators/Fibonacci.php @@ -10,13 +10,6 @@ */ class Fibonacci extends FibonacciIterator implements GeneratorInterface { - /** - * The maximum value. - * - * @var int - */ - protected $max; - /** * {@inheritdoc} */ diff --git a/src/Iterators/Combinations.php b/src/Iterators/Combinations.php index 8790de4..45fb248 100644 --- a/src/Iterators/Combinations.php +++ b/src/Iterators/Combinations.php @@ -38,14 +38,6 @@ public function __construct(array $dataset = [], $length = null) $this->rewind(); } - /** - * {@inheritdoc} - */ - public function key() - { - return $this->key; - } - /** * {@inheritdoc} */ @@ -88,23 +80,6 @@ public function valid() return $this->key >= 0; } - /** - * Convert the iterator into an array. - * - * @return array - * The elements - */ - public function toArray() - { - $data = []; - - for ($this->rewind(); $this->valid(); $this->next()) { - $data[] = $this->current(); - } - - return $data; - } - /** * {@inheritdoc} */ diff --git a/src/Iterators/Cycle.php b/src/Iterators/Cycle.php index f103479..cb9f93b 100644 --- a/src/Iterators/Cycle.php +++ b/src/Iterators/Cycle.php @@ -17,14 +17,6 @@ class Cycle extends Combinatorics implements IteratorInterface */ protected $key = 0; - /** - * {@inheritdoc} - */ - public function key() - { - return $this->key; - } - /** * {@inheritdoc} */ diff --git a/src/Iterators/Fibonacci.php b/src/Iterators/Fibonacci.php index 2b784ea..14c2559 100644 --- a/src/Iterators/Fibonacci.php +++ b/src/Iterators/Fibonacci.php @@ -54,14 +54,6 @@ public function current() return $this->current; } - /** - * {@inheritdoc} - */ - public function key() - { - return $this->key; - } - /** * {@inheritdoc} */ @@ -89,31 +81,6 @@ public function valid() return $this->current < $this->getMaxLimit(); } - /** - * {@inheritdoc} - */ - public function count() - { - return count($this->toArray()); - } - - /** - * Convert the iterator into an array. - * - * @return array - * The elements - */ - public function toArray() - { - $data = []; - - for ($this->rewind(); $this->valid(); $this->next()) { - $data[] = $this->current(); - } - - return $data; - } - /** * Set the maximum limit. * diff --git a/src/Iterators/FiniteGroup.php b/src/Iterators/FiniteGroup.php index 01fc909..442bbd7 100644 --- a/src/Iterators/FiniteGroup.php +++ b/src/Iterators/FiniteGroup.php @@ -58,14 +58,6 @@ public function next() next($this->group); } - /** - * {@inheritdoc} - */ - public function key() - { - return $this->key; - } - /** * {@inheritdoc} */ @@ -93,23 +85,6 @@ public function count() return count($this->group); } - /** - * Convert the iterator into an array. - * - * @return array - * The elements - */ - public function toArray() - { - $data = []; - - for ($this->rewind(); $this->valid(); $this->next()) { - $data[] = $this->current(); - } - - return $data; - } - /** * Set the group size. * diff --git a/src/Iterators/Perfect.php b/src/Iterators/Perfect.php index e0d10c4..a3acc6c 100644 --- a/src/Iterators/Perfect.php +++ b/src/Iterators/Perfect.php @@ -64,14 +64,6 @@ public function next() ++$this->key; } - /** - * {@inheritdoc} - */ - public function key() - { - return $this->key; - } - /** * {@inheritdoc} */ @@ -88,34 +80,6 @@ public function rewind() $this->key = $this->getMinLimit(); } - /** - * Count elements of an object. - * - * @return int - * The number of element - */ - public function count() - { - return count($this->toArray()); - } - - /** - * Convert the iterator into an array. - * - * @return array - * The elements - */ - public function toArray() - { - $data = []; - - for ($this->rewind(); $this->valid(); $this->next()) { - $data[] = $this->current(); - } - - return $data; - } - /** * Set the maximum limit. * diff --git a/src/Iterators/Prime.php b/src/Iterators/Prime.php index 99fc64d..2dce498 100644 --- a/src/Iterators/Prime.php +++ b/src/Iterators/Prime.php @@ -64,14 +64,6 @@ public function next() ++$this->key; } - /** - * {@inheritdoc} - */ - public function key() - { - return $this->key; - } - /** * {@inheritdoc} */ @@ -88,34 +80,6 @@ public function rewind() $this->key = $this->getMinLimit(); } - /** - * Count elements of an object. - * - * @return int - * The number of element - */ - public function count() - { - return count($this->toArray()); - } - - /** - * Convert the iterator into an array. - * - * @return array - * The elements - */ - public function toArray() - { - $data = []; - - for ($this->rewind(); $this->valid(); $this->next()) { - $data[] = $this->current(); - } - - return $data; - } - /** * Set the maximum limit. * diff --git a/src/Iterators/PrimeFactors.php b/src/Iterators/PrimeFactors.php index 6131f75..8ec2aa0 100644 --- a/src/Iterators/PrimeFactors.php +++ b/src/Iterators/PrimeFactors.php @@ -48,14 +48,6 @@ public function next() next($this->factors); } - /** - * {@inheritdoc} - */ - public function key() - { - return $this->key; - } - /** * {@inheritdoc} */ @@ -83,23 +75,6 @@ public function count() return count($this->factors); } - /** - * Convert the iterator into an array. - * - * @return array - * The elements - */ - public function toArray() - { - $data = []; - - for ($this->rewind(); $this->valid(); $this->next()) { - $data[] = $this->current(); - } - - return $data; - } - /** * Set the number. * diff --git a/src/Iterators/Product.php b/src/Iterators/Product.php index 3c0f55a..419e331 100644 --- a/src/Iterators/Product.php +++ b/src/Iterators/Product.php @@ -77,14 +77,6 @@ public function next() ++$this->key; } - /** - * {@inheritdoc} - */ - public function key() - { - return $this->key; - } - /** * {@inheritdoc} */ diff --git a/src/Iterators/Rotation.php b/src/Iterators/Rotation.php index ea0c23d..80e804a 100644 --- a/src/Iterators/Rotation.php +++ b/src/Iterators/Rotation.php @@ -36,14 +36,6 @@ public function __construct(array $dataset = []) $this->rotation = $this->getDataset(); } - /** - * {@inheritdoc} - */ - public function key() - { - return $this->key; - } - /** * {@inheritdoc} */ diff --git a/src/Iterators/Shift.php b/src/Iterators/Shift.php index d1cff11..5433d99 100644 --- a/src/Iterators/Shift.php +++ b/src/Iterators/Shift.php @@ -49,14 +49,6 @@ public function setLength($length = null) return $this; } - /** - * {@inheritdoc} - */ - public function key() - { - return $this->key; - } - /** * {@inheritdoc} */ From 35f451fa92e926cafe8267ce30316e2410719464 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Fri, 20 Apr 2018 10:45:42 +0200 Subject: [PATCH 4/5] Small code cleanup. --- src/Combinatorics.php | 2 +- src/Iterators/Combinations.php | 3 +++ src/Iterators/Fibonacci.php | 3 ++- src/Iterators/FiniteGroup.php | 4 ++-- src/Iterators/Perfect.php | 3 ++- src/Iterators/Prime.php | 5 +++-- src/Iterators/PrimeFactors.php | 6 ++---- src/Iterators/Rotation.php | 2 +- src/Iterators/Shift.php | 8 ++++---- 9 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Combinatorics.php b/src/Combinatorics.php index 7414beb..aad06ff 100644 --- a/src/Combinatorics.php +++ b/src/Combinatorics.php @@ -53,7 +53,7 @@ public function __construct(array $dataset = [], $length = null) */ public function setLength($length = null) { - $length = is_null($length) ? $this->datasetCount : $length; + $length = (null === $length) ? $this->datasetCount : $length; $this->length = ($length > $this->datasetCount) ? $this->datasetCount : $length; return $this; diff --git a/src/Iterators/Combinations.php b/src/Iterators/Combinations.php index 45fb248..2c8274d 100644 --- a/src/Iterators/Combinations.php +++ b/src/Iterators/Combinations.php @@ -44,6 +44,7 @@ public function __construct(array $dataset = [], $length = null) public function current() { $r = []; + for ($i = 0; $i < $this->length; ++$i) { $r[] = $this->dataset[$this->c[$i]]; } @@ -101,9 +102,11 @@ protected function nextHelper() while ($i >= 0 && $this->c[$i] === $this->datasetCount - $this->length + $i) { --$i; } + if ($i < 0) { return false; } + ++$this->c[$i]; while ($i++ < $this->length - 1) { $this->c[$i] = $this->c[$i - 1] + 1; diff --git a/src/Iterators/Fibonacci.php b/src/Iterators/Fibonacci.php index 14c2559..cab3213 100644 --- a/src/Iterators/Fibonacci.php +++ b/src/Iterators/Fibonacci.php @@ -44,6 +44,7 @@ class Fibonacci extends Combinatorics implements IteratorInterface public function __construct() { $this->setMaxLimit(PHP_INT_MAX); + parent::__construct([], null); } /** @@ -100,6 +101,6 @@ public function setMaxLimit($max) */ public function getMaxLimit() { - return intval($this->max); + return (int) $this->max; } } diff --git a/src/Iterators/FiniteGroup.php b/src/Iterators/FiniteGroup.php index 442bbd7..f6c404d 100644 --- a/src/Iterators/FiniteGroup.php +++ b/src/Iterators/FiniteGroup.php @@ -105,7 +105,7 @@ public function setSize($size) */ public function getSize() { - return intval($this->size); + return (int) $this->size; } /** @@ -122,7 +122,7 @@ public function order($generator) $result = []; foreach (range(1, $this->getSize() - 1) as $number) { - $value = pow($generator, $number) % $this->getSize(); + $value = ($generator ** $number) % $this->getSize(); $result[$value] = $value; } diff --git a/src/Iterators/Perfect.php b/src/Iterators/Perfect.php index a3acc6c..650fda3 100644 --- a/src/Iterators/Perfect.php +++ b/src/Iterators/Perfect.php @@ -38,6 +38,7 @@ public function __construct() { $this->setMaxLimit(PHP_INT_MAX); $this->setMinLimit(2); + parent::__construct([], null); } /** @@ -99,7 +100,7 @@ public function setMaxLimit($max) */ public function getMaxLimit() { - return intval($this->max); + return (int) $this->max; } /** diff --git a/src/Iterators/Prime.php b/src/Iterators/Prime.php index 2dce498..a5089c7 100644 --- a/src/Iterators/Prime.php +++ b/src/Iterators/Prime.php @@ -38,6 +38,7 @@ public function __construct() { $this->setMaxLimit(PHP_INT_MAX); $this->setMinLimit(0); + parent::__construct([], null); } /** @@ -99,7 +100,7 @@ public function setMaxLimit($max) */ public function getMaxLimit() { - return intval($this->max); + return (int) $this->max; } /** @@ -121,7 +122,7 @@ public function setMinLimit($min) */ public function getMinLimit() { - return $this->min <= 2 ? 2 : intval($this->min); + return $this->min <= 2 ? 2 : (int) $this->min; } /** diff --git a/src/Iterators/PrimeFactors.php b/src/Iterators/PrimeFactors.php index 8ec2aa0..fe73114 100644 --- a/src/Iterators/PrimeFactors.php +++ b/src/Iterators/PrimeFactors.php @@ -95,7 +95,7 @@ public function setNumber($number) */ public function getNumber() { - return intval($this->number); + return (int) $this->number; } /** @@ -108,9 +108,7 @@ public function getNumber() */ private function getFactors($number) { - if ($number <= 0) { - $factors = []; - } + $factors = []; for ($i = 2; $i <= $number / $i; ++$i) { while (0 === $number % $i) { diff --git a/src/Iterators/Rotation.php b/src/Iterators/Rotation.php index 80e804a..f355536 100644 --- a/src/Iterators/Rotation.php +++ b/src/Iterators/Rotation.php @@ -52,7 +52,7 @@ public function current() */ public function next($offset = 1) { - $offset = is_null($offset) ? 1 : $offset % $this->count(); + $offset = (null === $offset) ? 1 : $offset % $this->count(); $this->rotation = array_merge(array_slice($this->rotation, $offset), array_slice($this->rotation, 0, $offset)); } diff --git a/src/Iterators/Shift.php b/src/Iterators/Shift.php index 5433d99..b9a9d8d 100644 --- a/src/Iterators/Shift.php +++ b/src/Iterators/Shift.php @@ -43,7 +43,7 @@ public function __construct(array $dataset = [], $length = 1) */ public function setLength($length = null) { - $length = is_null($length) ? $this->datasetCount : $length; + $length = (null === $length) ? $this->datasetCount : $length; $this->length = (abs($length) > $this->datasetCount) ? $this->datasetCount : $length; return $this; @@ -107,8 +107,8 @@ protected function doShift($length = 1) } $this->current = array_merge( - array_slice($this->current, $parameters[0]['start'], $parameters[0]['end']), - array_slice($this->current, $parameters[1]['start'], $parameters[1]['end']) - ); + array_slice($this->current, $parameters[0]['start'], $parameters[0]['end']), + array_slice($this->current, $parameters[1]['start'], $parameters[1]['end']) + ); } } From 280927644bd92053058cf5c01cf3e72086bf4248 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Fri, 20 Apr 2018 10:46:02 +0200 Subject: [PATCH 5/5] Add the NGrams generator and its tests. --- src/Generators/NGrams.php | 45 ++++++++++++++++++++++++++++++ tests/src/Iterators/NgramsTest.php | 10 +++---- 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/Generators/NGrams.php diff --git a/src/Generators/NGrams.php b/src/Generators/NGrams.php new file mode 100644 index 0000000..d088154 --- /dev/null +++ b/src/Generators/NGrams.php @@ -0,0 +1,45 @@ +get(); + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + return []; + } + + /** + * Get the generator. + * + * @codingStandardsIgnoreStart + * + * @return \Generator + * The generator + * @codingStandardsIgnoreEnd + */ + protected function get() + { + while (true) { + $this->next(); + yield $this->current(); + } + } +} diff --git a/tests/src/Iterators/NgramsTest.php b/tests/src/Iterators/NgramsTest.php index 534d148..fe81c3b 100644 --- a/tests/src/Iterators/NgramsTest.php +++ b/tests/src/Iterators/NgramsTest.php @@ -25,14 +25,14 @@ class NgramsTest extends AbstractTest */ public function testNgrams($input, $expected) { - $shift = new NGrams($input['dataset'], $input['length']); + $ngrams = new NGrams($input['dataset'], $input['length']); for ($i = 0; $i < $input['turn']; ++$i) { - $shift->next(); + $ngrams->next(); } - $this->assertEquals($expected['current'], $shift->current()); + $this->assertEquals($expected['current'], $ngrams->current()); - $this->assertEquals($input['dataset'], $shift->getDataset()); - $this->assertEquals($expected['count'], $shift->count()); + $this->assertEquals($input['dataset'], $ngrams->getDataset()); + $this->assertEquals($expected['count'], $ngrams->count()); } }