Skip to content

Commit

Permalink
Merge pull request #8193 from kenjis/refactor-Model-if-conditions
Browse files Browse the repository at this point in the history
refactor: replace non-boolean if conditions in Model
  • Loading branch information
kenjis authored Nov 13, 2023
2 parents 22bf430 + 72e7695 commit 278265e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
7 changes: 1 addition & 6 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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[] = [
Expand Down
14 changes: 7 additions & 7 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/models/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 updating it (even ``$useTimestamps`` is enabled).

$deletedField
^^^^^^^^^^^^^
Expand Down

0 comments on commit 278265e

Please sign in to comment.