Skip to content

Commit 02e5a33

Browse files
committed
refactor: replace $data with $row
1 parent aefc438 commit 02e5a33

File tree

2 files changed

+89
-76
lines changed

2 files changed

+89
-76
lines changed

system/BaseModel.php

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,12 @@ abstract protected function doFirst();
401401
* Inserts data into the current database.
402402
* This method works only with dbCalls.
403403
*
404-
* @param array $data Data
404+
* @param array $row Row data
405+
* @phpstan-param row_array $row
405406
*
406407
* @return bool
407408
*/
408-
abstract protected function doInsert(array $data);
409+
abstract protected function doInsert(array $row);
409410

410411
/**
411412
* Compiles batch insert and runs the queries, validating each row prior.
@@ -424,10 +425,11 @@ abstract protected function doInsertBatch(?array $set = null, ?bool $escape = nu
424425
* Updates a single record in the database.
425426
* This method works only with dbCalls.
426427
*
427-
* @param array|int|string|null $id ID
428-
* @param array|null $data Data
428+
* @param array|int|string|null $id ID
429+
* @param array|null $row Row data
430+
* @phpstan-param row_array|null $row
429431
*/
430-
abstract protected function doUpdate($id = null, $data = null): bool;
432+
abstract protected function doUpdate($id = null, $row = null): bool;
431433

432434
/**
433435
* Compiles an update and runs the query.
@@ -510,15 +512,16 @@ abstract protected function idValue($data);
510512
* Public getter to return the id value using the idValue() method.
511513
* For example with SQL this will return $data->$this->primaryKey.
512514
*
513-
* @param array|object $data
515+
* @param array|object $row Row data
516+
* @phpstan-param row_array|object $row
514517
*
515518
* @return array|int|string|null
516519
*
517520
* @todo: Make abstract in version 5.0
518521
*/
519-
public function getIdValue($data)
522+
public function getIdValue($row)
520523
{
521-
return $this->idValue($data);
524+
return $this->idValue($row);
522525
}
523526

524527
/**
@@ -694,20 +697,21 @@ public function first()
694697
* you must ensure that the class will provide access to the class
695698
* variables, even if through a magic method.
696699
*
697-
* @param array|object $data Data
700+
* @param array|object $row Row data
701+
* @phpstan-param row_array|object $row
698702
*
699703
* @throws ReflectionException
700704
*/
701-
public function save($data): bool
705+
public function save($row): bool
702706
{
703-
if (empty($data)) {
707+
if (empty($row)) {
704708
return true;
705709
}
706710

707-
if ($this->shouldUpdate($data)) {
708-
$response = $this->update($this->getIdValue($data), $data);
711+
if ($this->shouldUpdate($row)) {
712+
$response = $this->update($this->getIdValue($row), $row);
709713
} else {
710-
$response = $this->insert($data, false);
714+
$response = $this->insert($row, false);
711715

712716
if ($response !== false) {
713717
$response = true;
@@ -721,11 +725,12 @@ public function save($data): bool
721725
* This method is called on save to determine if entry have to be updated.
722726
* If this method returns false insert operation will be executed
723727
*
724-
* @param array|object $data Data
728+
* @param array|object $row Row data
729+
* @phpstan-param row_array|object $row
725730
*/
726-
protected function shouldUpdate($data): bool
731+
protected function shouldUpdate($row): bool
727732
{
728-
return ! empty($this->getIdValue($data));
733+
return ! empty($this->getIdValue($row));
729734
}
730735

731736
/**
@@ -777,7 +782,7 @@ public function insert($row = null, bool $returnID = true)
777782
$row = $this->doProtectFieldsForInsert($row);
778783

779784
// doProtectFields() can further remove elements from
780-
// $data so we need to check for empty dataset again
785+
// $row, so we need to check for empty dataset again
781786
if (! $this->allowEmptyInserts && empty($row)) {
782787
throw DataException::forEmptyDataset('insert');
783788
}
@@ -868,7 +873,7 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
868873

869874
if (is_array($set)) {
870875
foreach ($set as &$row) {
871-
// If $data is using a custom class with public or protected
876+
// If $row is using a custom class with public or protected
872877
// properties representing the collection elements, we need to grab
873878
// them as an array.
874879
if (is_object($row) && ! $row instanceof stdClass) {
@@ -959,7 +964,7 @@ public function update($id = null, $row = null): bool
959964
$row = $this->doProtectFields($row);
960965

961966
// doProtectFields() can further remove elements from
962-
// $data, so we need to check for empty dataset again
967+
// $row, so we need to check for empty dataset again
963968
if (empty($row)) {
964969
throw DataException::forEmptyDataset('update');
965970
}
@@ -1008,7 +1013,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
10081013
{
10091014
if (is_array($set)) {
10101015
foreach ($set as &$row) {
1011-
// If $data is using a custom class with public or protected
1016+
// If $row is using a custom class with public or protected
10121017
// properties representing the collection elements, we need to grab
10131018
// them as an array.
10141019
if (is_object($row) && ! $row instanceof stdClass) {
@@ -1270,46 +1275,48 @@ public function protect(bool $protect = true)
12701275
* Ensures that only the fields that are allowed to be updated are
12711276
* in the data array.
12721277
*
1273-
* Used by update() and updateBatch() to protect against mass assignment
1274-
* vulnerabilities.
1278+
* @used-by update() to protect against mass assignment vulnerabilities.
1279+
* @used-by updateBatch() to protect against mass assignment vulnerabilities.
12751280
*
1276-
* @param array $data Data
1281+
* @param array $row Row data
1282+
* @phpstan-param row_array $row
12771283
*
12781284
* @throws DataException
12791285
*/
1280-
protected function doProtectFields(array $data): array
1286+
protected function doProtectFields(array $row): array
12811287
{
12821288
if (! $this->protectFields) {
1283-
return $data;
1289+
return $row;
12841290
}
12851291

12861292
if (empty($this->allowedFields)) {
12871293
throw DataException::forInvalidAllowedFields(static::class);
12881294
}
12891295

1290-
foreach (array_keys($data) as $key) {
1296+
foreach (array_keys($row) as $key) {
12911297
if (! in_array($key, $this->allowedFields, true)) {
1292-
unset($data[$key]);
1298+
unset($row[$key]);
12931299
}
12941300
}
12951301

1296-
return $data;
1302+
return $row;
12971303
}
12981304

12991305
/**
13001306
* Ensures that only the fields that are allowed to be inserted are in
13011307
* the data array.
13021308
*
1303-
* Used by insert() and insertBatch() to protect against mass assignment
1304-
* vulnerabilities.
1309+
* @used-by insert() to protect against mass assignment vulnerabilities.
1310+
* @used-by insertBatch() to protect against mass assignment vulnerabilities.
13051311
*
1306-
* @param array $data Data
1312+
* @param array $row Row data
1313+
* @phpstan-param row_array $row
13071314
*
13081315
* @throws DataException
13091316
*/
1310-
protected function doProtectFieldsForInsert(array $data): array
1317+
protected function doProtectFieldsForInsert(array $row): array
13111318
{
1312-
return $this->doProtectFields($data);
1319+
return $this->doProtectFields($row);
13131320
}
13141321

13151322
/**
@@ -1494,25 +1501,26 @@ public function cleanRules(bool $choice = false)
14941501
}
14951502

14961503
/**
1497-
* Validate the data against the validation rules (or the validation group)
1504+
* Validate the row data against the validation rules (or the validation group)
14981505
* specified in the class property, $validationRules.
14991506
*
1500-
* @param array|object $data Data
1507+
* @param array|object $row Row data
1508+
* @phpstan-param row_array|object $row
15011509
*/
1502-
public function validate($data): bool
1510+
public function validate($row): bool
15031511
{
15041512
$rules = $this->getValidationRules();
15051513

1506-
if ($this->skipValidation || empty($rules) || empty($data)) {
1514+
if ($this->skipValidation || empty($rules) || empty($row)) {
15071515
return true;
15081516
}
15091517

15101518
// Validation requires array, so cast away.
1511-
if (is_object($data)) {
1512-
$data = (array) $data;
1519+
if (is_object($row)) {
1520+
$row = (array) $row;
15131521
}
15141522

1515-
$rules = $this->cleanValidationRules ? $this->cleanValidationRules($rules, $data) : $rules;
1523+
$rules = $this->cleanValidationRules ? $this->cleanValidationRules($rules, $row) : $rules;
15161524

15171525
// If no data existed that needs validation
15181526
// our job is done here.
@@ -1522,7 +1530,7 @@ public function validate($data): bool
15221530

15231531
$this->validation->reset()->setRules($rules, $this->validationMessages);
15241532

1525-
return $this->validation->run($data, null, $this->DBGroup);
1533+
return $this->validation->run($row, null, $this->DBGroup);
15261534
}
15271535

15281536
/**
@@ -1566,17 +1574,18 @@ public function getValidationMessages(): array
15661574
* currently so that rules don't block updating when only updating
15671575
* a partial row.
15681576
*
1569-
* @param array $rules Array containing field name and rule
1570-
* @param array|null $data Data
1577+
* @param array $rules Array containing field name and rule
1578+
* @param array $row Row data (@TODO Remove null in param type)
1579+
* @phpstan-param row_array $row
15711580
*/
1572-
protected function cleanValidationRules(array $rules, ?array $data = null): array
1581+
protected function cleanValidationRules(array $rules, ?array $row = null): array
15731582
{
1574-
if (empty($data)) {
1583+
if (empty($row)) {
15751584
return [];
15761585
}
15771586

15781587
foreach (array_keys($rules) as $field) {
1579-
if (! array_key_exists($field, $data)) {
1588+
if (! array_key_exists($field, $row)) {
15801589
unset($rules[$field]);
15811590
}
15821591
}
@@ -1767,7 +1776,7 @@ protected function transformDataToArray($row, string $type): array
17671776
throw DataException::forEmptyDataset($type);
17681777
}
17691778

1770-
// If $data is using a custom class with public or protected
1779+
// If $row is using a custom class with public or protected
17711780
// properties representing the collection elements, we need to grab
17721781
// them as an array.
17731782
if (is_object($row) && ! $row instanceof stdClass) {
@@ -1785,7 +1794,7 @@ protected function transformDataToArray($row, string $type): array
17851794
$row = (array) $row;
17861795
}
17871796

1788-
// If it's still empty here, means $data is no change or is empty object
1797+
// If it's still empty here, means $row is no change or is empty object
17891798
if (! $this->allowEmptyInserts && empty($row)) {
17901799
throw DataException::forEmptyDataset($type);
17911800
}

0 commit comments

Comments
 (0)