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] Simplify conditional returns #53920

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 3 additions & 5 deletions src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ public function serialize($model, string $key, $value, array $attributes)

protected function getStorableEnumValue($enum)
{
if (is_string($enum) || is_int($enum)) {
return $enum;
}

return enum_value($enum);
return is_string($enum) || is_int($enum)
? $enum
: enum_value($enum);
}
};
}
Expand Down
8 changes: 3 additions & 5 deletions src/Illuminate/Database/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,9 @@ protected function wrapSegments($segments)
*/
protected function wrapValue($value)
{
if ($value !== '*') {
return '"'.str_replace('"', '""', $value).'"';
}

return $value;
return $value !== '*'
? '"'.str_replace('"', '""', $value).'"'
: $value;
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,13 +991,12 @@ protected function parseHttpOptions(array $options)
$options[$this->bodyFormat] = $this->pendingBody;
}

return (new Collection($options))->map(function ($value, $key) {
if ($key === 'json' && $value instanceof JsonSerializable) {
return $value;
}

return $value instanceof Arrayable ? $value->toArray() : $value;
})->all();
return (new Collection($options))
->map(fn ($value, $key) => match (true) {
$key === 'json' && $value instanceof JsonSerializable => $value,
$value instanceof Arrayable => $value->toArray(),
default => $value,
})->all();
}

/**
Expand Down
11 changes: 5 additions & 6 deletions src/Illuminate/Http/Middleware/SetCacheHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ public static function using($options)
}

return (new Collection($options))
->map(function ($value, $key) {
if (is_bool($value)) {
return $value ? $key : null;
}

return is_int($key) ? $value : "{$key}={$value}";
->map(fn ($value, $key) => match (true) {
$value === true => $key,
$value === false => null,
is_int($key) => $value,
default => "{$key}={$value}",
})
->filter()
->map(fn ($value) => Str::finish($value, ';'))
Expand Down
23 changes: 8 additions & 15 deletions src/Illuminate/Mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,14 @@ protected function addAddresses($address, $name, $type)
if (is_array($address)) {
$type = lcfirst($type);

$addresses = (new Collection($address))->map(function ($address, $key) {
if (is_string($key) && is_string($address)) {
return new Address($key, $address);
}

if (is_array($address)) {
return new Address($address['email'] ?? $address['address'], $address['name'] ?? null);
}

if (is_null($address)) {
return new Address($key);
}

return $address;
})->all();
$addresses = (new Collection($address))
->map(fn ($address, $key) => match (true) {
is_string($key) && is_string($address) => new Address($key, $address),
is_array($address) => new Address($address['email'] ?? $address['address'], $address['name'] ?? null),
is_null($address) => new Address($key),
default => $address,
})
->all();

$this->message->{"{$type}"}(...$addresses);
} else {
Expand Down