Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not catch an Exception in isset() of magic properties #276

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BaseActiveRecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
{
try {
return $this->__get($name) !== null;
} catch (Throwable) {
} catch (InvalidCallException|UnknownPropertyException) {
return false;
}
}
Expand Down Expand Up @@ -252,7 +252,7 @@
* @param mixed $offset the offset to set element.
* @param mixed $item the element value.
*/
public function offsetSet(mixed $offset, mixed $item): void

Check failure on line 255 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

ParamNameMismatch: Argument 2 of Yiisoft\ActiveRecord\BaseActiveRecordTrait::offsetSet has wrong name $item, expecting $value as defined by ArrayAccess::offsetSet

Check failure on line 255 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

ParamNameMismatch: Argument 2 of Yiisoft\ActiveRecord\BaseActiveRecordTrait::offsetSet has wrong name $item, expecting $value as defined by ArrayAccess::offsetSet
{
$this->$offset = $item;
}
Expand Down Expand Up @@ -295,13 +295,13 @@
*/
public function hasProperty(string $name, bool $checkVars = true): bool
{
return $this->canGetProperty($name, $checkVars)

Check warning on line 298 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ */ public function hasProperty(string $name, bool $checkVars = true) : bool { - return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false); + return $this->canGetProperty($name, $checkVars) && $this->canSetProperty($name, false); } public function canGetProperty(string $name, bool $checkVars = true) : bool {
|| $this->canSetProperty($name, false);
}

public function canGetProperty(string $name, bool $checkVars = true): bool
{
if (method_exists($this, 'get' . ucfirst($name)) || ($checkVars && property_exists($this, $name))) {

Check warning on line 304 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "UnwrapUcFirst": --- Original +++ New @@ @@ } public function canGetProperty(string $name, bool $checkVars = true) : bool { - if (method_exists($this, 'get' . ucfirst($name)) || $checkVars && property_exists($this, $name)) { + if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) { return true; } try {

Check warning on line 304 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "UnwrapUcFirst": --- Original +++ New @@ @@ } public function canGetProperty(string $name, bool $checkVars = true) : bool { - if (method_exists($this, 'get' . ucfirst($name)) || $checkVars && property_exists($this, $name)) { + if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) { return true; } try {
return true;
}

Expand Down
17 changes: 15 additions & 2 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\ActiveRecord\Tests;

use DivisionByZeroError;
use ReflectionException;
use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Animal;
Expand Down Expand Up @@ -390,7 +391,8 @@ public function testIssetException(): void

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

$this->assertFalse(isset($cat->exception));
$this->expectException(Exception::class);
isset($cat->exception);
}

public function testIssetThrowable(): void
Expand All @@ -399,7 +401,18 @@ public function testIssetThrowable(): void

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

$this->assertFalse(isset($cat->throwable));
$this->expectException(DivisionByZeroError::class);
isset($cat->throwable);
}

public function testIssetNonExisting(): void
{
$this->checkFixture($this->db, 'cat');

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

$this->assertFalse(isset($cat->non_existing));
$this->assertFalse(isset($cat->non_existing_property));
}

public function testSetAttributes(): void
Expand Down
4 changes: 4 additions & 0 deletions tests/Stubs/ActiveRecord/Cat.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ public function getThrowable(): float|int
{
return 5/0;
}

public function setNonExistingProperty(string $value): void
{
}
}
Loading