From 1a25f1bfd80e47a29da995cfeed2f9ec05be410a Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 12 Nov 2023 09:16:28 +0900 Subject: [PATCH 1/3] refactor: replace non bool if conditions --- phpstan-baseline.php | 7 +------ system/BaseModel.php | 14 +++++++------- system/Model.php | 2 +- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 3cbe5a4732fc..beb2d85a4d84 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -56,11 +56,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/BaseModel.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Only booleans are allowed in &&, string given on the right side\\.$#', - 'count' => 7, - 'path' => __DIR__ . '/system/BaseModel.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a ternary operator condition, array\\|null given\\.$#', 'count' => 1, @@ -2628,7 +2623,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in &&, string given on the right side\\.$#', - 'count' => 3, + 'count' => 2, 'path' => __DIR__ . '/system/Model.php', ]; $ignoreErrors[] = [ diff --git a/system/BaseModel.php b/system/BaseModel.php index 80baaf3fb024..e8e746e64c25 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -770,11 +770,11 @@ public function insert($data = null, bool $returnID = true) // Set created_at and updated_at with same time $date = $this->setDate(); - if ($this->useTimestamps && $this->createdField && ! array_key_exists($this->createdField, $data)) { + if ($this->useTimestamps && $this->createdField !== '' && ! array_key_exists($this->createdField, $data)) { $data[$this->createdField] = $date; } - if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $data)) { + if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $data)) { $data[$this->updatedField] = $date; } @@ -857,11 +857,11 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch // Set created_at and updated_at with same time $date = $this->setDate(); - if ($this->useTimestamps && $this->createdField && ! array_key_exists($this->createdField, $row)) { + if ($this->useTimestamps && $this->createdField !== '' && ! array_key_exists($this->createdField, $row)) { $row[$this->createdField] = $date; } - if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $row)) { + if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $row)) { $row[$this->updatedField] = $date; } } @@ -929,7 +929,7 @@ public function update($id = null, $data = null): bool throw DataException::forEmptyDataset('update'); } - if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $data)) { + if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $data)) { $data[$this->updatedField] = $this->setDate(); } @@ -1014,7 +1014,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc $row[$index] = $updateIndex; } - if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $row)) { + if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $row)) { $row[$this->updatedField] = $this->setDate(); } } @@ -1147,7 +1147,7 @@ public function replace(?array $data = null, bool $returnSQL = false) return false; } - if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, (array) $data)) { + if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, (array) $data)) { $data[$this->updatedField] = $this->setDate(); } diff --git a/system/Model.php b/system/Model.php index e53c0aae4bd1..1873804a3c2d 100644 --- a/system/Model.php +++ b/system/Model.php @@ -435,7 +435,7 @@ protected function doDelete($id = null, bool $purge = false) $set[$this->deletedField] = $this->setDate(); - if ($this->useTimestamps && $this->updatedField) { + if ($this->useTimestamps && $this->updatedField !== '') { $set[$this->updatedField] = $this->setDate(); } From 698a30569d65d5ac03356828e945ca4c060e60c4 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 12 Nov 2023 09:17:17 +0900 Subject: [PATCH 2/3] docs: clarify what "empty" is --- user_guide_src/source/models/model.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index db4ecd80b59d..43fdbb9d6f18 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -180,13 +180,13 @@ $createdField ^^^^^^^^^^^^^ Specifies which database field to use for data record create timestamp. -Leave it empty to avoid updating it (even if ``$useTimestamps`` is enabled). +Leave it empty (``''``) to avoid updating it (even if ``$useTimestamps`` is enabled). $updatedField ^^^^^^^^^^^^^ Specifies which database field should use for keep data record update timestamp. -Leave it empty to avoid update it (even ``$useTimestamps`` is enabled). +Leave it empty (``''``) to avoid update it (even ``$useTimestamps`` is enabled). $deletedField ^^^^^^^^^^^^^ From 72e769562557207ccdcf69f746d1d39869608981 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 12 Nov 2023 09:20:16 +0900 Subject: [PATCH 3/3] docs: fix typo --- user_guide_src/source/models/model.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index 43fdbb9d6f18..6a2700520fd0 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -186,7 +186,7 @@ $updatedField ^^^^^^^^^^^^^ Specifies which database field should use for keep data record update timestamp. -Leave it empty (``''``) to avoid update it (even ``$useTimestamps`` is enabled). +Leave it empty (``''``) to avoid updating it (even ``$useTimestamps`` is enabled). $deletedField ^^^^^^^^^^^^^