Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: defer instantiation of Validation in Model #8087

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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