Skip to content

Commit dd5605f

Browse files
committed
refactor!: Throw exception for unkown field method
1 parent 99d5c4c commit dd5605f

File tree

7 files changed

+65
-45
lines changed

7 files changed

+65
-45
lines changed

src/Cms/Collection.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Kirby\Toolkit\Collection as BaseCollection;
88
use Kirby\Toolkit\Str;
99
use Kirby\Uuid\Uuid;
10+
use Throwable;
1011

1112
/**
1213
* The Collection class serves as foundation
@@ -126,7 +127,13 @@ public function append(...$args): static
126127
is_object($args[0]) === true &&
127128
is_callable([$args[0], 'id']) === true
128129
) {
129-
return parent::append($args[0]->id(), $args[0]);
130+
try {
131+
return parent::append($args[0]->id(), $args[0]);
132+
} catch (Throwable) {
133+
// is_callable might be true when the object implements
134+
// the magic __call method, but __call can still throw
135+
// an exception
136+
}
130137
}
131138

132139
return parent::append($args[0]);

src/Content/Field.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Kirby\Cms\User;
2121
use Kirby\Cms\Users;
2222
use Kirby\Data\Data;
23+
use Kirby\Exception\BadMethodCallException;
2324
use Kirby\Exception\InvalidArgumentException;
2425
use Kirby\Exception\NotFoundException;
2526
use Kirby\Image\QrCode;
@@ -76,13 +77,14 @@ public function __call(string $method, array $arguments = []): mixed
7677
{
7778
$method = strtolower($method);
7879

80+
// call custom field method
7981
if ($this->hasMethod($method) === true) {
8082
return $this->callMethod($method, [clone $this, ...$arguments]);
8183
}
8284

83-
// TODO: throw deprecation, then exception
84-
// when unknown method is called
85-
return $this;
85+
throw new BadMethodCallException(
86+
message: 'Field method "' . $method . '" does not exist'
87+
);
8688
}
8789

8890
/**

src/Panel/Ui/FilePreviews/ImageFilePreview.php

+15-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Kirby\Panel\Ui\FilePreviews;
44

55
use Kirby\Cms\File;
6+
use Kirby\Image\Dimensions;
67
use Kirby\Panel\Ui\FilePreview;
78
use Kirby\Toolkit\I18n;
89

@@ -30,17 +31,22 @@ public static function accepts(File $file): bool
3031

3132
public function details(): array
3233
{
33-
return [
34-
...parent::details(),
35-
[
34+
$details = parent::details();
35+
$dimensions = $this->file->dimensions();
36+
37+
if ($dimensions instanceof Dimensions) {
38+
$details[] = [
3639
'title' => I18n::translate('dimensions'),
37-
'text' => $this->file->dimensions() . ' ' . I18n::translate('pixel')
38-
],
39-
[
40+
'text' => $dimensions . ' ' . I18n::translate('pixel')
41+
];
42+
43+
$details[] = [
4044
'title' => I18n::translate('orientation'),
41-
'text' => I18n::translate('orientation.' . $this->file->dimensions()->orientation())
42-
]
43-
];
45+
'text' => I18n::translate('orientation.' . $dimensions->orientation())
46+
];
47+
}
48+
49+
return $details;
4450
}
4551

4652
public function props(): array

tests/Cms/Collections/CollectionTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public function uuid(): string|Field
4040
}
4141
}
4242

43+
class MockObjectWith__CallException
44+
{
45+
public function __call($name, $arguments)
46+
{
47+
throw new Exception('Test exception');
48+
}
49+
}
50+
4351
#[CoversClass(Collection::class)]
4452
class CollectionTest extends TestCase
4553
{
@@ -145,6 +153,14 @@ public function testAppend()
145153
$this->assertSame([$a, $b, $c, $d, 'a simple string'], $collection->values());
146154
}
147155

156+
public function testAppendWith__CallException(): void
157+
{
158+
$collection = new Collection();
159+
$obj = new MockObjectWith__CallException();
160+
$collection = $collection->append($obj);
161+
$this->assertSame([0], $collection->keys());
162+
}
163+
148164
public function testFindByUuid()
149165
{
150166
$collection = new Collection([

tests/Content/FieldTest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Kirby\Data\Data;
1515
use Kirby\Data\Json;
1616
use Kirby\Data\Yaml;
17+
use Kirby\Exception\BadMethodCallException;
1718
use Kirby\Exception\InvalidArgumentException;
1819
use Kirby\Filesystem\Dir;
1920
use Kirby\Image\QrCode;
@@ -68,10 +69,11 @@ public function test__call(): void
6869

6970
public function test__callNonExistingMethod(): void
7071
{
71-
$field = $this->field('value');
72-
$result = $field->methodDoesNotExist();
72+
$this->expectException(BadMethodCallException::class);
73+
$this->expectExceptionMessage('Field method "methoddoesnotexist" does not exist');
7374

74-
$this->assertSame($field, $result);
75+
$field = $this->field('value');
76+
$field->methodDoesNotExist();
7577
}
7678

7779
public function test__debugInfo(): void

tests/Panel/Ui/FilePreviews/ImageFilePreviewTest.php

+6-19
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66
use Kirby\Cms\Page;
77
use Kirby\Panel\Ui\FilePreview;
88
use Kirby\TestCase;
9+
use PHPUnit\Framework\Attributes\CoversClass;
910

10-
/**
11-
* @coversDefaultClass \Kirby\Panel\Ui\FilePreviews\ImageFilePreview
12-
*/
11+
#[CoversClass(ImageFilePreview::class)]
1312
class ImageFilePreviewTest extends TestCase
1413
{
15-
/**
16-
* @covers ::accepts
17-
*/
18-
public function testAccepts()
14+
public function testAccepts(): void
1915
{
2016
$page = new Page(['slug' => 'test']);
2117

@@ -26,10 +22,7 @@ public function testAccepts()
2622
$this->assertFalse(ImageFilePreview::accepts($file));
2723
}
2824

29-
/**
30-
* @covers ::details
31-
*/
32-
public function testDetails()
25+
public function testDetails(): void
3326
{
3427
$page = new Page(['slug' => 'test']);
3528
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
@@ -43,10 +36,7 @@ public function testDetails()
4336
$this->assertSame('Dimensions', $detail['title']);
4437
}
4538

46-
/**
47-
* @covers ::__construct
48-
*/
49-
public function testFactory()
39+
public function testFactory(): void
5040
{
5141
$page = new Page(['slug' => 'test']);
5242
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
@@ -56,10 +46,7 @@ public function testFactory()
5646
$this->assertSame('k-image-file-preview', $preview->component);
5747
}
5848

59-
/**
60-
* @covers ::props
61-
*/
62-
public function testProps()
49+
public function testProps(): void
6350
{
6451
$page = new Page(['slug' => 'test']);
6552
$file = new File(['filename' => 'test.xls', 'parent' => $page]);

tests/Toolkit/VTest.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -600,18 +600,18 @@ public function testSize()
600600
$this->assertFalse(V::size(7, 5, '<'));
601601

602602
$field = new Field(null, 'foo', 2);
603-
$this->assertTrue(V::size($field->foo(), 2));
604-
$this->assertTrue(V::size($field->foo(), 3, '<'));
605-
$this->assertTrue(V::size($field->foo(), 1, '>'));
606-
$this->assertFalse(V::size($field->foo(), 1, '<'));
607-
$this->assertFalse(V::size($field->foo(), 3, '>'));
603+
$this->assertTrue(V::size($field, 2));
604+
$this->assertTrue(V::size($field, 3, '<'));
605+
$this->assertTrue(V::size($field, 1, '>'));
606+
$this->assertFalse(V::size($field, 1, '<'));
607+
$this->assertFalse(V::size($field, 3, '>'));
608608

609609
$field = new Field(null, 'foo', 'hello');
610-
$this->assertTrue(V::size($field->foo(), 5));
611-
$this->assertTrue(V::size($field->foo(), 6, '<'));
612-
$this->assertTrue(V::size($field->foo(), 4, '>'));
613-
$this->assertFalse(V::size($field->foo(), 4, '<'));
614-
$this->assertFalse(V::size($field->foo(), 6, '>'));
610+
$this->assertTrue(V::size($field, 5));
611+
$this->assertTrue(V::size($field, 6, '<'));
612+
$this->assertTrue(V::size($field, 4, '>'));
613+
$this->assertFalse(V::size($field, 4, '<'));
614+
$this->assertFalse(V::size($field, 6, '>'));
615615
}
616616

617617
public function testSizeInvalid()

0 commit comments

Comments
 (0)