Skip to content

Commit 278265e

Browse files
authored
Merge pull request #8193 from kenjis/refactor-Model-if-conditions
refactor: replace non-boolean if conditions in Model
2 parents 22bf430 + 72e7695 commit 278265e

File tree

4 files changed

+11
-16
lines changed

4 files changed

+11
-16
lines changed

phpstan-baseline.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@
5656
'count' => 1,
5757
'path' => __DIR__ . '/system/BaseModel.php',
5858
];
59-
$ignoreErrors[] = [
60-
'message' => '#^Only booleans are allowed in &&, string given on the right side\\.$#',
61-
'count' => 7,
62-
'path' => __DIR__ . '/system/BaseModel.php',
63-
];
6459
$ignoreErrors[] = [
6560
'message' => '#^Only booleans are allowed in a ternary operator condition, array\\|null given\\.$#',
6661
'count' => 1,
@@ -2628,7 +2623,7 @@
26282623
];
26292624
$ignoreErrors[] = [
26302625
'message' => '#^Only booleans are allowed in &&, string given on the right side\\.$#',
2631-
'count' => 3,
2626+
'count' => 2,
26322627
'path' => __DIR__ . '/system/Model.php',
26332628
];
26342629
$ignoreErrors[] = [

system/BaseModel.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -770,11 +770,11 @@ public function insert($data = null, bool $returnID = true)
770770
// Set created_at and updated_at with same time
771771
$date = $this->setDate();
772772

773-
if ($this->useTimestamps && $this->createdField && ! array_key_exists($this->createdField, $data)) {
773+
if ($this->useTimestamps && $this->createdField !== '' && ! array_key_exists($this->createdField, $data)) {
774774
$data[$this->createdField] = $date;
775775
}
776776

777-
if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $data)) {
777+
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $data)) {
778778
$data[$this->updatedField] = $date;
779779
}
780780

@@ -857,11 +857,11 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
857857
// Set created_at and updated_at with same time
858858
$date = $this->setDate();
859859

860-
if ($this->useTimestamps && $this->createdField && ! array_key_exists($this->createdField, $row)) {
860+
if ($this->useTimestamps && $this->createdField !== '' && ! array_key_exists($this->createdField, $row)) {
861861
$row[$this->createdField] = $date;
862862
}
863863

864-
if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $row)) {
864+
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $row)) {
865865
$row[$this->updatedField] = $date;
866866
}
867867
}
@@ -929,7 +929,7 @@ public function update($id = null, $data = null): bool
929929
throw DataException::forEmptyDataset('update');
930930
}
931931

932-
if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $data)) {
932+
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $data)) {
933933
$data[$this->updatedField] = $this->setDate();
934934
}
935935

@@ -1014,7 +1014,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
10141014
$row[$index] = $updateIndex;
10151015
}
10161016

1017-
if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $row)) {
1017+
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $row)) {
10181018
$row[$this->updatedField] = $this->setDate();
10191019
}
10201020
}
@@ -1147,7 +1147,7 @@ public function replace(?array $data = null, bool $returnSQL = false)
11471147
return false;
11481148
}
11491149

1150-
if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, (array) $data)) {
1150+
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, (array) $data)) {
11511151
$data[$this->updatedField] = $this->setDate();
11521152
}
11531153

system/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ protected function doDelete($id = null, bool $purge = false)
435435

436436
$set[$this->deletedField] = $this->setDate();
437437

438-
if ($this->useTimestamps && $this->updatedField) {
438+
if ($this->useTimestamps && $this->updatedField !== '') {
439439
$set[$this->updatedField] = $this->setDate();
440440
}
441441

user_guide_src/source/models/model.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ $createdField
180180
^^^^^^^^^^^^^
181181

182182
Specifies which database field to use for data record create timestamp.
183-
Leave it empty to avoid updating it (even if ``$useTimestamps`` is enabled).
183+
Leave it empty (``''``) to avoid updating it (even if ``$useTimestamps`` is enabled).
184184

185185
$updatedField
186186
^^^^^^^^^^^^^
187187

188188
Specifies which database field should use for keep data record update timestamp.
189-
Leave it empty to avoid update it (even ``$useTimestamps`` is enabled).
189+
Leave it empty (``''``) to avoid updating it (even ``$useTimestamps`` is enabled).
190190

191191
$deletedField
192192
^^^^^^^^^^^^^

0 commit comments

Comments
 (0)