Skip to content

Commit

Permalink
Add message key to more exception calls
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Sep 21, 2024
1 parent 211934a commit af1da17
Show file tree
Hide file tree
Showing 110 changed files with 548 additions and 211 deletions.
4 changes: 2 additions & 2 deletions config/api/authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$auth->type($allowImpersonation) === 'session' &&
$auth->csrf() === false
) {
throw new AuthException('Unauthenticated');
throw new AuthException(message: 'Unauthenticated');
}

// get user from session or basic auth
Expand All @@ -23,5 +23,5 @@
return $user;
}

throw new AuthException('Unauthenticated');
throw new AuthException(message: 'Unauthenticated');
};
2 changes: 1 addition & 1 deletion config/areas/system/dialogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
];
}

throw new LogicException('The upgrade failed');
throw new LogicException(message: 'The upgrade failed');
// @codeCoverageIgnoreEnd
}
],
Expand Down
6 changes: 3 additions & 3 deletions src/Api/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(

if ($data === null) {
if (($schema['default'] ?? null) instanceof Closure === false) {
throw new Exception('Missing collection data');
throw new Exception(message: 'Missing collection data');
}

$this->data = $schema['default']->call($this->api);
Expand All @@ -50,7 +50,7 @@ public function __construct(
isset($schema['type']) === true &&
$this->data instanceof $schema['type'] === false
) {
throw new Exception('Invalid collection type');
throw new Exception(message: 'Invalid collection type');
}
}

Expand All @@ -69,7 +69,7 @@ public function select($keys = null): static
}

if ($keys !== null && is_array($keys) === false) {
throw new Exception('Invalid select keys');
throw new Exception(message: 'Invalid select keys');
}

$this->select = $keys;
Expand Down
6 changes: 3 additions & 3 deletions src/Api/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(

if ($data === null) {
if (($schema['default'] ?? null) instanceof Closure === false) {
throw new Exception('Missing model data');
throw new Exception(message: 'Missing model data');
}

$this->data = $schema['default']->call($this->api);
Expand Down Expand Up @@ -82,7 +82,7 @@ public function select($keys = null): static
}

if ($keys !== null && is_array($keys) === false) {
throw new Exception('Invalid select keys');
throw new Exception(message: 'Invalid select keys');
}

$this->select = $keys;
Expand All @@ -109,7 +109,7 @@ public function selection(): array

if (is_string($value) === true) {
if ($value === 'any') {
throw new Exception('Invalid sub view: "any"');
throw new Exception(message: 'Invalid sub view: "any"');
}

$selection[$key] = [
Expand Down
2 changes: 1 addition & 1 deletion src/Cms/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public function impersonate(string|null $who = null): User|null
'id' => 'nobody',
'role' => 'nobody',
]),
default => ($this->kirby->users()->find($who) ?? throw new NotFoundException('The user "' . $who . '" cannot be found'))
default => $this->kirby->users()->find($who) ?? throw new NotFoundException(message: 'The user "' . $who . '" cannot be found')
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/Cms/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static function parse(array|string|null $input): array
isset($first['type']) === false
)
) {
throw new Exception('Invalid YAML');
throw new Exception(message: 'Invalid YAML');
}

$input = $yaml;
Expand Down
9 changes: 7 additions & 2 deletions src/Cms/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ public function load(string $name): mixed
// fallback to collections from plugins
$collections = $kirby->extensions('collections');

return $collections[$name] ??
throw new NotFoundException('The collection cannot be found');
if ($collection = $collections[$name] ?? null) {
return $collection;
}

throw new NotFoundException(
message: 'The collection cannot be found'
);
}
}
8 changes: 6 additions & 2 deletions src/Cms/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ protected function template(): void
} elseif ($text->exists() === true) {
$this->props['body'] = $text->render($data);
} else {
throw new NotFoundException('The email template "' . $this->props['template'] . '" cannot be found');
throw new NotFoundException(
message: 'The email template "' . $this->props['template'] . '" cannot be found'
);
}
}
}
Expand Down Expand Up @@ -187,7 +189,9 @@ protected function transformModel(
}
} else {
// invalid input
throw new InvalidArgumentException('Invalid input for prop "' . $prop . '", expected string or "' . $class . '" object or collection');
throw new InvalidArgumentException(
message: 'Invalid input for prop "' . $prop . '", expected string or "' . $class . '" object or collection'
);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Cms/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ public function type(): string
public function updateArgument(string $name, $value): void
{
if (array_key_exists($name, $this->arguments) !== true) {
throw new InvalidArgumentException('The argument ' . $name . ' does not exist');
throw new InvalidArgumentException(
message: 'The argument ' . $name . ' does not exist'
);
}

$this->arguments[$name] = $value;
Expand Down
4 changes: 3 additions & 1 deletion src/Cms/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ class Fieldset extends Item
public function __construct(array $params = [])
{
if (empty($params['type']) === true) {
throw new InvalidArgumentException('The fieldset type is missing');
throw new InvalidArgumentException(
message: 'The fieldset type is missing'
);
}

$this->type = $params['id'] = $params['type'];
Expand Down
4 changes: 3 additions & 1 deletion src/Cms/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ class File extends ModelWithContent
public function __construct(array $props)
{
if (isset($props['filename'], $props['parent']) === false) {
throw new InvalidArgumentException('The filename and parent are required');
throw new InvalidArgumentException(
message: 'The filename and parent are required'
);
}

$this->filename = $props['filename'];
Expand Down
16 changes: 12 additions & 4 deletions src/Cms/FileActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public function changeName(
}

if ($newFile->exists() === true) {
throw new LogicException('The new file exists and cannot be overwritten');
throw new LogicException(
message: 'The new file exists and cannot be overwritten'
);
}

// rename the main file
Expand Down Expand Up @@ -246,7 +248,9 @@ public function copy(Page $page): static
public static function create(array $props, bool $move = false): File
{
if (isset($props['source'], $props['parent']) === false) {
throw new InvalidArgumentException('Please provide the "source" and "parent" props for the File');
throw new InvalidArgumentException(
message: 'Please provide the "source" and "parent" props for the File'
);
}

// prefer the filename from the props
Expand Down Expand Up @@ -306,7 +310,9 @@ public static function create(array $props, bool $move = false): File

// overwrite the original
if (F::$method($upload->root(), $file->root(), true) !== true) {
throw new LogicException('The file could not be created');
throw new LogicException(
message: 'The file could not be created'
);
}

// resize the file on upload if configured
Expand Down Expand Up @@ -407,7 +413,9 @@ public function replace(string $source, bool $move = false): static

// overwrite the original
if (F::$method($upload->root(), $file->root(), true) !== true) {
throw new LogicException('The file could not be created');
throw new LogicException(
message: 'The file could not be created'
);
}

// apply the resizing/crop options from the blueprint
Expand Down
4 changes: 3 additions & 1 deletion src/Cms/FileModifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ public function thumb(
$result instanceof File === false &&
$result instanceof Asset === false
) {
throw new InvalidArgumentException('The file::version component must return a File, FileVersion or Asset object');
throw new InvalidArgumentException(
message: 'The file::version component must return a File, FileVersion or Asset object'
);
}

return $result;
Expand Down
4 changes: 3 additions & 1 deletion src/Cms/FilePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public function items(): Files|null
$files instanceof User => $files->files(),
$files instanceof Files => $files,

default => throw new InvalidArgumentException('Your query must return a set of files')
default => throw new InvalidArgumentException(
message: 'Your query must return a set of files'
)
};

// filter protected and hidden pages
Expand Down
16 changes: 12 additions & 4 deletions src/Cms/FileRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ public static function create(File $file, BaseFile $upload): void
}

if ($file->permissions()->create() !== true) {
throw new PermissionException('The file cannot be created');
throw new PermissionException(
message: 'The file cannot be created'
);
}

static::validFile($file, $upload->mime());
Expand All @@ -152,7 +154,9 @@ public static function create(File $file, BaseFile $upload): void
public static function delete(File $file): void
{
if ($file->permissions()->delete() !== true) {
throw new PermissionException('The file cannot be deleted');
throw new PermissionException(
message: 'The file cannot be deleted'
);
}
}

Expand All @@ -165,7 +169,9 @@ public static function delete(File $file): void
public static function replace(File $file, BaseFile $upload): void
{
if ($file->permissions()->replace() !== true) {
throw new PermissionException('The file cannot be replaced');
throw new PermissionException(
message: 'The file cannot be replaced'
);
}

static::validMime($file, $upload->mime());
Expand All @@ -192,7 +198,9 @@ public static function replace(File $file, BaseFile $upload): void
public static function update(File $file, array $content = []): void
{
if ($file->permissions()->update() !== true) {
throw new PermissionException('The file cannot be updated');
throw new PermissionException(
message: 'The file cannot be updated'
);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Cms/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public function add($object): static
// give a useful error message on invalid input;
// silently ignore "empty" values for compatibility with existing setups
} elseif (in_array($object, [null, false, true], true) !== true) {
throw new InvalidArgumentException('You must pass a Files or File object or an ID of an existing file to the Files collection');
throw new InvalidArgumentException(
message: 'You must pass a Files or File object or an ID of an existing file to the Files collection'
);
}

return $this;
Expand Down
4 changes: 3 additions & 1 deletion src/Cms/HasMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public function callMethod(string $method, array $args = []): mixed
$closure = $this->getMethod($method);

if ($closure === null) {
throw new BadMethodCallException('The method ' . $method . ' does not exist');
throw new BadMethodCallException(
message: 'The method ' . $method . ' does not exist'
);
}

return $closure->call($this, ...$args);
Expand Down
4 changes: 3 additions & 1 deletion src/Cms/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ public static function size(mixed $value): int
return count($value);
}

throw new InvalidArgumentException('Could not determine the size of the given value');
throw new InvalidArgumentException(
message: 'Could not determine the size of the given value'
);
}
}
6 changes: 4 additions & 2 deletions src/Cms/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ public static function factory(
}

if (is_array($params) === false) {
throw new InvalidArgumentException('Invalid item options');
throw new InvalidArgumentException(message: 'Invalid item options');
}

// create a new collection of blocks
$collection = new static([], $params);

foreach ($items as $item) {
if (is_array($item) === false) {
throw new InvalidArgumentException('Invalid data for ' . static::ITEM_CLASS);
throw new InvalidArgumentException(
message: 'Invalid data for ' . static::ITEM_CLASS
);
}

// inject properties from the parent
Expand Down
8 changes: 5 additions & 3 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class Language implements Stringable
public function __construct(array $props)
{
if (isset($props['code']) === false) {
throw new InvalidArgumentException('The property "code" is required');
throw new InvalidArgumentException(
message: 'The property "code" is required'
);
}

static::$kirby = $props['kirby'] ?? null;
Expand Down Expand Up @@ -232,7 +234,7 @@ public function delete(): bool
LanguageRules::delete($language);

if (F::remove($language->root()) !== true) {
throw new Exception('The language could not be deleted');
throw new Exception(message: 'The language could not be deleted');
}

// if needed, convert content storage to single lang
Expand Down Expand Up @@ -288,7 +290,7 @@ public static function ensure(self|string|null $code = null): static
}

// validate the language code
throw new NotFoundException('Invalid language: ' . $code);
throw new NotFoundException(message: 'Invalid language: ' . $code);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Cms/LanguageRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public function routes(): array
$routes[$index]['pattern'] = $patterns;
$routes[$index]['page'] = $page;
} else {
throw new NotFoundException('The page "' . $pageId . '" does not exist');
throw new NotFoundException(
message: 'The page "' . $pageId . '" does not exist'
);
}
}
}
Expand Down
Loading

0 comments on commit af1da17

Please sign in to comment.