From 5e8771389f3a464e39a849ca399e0dc9b1cc28f6 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Sat, 8 Jun 2024 20:37:08 +0200 Subject: [PATCH] Use more match statements --- src/Cms/FileBlueprint.php | 17 ++++++++-------- src/Cms/FileModifications.php | 11 +++++----- src/Cms/License.php | 5 ++++- src/Cms/Loader.php | 33 +++++++++++++----------------- src/Cms/Page.php | 9 ++++----- src/Cms/Roles.php | 9 ++++----- src/Cms/Site.php | 38 +++++++++++++++-------------------- src/Cms/UserActions.php | 9 ++++----- 8 files changed, 60 insertions(+), 71 deletions(-) diff --git a/src/Cms/FileBlueprint.php b/src/Cms/FileBlueprint.php index d7287ac381..7301121bdc 100644 --- a/src/Cms/FileBlueprint.php +++ b/src/Cms/FileBlueprint.php @@ -104,12 +104,11 @@ public function acceptMime(): string } if ($restrictions !== []) { - if (count($restrictions) > 1) { - // only return the MIME types that are allowed by all restrictions - $mimes = array_intersect(...$restrictions); - } else { - $mimes = $restrictions[0]; - } + // only return the MIME types that are allowed by all restrictions + $mimes = match (count($restrictions) > 1) { + true => array_intersect(...$restrictions), + false => $restrictions[0] + }; // filter out empty MIME types and duplicates return implode(', ', array_filter(array_unique($mimes))); @@ -190,13 +189,13 @@ public function acceptAttribute(): string protected function normalizeAccept(mixed $accept = null): array { $accept = match (true) { - is_string($accept) => ['mime' => $accept], + is_string($accept) => ['mime' => $accept], // explicitly no restrictions at all - $accept === true => ['mime' => null], + $accept === true => ['mime' => null], // no custom restrictions empty($accept) === true => [], // custom restrictions - default => $accept + default => $accept }; $accept = array_change_key_case($accept); diff --git a/src/Cms/FileModifications.php b/src/Cms/FileModifications.php index 38629ab8ff..842e04314e 100644 --- a/src/Cms/FileModifications.php +++ b/src/Cms/FileModifications.php @@ -184,11 +184,12 @@ public function thumb( // fallback to content file options if (($options['crop'] ?? false) === true) { - if ($this instanceof ModelWithContent === true) { - $options['crop'] = $this->focus()->value() ?? 'center'; - } else { - $options['crop'] = 'center'; - } + $options['crop'] = match (true) { + $this instanceof ModelWithContent + => $this->focus()->value() ?? 'center', + default + => 'center' + }; } // fallback to global config options diff --git a/src/Cms/License.php b/src/Cms/License.php index f81616b16e..2eb3a425c4 100644 --- a/src/Cms/License.php +++ b/src/Cms/License.php @@ -43,7 +43,10 @@ public function __construct( protected string|null $signature = null, ) { // normalize the email address - $this->email = $this->email === null ? null : $this->normalizeEmail($this->email); + $this->email = match($email) { + null => null, + default => $this->normalizeEmail($email) + }; } /** diff --git a/src/Cms/Loader.php b/src/Cms/Loader.php index 1dd70c0997..1fcc06b71d 100644 --- a/src/Cms/Loader.php +++ b/src/Cms/Loader.php @@ -28,20 +28,10 @@ */ class Loader { - /** - * @var \Kirby\Cms\App - */ - protected $kirby; - - /** - * @var bool - */ - protected $withPlugins; - - public function __construct(App $kirby, bool $withPlugins = true) - { - $this->kirby = $kirby; - $this->withPlugins = $withPlugins; + public function __construct( + protected App $kirby, + protected bool $withPlugins = true + ) { } /** @@ -59,7 +49,10 @@ public function area(string $name): array|null public function areas(): array { $areas = []; - $extensions = $this->withPlugins === true ? $this->kirby->extensions('areas') : []; + $extensions = match ($this->withPlugins) { + true => $this->kirby->extensions('areas'), + false => [] + }; // load core areas and extend them with elements // from plugins if they exist @@ -117,7 +110,10 @@ public function extension(string $type, string $name): mixed */ public function extensions(string $type): array { - return $this->withPlugins === false ? $this->kirby->core()->$type() : $this->kirby->extensions($type); + return match ($this->withPlugins) { + true => $this->kirby->extensions($type), + false => $this->kirby->core()->$type() + }; } /** @@ -170,12 +166,11 @@ public function resolveAll(array $items): array */ public function resolveArea(string|array|Closure $area): array { - $area = $this->resolve($area); - $dropdowns = $area['dropdowns'] ?? []; + $area = $this->resolve($area); // convert closure dropdowns to an array definition // otherwise they cannot be merged properly later - foreach ($dropdowns as $key => $dropdown) { + foreach ($area['dropdowns'] ?? [] as $key => $dropdown) { if ($dropdown instanceof Closure) { $area['dropdowns'][$key] = [ 'options' => $dropdown diff --git a/src/Cms/Page.php b/src/Cms/Page.php index 46c1ea749f..28fd385eef 100644 --- a/src/Cms/Page.php +++ b/src/Cms/Page.php @@ -379,11 +379,10 @@ public function diruri(): string return $this->diruri; } - if ($this->isDraft() === true) { - $dirname = '_drafts/' . $this->dirname(); - } else { - $dirname = $this->dirname(); - } + $dirname = match ($this->isDraft()) { + true => '_drafts/' . $this->dirname(), + false => $this->dirname() + }; if ($parent = $this->parent()) { return $this->diruri = $parent->diruri() . '/' . $dirname; diff --git a/src/Cms/Roles.php b/src/Cms/Roles.php index 276868d236..1fa8986800 100644 --- a/src/Cms/Roles.php +++ b/src/Cms/Roles.php @@ -108,11 +108,10 @@ public static function load(string|null $root = null, array $inject = []): stati $blueprint = $blueprint($kirby); } - if (is_array($blueprint) === true) { - $role = Role::factory($blueprint, $inject); - } else { - $role = Role::load($blueprint, $inject); - } + $role = match (is_array($blueprint)) { + true => Role::factory($blueprint, $inject), + false => Role::load($blueprint, $inject) + }; $roles->set($role->id(), $role); } diff --git a/src/Cms/Site.php b/src/Cms/Site.php index 074da1eed8..9c305db78f 100644 --- a/src/Cms/Site.php +++ b/src/Cms/Site.php @@ -353,19 +353,11 @@ public function permissions(): SitePermissions */ public function previewUrl(): string|null { - $preview = $this->blueprint()->preview(); - - if ($preview === false) { - return null; - } - - if ($preview === true) { - $url = $this->url(); - } else { - $url = $preview; - } - - return $url; + return match ($preview = $this->blueprint()->preview()) { + true => $this->url(), + false => null, + default => $preview + }; } /** @@ -389,8 +381,10 @@ protected function rules(): SiteRules /** * Search all pages in the site */ - public function search(string|null $query = null, string|array $params = []): Pages - { + public function search( + string|null $query = null, + string|array $params = [] + ): Pages { return $this->index()->search($query, $params); } @@ -402,8 +396,10 @@ public function search(string|null $query = null, string|array $params = []): Pa protected function setBlueprint(array|null $blueprint = null): static { if ($blueprint !== null) { - $blueprint['model'] = $this; - $this->blueprint = new SiteBlueprint($blueprint); + $this->blueprint = new SiteBlueprint([ + 'model' => $this, + ...$blueprint + ]); } return $this; @@ -447,11 +443,9 @@ public function urlForLanguage( string|null $languageCode = null, array|null $options = null ): string { - if ($language = $this->kirby()->language($languageCode)) { - return $language->url(); - } - - return $this->kirby()->url(); + return + $this->kirby()->language($languageCode)?->url() ?? + $this->kirby()->url(); } /** diff --git a/src/Cms/UserActions.php b/src/Cms/UserActions.php index 10246b1f5a..8ab50a8840 100644 --- a/src/Cms/UserActions.php +++ b/src/Cms/UserActions.php @@ -226,11 +226,10 @@ public static function create(array|null $props = null): User $user->writePassword($user->password()); // always create users in the default language - if ($user->kirby()->multilang() === true) { - $languageCode = $user->kirby()->defaultLanguage()->code(); - } else { - $languageCode = null; - } + $languageCode = match ($user->kirby()->multilang()) { + true => $user->kirby()->defaultLanguage()->code(), + false => null + }; // add the user to users collection $user->kirby()->users()->add($user);