diff --git a/.gitignore b/.gitignore index 3e4ac79..9e9e092 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ vendor/ .DS_Store composer.lock phpunit.xml +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1122286..0000000 --- a/.travis.yml +++ /dev/null @@ -1,25 +0,0 @@ -language: php - -php: - - 5.3 - - 5.4 - - 5.5 - - 5.6 - - 7.0 - - hhvm - -matrix: - allow_failures: - - php: 7.0 - include: - - php: 5.3 - env: - - COMPOSER_FLAGS="--prefer-stable --prefer-lowest" - - PHPUNIT_FLAGS="-c phpunit.xml.ci" - - COVERAGE=true - -install: - - travis_retry composer self-update - - travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction - -script: vendor/bin/phpunit ${PHPUNIT_FLAGS} diff --git a/README.md b/README.md index 37432fa..4d03575 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ without affecting the memory footprint. Via Composer ``` bash -$ composer require th3n3rd/cartesian-product +$ composer require "th3n3rd/cartesian-product" ``` ## Usage @@ -28,18 +28,18 @@ use Nerd\CartesianProduct\CartesianProduct; $cartesianProduct = new CartesianProduct(); $cartesianProduct - ->appendSet(array('a', 'b', 'c')) - ->appendSet(array('d', 'e')) - ->appendSet(array('f', 'g', 'h')) - ->appendSet(array('i', 'j')) - ->appendSet(array('k', 'l')) - ->appendSet(array('m', 'n')) - ->appendSet(array('o')) - ->appendSet(array('p')) - ->appendSet(array('q', 'r', 's', 't')) - ->appendSet(array('u', 'v', 'w')) - ->appendSet(array('x', 'y')) - ->appendSet(array('z')) + ->appendSet(['a', 'b', 'c']) + ->appendSet(['d', 'e']) + ->appendSet(['f', 'g', 'h']) + ->appendSet(['i', 'j']) + ->appendSet(['k', 'l']) + ->appendSet(['m', 'n']) + ->appendSet(['o']) + ->appendSet(['p']) + ->appendSet(['q', 'r', 's', 't']) + ->appendSet(['u', 'v', 'w']) + ->appendSet(['x', 'y']) + ->appendSet(['z']) ; foreach ($cartesianProduct as $index => $product) { @@ -58,7 +58,7 @@ foreach ($result as $index => $product) { ## Testing ``` bash -$ phpunit +$ vendor/bin/phpunit ``` ## License diff --git a/composer.json b/composer.json index 31ed908..4ff6801 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,11 @@ } ], "require": { - "php": ">=5.3.3" + "php": ">=8.0" }, "require-dev": { - "devster/ubench": "~1.1", - "phpunit/phpunit": "~4.4" + "devster/ubench": "^2.1", + "phpunit/phpunit": "^9.6.17" }, "autoload": { "psr-4": { diff --git a/examples/iterator.php b/examples/iterator.php index e8f7a43..a1b2644 100644 --- a/examples/iterator.php +++ b/examples/iterator.php @@ -17,18 +17,18 @@ $cartesianProduct = new CartesianProduct(); $cartesianProduct - ->appendSet(array('a', 'b', 'c')) - ->appendSet(array('d', 'e')) - ->appendSet(array('f', 'g', 'h')) - ->appendSet(array('i', 'j')) - ->appendSet(array('k', 'l')) - ->appendSet(array('m', 'n')) - ->appendSet(array('o')) - ->appendSet(array('p')) - ->appendSet(array('q', 'r', 's', 't')) - ->appendSet(array('u', 'v', 'w')) - ->appendSet(array('x', 'y')) - ->appendSet(array('z')) + ->appendSet(['a', 'b', 'c']) + ->appendSet(['d', 'e']) + ->appendSet(['f', 'g', 'h']) + ->appendSet(['i', 'j']) + ->appendSet(['k', 'l']) + ->appendSet(['m', 'n']) + ->appendSet(['o']) + ->appendSet(['p']) + ->appendSet(['q', 'r', 's', 't']) + ->appendSet(['u', 'v', 'w']) + ->appendSet(['x', 'y']) + ->appendSet(['z']) ; $bench->start(); @@ -38,4 +38,4 @@ $bench->end(); printf("Time elapsed: %s\n", $bench->getTime()); -printf("Memory footprint: %s\n", $bench->getMemoryPeak()); \ No newline at end of file +printf("Memory footprint: %s\n", $bench->getMemoryPeak()); diff --git a/examples/memory-usage-comparison.php b/examples/memory-usage-comparison.php index 3bdcbb8..7bffe8b 100644 --- a/examples/memory-usage-comparison.php +++ b/examples/memory-usage-comparison.php @@ -17,18 +17,18 @@ $cartesianProduct = new CartesianProduct(); $cartesianProduct - ->appendSet(array('a', 'b', 'c')) - ->appendSet(array('d', 'e')) - ->appendSet(array('f', 'g', 'h')) - ->appendSet(array('i', 'j')) - ->appendSet(array('k', 'l')) - ->appendSet(array('m', 'n')) - ->appendSet(array('o')) - ->appendSet(array('p')) - ->appendSet(array('q', 'r', 's', 't')) - ->appendSet(array('u', 'v', 'w')) - ->appendSet(array('x', 'y')) - ->appendSet(array('z')) + ->appendSet(['a', 'b', 'c']) + ->appendSet(['d', 'e']) + ->appendSet(['f', 'g', 'h']) + ->appendSet(['i', 'j']) + ->appendSet(['k', 'l']) + ->appendSet(['m', 'n']) + ->appendSet(['o']) + ->appendSet(['p']) + ->appendSet(['q', 'r', 's', 't']) + ->appendSet(['u', 'v', 'w']) + ->appendSet(['x', 'y']) + ->appendSet(['z']) ; $bench->start(); @@ -52,4 +52,4 @@ printf("Memory Usage Comparison: \n"); printf("Whole -> %s\n", $wholeUseCase); printf("Iterator -> %s\n", $iteratorUseCase); -printf("Ratio: 1:%s (whole vs iterator) \n", ceil($iteratorUseCaseBytes / $wholeUseCaseBytes * 100)); \ No newline at end of file +printf("Ratio: 1:%s (whole vs iterator) \n", ceil($iteratorUseCaseBytes / $wholeUseCaseBytes * 100)); diff --git a/examples/whole-result.php b/examples/whole-result.php index d70b028..d069ebb 100644 --- a/examples/whole-result.php +++ b/examples/whole-result.php @@ -17,18 +17,18 @@ $cartesianProduct = new CartesianProduct(); $cartesianProduct - ->appendSet(array('a', 'b', 'c')) - ->appendSet(array('d', 'e')) - ->appendSet(array('f', 'g', 'h')) - ->appendSet(array('i', 'j')) - ->appendSet(array('k', 'l')) - ->appendSet(array('m', 'n')) - ->appendSet(array('o')) - ->appendSet(array('p')) - ->appendSet(array('q', 'r', 's', 't')) - ->appendSet(array('u', 'v', 'w')) - ->appendSet(array('x', 'y')) - ->appendSet(array('z')) + ->appendSet(['a', 'b', 'c']) + ->appendSet(['d', 'e']) + ->appendSet(['f', 'g', 'h']) + ->appendSet(['i', 'j']) + ->appendSet(['k', 'l']) + ->appendSet(['m', 'n']) + ->appendSet(['o']) + ->appendSet(['p']) + ->appendSet(['q', 'r', 's', 't']) + ->appendSet(['u', 'v', 'w']) + ->appendSet(['x', 'y']) + ->appendSet(['z']) ; $bench->start(); @@ -39,4 +39,4 @@ $bench->end(); printf("Time elapsed: %s\n", $bench->getTime()); -printf("Memory footprint: %s\n", $bench->getMemoryPeak()); \ No newline at end of file +printf("Memory footprint: %s\n", $bench->getMemoryPeak()); diff --git a/phpunit.xml.ci b/phpunit.xml.ci deleted file mode 100644 index a42ac43..0000000 --- a/phpunit.xml.ci +++ /dev/null @@ -1,17 +0,0 @@ - - - - - tests/ - - - - - src/ - - - - - - - diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 63da1e0..7cc8f42 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,19 @@ - - - - tests/ - - - - - src/ - - + + + + src/ + + + + + + tests/ + + + diff --git a/src/CartesianProduct.php b/src/CartesianProduct.php index 05d8725..3d0f584 100644 --- a/src/CartesianProduct.php +++ b/src/CartesianProduct.php @@ -16,25 +16,12 @@ */ class CartesianProduct implements \Iterator { - /** - * @var array - */ - private $sets = array(); + private array $sets = []; + private \Iterator $referenceSet; - /** - * @var \Iterator - */ - private $referenceSet; + private int $cursor = 0; - /** - * @var integer - */ - private $cursor = 0; - - /** - * @param array $sets - */ - public function __construct(array $sets = array()) + public function __construct(array $sets = []) { foreach ($sets as $set) { $this->addSet($set); @@ -46,11 +33,9 @@ public function __construct(array $sets = array()) /** * Adds a set. * - * @param array|Traversable $set - * * @throws \InvalidArgumentException */ - private function addSet($set) + private function addSet(iterable $set): void { if (is_array($set)) { $set = new \ArrayIterator($set); @@ -66,13 +51,11 @@ private function addSet($set) /** * Appends the given set. * - * @param array|Traversable $set - * * @return $this * * @throws \InvalidArgumentException */ - public function appendSet($set) + public function appendSet(iterable $set): static { $this->addSet($set); $this->computeReferenceSet(); @@ -83,7 +66,7 @@ public function appendSet($set) /** * Computes the reference set used for iterate over the product. */ - private function computeReferenceSet() + private function computeReferenceSet(): void { if (empty($this->sets)) { return; @@ -100,7 +83,7 @@ private function computeReferenceSet() /** * {@inheritdoc} */ - public function current() + public function current(): mixed { $current = $this->referenceSet->current(); @@ -110,7 +93,7 @@ public function current() /** * {@inheritdoc} */ - public function next() + public function next(): void { $this->cursor++; $this->referenceSet->next(); @@ -119,7 +102,7 @@ public function next() /** * {@inheritdoc} */ - public function key() + public function key(): mixed { return $this->cursor; } @@ -127,7 +110,7 @@ public function key() /** * {@inheritdoc} */ - public function valid() + public function valid(): bool { return $this->referenceSet->valid(); } @@ -135,7 +118,7 @@ public function valid() /** * {@inheritdoc} */ - public function rewind() + public function rewind(): void { $this->cursor = 0; $this->referenceSet->rewind(); @@ -149,9 +132,9 @@ public function rewind() * The recommended way to use the Cartesian Product is through its iterator interface * which is memory efficient. */ - public function compute() + public function compute(): array { - $product = array(); + $product = []; $this->referenceSet->rewind(); diff --git a/src/Set.php b/src/Set.php index 74c09e5..90a85ce 100644 --- a/src/Set.php +++ b/src/Set.php @@ -16,19 +16,8 @@ */ class Set extends \IteratorIterator { - /** - * @var \Iterator - */ - private $neighbour; - - /** - * @param \Iterator $set - * @param \Iterator $neighbour - */ - public function __construct(\Iterator $set, \Iterator $neighbour) + public function __construct(\Iterator $set, private \Iterator $neighbour) { - $this->neighbour = $neighbour; - parent::__construct($set); parent::rewind(); } @@ -36,12 +25,12 @@ public function __construct(\Iterator $set, \Iterator $neighbour) /** * {@inheritdoc} */ - public function current() + public function current(): mixed { $neighbourCurrent = $this->neighbour->current(); $current = parent::current(); - if (!$this->neighbour instanceof Set) { + if (!$this->neighbour instanceof self) { $neighbourCurrent = array($neighbourCurrent); } @@ -53,7 +42,7 @@ public function current() /** * {@inheritdoc} */ - public function next() + public function next(): void { $this->neighbour->next(); @@ -66,7 +55,7 @@ public function next() /** * {@inheritdoc} */ - public function rewind() + public function rewind(): void { $this->neighbour->rewind(); parent::rewind(); diff --git a/tests/CartesianProductTest.php b/tests/CartesianProductTest.php index ba03e61..309a44e 100644 --- a/tests/CartesianProductTest.php +++ b/tests/CartesianProductTest.php @@ -11,32 +11,28 @@ namespace Nerd\CartesianProduct; +use PHPUnit\Framework\TestCase; + /** * @author Marco Garofalo */ -class CartesianProductTest extends \PHPUnit_Framework_TestCase +class CartesianProductTest extends TestCase { - /** - * @var CartesianProduct - */ - private $cartesianProduct; + private CartesianProduct $cartesianProduct; - /** - * @var array - */ - private static $sets = array( - array('a', 'b'), - array('c', 'd') - ); + private static array $sets = [ + ['a', 'b'], + ['c', 'd'] + ]; - public function setUp() + public function setUp(): void { $this->cartesianProduct = new CartesianProduct(self::$sets); } public function testShouldBeAbleToHandleASingleSet() { - $singleSet = array(array('a', 'b')); + $singleSet = [['a', 'b']]; $cartesianProduct = new CartesianProduct($singleSet); $this->assertTrue(is_array($cartesianProduct->current())); @@ -46,15 +42,15 @@ public function testShouldBeAbleToHandleASingleSet() public function testShouldComputeTheCartesianProductIterativelyAndAsWholeCorrectly() { - $expectedProduct = array( - array('a', 'c'), - array('a', 'd'), - array('b', 'c'), - array('b', 'd'), - ); + $expectedProduct = [ + ['a', 'c'], + ['a', 'd'], + ['b', 'c'], + ['b', 'd'], + ]; // using the iterator interface - $actualProductIteratively = array(); + $actualProductIteratively = []; foreach ($this->cartesianProduct as $product) { $actualProductIteratively[] = $product; } diff --git a/tests/SetTest.php b/tests/SetTest.php index 93b9e0a..6ddda2b 100644 --- a/tests/SetTest.php +++ b/tests/SetTest.php @@ -11,15 +11,14 @@ namespace Nerd\CartesianProduct; +use PHPUnit\Framework\TestCase; + /** * @author Marco Garofalo */ -class SetTest extends \PHPUnit_Framework_TestCase +class SetTest extends TestCase { - /** - * @var array - */ - private static $values = array('a', 'b'); + private static array $values = array('a', 'b'); public function testShouldComputeTheCurrentElementForBothArrayAndNonArrayValues() {