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

[12.x] Improve performance using match expression #54481

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 5 additions & 7 deletions src/Illuminate/Http/Resources/MergeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ class MergeValue
*/
public function __construct($data)
{
if ($data instanceof Collection) {
$this->data = $data->all();
} elseif ($data instanceof JsonSerializable) {
$this->data = $data->jsonSerialize();
} else {
$this->data = $data;
}
$this->data = match (true) {
$data instanceof Collection => $data->all(),
$data instanceof JsonSerializable => $data->jsonSerialize(),
default => $data
};
Comment on lines +25 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

You could even do:

Suggested change
$this->data = match (true) {
$data instanceof Collection => $data->all(),
$data instanceof JsonSerializable => $data->jsonSerialize(),
default => $data
};
$this->data = match (get_class($data)) {
Collection::class => $data->all(),
JsonSerializable::class => $data->jsonSerialize(),
default => $data
};

}
}
15 changes: 6 additions & 9 deletions src/Illuminate/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,12 @@ protected function fireLogEvent($level, $message, array $context = [])
*/
protected function formatMessage($message)
{
if (is_array($message)) {
return var_export($message, true);
} elseif ($message instanceof Jsonable) {
return $message->toJson();
} elseif ($message instanceof Arrayable) {
return var_export($message->toArray(), true);
}

return (string) $message;
return match (true) {
is_array($message) => var_export($message, true),
$message instanceof Jsonable => $message->toJson(),
$message instanceof Arrayable => var_export($message->toArray(), true),
default => (string) $message
};
}

/**
Expand Down