Skip to content

Commit 4bfa67c

Browse files
committed
refactor: replace $data with $row
1 parent 6b12b57 commit 4bfa67c

File tree

2 files changed

+94
-81
lines changed

2 files changed

+94
-81
lines changed

system/BaseModel.php

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

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

431433
/**
432434
* Compiles an update and runs the query.
@@ -509,15 +511,16 @@ abstract protected function idValue($data);
509511
* Public getter to return the id value using the idValue() method.
510512
* For example with SQL this will return $data->$this->primaryKey.
511513
*
512-
* @param array|object $data
514+
* @param array|object $row Row data
515+
* @phpstan-param row_array|object $row
513516
*
514517
* @return array|int|string|null
515518
*
516519
* @todo: Make abstract in version 5.0
517520
*/
518-
public function getIdValue($data)
521+
public function getIdValue($row)
519522
{
520-
return $this->idValue($data);
523+
return $this->idValue($row);
521524
}
522525

523526
/**
@@ -693,20 +696,21 @@ public function first()
693696
* you must ensure that the class will provide access to the class
694697
* variables, even if through a magic method.
695698
*
696-
* @param array|object $data Data
699+
* @param array|object $row Row data
700+
* @phpstan-param row_array|object $row
697701
*
698702
* @throws ReflectionException
699703
*/
700-
public function save($data): bool
704+
public function save($row): bool
701705
{
702-
if (empty($data)) {
706+
if (empty($row)) {
703707
return true;
704708
}
705709

706-
if ($this->shouldUpdate($data)) {
707-
$response = $this->update($this->getIdValue($data), $data);
710+
if ($this->shouldUpdate($row)) {
711+
$response = $this->update($this->getIdValue($row), $row);
708712
} else {
709-
$response = $this->insert($data, false);
713+
$response = $this->insert($row, false);
710714

711715
if ($response !== false) {
712716
$response = true;
@@ -720,11 +724,12 @@ public function save($data): bool
720724
* This method is called on save to determine if entry have to be updated.
721725
* If this method returns false insert operation will be executed
722726
*
723-
* @param array|object $data Data
727+
* @param array|object $row Row data
728+
* @phpstan-param row_array|object $row
724729
*/
725-
protected function shouldUpdate($data): bool
730+
protected function shouldUpdate($row): bool
726731
{
727-
return ! empty($this->getIdValue($data));
732+
return ! empty($this->getIdValue($row));
728733
}
729734

730735
/**
@@ -776,7 +781,7 @@ public function insert($row = null, bool $returnID = true)
776781
$row = $this->doProtectFieldsForInsert($row);
777782

778783
// doProtectFields() can further remove elements from
779-
// $data so we need to check for empty dataset again
784+
// $row, so we need to check for empty dataset again
780785
if (! $this->allowEmptyInserts && empty($row)) {
781786
throw DataException::forEmptyDataset('insert');
782787
}
@@ -867,7 +872,7 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
867872

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

960965
// doProtectFields() can further remove elements from
961-
// $data, so we need to check for empty dataset again
966+
// $row, so we need to check for empty dataset again
962967
if (empty($row)) {
963968
throw DataException::forEmptyDataset('update');
964969
}
@@ -1007,7 +1012,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
10071012
{
10081013
if (is_array($set)) {
10091014
foreach ($set as &$row) {
1010-
// If $data is using a custom class with public or protected
1015+
// If $row is using a custom class with public or protected
10111016
// properties representing the collection elements, we need to grab
10121017
// them as an array.
10131018
if (is_object($row) && ! $row instanceof stdClass) {
@@ -1269,46 +1274,48 @@ public function protect(bool $protect = true)
12691274
* Ensures that only the fields that are allowed to be updated are
12701275
* in the data array.
12711276
*
1272-
* Used by update() and updateBatch() to protect against mass assignment
1273-
* vulnerabilities.
1277+
* @used-by update() to protect against mass assignment vulnerabilities.
1278+
* @used-by updateBatch() to protect against mass assignment vulnerabilities.
12741279
*
1275-
* @param array $data Data
1280+
* @param array $row Row data
1281+
* @phpstan-param row_array $row
12761282
*
12771283
* @throws DataException
12781284
*/
1279-
protected function doProtectFields(array $data): array
1285+
protected function doProtectFields(array $row): array
12801286
{
12811287
if (! $this->protectFields) {
1282-
return $data;
1288+
return $row;
12831289
}
12841290

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

1289-
foreach (array_keys($data) as $key) {
1295+
foreach (array_keys($row) as $key) {
12901296
if (! in_array($key, $this->allowedFields, true)) {
1291-
unset($data[$key]);
1297+
unset($row[$key]);
12921298
}
12931299
}
12941300

1295-
return $data;
1301+
return $row;
12961302
}
12971303

12981304
/**
12991305
* Ensures that only the fields that are allowed to be inserted are in
13001306
* the data array.
13011307
*
1302-
* Used by insert() and insertBatch() to protect against mass assignment
1303-
* vulnerabilities.
1308+
* @used-by insert() to protect against mass assignment vulnerabilities.
1309+
* @used-by insertBatch() to protect against mass assignment vulnerabilities.
13041310
*
1305-
* @param array $data Data
1311+
* @param array $row Row data
1312+
* @phpstan-param row_array $row
13061313
*
13071314
* @throws DataException
13081315
*/
1309-
protected function doProtectFieldsForInsert(array $data): array
1316+
protected function doProtectFieldsForInsert(array $row): array
13101317
{
1311-
return $this->doProtectFields($data);
1318+
return $this->doProtectFields($row);
13121319
}
13131320

13141321
/**
@@ -1493,25 +1500,26 @@ public function cleanRules(bool $choice = false)
14931500
}
14941501

14951502
/**
1496-
* Validate the data against the validation rules (or the validation group)
1503+
* Validate the row data against the validation rules (or the validation group)
14971504
* specified in the class property, $validationRules.
14981505
*
1499-
* @param array|object $data Data
1506+
* @param array|object $row Row data
1507+
* @phpstan-param row_array|object $row
15001508
*/
1501-
public function validate($data): bool
1509+
public function validate($row): bool
15021510
{
15031511
$rules = $this->getValidationRules();
15041512

1505-
if ($this->skipValidation || empty($rules) || empty($data)) {
1513+
if ($this->skipValidation || empty($rules) || empty($row)) {
15061514
return true;
15071515
}
15081516

15091517
// Validation requires array, so cast away.
1510-
if (is_object($data)) {
1511-
$data = (array) $data;
1518+
if (is_object($row)) {
1519+
$row = (array) $row;
15121520
}
15131521

1514-
$rules = $this->cleanValidationRules ? $this->cleanValidationRules($rules, $data) : $rules;
1522+
$rules = $this->cleanValidationRules ? $this->cleanValidationRules($rules, $row) : $rules;
15151523

15161524
// If no data existed that needs validation
15171525
// our job is done here.
@@ -1521,7 +1529,7 @@ public function validate($data): bool
15211529

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

1524-
return $this->validation->run($data, null, $this->DBGroup);
1532+
return $this->validation->run($row, null, $this->DBGroup);
15251533
}
15261534

15271535
/**
@@ -1565,17 +1573,18 @@ public function getValidationMessages(): array
15651573
* currently so that rules don't block updating when only updating
15661574
* a partial row.
15671575
*
1568-
* @param array $rules Array containing field name and rule
1569-
* @param array|null $data Data
1576+
* @param array $rules Array containing field name and rule
1577+
* @param array $row Row data (@TODO Remove null in param type)
1578+
* @phpstan-param row_array $row
15701579
*/
1571-
protected function cleanValidationRules(array $rules, ?array $data = null): array
1580+
protected function cleanValidationRules(array $rules, ?array $row = null): array
15721581
{
1573-
if (empty($data)) {
1582+
if (empty($row)) {
15741583
return [];
15751584
}
15761585

15771586
foreach (array_keys($rules) as $field) {
1578-
if (! array_key_exists($field, $data)) {
1587+
if (! array_key_exists($field, $row)) {
15791588
unset($rules[$field]);
15801589
}
15811590
}
@@ -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)