From 2d1f9e6d919659c9a258648de2958253b611661a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 13:15:38 +0100 Subject: [PATCH 01/10] Updated file header comment, added strict_types declaration --- src/RowGateway/AbstractRowGateway.php | 10 +++++----- src/RowGateway/Exception/ExceptionInterface.php | 10 +++++----- src/RowGateway/Exception/InvalidArgumentException.php | 10 +++++----- src/RowGateway/Exception/RuntimeException.php | 10 +++++----- src/RowGateway/Feature/AbstractFeature.php | 10 +++++----- src/RowGateway/Feature/FeatureSet.php | 10 +++++----- src/RowGateway/RowGateway.php | 10 +++++----- src/RowGateway/RowGatewayInterface.php | 10 +++++----- 8 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/RowGateway/AbstractRowGateway.php b/src/RowGateway/AbstractRowGateway.php index 89b2068fff..b04ff6305e 100644 --- a/src/RowGateway/AbstractRowGateway.php +++ b/src/RowGateway/AbstractRowGateway.php @@ -1,12 +1,12 @@ Date: Thu, 14 Mar 2019 13:17:57 +0100 Subject: [PATCH 02/10] Write single-line comments with @var as one-liners --- src/RowGateway/AbstractRowGateway.php | 28 ++++++---------------- src/RowGateway/Feature/AbstractFeature.php | 8 ++----- src/RowGateway/Feature/FeatureSet.php | 12 +++------- 3 files changed, 12 insertions(+), 36 deletions(-) diff --git a/src/RowGateway/AbstractRowGateway.php b/src/RowGateway/AbstractRowGateway.php index b04ff6305e..a86f505ae0 100644 --- a/src/RowGateway/AbstractRowGateway.php +++ b/src/RowGateway/AbstractRowGateway.php @@ -16,39 +16,25 @@ abstract class AbstractRowGateway implements ArrayAccess, Countable, RowGatewayInterface { - /** - * @var bool - */ + /** @var bool */ protected $isInitialized = false; - /** - * @var string|TableIdentifier - */ + /** @var string|TableIdentifier */ protected $table = null; - /** - * @var array - */ + /** @var array */ protected $primaryKeyColumn = null; - /** - * @var array - */ + /** @var array */ protected $primaryKeyData = null; - /** - * @var array - */ + /** @var array */ protected $data = []; - /** - * @var Sql - */ + /** @var Sql */ protected $sql = null; - /** - * @var Feature\FeatureSet - */ + /** @var Feature\FeatureSet */ protected $featureSet = null; /** diff --git a/src/RowGateway/Feature/AbstractFeature.php b/src/RowGateway/Feature/AbstractFeature.php index 5d60a45436..3d66b20b81 100644 --- a/src/RowGateway/Feature/AbstractFeature.php +++ b/src/RowGateway/Feature/AbstractFeature.php @@ -14,14 +14,10 @@ abstract class AbstractFeature extends AbstractRowGateway { - /** - * @var AbstractRowGateway - */ + /** @var AbstractRowGateway */ protected $rowGateway = null; - /** - * @var array - */ + /** @var array */ protected $sharedData = []; /** diff --git a/src/RowGateway/Feature/FeatureSet.php b/src/RowGateway/Feature/FeatureSet.php index 43a84b194c..175eaf2ce0 100644 --- a/src/RowGateway/Feature/FeatureSet.php +++ b/src/RowGateway/Feature/FeatureSet.php @@ -15,19 +15,13 @@ class FeatureSet { const APPLY_HALT = 'halt'; - /** - * @var AbstractRowGateway - */ + /** @var AbstractRowGateway */ protected $rowGateway = null; - /** - * @var AbstractFeature[] - */ + /** @var AbstractFeature[] */ protected $features = []; - /** - * @var array - */ + /** @var array */ protected $magicSpecifications = []; /** From f0666957223c28533e06502c0487837f529d0856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 13:30:08 +0100 Subject: [PATCH 03/10] Removed method comments which do not add valuable information, declared argument and return types --- 3.0.0.md | 4 + src/RowGateway/AbstractRowGateway.php | 91 ++++------------------ src/RowGateway/Feature/AbstractFeature.php | 15 +--- src/RowGateway/Feature/FeatureSet.php | 78 +++++-------------- src/RowGateway/RowGateway.php | 4 +- 5 files changed, 42 insertions(+), 150 deletions(-) diff --git a/3.0.0.md b/3.0.0.md index b1c3abfb90..c48427557f 100644 --- a/3.0.0.md +++ b/3.0.0.md @@ -28,3 +28,7 @@ Changed `setDriver(DriverInterface $driver)` for all the following platforms: - Zend\Db\Adapter\Platform\Postgresql - Zend\Db\Adapter\Platform\Sqlite - Zend\Db\Adapter\Platform\SqlServer + +- Zend\Db\RowGateway\AbstractRowGateway + +We cannot use type hint for parameter in `offsetExists()` because of definition in `\ArrayAccess` diff --git a/src/RowGateway/AbstractRowGateway.php b/src/RowGateway/AbstractRowGateway.php index a86f505ae0..04cfbfa950 100644 --- a/src/RowGateway/AbstractRowGateway.php +++ b/src/RowGateway/AbstractRowGateway.php @@ -72,14 +72,7 @@ public function initialize() $this->isInitialized = true; } - /** - * Populate Data - * - * @param array $rowData - * @param bool $rowExistsInDatabase - * @return self Provides a fluent interface - */ - public function populate(array $rowData, $rowExistsInDatabase = false) + public function populate(array $rowData, bool $rowExistsInDatabase = false) : self { $this->initialize(); @@ -93,21 +86,12 @@ public function populate(array $rowData, $rowExistsInDatabase = false) return $this; } - /** - * @param mixed $array - * @return AbstractRowGateway - */ - public function exchangeArray($array) + public function exchangeArray(array $array) : self { return $this->populate($array, true); } - /** - * Save - * - * @return int - */ - public function save() + public function save() : int { $this->initialize(); @@ -178,12 +162,7 @@ public function save() return $rowsAffected; } - /** - * Delete - * - * @return int - */ - public function delete() + public function delete() : int { $this->initialize(); @@ -213,7 +192,7 @@ public function delete() * @param string $offset * @return bool */ - public function offsetExists($offset) + public function offsetExists($offset) : bool { return array_key_exists($offset, $this->data); } @@ -236,7 +215,7 @@ public function offsetGet($offset) * @param mixed $value * @return self Provides a fluent interface */ - public function offsetSet($offset, $value) + public function offsetSet($offset, $value) : self { $this->data[$offset] = $value; return $this; @@ -248,38 +227,23 @@ public function offsetSet($offset, $value) * @param string $offset * @return self Provides a fluent interface */ - public function offsetUnset($offset) + public function offsetUnset($offset) : self { $this->data[$offset] = null; return $this; } - /** - * @return int - */ - public function count() + public function count() : int { return count($this->data); } - /** - * To array - * - * @return array - */ - public function toArray() + public function toArray() : array { return $this->data; } - /** - * __get - * - * @param string $name - * @throws Exception\InvalidArgumentException - * @return mixed - */ - public function __get($name) + public function __get(string $name) { if (array_key_exists($name, $this->data)) { return $this->data[$name]; @@ -288,52 +252,27 @@ public function __get($name) } } - /** - * __set - * - * @param string $name - * @param mixed $value - * @return void - */ - public function __set($name, $value) + public function __set(string $name, $value) : void { $this->offsetSet($name, $value); } - /** - * __isset - * - * @param string $name - * @return bool - */ - public function __isset($name) + public function __isset(string $name) : bool { return $this->offsetExists($name); } - /** - * __unset - * - * @param string $name - * @return void - */ - public function __unset($name) + public function __unset(string $name) : void { $this->offsetUnset($name); } - /** - * @return bool - */ - public function rowExistsInDatabase() + public function rowExistsInDatabase() : bool { return ($this->primaryKeyData !== null); } - /** - * @throws Exception\RuntimeException - */ - protected function processPrimaryKeyData() + protected function processPrimaryKeyData() : void { $this->primaryKeyData = []; foreach ($this->primaryKeyColumn as $column) { diff --git a/src/RowGateway/Feature/AbstractFeature.php b/src/RowGateway/Feature/AbstractFeature.php index 3d66b20b81..7df4cebb3a 100644 --- a/src/RowGateway/Feature/AbstractFeature.php +++ b/src/RowGateway/Feature/AbstractFeature.php @@ -21,33 +21,24 @@ abstract class AbstractFeature extends AbstractRowGateway protected $sharedData = []; /** - * @return string + * @return string|bool */ public function getName() { return get_class($this); } - /** - * @param AbstractRowGateway $rowGateway - */ - public function setRowGateway(AbstractRowGateway $rowGateway) + public function setRowGateway(AbstractRowGateway $rowGateway) : void { $this->rowGateway = $rowGateway; } - /** - * @throws \Zend\Db\RowGateway\Exception\RuntimeException - */ public function initialize() { throw new Exception\RuntimeException('This method is not intended to be called on this object.'); } - /** - * @return array - */ - public function getMagicMethodSpecifications() + public function getMagicMethodSpecifications() : array { return []; } diff --git a/src/RowGateway/Feature/FeatureSet.php b/src/RowGateway/Feature/FeatureSet.php index 175eaf2ce0..a5ef602e72 100644 --- a/src/RowGateway/Feature/FeatureSet.php +++ b/src/RowGateway/Feature/FeatureSet.php @@ -24,21 +24,12 @@ class FeatureSet /** @var array */ protected $magicSpecifications = []; - /** - * @param array $features - */ public function __construct(array $features = []) { - if ($features) { - $this->addFeatures($features); - } + $this->addFeatures($features); } - /** - * @param AbstractRowGateway $rowGateway - * @return self Provides a fluent interface - */ - public function setRowGateway(AbstractRowGateway $rowGateway) + public function setRowGateway(AbstractRowGateway $rowGateway) : self { $this->rowGateway = $rowGateway; foreach ($this->features as $feature) { @@ -47,6 +38,10 @@ public function setRowGateway(AbstractRowGateway $rowGateway) return $this; } + /** + * @param mixed $featureClassName + * @return bool|AbstractFeature + */ public function getFeatureByClassName($featureClassName) { $feature = false; @@ -59,11 +54,7 @@ public function getFeatureByClassName($featureClassName) return $feature; } - /** - * @param array $features - * @return self Provides a fluent interface - */ - public function addFeatures(array $features) + public function addFeatures(array $features) : self { foreach ($features as $feature) { $this->addFeature($feature); @@ -71,18 +62,14 @@ public function addFeatures(array $features) return $this; } - /** - * @param AbstractFeature $feature - * @return self Provides a fluent interface - */ - public function addFeature(AbstractFeature $feature) + public function addFeature(AbstractFeature $feature) : self { $this->features[] = $feature; $feature->setRowGateway($feature); return $this; } - public function apply($method, $args) + public function apply(string $method, array $args) : void { foreach ($this->features as $feature) { if (method_exists($feature, $method)) { @@ -94,62 +81,33 @@ public function apply($method, $args) } } - /** - * @param string $property - * @return bool - */ - public function canCallMagicGet($property) + public function canCallMagicGet(string $property) : bool { return false; } - /** - * @param string $property - * @return mixed - */ - public function callMagicGet($property) + public function callMagicGet(string $property) : void { - $return = null; - return $return; + return null; } - /** - * @param string $property - * @return bool - */ - public function canCallMagicSet($property) + public function canCallMagicSet(string $property) : bool { return false; } - /** - * @param $property - * @param $value - * @return mixed - */ - public function callMagicSet($property, $value) + public function callMagicSet(string $property, $value) : void { - $return = null; - return $return; + return null; } - /** - * @param string $method - * @return bool - */ - public function canCallMagicCall($method) + public function canCallMagicCall(string $method) : bool { return false; } - /** - * @param string $method - * @param array $arguments - * @return mixed - */ - public function callMagicCall($method, $arguments) + public function callMagicCall(string $method, array $arguments) : void { - $return = null; - return $return; + return null; } } diff --git a/src/RowGateway/RowGateway.php b/src/RowGateway/RowGateway.php index f2fa5ee8ae..a40bfa92c1 100644 --- a/src/RowGateway/RowGateway.php +++ b/src/RowGateway/RowGateway.php @@ -19,10 +19,10 @@ class RowGateway extends AbstractRowGateway * * @param string $primaryKeyColumn * @param string|\Zend\Db\Sql\TableIdentifier $table - * @param Adapter|Sql $adapterOrSql + * @param null|Adapter|Sql $adapterOrSql * @throws Exception\InvalidArgumentException */ - public function __construct($primaryKeyColumn, $table, $adapterOrSql = null) + public function __construct(string $primaryKeyColumn, $table, $adapterOrSql = null) { // setup primary key $this->primaryKeyColumn = empty($primaryKeyColumn) ? null : (array) $primaryKeyColumn; From 036aa36e7adbebb47e4cd570cc499b623a0032ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 13:37:07 +0100 Subject: [PATCH 04/10] Initialize as array and treat as such --- src/RowGateway/AbstractRowGateway.php | 12 +++++++----- src/RowGateway/RowGateway.php | 5 +---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/RowGateway/AbstractRowGateway.php b/src/RowGateway/AbstractRowGateway.php index 04cfbfa950..d81820a2c5 100644 --- a/src/RowGateway/AbstractRowGateway.php +++ b/src/RowGateway/AbstractRowGateway.php @@ -20,10 +20,10 @@ abstract class AbstractRowGateway implements ArrayAccess, Countable, RowGatewayI protected $isInitialized = false; /** @var string|TableIdentifier */ - protected $table = null; + protected $table; /** @var array */ - protected $primaryKeyColumn = null; + protected $primaryKeyColumn = []; /** @var array */ protected $primaryKeyData = null; @@ -57,12 +57,14 @@ public function initialize() throw new Exception\RuntimeException('This row object does not have a valid table set.'); } - if ($this->primaryKeyColumn === null) { - throw new Exception\RuntimeException('This row object does not have a primary key column set.'); - } elseif (is_string($this->primaryKeyColumn)) { + if (is_string($this->primaryKeyColumn)) { $this->primaryKeyColumn = (array) $this->primaryKeyColumn; } + if (count($this->primaryKeyColumn) === 0) { + throw new Exception\RuntimeException('This row object does not have a primary key column set.'); + } + if (! $this->sql instanceof Sql) { throw new Exception\RuntimeException('This row object does not have a Sql object set.'); } diff --git a/src/RowGateway/RowGateway.php b/src/RowGateway/RowGateway.php index a40bfa92c1..a014e6d790 100644 --- a/src/RowGateway/RowGateway.php +++ b/src/RowGateway/RowGateway.php @@ -24,13 +24,10 @@ class RowGateway extends AbstractRowGateway */ public function __construct(string $primaryKeyColumn, $table, $adapterOrSql = null) { - // setup primary key - $this->primaryKeyColumn = empty($primaryKeyColumn) ? null : (array) $primaryKeyColumn; + $this->primaryKeyColumn = $primaryKeyColumn === '' ? [] : (array) $primaryKeyColumn; - // set table $this->table = $table; - // set Sql object if ($adapterOrSql instanceof Sql) { $this->sql = $adapterOrSql; } elseif ($adapterOrSql instanceof Adapter) { From 011fa837b33968197745c747e6da76a931f4f33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 13:38:12 +0100 Subject: [PATCH 05/10] Removed null assignment --- src/RowGateway/AbstractRowGateway.php | 4 ++-- src/RowGateway/Feature/AbstractFeature.php | 2 +- src/RowGateway/Feature/FeatureSet.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/RowGateway/AbstractRowGateway.php b/src/RowGateway/AbstractRowGateway.php index d81820a2c5..2171bd5cca 100644 --- a/src/RowGateway/AbstractRowGateway.php +++ b/src/RowGateway/AbstractRowGateway.php @@ -32,10 +32,10 @@ abstract class AbstractRowGateway implements ArrayAccess, Countable, RowGatewayI protected $data = []; /** @var Sql */ - protected $sql = null; + protected $sql; /** @var Feature\FeatureSet */ - protected $featureSet = null; + protected $featureSet; /** * initialize() diff --git a/src/RowGateway/Feature/AbstractFeature.php b/src/RowGateway/Feature/AbstractFeature.php index 7df4cebb3a..b9359b9ca3 100644 --- a/src/RowGateway/Feature/AbstractFeature.php +++ b/src/RowGateway/Feature/AbstractFeature.php @@ -15,7 +15,7 @@ abstract class AbstractFeature extends AbstractRowGateway { /** @var AbstractRowGateway */ - protected $rowGateway = null; + protected $rowGateway; /** @var array */ protected $sharedData = []; diff --git a/src/RowGateway/Feature/FeatureSet.php b/src/RowGateway/Feature/FeatureSet.php index a5ef602e72..fe84734b95 100644 --- a/src/RowGateway/Feature/FeatureSet.php +++ b/src/RowGateway/Feature/FeatureSet.php @@ -16,7 +16,7 @@ class FeatureSet const APPLY_HALT = 'halt'; /** @var AbstractRowGateway */ - protected $rowGateway = null; + protected $rowGateway; /** @var AbstractFeature[] */ protected $features = []; From a6f927b5a7828c7974361ffc56811daa8705401d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 13:40:31 +0100 Subject: [PATCH 06/10] Initialize as array and treat as such --- src/RowGateway/AbstractRowGateway.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/RowGateway/AbstractRowGateway.php b/src/RowGateway/AbstractRowGateway.php index 2171bd5cca..191523dd1a 100644 --- a/src/RowGateway/AbstractRowGateway.php +++ b/src/RowGateway/AbstractRowGateway.php @@ -26,7 +26,7 @@ abstract class AbstractRowGateway implements ArrayAccess, Countable, RowGatewayI protected $primaryKeyColumn = []; /** @var array */ - protected $primaryKeyData = null; + protected $primaryKeyData = []; /** @var array */ protected $data = []; @@ -82,7 +82,7 @@ public function populate(array $rowData, bool $rowExistsInDatabase = false) : se if ($rowExistsInDatabase == true) { $this->processPrimaryKeyData(); } else { - $this->primaryKeyData = null; + $this->primaryKeyData = []; } return $this; @@ -182,7 +182,7 @@ public function delete() : int $affectedRows = $result->getAffectedRows(); if ($affectedRows == 1) { // detach from database - $this->primaryKeyData = null; + $this->primaryKeyData = []; } return $affectedRows; @@ -271,7 +271,7 @@ public function __unset(string $name) : void public function rowExistsInDatabase() : bool { - return ($this->primaryKeyData !== null); + return count($this->primaryKeyData) > 0; } protected function processPrimaryKeyData() : void From 7e354e3c8eceb44dccc10262156ad36f9d846998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 13:41:41 +0100 Subject: [PATCH 07/10] Apply strict comparison --- src/RowGateway/AbstractRowGateway.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/RowGateway/AbstractRowGateway.php b/src/RowGateway/AbstractRowGateway.php index 191523dd1a..6c4ac50caf 100644 --- a/src/RowGateway/AbstractRowGateway.php +++ b/src/RowGateway/AbstractRowGateway.php @@ -79,7 +79,7 @@ public function populate(array $rowData, bool $rowExistsInDatabase = false) : se $this->initialize(); $this->data = $rowData; - if ($rowExistsInDatabase == true) { + if ($rowExistsInDatabase === true) { $this->processPrimaryKeyData(); } else { $this->primaryKeyData = []; @@ -135,7 +135,7 @@ public function save() : int $statement = $this->sql->prepareStatementForSqlObject($insert); $result = $statement->execute(); - if (($primaryKeyValue = $result->getGeneratedValue()) && count($this->primaryKeyColumn) == 1) { + if (($primaryKeyValue = $result->getGeneratedValue()) && count($this->primaryKeyColumn) === 1) { $this->primaryKeyData = [$this->primaryKeyColumn[0] => $primaryKeyValue]; } else { // make primary key data available so that $where can be complete @@ -180,7 +180,7 @@ public function delete() : int $result = $statement->execute(); $affectedRows = $result->getAffectedRows(); - if ($affectedRows == 1) { + if ($affectedRows === 1) { // detach from database $this->primaryKeyData = []; } From bd6824174618fecfade8751e909eaecd9275b7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 13:42:42 +0100 Subject: [PATCH 08/10] Remove else / extract child instructions of else (clearer intention, lower complexity number) --- src/RowGateway/AbstractRowGateway.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RowGateway/AbstractRowGateway.php b/src/RowGateway/AbstractRowGateway.php index 6c4ac50caf..746252bf8b 100644 --- a/src/RowGateway/AbstractRowGateway.php +++ b/src/RowGateway/AbstractRowGateway.php @@ -249,9 +249,9 @@ public function __get(string $name) { if (array_key_exists($name, $this->data)) { return $this->data[$name]; - } else { - throw new Exception\InvalidArgumentException('Not a valid column in this row: ' . $name); } + + throw new Exception\InvalidArgumentException('Not a valid column in this row: ' . $name); } public function __set(string $name, $value) : void From 98e22292700f710a1c62478afcb7b5bdae164e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 13:43:39 +0100 Subject: [PATCH 09/10] Defined access modifier for class constant --- src/RowGateway/Feature/FeatureSet.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RowGateway/Feature/FeatureSet.php b/src/RowGateway/Feature/FeatureSet.php index fe84734b95..098f4674cd 100644 --- a/src/RowGateway/Feature/FeatureSet.php +++ b/src/RowGateway/Feature/FeatureSet.php @@ -13,7 +13,7 @@ class FeatureSet { - const APPLY_HALT = 'halt'; + public const APPLY_HALT = 'halt'; /** @var AbstractRowGateway */ protected $rowGateway; From 3eca3af93c34f65e7bdf866c6f644dcb7f8ec283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20R=C3=BCckauer?= Date: Thu, 14 Mar 2019 13:45:16 +0100 Subject: [PATCH 10/10] A void functino must not return a value --- src/RowGateway/Feature/FeatureSet.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/RowGateway/Feature/FeatureSet.php b/src/RowGateway/Feature/FeatureSet.php index 098f4674cd..0c8d413288 100644 --- a/src/RowGateway/Feature/FeatureSet.php +++ b/src/RowGateway/Feature/FeatureSet.php @@ -88,7 +88,6 @@ public function canCallMagicGet(string $property) : bool public function callMagicGet(string $property) : void { - return null; } public function canCallMagicSet(string $property) : bool @@ -98,7 +97,6 @@ public function canCallMagicSet(string $property) : bool public function callMagicSet(string $property, $value) : void { - return null; } public function canCallMagicCall(string $method) : bool @@ -108,6 +106,5 @@ public function canCallMagicCall(string $method) : bool public function callMagicCall(string $method, array $arguments) : void { - return null; } }