From d6adeb386c8323410756b635851a67425e96d5ba Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 24 Oct 2023 16:27:02 +0900 Subject: [PATCH 1/2] perf: defer instantiation of validation --- system/BaseModel.php | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/system/BaseModel.php b/system/BaseModel.php index 227b54b4b15f..a51ae1efbbe6 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -199,7 +199,7 @@ abstract class BaseModel /** * Our validator instance. * - * @var ValidationInterface + * @var ValidationInterface|null */ protected $validation; @@ -326,10 +326,6 @@ public function __construct(?ValidationInterface $validation = null) $this->tempUseSoftDeletes = $this->useSoftDeletes; $this->tempAllowCallbacks = $this->allowCallbacks; - /** - * @var ValidationInterface|null $validation - */ - $validation ??= Services::validation(null, false); $this->validation = $validation; $this->initialize(); @@ -1153,6 +1149,10 @@ public function replace(?array $data = null, bool $returnSQL = false) */ public function errors(bool $forceDB = false) { + if ($this->validation === null) { + return $this->doErrors(); + } + // Do we have validation errors? if (! $forceDB && ! $this->skipValidation && ($errors = $this->validation->getErrors())) { return $errors; @@ -1421,6 +1421,8 @@ public function setValidationRule(string $field, $fieldRules) // ValidationRules can be either a string, which is the group name, // or an array of rules. if (is_string($rules)) { + $this->ensureValidation(); + [$rules, $customErrors] = $this->validation->loadRuleGroup($rules); $this->validationRules = $rules; @@ -1455,9 +1457,13 @@ public function cleanRules(bool $choice = false) */ public function validate($data): bool { + if ($this->skipValidation || empty($data)) { + return true; + } + $rules = $this->getValidationRules(); - if ($this->skipValidation || empty($rules) || empty($data)) { + if (empty($rules)) { return true; } @@ -1474,6 +1480,8 @@ public function validate($data): bool return true; } + $this->ensureValidation(); + $this->validation->reset()->setRules($rules, $this->validationMessages); return $this->validation->run($data, null, $this->DBGroup); @@ -1492,6 +1500,8 @@ public function getValidationRules(array $options = []): array // ValidationRules can be either a string, which is the group name, // or an array of rules. if (is_string($rules)) { + $this->ensureValidation(); + [$rules, $customErrors] = $this->validation->loadRuleGroup($rules); $this->validationMessages += $customErrors; @@ -1506,6 +1516,13 @@ public function getValidationRules(array $options = []): array return $rules; } + protected function ensureValidation(): void + { + if ($this->validation === null) { + $this->validation = Services::validation(null, false); + } + } + /** * Returns the model's validation messages, so they * can be used elsewhere, if needed. From e8536c3f18b14629e508b03271a4fcdd9c79edef Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 25 Oct 2023 11:11:40 +0900 Subject: [PATCH 2/2] refactor: replace empty() --- phpstan-baseline.php | 2 +- system/BaseModel.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 7c60780cc31e..873cd7492294 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -33,7 +33,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 15, + 'count' => 14, 'path' => __DIR__ . '/system/BaseModel.php', ]; $ignoreErrors[] = [ diff --git a/system/BaseModel.php b/system/BaseModel.php index a51ae1efbbe6..4a34fe18fb27 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -1463,7 +1463,7 @@ public function validate($data): bool $rules = $this->getValidationRules(); - if (empty($rules)) { + if ($rules === []) { return true; }