Skip to content

Commit

Permalink
Use more match statements
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jun 8, 2024
1 parent e5960b6 commit 5e87713
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 71 deletions.
17 changes: 8 additions & 9 deletions src/Cms/FileBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions src/Cms/FileModifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/Cms/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}

/**
Expand Down
33 changes: 14 additions & 19 deletions src/Cms/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -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()
};
}

/**
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions src/Cms/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
38 changes: 16 additions & 22 deletions src/Cms/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}

/**
Expand All @@ -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);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/Cms/UserActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5e87713

Please sign in to comment.