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

Refactor MagicPropertiesTrait #337

Merged
merged 1 commit into from
May 22, 2024
Merged
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
75 changes: 48 additions & 27 deletions src/Trait/MagicPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
/**
* Trait to define magic methods to access values of an ActiveRecord instance.
*
* @method array getOldAttributes()
* @see AbstractActiveRecord::getOldAttributes()
*
* @method mixed getOldAttribute(string $name)
* @see AbstractActiveRecord::getOldAttribute()
*
* @method array getRelatedRecords()
* @see AbstractActiveRecord::getRelatedRecords()
*
Expand Down Expand Up @@ -122,6 +128,10 @@
if ($this->hasAttribute($name)) {
unset($this->attributes[$name]);

if (property_exists($this, $name)) {
$this->$name = null;

Check warning on line 132 in src/Trait/MagicPropertiesTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Trait/MagicPropertiesTrait.php#L132

Added line #L132 was not covered by tests
}

if ($this->hasDependentRelations($name)) {
$this->resetDependentRelations($name);
}
Expand All @@ -142,14 +152,7 @@
public function __set(string $name, mixed $value): void
{
if ($this->hasAttribute($name)) {
if (
$this->hasDependentRelations($name)
&& (!array_key_exists($name, $this->attributes) || $this->attributes[$name] !== $value)
) {
$this->resetDependentRelations($name);
}

$this->attributes[$name] = $value;
$this->setAttributeInternal($name, $value);
return;
}

Expand All @@ -170,6 +173,10 @@

public function getAttribute(string $name): mixed
{
if (property_exists($this, $name)) {
return get_object_vars($this)[$name] ?? null;

Check warning on line 177 in src/Trait/MagicPropertiesTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Trait/MagicPropertiesTrait.php#L177

Added line #L177 was not covered by tests
}

return $this->attributes[$name] ?? null;
}

Expand All @@ -192,37 +199,31 @@

public function isAttributeChanged(string $name, bool $identical = true): bool
{
if (isset($this->attributes[$name], $this->oldAttributes[$name])) {
return $this->attributes[$name] !== $this->oldAttributes[$name];
$hasOldAttribute = array_key_exists($name, $this->getOldAttributes());

if (!$hasOldAttribute) {
return property_exists($this, $name) && array_key_exists($name, get_object_vars($this))
|| array_key_exists($name, $this->attributes);
}

return isset($this->attributes[$name]) || isset($this->oldAttributes[$name]);
if (property_exists($this, $name)) {
return !array_key_exists($name, get_object_vars($this))
|| $this->getOldAttribute($name) !== $this->$name;

Check warning on line 211 in src/Trait/MagicPropertiesTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Trait/MagicPropertiesTrait.php#L210-L211

Added lines #L210 - L211 were not covered by tests
}

return !array_key_exists($name, $this->attributes)
|| $this->getOldAttribute($name) !== $this->attributes[$name];
}

public function setAttribute(string $name, mixed $value): void
{
if ($this->hasAttribute($name)) {
if (
$this->hasDependentRelations($name)
&& (!array_key_exists($name, $this->attributes) || $this->attributes[$name] !== $value)
) {
$this->resetDependentRelations($name);
}
$this->attributes[$name] = $value;
$this->setAttributeInternal($name, $value);
} else {
throw new InvalidArgumentException(static::class . ' has no attribute named "' . $name . '".');
}
}

protected function populateAttribute(string $name, mixed $value): void
{
if (property_exists($this, $name)) {
$this->$name = $value;
} else {
$this->attributes[$name] = $value;
}
}

/**
* Returns a value indicating whether a property is defined for this component.
*
Expand Down Expand Up @@ -263,4 +264,24 @@
|| ($checkVars && property_exists($this, $name))
|| $this->hasAttribute($name);
}

protected function populateAttribute(string $name, mixed $value): void
{
if (property_exists($this, $name)) {
$this->$name = $value;
} else {
$this->attributes[$name] = $value;
}
}

private function setAttributeInternal(string $name, mixed $value): void
{
if ($this->hasDependentRelations($name)
&& ($value === null || $this->getAttribute($name) !== $value)
) {
$this->resetDependentRelations($name);
}

$this->populateAttribute($name, $value);
}
}
Loading