Skip to content

Commit

Permalink
Switch from class_uses() to duck test for File::setModel() #4748
Browse files Browse the repository at this point in the history
Co-Authored-By: Nico Hoffmann  ෴. <3788865+distantnative@users.noreply.github.com>
Co-Authored-By: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-Authored-By: Lukas Bestle <1595007+lukasbestle@users.noreply.github.com>
  • Loading branch information
4 people committed Oct 4, 2022
1 parent 8929349 commit 7026bfb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Filesystem/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public function root(): string|null
*/
protected function setModel(object|null $model = null): static
{
if ($model !== null && in_array(IsFile::class, class_uses($model)) !== true) {
if ($model !== null && method_exists($model, 'hasIsFileTrait') !== true) {
throw new InvalidArgumentException('The model object must use the "Kirby\Filesystem\IsFile" trait');
}

Expand Down
10 changes: 10 additions & 0 deletions src/Filesystem/IsFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ 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
* @codeCoverageIgnore
*/
protected function hasIsFileTrait(): bool
{
return true;
}

/**
* Returns the app instance
Expand Down
7 changes: 6 additions & 1 deletion tests/Filesystem/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

require_once __DIR__ . '/mocks.php';

class InvalidFileModel
{
public string $foo = 'bar';
}

/**
* @coversDefaultClass \Kirby\Filesystem\File
*/
Expand Down Expand Up @@ -462,7 +467,7 @@ public function testInvalidModel()

new File([
'root' => $this->fixtures . '/test.js',
'model' => Page::factory(['slug' => 'test'])
'model' => new InvalidFileModel()
]);
}

Expand Down

0 comments on commit 7026bfb

Please sign in to comment.