Skip to content

Commit

Permalink
Improve cs
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jun 8, 2024
1 parent b6e18b6 commit e5960b6
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 83 deletions.
3 changes: 2 additions & 1 deletion src/Cms/FileActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ public static function create(array $props, bool $move = false): File
}

// prefer the filename from the props
$props['filename'] = F::safeName($props['filename'] ?? basename($props['source']));
$props['filename'] ??= basename($props['source']);
$props['filename'] = F::safeName($props['filename']);

$props['model'] = strtolower($props['template'] ?? 'default');

Expand Down
8 changes: 4 additions & 4 deletions src/Cms/FilePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class FilePicker extends Picker
*/
public function defaults(): array
{
$defaults = parent::defaults();
$defaults['text'] = '{{ file.filename }}';

return $defaults;
return [
...parent::defaults(),
'text' => '{{ file.filename }}'
];
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Cms/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,10 @@ public function signatureData(): array
public function status(): LicenseStatus
{
return $this->status ??= match (true) {
$this->isMissing() === true => LicenseStatus::Missing,
$this->isLegacy() === true => LicenseStatus::Legacy,
$this->isInactive() === true => LicenseStatus::Inactive,
default => LicenseStatus::Active
$this->isMissing() => LicenseStatus::Missing,
$this->isLegacy() => LicenseStatus::Legacy,
$this->isInactive() => LicenseStatus::Inactive,
default => LicenseStatus::Active
};
}

Expand Down
4 changes: 3 additions & 1 deletion src/Cms/NestCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class NestCollection extends BaseCollection
*/
public function toArray(Closure|null $map = null): array
{
return parent::toArray($map ?? fn ($object) => $object->toArray());
return parent::toArray(
$map ?? fn ($object) => $object->toArray()
);
}
}
11 changes: 7 additions & 4 deletions src/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ public function blueprints(string|null $inSection = null): array
return $this->blueprints;
}

$blueprints = [];
$templates = $this->blueprint()->changeTemplate() ?? $this->blueprint()->options()['changeTemplate'] ?? [];
$blueprints = [];
$templates = $this->blueprint()->changeTemplate() ?? null;
$templates ??= $this->blueprint()->options()['changeTemplate'] ?? [];

$currentTemplate = $this->intendedTemplate()->name();

if (is_array($templates) === false) {
Expand Down Expand Up @@ -458,7 +460,9 @@ public function intendedTemplate(): Template
return $this->intendedTemplate;
}

return $this->setTemplate($this->inventory()['template'])->intendedTemplate();
return $this
->setTemplate($this->inventory()['template'])
->intendedTemplate();
}

/**
Expand Down Expand Up @@ -936,7 +940,6 @@ public function previewUrl(): string|null
if ($this->isDraft() === true) {
$uri = new Uri($url);
$uri->query->token = $this->token();

$url = $uri->toString();
}

Expand Down
13 changes: 9 additions & 4 deletions src/Cms/PageActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ trait PageActions
* of copy objects for single or multilang environments
* @internal
*/
protected function adaptCopy(Page $copy, bool $files = false, bool $children = false): Page
{
protected function adaptCopy(
Page $copy,
bool $files = false,
bool $children = false
): Page {
if ($this->kirby()->multilang() === true) {
foreach ($this->kirby()->languages() as $language) {
// overwrite with new UUID for the page and files
Expand Down Expand Up @@ -248,8 +251,10 @@ protected function changeSlugForLanguage(
* @param int|null $position Optional sorting number
* @throws \Kirby\Exception\InvalidArgumentException If an invalid status is being passed
*/
public function changeStatus(string $status, int|null $position = null): static
{
public function changeStatus(
string $status,
int|null $position = null
): static {
return match ($status) {
'draft' => $this->changeStatusToDraft(),
'listed' => $this->changeStatusToListed($position),
Expand Down
2 changes: 1 addition & 1 deletion src/Cms/PagePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function defaults(): array
return [
...parent::defaults(),
// Page ID of the selected parent. Used to navigate
'parent' => null,
'parent' => null,
// enable/disable subpage navigation
'subpages' => true,
];
Expand Down
5 changes: 4 additions & 1 deletion src/Cms/PageSiblings.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ protected function siblingsCollection(): Pages
*/
public function templateSiblings(bool $self = true): Pages
{
return $this->siblings($self)->filter('intendedTemplate', $this->intendedTemplate()->name());
return $this->siblings($self)->filter(
'intendedTemplate',
$this->intendedTemplate()->name()
);
}
}
16 changes: 8 additions & 8 deletions src/Cms/Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ protected function setAll(bool $setting): static
*/
protected function setCategories(array $settings): static
{
foreach ($settings as $categoryName => $categoryActions) {
if (is_bool($categoryActions) === true) {
$this->setCategory($categoryName, $categoryActions);
foreach ($settings as $name => $actions) {
if (is_bool($actions) === true) {
$this->setCategory($name, $actions);
}

if (is_array($categoryActions) === true) {
foreach ($categoryActions as $actionName => $actionSetting) {
$this->setAction($categoryName, $actionName, $actionSetting);
if (is_array($actions) === true) {
foreach ($actions as $action => $setting) {
$this->setAction($name, $action, $setting);
}
}
}
Expand All @@ -198,8 +198,8 @@ protected function setCategory(string $category, bool $setting): static
throw new InvalidArgumentException('Invalid permissions category');
}

foreach ($this->actions[$category] as $actionName => $actionSetting) {
$this->actions[$category][$actionName] = $setting;
foreach ($this->actions[$category] as $action => $actionSetting) {
$this->actions[$category][$action] = $setting;
}

return $this;
Expand Down
7 changes: 5 additions & 2 deletions src/Cms/PluginAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ public function publish(): void
*/
public function publishAt(string $path): void
{
$media = $this->plugin()->mediaRoot() . '/' . $path;
F::link($this->root(), $media, 'symlink');
F::link(
$this->root(),
$this->plugin()->mediaRoot() . '/' . $path,
'symlink'
);
}

public function root(): string
Expand Down
6 changes: 4 additions & 2 deletions src/Cms/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ public function isNobody(): bool

public static function load(string $file, array $inject = []): static
{
$data = Data::read($file);
$data['name'] = F::name($file);
$data = [
...Data::read($file),
'name' => F::name($file)
];

return static::factory($data, $inject);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cms/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public static function load(string|null $root = null, array $inject = []): stati
$roles = new static();

// load roles from plugins
foreach ($kirby->extensions('blueprints') as $blueprintName => $blueprint) {
if (substr($blueprintName, 0, 6) !== 'users/') {
foreach ($kirby->extensions('blueprints') as $name => $blueprint) {
if (substr($name, 0, 6) !== 'users/') {
continue;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Cms/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public static function load(string $root, array $inject = []): static
}

$locale = F::name($filename);
$translation = Translation::load($locale, $root . '/' . $filename, $inject[$locale] ?? []);
$translation = Translation::load(
$locale,
$root . '/' . $filename,
$inject[$locale] ?? []
);

$collection->data[$locale] = $translation;
}
Expand Down
32 changes: 22 additions & 10 deletions src/Cms/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ public function avatar(): File|null
public function blueprint(): UserBlueprint
{
try {
return $this->blueprint ??= UserBlueprint::factory('users/' . $this->role(), 'users/default', $this);
return $this->blueprint ??= UserBlueprint::factory(
'users/' . $this->role(),
'users/default',
$this
);
} catch (Exception) {
return $this->blueprint ??= new UserBlueprint([
'model' => $this,
Expand Down Expand Up @@ -562,7 +566,9 @@ public function role(): Role

$name = $this->role ?? $this->credentials()['role'] ?? 'visitor';

return $this->role = $this->kirby()->roles()->find($name) ?? Role::nobody();
return $this->role =
$this->kirby()->roles()->find($name) ??
Role::nobody();
}

/**
Expand Down Expand Up @@ -629,8 +635,10 @@ public function secret(string $key): mixed
protected function setBlueprint(array|null $blueprint = null): static
{
if ($blueprint !== null) {
$blueprint['model'] = $this;
$this->blueprint = new UserBlueprint($blueprint);
$this->blueprint = new UserBlueprint([
...$blueprint,
'model' => $this
]);
}

return $this;
Expand All @@ -644,10 +652,10 @@ protected function setBlueprint(array|null $blueprint = null): static
protected function sessionFromOptions(Session|array|null $session): Session
{
// use passed session options or session object if set
if (is_array($session) === true) {
$session ??= ['detect' => true];

if ($session instanceof Session === false) {
$session = $this->kirby()->session($session);
} elseif ($session instanceof Session === false) {
$session = $this->kirby()->session(['detect' => true]);
}

return $session;
Expand Down Expand Up @@ -690,8 +698,12 @@ public function toString(
string|null $fallback = '',
string $handler = 'template'
): string {
$template ??= $this->email();
return parent::toString($template, $data, $fallback, $handler);
return parent::toString(
$template ?? $this->email(),
$data,
$fallback,
$handler
);
}

/**
Expand All @@ -701,7 +713,7 @@ public function toString(
*/
public function username(): string|null
{
return $this->name()->or($this->email())->value();
return $this->nameOrEmail()->value();
}

/**
Expand Down
39 changes: 10 additions & 29 deletions src/Cms/UserActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ public function changeEmail(string $email): static
$email = trim($email);

return $this->commit('changeEmail', ['user' => $this, 'email' => Idn::decodeEmail($email)], function ($user, $email) {
$user = $user->clone([
'email' => $email
]);

$user->updateCredentials([
'email' => $email
]);
$user = $user->clone(['email' => $email]);
$user->updateCredentials(['email' => $email]);

// update the users collection
$user->kirby()->users()->set($user->id(), $user);
Expand All @@ -56,13 +51,8 @@ public function changeEmail(string $email): static
public function changeLanguage(string $language): static
{
return $this->commit('changeLanguage', ['user' => $this, 'language' => $language], function ($user, $language) {
$user = $user->clone([
'language' => $language,
]);

$user->updateCredentials([
'language' => $language
]);
$user = $user->clone(['language' => $language]);
$user->updateCredentials(['language' => $language]);

// update the users collection
$user->kirby()->users()->set($user->id(), $user);
Expand All @@ -79,13 +69,8 @@ public function changeName(string $name): static
$name = trim($name);

return $this->commit('changeName', ['user' => $this, 'name' => $name], function ($user, $name) {
$user = $user->clone([
'name' => $name
]);

$user->updateCredentials([
'name' => $name
]);
$user = $user->clone(['name' => $name]);
$user->updateCredentials(['name' => $name]);

// update the users collection
$user->kirby()->users()->set($user->id(), $user);
Expand Down Expand Up @@ -128,13 +113,8 @@ public function changePassword(
public function changeRole(string $role): static
{
return $this->commit('changeRole', ['user' => $this, 'role' => $role], function ($user, $role) {
$user = $user->clone([
'role' => $role,
]);

$user->updateCredentials([
'role' => $role
]);
$user = $user->clone(['role' => $role]);
$user->updateCredentials(['role' => $role]);

// update the users collection
$user->kirby()->users()->set($user->id(), $user);
Expand Down Expand Up @@ -221,7 +201,8 @@ public static function create(array|null $props = null): User
$data['password'] = User::hashPassword($props['password']);
}

$props['role'] = $props['model'] = strtolower($props['role'] ?? 'default');
$props['role'] ??= 'default';
$props['role'] = $props['model'] = strtolower($props['role']);

$user = User::factory($data);

Expand Down
15 changes: 6 additions & 9 deletions src/Cms/UserPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class UserPicker extends Picker
*/
public function defaults(): array
{
$defaults = parent::defaults();
$defaults['text'] = '{{ user.username }}';

return $defaults;
return [
...parent::defaults(),
'text' => '{{ user.username }}'
];
}

/**
Expand Down Expand Up @@ -55,11 +55,8 @@ public function items(): Users|null
throw new InvalidArgumentException('Your query must return a set of users');
}

// search
$users = $this->search($users);

// sort
$users = $users->sort('username', 'asc');
// search & sort
$users = $this->search($users)->sort('username', 'asc');

// paginate
return $this->paginate($users);
Expand Down

0 comments on commit e5960b6

Please sign in to comment.