Skip to content

Commit

Permalink
Switches from root and url checks to hasIsFileTrait method #4748
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora committed Oct 4, 2022
1 parent d55d161 commit 599771b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
15 changes: 2 additions & 13 deletions src/Filesystem/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class File
/**
* Parent file model
* The model object must use the `Kirby\Filesystem\IsFile` trait
* or have `root` and `url` methods
*/
protected object|null $model = null;

Expand Down Expand Up @@ -442,21 +441,11 @@ public function root(): string|null
* @return $this
*
* @throws \Kirby\Exception\InvalidArgumentException When the model does not use the `Kirby\Filesystem\IsFile` trait
* or does not have `root` and `url` methods
*/
protected function setModel(object|null $model = null): static
{
if ($model === null) {
$this->model = null;
return $this;
}

// TODO: create an interface instead of checking methods
if (
method_exists($model, 'root') !== true ||
method_exists($model, 'url') !== true
) {
throw new InvalidArgumentException('The model object must use the "Kirby\Filesystem\IsFile" trait or have "root" and "url" methods');
if ($model !== null && method_exists($model, 'hasIsFileTrait') !== true) {
throw new InvalidArgumentException('The model object must use the "Kirby\Filesystem\IsFile" trait');
}

$this->model = $model;
Expand Down
9 changes: 9 additions & 0 deletions src/Filesystem/IsFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ public function exists(): bool
return file_exists($this->root()) === true;
}

/**
* To check the existence of the IsFile trait
*
* @todo Switch to class constant in traits when min PHP version 8.2 required
*/
protected function hasIsFileTrait(): bool
{
return true;
}

/**
* Returns the app instance
Expand Down
2 changes: 1 addition & 1 deletion tests/Filesystem/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public function testParentModel()
public function testInvalidModel()
{
$this->expectException('\Kirby\Exception\InvalidArgumentException');
$this->expectExceptionMessage('The model object must use the "Kirby\Filesystem\IsFile" trait or have "root" and "url" methods');
$this->expectExceptionMessage('The model object must use the "Kirby\Filesystem\IsFile" trait');

new File([
'root' => $this->fixtures . '/test.js',
Expand Down

0 comments on commit 599771b

Please sign in to comment.