Skip to content
Merged
Changes from 1 commit
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
18 changes: 7 additions & 11 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,26 +363,22 @@ protected static function bootTraits()
static::$traitInitializers[$class] = [];

$uses = class_uses_recursive($class);

$conventionalBootMethods = array_map(static fn ($trait) => 'boot'.class_basename($trait), $uses);
$conventionalInitMethods = array_map(static fn ($trait) => 'initialize'.class_basename($trait), $uses);

foreach ((new ReflectionClass($class))->getMethods() as $method) {
if (
! in_array($method->getName(), $booted) &&
if (! in_array($method->getName(), $booted) &&
$method->isStatic() &&
(
in_array($method->getName(), $conventionalBootMethods) ||
$method->getAttributes(Boot::class) !== []
)
) {
(in_array($method->getName(), $conventionalBootMethods) ||
$method->getAttributes(Boot::class) !== [])) {
$method->invoke(null);

$booted[] = $method->getName();
}

if (
in_array($method->getName(), $conventionalInitMethods) ||
$method->getAttributes(Initialize::class) !== []
) {
if (in_array($method->getName(), $conventionalInitMethods) ||
$method->getAttributes(Initialize::class) !== []) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make the ifs a whole lot more readable if we moved logic out of there:

Suggested change
foreach ((new ReflectionClass($class))->getMethods() as $method) {
if (
! in_array($method->getName(), $booted) &&
if (! in_array($method->getName(), $booted) &&
$method->isStatic() &&
(
in_array($method->getName(), $conventionalBootMethods) ||
$method->getAttributes(Boot::class) !== []
)
) {
(in_array($method->getName(), $conventionalBootMethods) ||
$method->getAttributes(Boot::class) !== [])) {
$method->invoke(null);
$booted[] = $method->getName();
}
if (
in_array($method->getName(), $conventionalInitMethods) ||
$method->getAttributes(Initialize::class) !== []
) {
if (in_array($method->getName(), $conventionalInitMethods) ||
$method->getAttributes(Initialize::class) !== []) {
$isBootedMethod = in_array($method->getName(), $booted);
$isConventionalBootMethod = in_array($method->getName(), $conventionalBootMethods);
$hasBootAttributes = $method->getAttributes(Boot::class) !== [];
$isConventionalInitMethod = in_array($method->getName(), $conventionalInitMethods);
$hasInitMethods = $method->getAttributes(Initialize::class) !== [];
foreach ((new ReflectionClass($class))->getMethods() as $method) {
if (!$isBootedMethod &&
$method->isStatic() &&
($isConventionalBootMethod || $hasBootAttributes)) {
$method->invoke(null);
$booted[] = $method->getName();
}
if ($isConventionalInitMethod || $hasInitMethods) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #56524

static::$traitInitializers[$class][] = $method->getName();
}
}
Expand Down