Skip to content

Commit

Permalink
Merge pull request #8087 from kenjis/perf-model-defer-validation
Browse files Browse the repository at this point in the history
perf: defer instantiation of Validation in Model
  • Loading branch information
kenjis authored Oct 26, 2023
2 parents 86a84ce + e8536c3 commit bfd7719
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down
29 changes: 23 additions & 6 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ abstract class BaseModel
/**
* Our validator instance.
*
* @var ValidationInterface
* @var ValidationInterface|null
*/
protected $validation;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ($rules === []) {
return true;
}

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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.
Expand Down

0 comments on commit bfd7719

Please sign in to comment.