From 6c2ad28eebd8252ecb0ca02c54c60d1f9e70ce4f Mon Sep 17 00:00:00 2001 From: Ilija Studen Date: Mon, 25 Apr 2022 21:11:43 +0200 Subject: [PATCH] Improve signature of setFieldValue() method --- src/Entity/Entity.php | 6 +----- src/Entity/EntityInterface.php | 7 +------ .../SpatialEntity/Base/SpatialEntity.php | 20 +++++++++---------- .../StatSnapshots/Base/StatsSnapshot.php | 20 +++++++++---------- test/src/Fixtures/Users/Base/User.php | 16 ++++++--------- test/src/Fixtures/Writers/BaseWriter.php | 20 ++++++++----------- 6 files changed, 36 insertions(+), 53 deletions(-) diff --git a/src/Entity/Entity.php b/src/Entity/Entity.php index bc144f7..66088ff 100644 --- a/src/Entity/Entity.php +++ b/src/Entity/Entity.php @@ -713,12 +713,8 @@ public function getOldFieldValue($field) * Set value of the $field. This function will make sure that everything * runs fine - modifications are saved, in case of primary key old value * will be remembered in case we need to update the row and so on - * - * @param string $field - * @param mixed $value - * @return $this */ - public function &setFieldValue($field, $value) + public function setFieldValue(string $field, mixed $value): static { if ($this->entityFieldExists($field)) { if ($field === 'id') { diff --git a/src/Entity/EntityInterface.php b/src/Entity/EntityInterface.php index 9c2b051..a71c2ff 100644 --- a/src/Entity/EntityInterface.php +++ b/src/Entity/EntityInterface.php @@ -245,13 +245,8 @@ public function revertField($field); * Set value of the $field. This function will make sure that everything * runs fine - modifications are saved, in case of primary key old value * will be remembered in case we need to update the row and so on - * - * @param string $field - * @param mixed $value - * @return mixed - * @throws InvalidArgumentException */ - public function &setFieldValue($field, $value); + public function setFieldValue(string $field, mixed $value): static; /** * Set non-field value during DataManager::create() and DataManager::update() calls. diff --git a/test/src/Fixtures/SpatialEntity/Base/SpatialEntity.php b/test/src/Fixtures/SpatialEntity/Base/SpatialEntity.php index ecba375..a5e8c98 100644 --- a/test/src/Fixtures/SpatialEntity/Base/SpatialEntity.php +++ b/test/src/Fixtures/SpatialEntity/Base/SpatialEntity.php @@ -181,27 +181,27 @@ public function getFieldValue($field, $default = null) } } - public function &setFieldValue($name, $value) + public function setFieldValue(string $field, mixed $value): static { if ($value === null) { - parent::setFieldValue($name, null); + parent::setFieldValue($field, null); } else { - switch ($name) { + switch ($field) { case 'id': case 'name': - return parent::setFieldValue($name, (string) $value); + return parent::setFieldValue($field, (string) $value); case 'day': - return parent::setFieldValue($name, $this->getDateTimeValueInstanceFrom($value)); + return parent::setFieldValue($field, $this->getDateTimeValueInstanceFrom($value)); case 'stats': - return parent::setFieldValue($name, $this->isLoading() ? $value : json_encode($value)); + return parent::setFieldValue($field, $this->isLoading() ? $value : json_encode($value)); default: if ($this->isLoading()) { - return parent::setFieldValue($name, $value); + return parent::setFieldValue($field, $value); } else { - if ($this->isGeneratedField($name)) { - throw new \LogicException("Generated field $name cannot be set by directly assigning a value"); + if ($this->isGeneratedField($field)) { + throw new \LogicException("Generated field $field cannot be set by directly assigning a value"); } else { - throw new \InvalidArgumentException("Field $name does not exist in this table"); + throw new \InvalidArgumentException("Field $field does not exist in this table"); } } } diff --git a/test/src/Fixtures/StatSnapshots/Base/StatsSnapshot.php b/test/src/Fixtures/StatSnapshots/Base/StatsSnapshot.php index eb1d92b..05f8cba 100644 --- a/test/src/Fixtures/StatSnapshots/Base/StatsSnapshot.php +++ b/test/src/Fixtures/StatSnapshots/Base/StatsSnapshot.php @@ -197,27 +197,27 @@ public function getFieldValue($field, $default = null) } } - public function &setFieldValue($name, $value) + public function setFieldValue(string $field, mixed $value): static { if ($value === null) { - parent::setFieldValue($name, null); + parent::setFieldValue($field, null); } else { - switch ($name) { + switch ($field) { case 'id': case 'account_id': - return parent::setFieldValue($name, (int) $value); + return parent::setFieldValue($field, (int) $value); case 'day': - return parent::setFieldValue($name, $this->getDateTimeValueInstanceFrom($value)); + return parent::setFieldValue($field, $this->getDateTimeValueInstanceFrom($value)); case 'stats': - return parent::setFieldValue($name, $this->isLoading() ? $value : json_encode($value)); + return parent::setFieldValue($field, $this->isLoading() ? $value : json_encode($value)); default: if ($this->isLoading()) { - return parent::setFieldValue($name, $value); + return parent::setFieldValue($field, $value); } else { - if ($this->isGeneratedField($name)) { - throw new \LogicException("Generated field $name cannot be set by directly assigning a value"); + if ($this->isGeneratedField($field)) { + throw new \LogicException("Generated field $field cannot be set by directly assigning a value"); } else { - throw new \InvalidArgumentException("Field $name does not exist in this table"); + throw new \InvalidArgumentException("Field $field does not exist in this table"); } } } diff --git a/test/src/Fixtures/Users/Base/User.php b/test/src/Fixtures/Users/Base/User.php index 60dd403..3c3a763 100644 --- a/test/src/Fixtures/Users/Base/User.php +++ b/test/src/Fixtures/Users/Base/User.php @@ -190,28 +190,24 @@ public function &setPassword($value) /** * Set value of specific field. - * - * @param string $name - * @param mixed $value - * @return $this */ - public function &setFieldValue($name, $value) + public function setFieldValue(string $field, mixed $value): static { if ($value === null) { - parent::setFieldValue($name, null); + parent::setFieldValue($field, null); } else { - switch ($name) { + switch ($field) { case 'id': - return parent::setFieldValue($name, (int) $value); + return parent::setFieldValue($field, (int) $value); case 'type': case 'first_name': case 'last_name': case 'email': case 'homepage_url': case 'password': - return parent::setFieldValue($name, (string) $value); + return parent::setFieldValue($field, (string) $value); default: - throw new \InvalidArgumentException("Field $name does not exist in this table"); + throw new \InvalidArgumentException("Field $field does not exist in this table"); } } diff --git a/test/src/Fixtures/Writers/BaseWriter.php b/test/src/Fixtures/Writers/BaseWriter.php index 63d4e45..9dc973f 100644 --- a/test/src/Fixtures/Writers/BaseWriter.php +++ b/test/src/Fixtures/Writers/BaseWriter.php @@ -154,31 +154,27 @@ public function &setUpdatedAt($value) /** * Set value of specific field. - * - * @param string $name - * @param mixed $value - * @return mixed */ - public function &setFieldValue($name, $value) + public function setFieldValue(string $field, mixed $value): static { if ($value === null) { - parent::setFieldValue($name, null); + parent::setFieldValue($field, null); } else { - switch ($name) { + switch ($field) { case 'id': - parent::setFieldValue($name, (int) $value); + parent::setFieldValue($field, (int) $value); break; case 'name': - parent::setFieldValue($name, (string) $value); + parent::setFieldValue($field, (string) $value); break; case 'birthday': - parent::setFieldValue($name, $this->getDateValueInstanceFrom($value)); + parent::setFieldValue($field, $this->getDateValueInstanceFrom($value)); break; case 'created_at': case 'updated_at': - return parent::setFieldValue($name, $this->getDateTimeValueInstanceFrom($value)); + return parent::setFieldValue($field, $this->getDateTimeValueInstanceFrom($value)); default: - throw new InvalidArgumentException("'$name' is not a known field"); + throw new InvalidArgumentException("'$field' is not a known field"); } }