Skip to content

Commit 2b1c604

Browse files
committed
Attempt to fix CPD by introducing a common casting function.
1 parent 252cd49 commit 2b1c604

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

system/BaseModel.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -911,16 +911,7 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
911911
foreach ($set as &$row) {
912912
// If casts are used, convert the data first
913913
if ($this->useCasts()) {
914-
if (is_array($row)) {
915-
$row = $this->converter->toDataSource($row);
916-
} elseif ($row instanceof stdClass) {
917-
$row = (array) $row;
918-
$row = $this->converter->toDataSource($row);
919-
} elseif ($row instanceof Entity) {
920-
$row = $this->converter->extract($row);
921-
} elseif (is_object($row)) {
922-
$row = $this->converter->extract($row);
923-
}
914+
$row = $this->performCasting($row);
924915
} elseif (is_object($row) && ! $row instanceof stdClass) {
925916
// If $row is using a custom class with public or protected
926917
// properties representing the collection elements, we need to grab
@@ -1065,16 +1056,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
10651056
foreach ($set as &$row) {
10661057
// If casts are used, convert the data first
10671058
if ($this->useCasts()) {
1068-
if (is_array($row)) {
1069-
$row = $this->converter->toDataSource($row);
1070-
} elseif ($row instanceof stdClass) {
1071-
$row = (array) $row;
1072-
$row = $this->converter->toDataSource($row);
1073-
} elseif ($row instanceof Entity) {
1074-
$row = $this->converter->extract($row);
1075-
} elseif (is_object($row)) {
1076-
$row = $this->converter->extract($row);
1077-
}
1059+
$row = $this->performCasting($row);
10781060
} elseif (is_object($row) && ! $row instanceof stdClass) {
10791061
// If $row is using a custom class with public or protected
10801062
// properties representing the collection elements, we need to grab
@@ -1721,6 +1703,39 @@ protected function trigger(string $event, array $eventData)
17211703
return $eventData;
17221704
}
17231705

1706+
/**
1707+
* If the model is using casts, this will convert the data
1708+
* in $row according to the rules defined in `$casts`.
1709+
*
1710+
* @param object|row_array|null $row Row data
1711+
*
1712+
* @return object|row_array|null Converted row data
1713+
*
1714+
* @used-by insertBatch()
1715+
* @used-by updateBatch()
1716+
*
1717+
* @deprecated Since 4.6.4, temporary solution - will be removed in 4.7
1718+
*/
1719+
protected function performCasting(array|object|null $row = null): array|object|null
1720+
{
1721+
if (! $this->useCasts()) {
1722+
return $row;
1723+
}
1724+
1725+
if (is_array($row)) {
1726+
$row = $this->converter->toDataSource($row);
1727+
} elseif ($row instanceof stdClass) {
1728+
$row = (array) $row;
1729+
$row = $this->converter->toDataSource($row);
1730+
} elseif ($row instanceof Entity) {
1731+
$row = $this->converter->extract($row);
1732+
} elseif (is_object($row)) {
1733+
$row = $this->converter->extract($row);
1734+
}
1735+
1736+
return $row;
1737+
}
1738+
17241739
/**
17251740
* Sets the return type of the results to be as an associative array.
17261741
*

0 commit comments

Comments
 (0)