From d478bfde3e08f349b6f48f862f0a7ebfe789fe14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 11:36:47 +0100 Subject: [PATCH 1/8] Updated file header comment, added strict_types declaration --- src/ResultSet/Exception/ExceptionInterface.php | 10 +++++----- src/ResultSet/Exception/InvalidArgumentException.php | 10 +++++----- src/ResultSet/Exception/RuntimeException.php | 10 +++++----- src/ResultSet/HydratingResultSet.php | 10 +++++----- src/ResultSet/ResultSet.php | 10 +++++----- src/ResultSet/ResultSetInterface.php | 10 +++++----- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/ResultSet/Exception/ExceptionInterface.php b/src/ResultSet/Exception/ExceptionInterface.php index 646aec4c88..6276bc40f1 100644 --- a/src/ResultSet/Exception/ExceptionInterface.php +++ b/src/ResultSet/Exception/ExceptionInterface.php @@ -1,12 +1,12 @@ Date: Thu, 14 Mar 2019 12:36:13 +0100 Subject: [PATCH 2/8] Declared return types --- src/ResultSet/AbstractResultSet.php | 24 ++++++++++++------------ src/ResultSet/HydratingResultSet.php | 12 ++++++------ src/ResultSet/ResultSet.php | 6 +++--- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/ResultSet/AbstractResultSet.php b/src/ResultSet/AbstractResultSet.php index 3911885e38..1618204263 100644 --- a/src/ResultSet/AbstractResultSet.php +++ b/src/ResultSet/AbstractResultSet.php @@ -54,7 +54,7 @@ abstract class AbstractResultSet implements Iterator, ResultSetInterface * @return self Provides a fluent interface * @throws Exception\InvalidArgumentException */ - public function initialize($dataSource) + public function initialize($dataSource) : self { // reset buffering if (is_array($this->buffer)) { @@ -97,7 +97,7 @@ public function initialize($dataSource) * @return self Provides a fluent interface * @throws Exception\RuntimeException */ - public function buffer() + public function buffer() : self { if ($this->buffer === -2) { throw new Exception\RuntimeException('Buffering must be enabled before iteration is started'); @@ -110,7 +110,7 @@ public function buffer() return $this; } - public function isBuffered() + public function isBuffered() : bool { if ($this->buffer === -1 || is_array($this->buffer)) { return true; @@ -123,12 +123,12 @@ public function isBuffered() * * @return null|Iterator */ - public function getDataSource() + public function getDataSource() : ?Iterator { return $this->dataSource; } - public function getFieldCount(): int + public function getFieldCount() : int { if (null !== $this->fieldCount) { return $this->fieldCount; @@ -160,7 +160,7 @@ public function getFieldCount(): int * * @return void */ - public function next() + public function next() : void { if ($this->buffer === null) { $this->buffer = -2; // implicitly disable buffering from here on @@ -174,9 +174,9 @@ public function next() /** * Iterator: retrieve current key * - * @return mixed + * @return int */ - public function key() + public function key() : int { return $this->position; } @@ -210,7 +210,7 @@ public function current() * * @return bool */ - public function valid() + public function valid() : bool { if (is_array($this->buffer) && isset($this->buffer[$this->position])) { return true; @@ -228,7 +228,7 @@ public function valid() * * @return void */ - public function rewind() + public function rewind() : void { if (! is_array($this->buffer)) { if ($this->dataSource instanceof Iterator) { @@ -245,7 +245,7 @@ public function rewind() * * @return int */ - public function count() + public function count() : int { if ($this->count !== null) { return $this->count; @@ -264,7 +264,7 @@ public function count() * @return array * @throws Exception\RuntimeException if any row is not castable to an array */ - public function toArray() + public function toArray() : array { $return = []; foreach ($this as $row) { diff --git a/src/ResultSet/HydratingResultSet.php b/src/ResultSet/HydratingResultSet.php index 8d6d5e5a8e..d989562a2a 100644 --- a/src/ResultSet/HydratingResultSet.php +++ b/src/ResultSet/HydratingResultSet.php @@ -48,7 +48,7 @@ public function __construct(HydratorInterface $hydrator = null, $objectPrototype * @return self Provides a fluent interface * @throws Exception\InvalidArgumentException */ - public function setObjectPrototype($objectPrototype) + public function setObjectPrototype($objectPrototype) : self { if (! is_object($objectPrototype)) { throw new Exception\InvalidArgumentException( @@ -64,7 +64,7 @@ public function setObjectPrototype($objectPrototype) * * @return object */ - public function getObjectPrototype() + public function getObjectPrototype() : object { return $this->objectPrototype; } @@ -75,7 +75,7 @@ public function getObjectPrototype() * @param HydratorInterface $hydrator * @return self Provides a fluent interface */ - public function setHydrator(HydratorInterface $hydrator) + public function setHydrator(HydratorInterface $hydrator) : self { $this->hydrator = $hydrator; return $this; @@ -86,7 +86,7 @@ public function setHydrator(HydratorInterface $hydrator) * * @return HydratorInterface */ - public function getHydrator() + public function getHydrator() : HydratorInterface { return $this->hydrator; } @@ -94,7 +94,7 @@ public function getHydrator() /** * Iterator: get current item * - * @return object + * @return null|array|bool */ public function current() { @@ -119,7 +119,7 @@ public function current() * @return array * @throws Exception\RuntimeException if any row is not castable to an array */ - public function toArray() + public function toArray() : array { $return = []; foreach ($this as $row) { diff --git a/src/ResultSet/ResultSet.php b/src/ResultSet/ResultSet.php index 1500703452..926ea3b2be 100644 --- a/src/ResultSet/ResultSet.php +++ b/src/ResultSet/ResultSet.php @@ -63,7 +63,7 @@ public function __construct($returnType = self::TYPE_ARRAYOBJECT, $arrayObjectPr * @return self Provides a fluent interface * @throws Exception\InvalidArgumentException */ - public function setArrayObjectPrototype($arrayObjectPrototype) + public function setArrayObjectPrototype($arrayObjectPrototype) : self { if (! is_object($arrayObjectPrototype) || ( @@ -84,7 +84,7 @@ public function setArrayObjectPrototype($arrayObjectPrototype) * * @return ArrayObject */ - public function getArrayObjectPrototype() + public function getArrayObjectPrototype() : ArrayObject { return $this->arrayObjectPrototype; } @@ -94,7 +94,7 @@ public function getArrayObjectPrototype() * * @return string */ - public function getReturnType() + public function getReturnType() : string { return $this->returnType; } From e9467adbb61ca4aaecf6b54f3992d7aad75ada81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 12:44:28 +0100 Subject: [PATCH 3/8] Removed method comments which do not add valuable information, removed superfluous null assignment, declared argument types --- src/ResultSet/AbstractResultSet.php | 56 ++++------------------------ src/ResultSet/HydratingResultSet.php | 49 +++--------------------- src/ResultSet/ResultSet.php | 40 +++----------------- 3 files changed, 17 insertions(+), 128 deletions(-) diff --git a/src/ResultSet/AbstractResultSet.php b/src/ResultSet/AbstractResultSet.php index 1618204263..e350c76c23 100644 --- a/src/ResultSet/AbstractResultSet.php +++ b/src/ResultSet/AbstractResultSet.php @@ -25,26 +25,18 @@ abstract class AbstractResultSet implements Iterator, ResultSetInterface * if array, already buffering * @var mixed */ - protected $buffer = null; + protected $buffer; - /** - * @var null|int - */ - protected $count = null; + /** @var null|int */ + protected $count; - /** - * @var Iterator|IteratorAggregate|ResultInterface - */ - protected $dataSource = null; + /** @var Iterator|IteratorAggregate|ResultInterface */ + protected $dataSource; - /** - * @var int - */ + /** @var int */ protected $fieldCount = 0; - /** - * @var int - */ + /** @var int */ protected $position = 0; /** @@ -93,10 +85,6 @@ public function initialize($dataSource) : self return $this; } - /** - * @return self Provides a fluent interface - * @throws Exception\RuntimeException - */ public function buffer() : self { if ($this->buffer === -2) { @@ -118,11 +106,6 @@ public function isBuffered() : bool return false; } - /** - * Get the data source used to create the result set - * - * @return null|Iterator - */ public function getDataSource() : ?Iterator { return $this->dataSource; @@ -155,11 +138,6 @@ public function getFieldCount() : int return $this->fieldCount; } - /** - * Iterator: move pointer to next item - * - * @return void - */ public function next() : void { if ($this->buffer === null) { @@ -171,11 +149,6 @@ public function next() : void $this->position++; } - /** - * Iterator: retrieve current key - * - * @return int - */ public function key() : int { return $this->position; @@ -205,11 +178,6 @@ public function current() return is_array($data) ? $data : null; } - /** - * Iterator: is pointer valid? - * - * @return bool - */ public function valid() : bool { if (is_array($this->buffer) && isset($this->buffer[$this->position])) { @@ -223,11 +191,6 @@ public function valid() : bool } } - /** - * Iterator: rewind - * - * @return void - */ public function rewind() : void { if (! is_array($this->buffer)) { @@ -240,11 +203,6 @@ public function rewind() : void $this->position = 0; } - /** - * Countable: return count of rows - * - * @return int - */ public function count() : int { if ($this->count !== null) { diff --git a/src/ResultSet/HydratingResultSet.php b/src/ResultSet/HydratingResultSet.php index d989562a2a..498dd6a22d 100644 --- a/src/ResultSet/HydratingResultSet.php +++ b/src/ResultSet/HydratingResultSet.php @@ -16,23 +16,13 @@ class HydratingResultSet extends AbstractResultSet { - /** - * @var HydratorInterface - */ - protected $hydrator = null; + /** @var HydratorInterface */ + protected $hydrator; - /** - * @var null|object - */ - protected $objectPrototype = null; + /** @var null|object */ + protected $objectPrototype; - /** - * Constructor - * - * @param null|HydratorInterface $hydrator - * @param null|object $objectPrototype - */ - public function __construct(HydratorInterface $hydrator = null, $objectPrototype = null) + public function __construct(?HydratorInterface $hydrator = null, ?object $objectPrototype = null) { $defaultHydratorClass = class_exists(ArraySerializableHydrator::class) ? ArraySerializableHydrator::class @@ -41,13 +31,6 @@ public function __construct(HydratorInterface $hydrator = null, $objectPrototype $this->setObjectPrototype(($objectPrototype) ?: new ArrayObject); } - /** - * Set the row object prototype - * - * @param object $objectPrototype - * @return self Provides a fluent interface - * @throws Exception\InvalidArgumentException - */ public function setObjectPrototype($objectPrototype) : self { if (! is_object($objectPrototype)) { @@ -59,33 +42,17 @@ public function setObjectPrototype($objectPrototype) : self return $this; } - /** - * Get the row object prototype - * - * @return object - */ public function getObjectPrototype() : object { return $this->objectPrototype; } - /** - * Set the hydrator to use for each row object - * - * @param HydratorInterface $hydrator - * @return self Provides a fluent interface - */ public function setHydrator(HydratorInterface $hydrator) : self { $this->hydrator = $hydrator; return $this; } - /** - * Get the hydrator to use for each row object - * - * @return HydratorInterface - */ public function getHydrator() : HydratorInterface { return $this->hydrator; @@ -113,12 +80,6 @@ public function current() return $object; } - /** - * Cast result set to array of arrays - * - * @return array - * @throws Exception\RuntimeException if any row is not castable to an array - */ public function toArray() : array { $return = []; diff --git a/src/ResultSet/ResultSet.php b/src/ResultSet/ResultSet.php index 926ea3b2be..ce13dfdce2 100644 --- a/src/ResultSet/ResultSet.php +++ b/src/ResultSet/ResultSet.php @@ -16,35 +16,22 @@ class ResultSet extends AbstractResultSet const TYPE_ARRAYOBJECT = 'arrayobject'; const TYPE_ARRAY = 'array'; - /** - * Allowed return types - * - * @var array - */ protected $allowedReturnTypes = [ self::TYPE_ARRAYOBJECT, self::TYPE_ARRAY, ]; - /** - * @var ArrayObject - */ - protected $arrayObjectPrototype = null; + /** @var ArrayObject */ + protected $arrayObjectPrototype; /** * Return type to use when returning an object from the set * - * @var ResultSet::TYPE_ARRAYOBJECT|ResultSet::TYPE_ARRAY + * @var string One of the above declared TYPE constants */ protected $returnType = self::TYPE_ARRAYOBJECT; - /** - * Constructor - * - * @param string $returnType - * @param null|ArrayObject $arrayObjectPrototype - */ - public function __construct($returnType = self::TYPE_ARRAYOBJECT, $arrayObjectPrototype = null) + public function __construct(string $returnType = self::TYPE_ARRAYOBJECT, ?ArrayObject $arrayObjectPrototype = null) { if (in_array($returnType, [self::TYPE_ARRAY, self::TYPE_ARRAYOBJECT])) { $this->returnType = $returnType; @@ -56,13 +43,6 @@ public function __construct($returnType = self::TYPE_ARRAYOBJECT, $arrayObjectPr } } - /** - * Set the row object prototype - * - * @param ArrayObject $arrayObjectPrototype - * @return self Provides a fluent interface - * @throws Exception\InvalidArgumentException - */ public function setArrayObjectPrototype($arrayObjectPrototype) : self { if (! is_object($arrayObjectPrototype) @@ -79,28 +59,18 @@ public function setArrayObjectPrototype($arrayObjectPrototype) : self return $this; } - /** - * Get the row object prototype - * - * @return ArrayObject - */ public function getArrayObjectPrototype() : ArrayObject { return $this->arrayObjectPrototype; } - /** - * Get the return type to use when returning objects from the set - * - * @return string - */ public function getReturnType() : string { return $this->returnType; } /** - * @return array|\ArrayObject|null + * @return array|ArrayObject|null */ public function current() { From 4c4a74f283bd940bed4ef2ed934c2f076a387fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 12:47:12 +0100 Subject: [PATCH 4/8] Declared argument types and removed previous manual validation of argument type --- src/ResultSet/HydratingResultSet.php | 7 +------ src/ResultSet/ResultSet.php | 9 +++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/ResultSet/HydratingResultSet.php b/src/ResultSet/HydratingResultSet.php index 498dd6a22d..c5591475df 100644 --- a/src/ResultSet/HydratingResultSet.php +++ b/src/ResultSet/HydratingResultSet.php @@ -31,13 +31,8 @@ public function __construct(?HydratorInterface $hydrator = null, ?object $object $this->setObjectPrototype(($objectPrototype) ?: new ArrayObject); } - public function setObjectPrototype($objectPrototype) : self + public function setObjectPrototype(object $objectPrototype) : self { - if (! is_object($objectPrototype)) { - throw new Exception\InvalidArgumentException( - 'An object must be set as the object prototype, a ' . gettype($objectPrototype) . ' was provided.' - ); - } $this->objectPrototype = $objectPrototype; return $this; } diff --git a/src/ResultSet/ResultSet.php b/src/ResultSet/ResultSet.php index ce13dfdce2..33e0337e58 100644 --- a/src/ResultSet/ResultSet.php +++ b/src/ResultSet/ResultSet.php @@ -43,13 +43,10 @@ public function __construct(string $returnType = self::TYPE_ARRAYOBJECT, ?ArrayO } } - public function setArrayObjectPrototype($arrayObjectPrototype) : self + public function setArrayObjectPrototype(object $arrayObjectPrototype) : self { - if (! is_object($arrayObjectPrototype) - || ( - ! $arrayObjectPrototype instanceof ArrayObject - && ! method_exists($arrayObjectPrototype, 'exchangeArray') - ) + if (! $arrayObjectPrototype instanceof ArrayObject + && ! method_exists($arrayObjectPrototype, 'exchangeArray') ) { throw new Exception\InvalidArgumentException( 'Object must be of type ArrayObject, or at least implement exchangeArray' From 68ce435860f456849682234d6542237883e893ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 12:50:52 +0100 Subject: [PATCH 5/8] Defined access modifiers for class constants --- src/ResultSet/ResultSet.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ResultSet/ResultSet.php b/src/ResultSet/ResultSet.php index 33e0337e58..90a6119fe5 100644 --- a/src/ResultSet/ResultSet.php +++ b/src/ResultSet/ResultSet.php @@ -13,8 +13,8 @@ class ResultSet extends AbstractResultSet { - const TYPE_ARRAYOBJECT = 'arrayobject'; - const TYPE_ARRAY = 'array'; + public const TYPE_ARRAYOBJECT = 'arrayobject'; + public const TYPE_ARRAY = 'array'; protected $allowedReturnTypes = [ self::TYPE_ARRAYOBJECT, From d08211248c059a8d179d164c3ec0778d6ee05c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 12:51:23 +0100 Subject: [PATCH 6/8] Removed unnecessary parentheses --- src/ResultSet/HydratingResultSet.php | 2 +- src/ResultSet/ResultSet.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ResultSet/HydratingResultSet.php b/src/ResultSet/HydratingResultSet.php index c5591475df..4d0c2ffa2c 100644 --- a/src/ResultSet/HydratingResultSet.php +++ b/src/ResultSet/HydratingResultSet.php @@ -28,7 +28,7 @@ public function __construct(?HydratorInterface $hydrator = null, ?object $object ? ArraySerializableHydrator::class : ArraySerializable::class; $this->setHydrator($hydrator ?: new $defaultHydratorClass()); - $this->setObjectPrototype(($objectPrototype) ?: new ArrayObject); + $this->setObjectPrototype($objectPrototype ?: new ArrayObject); } public function setObjectPrototype(object $objectPrototype) : self diff --git a/src/ResultSet/ResultSet.php b/src/ResultSet/ResultSet.php index 90a6119fe5..f7a5d897e7 100644 --- a/src/ResultSet/ResultSet.php +++ b/src/ResultSet/ResultSet.php @@ -39,7 +39,7 @@ public function __construct(string $returnType = self::TYPE_ARRAYOBJECT, ?ArrayO $this->returnType = self::TYPE_ARRAYOBJECT; } if ($this->returnType === self::TYPE_ARRAYOBJECT) { - $this->setArrayObjectPrototype(($arrayObjectPrototype) ?: new ArrayObject([], ArrayObject::ARRAY_AS_PROPS)); + $this->setArrayObjectPrototype($arrayObjectPrototype ?: new ArrayObject([], ArrayObject::ARRAY_AS_PROPS)); } } From dec74f8cf339d1e13fa280dea7364e89e9ef467c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 12:51:52 +0100 Subject: [PATCH 7/8] Simplified return statement --- src/ResultSet/AbstractResultSet.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/ResultSet/AbstractResultSet.php b/src/ResultSet/AbstractResultSet.php index e350c76c23..314ce85e9e 100644 --- a/src/ResultSet/AbstractResultSet.php +++ b/src/ResultSet/AbstractResultSet.php @@ -100,10 +100,7 @@ public function buffer() : self public function isBuffered() : bool { - if ($this->buffer === -1 || is_array($this->buffer)) { - return true; - } - return false; + return $this->buffer === -1 || is_array($this->buffer); } public function getDataSource() : ?Iterator From 9ed97dd903b2f71484ff95c5b036d31439f64f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 12:53:12 +0100 Subject: [PATCH 8/8] Removed unnecessary elseif/else (clearer intention, lower comlexity numbers) --- src/ResultSet/AbstractResultSet.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ResultSet/AbstractResultSet.php b/src/ResultSet/AbstractResultSet.php index 314ce85e9e..a147baecb4 100644 --- a/src/ResultSet/AbstractResultSet.php +++ b/src/ResultSet/AbstractResultSet.php @@ -89,12 +89,15 @@ public function buffer() : self { if ($this->buffer === -2) { throw new Exception\RuntimeException('Buffering must be enabled before iteration is started'); - } elseif ($this->buffer === null) { + } + + if ($this->buffer === null) { $this->buffer = []; if ($this->dataSource instanceof ResultInterface) { $this->dataSource->rewind(); } } + return $this; } @@ -180,12 +183,13 @@ public function valid() : bool if (is_array($this->buffer) && isset($this->buffer[$this->position])) { return true; } + if ($this->dataSource instanceof Iterator) { return $this->dataSource->valid(); - } else { - $key = key($this->dataSource); - return ($key !== null); } + + $key = key($this->dataSource); + return ($key !== null); } public function rewind() : void