From dbaec438fccc6fe1258691718323bd8114d99bd1 Mon Sep 17 00:00:00 2001 From: Bartosz Kubicki Date: Wed, 12 May 2021 22:56:02 +0200 Subject: [PATCH] Replacing array with iterable + minor fixes --- CHANGELOG.md | 8 +++++ README.md | 4 +++ composer.json | 2 +- src/ObjectArray.php | 23 +++++++------- src/ObjectArrayFactory.php | 4 +-- tests/Unit/ObjectArrayFactoryTest.php | 7 ++-- tests/Unit/ObjectArrayTest.php | 46 ++++++++++++++++++++------- 7 files changed, 63 insertions(+), 31 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..4d38a64 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ +1.0.0 +* Initial version of module + +1.0.1 +* Adding missing php doc (important in frameworks basing on annotations) + +2.0.0 +* allowing to create `ObjectArray` from iterators (iterable psuedo-type - https://www.php.net/manual/en/language.types.iterable.php) \ No newline at end of file diff --git a/README.md b/README.md index 7ff8e34..5227110 100755 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ Use composer to install the library composer require mslwk/php-typesafe-array ``` +## Changelog + +See changelog [here](CHANGELOG.md). + ## Authors * [Maciej Sławik](https://github.com/maciejslawik) diff --git a/composer.json b/composer.json index e1f0652..38d1496 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "license": [ "MIT" ], - "version": "1.0.1", + "version": "2.0.0", "authors": [ { "name": "Maciej Sławik", diff --git a/src/ObjectArray.php b/src/ObjectArray.php index 2d31840..73828b9 100644 --- a/src/ObjectArray.php +++ b/src/ObjectArray.php @@ -15,6 +15,7 @@ use Countable; use InvalidArgumentException; use Iterator; +use function get_class; /** * Class ObjectArray @@ -35,10 +36,10 @@ class ObjectArray implements Countable, ArrayAccess, Iterator /** * ObjectArray constructor. * @param string $type - * @param array $objects + * @param iterable $objects * @throws InvalidArgumentException */ - public function __construct(string $type, array $objects = []) + public function __construct(string $type, iterable $objects = []) { $this->type = $type; foreach ($objects as $object) { @@ -52,7 +53,7 @@ public function __construct(string $type, array $objects = []) * @return void * @throws InvalidArgumentException */ - public function add($value) + public function add($value): void { $this->validateObjectType($value); $this->iterator->append($value); @@ -62,7 +63,7 @@ public function add($value) * @param mixed $offset * @return bool */ - public function offsetExists($offset) + public function offsetExists($offset): bool { return $this->iterator->offsetExists($offset); } @@ -82,7 +83,7 @@ public function offsetGet($offset) * @return void * @throws InvalidArgumentException */ - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { $this->validateObjectType($value); $this->iterator->offsetSet($offset, $value); @@ -92,7 +93,7 @@ public function offsetSet($offset, $value) * @param mixed $offset * @return void */ - public function offsetUnset($offset) + public function offsetUnset($offset): void { $this->iterator->offsetUnset($offset); } @@ -100,7 +101,7 @@ public function offsetUnset($offset) /** * @return int */ - public function count() + public function count(): int { return $this->iterator->count(); } @@ -116,7 +117,7 @@ public function current() /** * @return void */ - public function next() + public function next(): void { $this->iterator->next(); } @@ -132,7 +133,7 @@ public function key() /** * @return bool */ - public function valid() + public function valid(): bool { return $this->iterator->valid(); } @@ -140,7 +141,7 @@ public function valid() /** * @return void */ - public function rewind() + public function rewind(): void { $this->iterator->rewind(); } @@ -150,7 +151,7 @@ public function rewind() * @return void * @throws InvalidArgumentException */ - private function validateObjectType($object) + private function validateObjectType($object): void { if (!$object instanceof $this->type) { throw new InvalidArgumentException( diff --git a/src/ObjectArrayFactory.php b/src/ObjectArrayFactory.php index 6ee60d9..b89cecd 100644 --- a/src/ObjectArrayFactory.php +++ b/src/ObjectArrayFactory.php @@ -20,11 +20,11 @@ class ObjectArrayFactory { /** * @param string $type - * @param array $objects + * @param iterable $objects * @return ObjectArray * @throws InvalidArgumentException */ - public function create(string $type, array $objects = []): ObjectArray + public function create(string $type, iterable $objects = []): ObjectArray { return new ObjectArray($type, $objects); } diff --git a/tests/Unit/ObjectArrayFactoryTest.php b/tests/Unit/ObjectArrayFactoryTest.php index cd96487..485ac6b 100644 --- a/tests/Unit/ObjectArrayFactoryTest.php +++ b/tests/Unit/ObjectArrayFactoryTest.php @@ -22,16 +22,13 @@ class ObjectArrayFactoryTest extends TestCase { /** - * @test + * @return void */ - public function testFactoryReturnsCorrectClass() + public function testFactoryReturnsCorrectClass(): void { $factory = new ObjectArrayFactory(); - $expected = ObjectArray::class; - $arrayClass = $factory->create(DateTime::class); - $this->assertInstanceOf($expected, $arrayClass); } } diff --git a/tests/Unit/ObjectArrayTest.php b/tests/Unit/ObjectArrayTest.php index 7dde2fa..43438d8 100644 --- a/tests/Unit/ObjectArrayTest.php +++ b/tests/Unit/ObjectArrayTest.php @@ -10,6 +10,7 @@ namespace MSlwk\TypeSafeArray\Test\Unit; +use ArrayIterator; use DateTime; use DateTimeImmutable; use InvalidArgumentException; @@ -23,15 +24,18 @@ class ObjectArrayTest extends TestCase { /** - * @test + * @dataProvider provideDifferentIterables + * @param iterable $iterable + * @return void */ - public function testOperationsOnArray() + public function testOperationsOnArray(iterable $iterable): void { $object1 = new DateTime(); $object2 = new DateTime(); $object3 = new DateTime(); $object4 = new DateTime(); - $objectArray = new ObjectArray(DateTime::class, [$object2]); + $iterable[] = $object2; + $objectArray = new ObjectArray(DateTime::class, $iterable); $objectArray[1] = $object3; $objectArray->offsetSet(2, $object4); $objectArray->add($object1); @@ -63,20 +67,23 @@ public function testOperationsOnArray() } /** - * @test + * @dataProvider provideDifferentIterables + * @param iterable $iterable + * @return void */ - public function testAddingWrongTypeViaConstructor() + public function testAddingWrongTypeViaConstructor(iterable $iterable): void { $this->expectException(InvalidArgumentException::class); $object = new DateTimeImmutable(); - $objectArray = new ObjectArray(DateTime::class, [$object]); + $iterable[] = $object; + new ObjectArray(DateTime::class, $iterable); } /** - * @test + * @return void */ - public function testAddingWrongTypeViaOffsetSet() + public function testAddingWrongTypeViaOffsetSet(): void { $this->expectException(InvalidArgumentException::class); @@ -86,9 +93,9 @@ public function testAddingWrongTypeViaOffsetSet() } /** - * @test + * @return void */ - public function testAddingWrongTypeViaAdd() + public function testAddingWrongTypeViaAdd(): void { $this->expectException(InvalidArgumentException::class); @@ -98,9 +105,9 @@ public function testAddingWrongTypeViaAdd() } /** - * @test + * @return void */ - public function testAddingWrongTypeViaArrayAccess() + public function testAddingWrongTypeViaArrayAccess(): void { $this->expectException(InvalidArgumentException::class); @@ -108,4 +115,19 @@ public function testAddingWrongTypeViaArrayAccess() $objectArray = new ObjectArray(DateTime::class); $objectArray[0] = $object; } + + /** + * @return array + */ + public function provideDifferentIterables(): array + { + return [ + 'plain_array' => [ + [] + ], + 'iterator' => [ + new ArrayIterator() + ] + ]; + } }