Skip to content

Commit c78469c

Browse files
authored
Do not catch an Exception in isset() of magic properties (#276)
1 parent 813da82 commit c78469c

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/BaseActiveRecordTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function __isset(string $name): bool
156156
{
157157
try {
158158
return $this->__get($name) !== null;
159-
} catch (Throwable) {
159+
} catch (InvalidCallException|UnknownPropertyException) {
160160
return false;
161161
}
162162
}

tests/ActiveRecordTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\ActiveRecord\Tests;
66

7+
use DivisionByZeroError;
78
use ReflectionException;
89
use Yiisoft\ActiveRecord\ActiveQuery;
910
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Animal;
@@ -392,7 +393,8 @@ public function testIssetException(): void
392393

393394
$cat = new Cat($this->db);
394395

395-
$this->assertFalse(isset($cat->exception));
396+
$this->expectException(Exception::class);
397+
isset($cat->exception);
396398
}
397399

398400
public function testIssetThrowable(): void
@@ -401,7 +403,18 @@ public function testIssetThrowable(): void
401403

402404
$cat = new Cat($this->db);
403405

404-
$this->assertFalse(isset($cat->throwable));
406+
$this->expectException(DivisionByZeroError::class);
407+
isset($cat->throwable);
408+
}
409+
410+
public function testIssetNonExisting(): void
411+
{
412+
$this->checkFixture($this->db, 'cat');
413+
414+
$cat = new Cat($this->db);
415+
416+
$this->assertFalse(isset($cat->non_existing));
417+
$this->assertFalse(isset($cat->non_existing_property));
405418
}
406419

407420
public function testSetAttributes(): void

tests/Stubs/ActiveRecord/Cat.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ public function getThrowable(): float|int
2929
{
3030
return 5/0;
3131
}
32+
33+
public function setNonExistingProperty(string $value): void
34+
{
35+
}
3236
}

0 commit comments

Comments
 (0)