Skip to content

Commit 6c2ad28

Browse files
committed
Improve signature of setFieldValue() method
1 parent 4ba3bd4 commit 6c2ad28

File tree

6 files changed

+36
-53
lines changed

6 files changed

+36
-53
lines changed

src/Entity/Entity.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -713,12 +713,8 @@ public function getOldFieldValue($field)
713713
* Set value of the $field. This function will make sure that everything
714714
* runs fine - modifications are saved, in case of primary key old value
715715
* will be remembered in case we need to update the row and so on
716-
*
717-
* @param string $field
718-
* @param mixed $value
719-
* @return $this
720716
*/
721-
public function &setFieldValue($field, $value)
717+
public function setFieldValue(string $field, mixed $value): static
722718
{
723719
if ($this->entityFieldExists($field)) {
724720
if ($field === 'id') {

src/Entity/EntityInterface.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,8 @@ public function revertField($field);
245245
* Set value of the $field. This function will make sure that everything
246246
* runs fine - modifications are saved, in case of primary key old value
247247
* will be remembered in case we need to update the row and so on
248-
*
249-
* @param string $field
250-
* @param mixed $value
251-
* @return mixed
252-
* @throws InvalidArgumentException
253248
*/
254-
public function &setFieldValue($field, $value);
249+
public function setFieldValue(string $field, mixed $value): static;
255250

256251
/**
257252
* Set non-field value during DataManager::create() and DataManager::update() calls.

test/src/Fixtures/SpatialEntity/Base/SpatialEntity.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,27 +181,27 @@ public function getFieldValue($field, $default = null)
181181
}
182182
}
183183

184-
public function &setFieldValue($name, $value)
184+
public function setFieldValue(string $field, mixed $value): static
185185
{
186186
if ($value === null) {
187-
parent::setFieldValue($name, null);
187+
parent::setFieldValue($field, null);
188188
} else {
189-
switch ($name) {
189+
switch ($field) {
190190
case 'id':
191191
case 'name':
192-
return parent::setFieldValue($name, (string) $value);
192+
return parent::setFieldValue($field, (string) $value);
193193
case 'day':
194-
return parent::setFieldValue($name, $this->getDateTimeValueInstanceFrom($value));
194+
return parent::setFieldValue($field, $this->getDateTimeValueInstanceFrom($value));
195195
case 'stats':
196-
return parent::setFieldValue($name, $this->isLoading() ? $value : json_encode($value));
196+
return parent::setFieldValue($field, $this->isLoading() ? $value : json_encode($value));
197197
default:
198198
if ($this->isLoading()) {
199-
return parent::setFieldValue($name, $value);
199+
return parent::setFieldValue($field, $value);
200200
} else {
201-
if ($this->isGeneratedField($name)) {
202-
throw new \LogicException("Generated field $name cannot be set by directly assigning a value");
201+
if ($this->isGeneratedField($field)) {
202+
throw new \LogicException("Generated field $field cannot be set by directly assigning a value");
203203
} else {
204-
throw new \InvalidArgumentException("Field $name does not exist in this table");
204+
throw new \InvalidArgumentException("Field $field does not exist in this table");
205205
}
206206
}
207207
}

test/src/Fixtures/StatSnapshots/Base/StatsSnapshot.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,27 +197,27 @@ public function getFieldValue($field, $default = null)
197197
}
198198
}
199199

200-
public function &setFieldValue($name, $value)
200+
public function setFieldValue(string $field, mixed $value): static
201201
{
202202
if ($value === null) {
203-
parent::setFieldValue($name, null);
203+
parent::setFieldValue($field, null);
204204
} else {
205-
switch ($name) {
205+
switch ($field) {
206206
case 'id':
207207
case 'account_id':
208-
return parent::setFieldValue($name, (int) $value);
208+
return parent::setFieldValue($field, (int) $value);
209209
case 'day':
210-
return parent::setFieldValue($name, $this->getDateTimeValueInstanceFrom($value));
210+
return parent::setFieldValue($field, $this->getDateTimeValueInstanceFrom($value));
211211
case 'stats':
212-
return parent::setFieldValue($name, $this->isLoading() ? $value : json_encode($value));
212+
return parent::setFieldValue($field, $this->isLoading() ? $value : json_encode($value));
213213
default:
214214
if ($this->isLoading()) {
215-
return parent::setFieldValue($name, $value);
215+
return parent::setFieldValue($field, $value);
216216
} else {
217-
if ($this->isGeneratedField($name)) {
218-
throw new \LogicException("Generated field $name cannot be set by directly assigning a value");
217+
if ($this->isGeneratedField($field)) {
218+
throw new \LogicException("Generated field $field cannot be set by directly assigning a value");
219219
} else {
220-
throw new \InvalidArgumentException("Field $name does not exist in this table");
220+
throw new \InvalidArgumentException("Field $field does not exist in this table");
221221
}
222222
}
223223
}

test/src/Fixtures/Users/Base/User.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,28 +190,24 @@ public function &setPassword($value)
190190

191191
/**
192192
* Set value of specific field.
193-
*
194-
* @param string $name
195-
* @param mixed $value
196-
* @return $this
197193
*/
198-
public function &setFieldValue($name, $value)
194+
public function setFieldValue(string $field, mixed $value): static
199195
{
200196
if ($value === null) {
201-
parent::setFieldValue($name, null);
197+
parent::setFieldValue($field, null);
202198
} else {
203-
switch ($name) {
199+
switch ($field) {
204200
case 'id':
205-
return parent::setFieldValue($name, (int) $value);
201+
return parent::setFieldValue($field, (int) $value);
206202
case 'type':
207203
case 'first_name':
208204
case 'last_name':
209205
case 'email':
210206
case 'homepage_url':
211207
case 'password':
212-
return parent::setFieldValue($name, (string) $value);
208+
return parent::setFieldValue($field, (string) $value);
213209
default:
214-
throw new \InvalidArgumentException("Field $name does not exist in this table");
210+
throw new \InvalidArgumentException("Field $field does not exist in this table");
215211
}
216212
}
217213

test/src/Fixtures/Writers/BaseWriter.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,27 @@ public function &setUpdatedAt($value)
154154

155155
/**
156156
* Set value of specific field.
157-
*
158-
* @param string $name
159-
* @param mixed $value
160-
* @return mixed
161157
*/
162-
public function &setFieldValue($name, $value)
158+
public function setFieldValue(string $field, mixed $value): static
163159
{
164160
if ($value === null) {
165-
parent::setFieldValue($name, null);
161+
parent::setFieldValue($field, null);
166162
} else {
167-
switch ($name) {
163+
switch ($field) {
168164
case 'id':
169-
parent::setFieldValue($name, (int) $value);
165+
parent::setFieldValue($field, (int) $value);
170166
break;
171167
case 'name':
172-
parent::setFieldValue($name, (string) $value);
168+
parent::setFieldValue($field, (string) $value);
173169
break;
174170
case 'birthday':
175-
parent::setFieldValue($name, $this->getDateValueInstanceFrom($value));
171+
parent::setFieldValue($field, $this->getDateValueInstanceFrom($value));
176172
break;
177173
case 'created_at':
178174
case 'updated_at':
179-
return parent::setFieldValue($name, $this->getDateTimeValueInstanceFrom($value));
175+
return parent::setFieldValue($field, $this->getDateTimeValueInstanceFrom($value));
180176
default:
181-
throw new InvalidArgumentException("'$name' is not a known field");
177+
throw new InvalidArgumentException("'$field' is not a known field");
182178
}
183179
}
184180

0 commit comments

Comments
 (0)