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

Throw UnknownPropertyException when setting unknown property #275

Merged
merged 2 commits 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
3 changes: 3 additions & 0 deletions src/BaseActiveRecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
return $this->$getter();
}

if (method_exists($this, 'set' . ucfirst($name))) {

Check warning on line 79 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "UnwrapUcFirst": --- Original +++ New @@ @@ /** read property, e.g. getName() */ return $this->{$getter}(); } - if (method_exists($this, 'set' . ucfirst($name))) { + if (method_exists($this, 'set' . $name)) { throw new InvalidCallException('Getting write-only property: ' . static::class . '::' . $name); } throw new UnknownPropertyException('Getting unknown property: ' . static::class . '::' . $name);
throw new InvalidCallException('Getting write-only property: ' . static::class . '::' . $name);
}

Expand Down Expand Up @@ -200,11 +200,14 @@
$this->resetDependentRelations($name);
}
$this->attributes[$name] = $value;
return;
}

if (method_exists($this, 'get' . ucfirst($name))) {
throw new InvalidCallException('Setting read-only property: ' . static::class . '::' . $name);

Check warning on line 207 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ return; } if (method_exists($this, 'get' . ucfirst($name))) { - throw new InvalidCallException('Setting read-only property: ' . static::class . '::' . $name); + throw new InvalidCallException('Setting read-only property: ' . static::class . $name . '::'); } throw new UnknownPropertyException('Setting unknown property: ' . static::class . '::' . $name); }
}

throw new UnknownPropertyException('Setting unknown property: ' . static::class . '::' . $name);
}

/**
Expand Down Expand Up @@ -252,7 +255,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 258 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 258 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 +298,13 @@
*/
public function hasProperty(string $name, bool $checkVars = true): bool
{
return $this->canGetProperty($name, $checkVars)

Check warning on line 301 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 {

Check warning on line 301 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);

Check warning on line 302 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- 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, true); } public function canGetProperty(string $name, bool $checkVars = true) : bool {
}

public function canGetProperty(string $name, bool $checkVars = true): bool

Check warning on line 305 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

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

Check warning on line 305 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

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

Check warning on line 307 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 307 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
5 changes: 5 additions & 0 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\UnknownPropertyException;
use Yiisoft\Db\Query\Query;

abstract class ActiveRecordTest extends TestCase
Expand Down Expand Up @@ -595,6 +596,10 @@ public function testAttributeAccess(): void
$this->assertSame([], $customer->orderItems);
$this->assertFalse($customer->canGetProperty('non_existing_property'));
$this->assertFalse($customer->canSetProperty('non_existing_property'));

$this->expectException(UnknownPropertyException::class);
$this->expectExceptionMessage('Setting unknown property: ' . Customer::class . '::non_existing_property');
$customer->non_existing_property = null;
}

public function testHasAttribute(): void
Expand Down
Loading