Skip to content

Commit

Permalink
Rename arguments $name to $propertyName or $relationName
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Sep 12, 2024
1 parent f0566eb commit 2e35860
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 58 deletions.
76 changes: 38 additions & 38 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public function equals(ActiveRecordInterface $record): bool
return $this->getTableName() === $record->getTableName() && $this->getPrimaryKey() === $record->getPrimaryKey();
}

public function get(string $name): mixed
public function get(string $propertyName): mixed
{
return $this->propertyValuesInternal()[$name] ?? null;
return $this->propertyValuesInternal()[$propertyName] ?? null;
}

public function propertyValues(array|null $names = null, array $except = []): array
Expand All @@ -128,30 +128,30 @@ public function getIsNewRecord(): bool
*
* If this record is the result of a query and the property is not loaded, `null` will be returned.
*
* @param string $name The property name.
* @param string $propertyName The property name.
*
* @return mixed The old property value. `null` if the property is not loaded before or doesn't exist.
*
* @see hasProperty()
*/
public function oldValue(string $name): mixed
public function oldValue(string $propertyName): mixed
{
return $this->oldValues[$name] ?? null;
return $this->oldValues[$propertyName] ?? null;
}

/**
* Returns the property values that have been modified since they're loaded or saved most recently.
*
* The comparison of new and old values uses `===`.
*
* @param array|null $names The names of the properties whose values may be returned if they're changed recently.
* @param array|null $propertyNames The names of the properties whose values may be returned if they're changed recently.
* If null, {@see propertyNames()} will be used.
*
* @return array The changed property values (name-value pairs).
*/
public function dirtyValues(array|null $names = null): array
public function dirtyValues(array|null $propertyNames = null): array
{
$values = $this->propertyValues($names);
$values = $this->propertyValues($propertyNames);

if ($this->oldValues === null) {
return $values;
Expand Down Expand Up @@ -362,11 +362,11 @@ public function isRelationPopulated(string $name): bool
return array_key_exists($name, $this->related);
}

public function link(string $name, ActiveRecordInterface $arClass, array $extraColumns = []): void
public function link(string $relationName, ActiveRecordInterface $arClass, array $extraColumns = []): void
{
$viaClass = null;
$viaTable = null;
$relation = $this->relationQuery($name);
$relation = $this->relationQuery($relationName);
$via = $relation->getVia();

if ($via !== null) {
Expand Down Expand Up @@ -464,8 +464,8 @@ public function link(string $name, ActiveRecordInterface $arClass, array $extraC

// Update lazily loaded related objects.
if (!$relation->getMultiple()) {
$this->related[$name] = $arClass;
} elseif (isset($this->related[$name])) {
$this->related[$relationName] = $arClass;
} elseif (isset($this->related[$relationName])) {
$indexBy = $relation->getIndexBy();
if ($indexBy !== null) {
if ($indexBy instanceof Closure) {
Expand All @@ -475,10 +475,10 @@ public function link(string $name, ActiveRecordInterface $arClass, array $extraC
}

if ($index !== null) {
$this->related[$name][$index] = $arClass;
$this->related[$relationName][$index] = $arClass;
}
} else {
$this->related[$name][] = $arClass;
$this->related[$relationName][] = $arClass;
}
}
}
Expand Down Expand Up @@ -613,16 +613,16 @@ public function save(array|null $propertyNames = null): bool
return true;
}

public function set(string $name, mixed $value): void
public function set(string $propertyName, mixed $value): void
{
if (
isset($this->relationsDependencies[$name])
&& ($value === null || $this->get($name) !== $value)
isset($this->relationsDependencies[$propertyName])
&& ($value === null || $this->get($propertyName) !== $value)
) {
$this->resetDependentRelations($name);
$this->resetDependentRelations($propertyName);
}

$this->populateProperty($name, $value);
$this->populateProperty($propertyName, $value);
}

/**
Expand Down Expand Up @@ -657,18 +657,18 @@ public function setIsNewRecord(bool $value): void
/**
* Sets the old value of the named property.
*
* @param string $name The property name.
* @param string $propertyName The property name.
*
* @throws InvalidArgumentException If the named property doesn't exist.
*
* @see hasProperty()
*/
public function assignOldValue(string $name, mixed $value): void
public function assignOldValue(string $propertyName, mixed $value): void
{
if (isset($this->oldValues[$name]) || $this->hasProperty($name)) {
$this->oldValues[$name] = $value;
if (isset($this->oldValues[$propertyName]) || $this->hasProperty($propertyName)) {
$this->oldValues[$propertyName] = $value;
} else {
throw new InvalidArgumentException(static::class . ' has no property named "' . $name . '".');
throw new InvalidArgumentException(static::class . ' has no property named "' . $propertyName . '".');
}
}

Expand All @@ -677,11 +677,12 @@ public function assignOldValue(string $name, mixed $value): void
*
* All existing old property values will be discarded.
*
* @param array|null $values Old property values to be set. If set to `null` this record is {@see isNewRecord() new}.
* @param array|null $propertyValues Old property values (name => value) to be set. If set to `null` this record
* is {@see isNewRecord() new}.
*/
public function assignOldValues(array|null $values = null): void
public function assignOldValues(array|null $propertyValues = null): void
{
$this->oldValues = $values;
$this->oldValues = $propertyValues;
}

public function update(array|null $propertyNames = null): int
Expand Down Expand Up @@ -806,11 +807,11 @@ public function updateCounters(array $counters): bool
return true;
}

public function unlink(string $name, ActiveRecordInterface $arClass, bool $delete = false): void
public function unlink(string $relationName, ActiveRecordInterface $arClass, bool $delete = false): void
{
$viaClass = null;
$viaTable = null;
$relation = $this->relationQuery($name);
$relation = $this->relationQuery($relationName);
$viaRelation = $relation->getVia();

if ($viaRelation !== null) {
Expand Down Expand Up @@ -894,13 +895,13 @@ public function unlink(string $name, ActiveRecordInterface $arClass, bool $delet
}

if (!$relation->getMultiple()) {
unset($this->related[$name]);
} elseif (isset($this->related[$name]) && is_array($this->related[$name])) {
unset($this->related[$relationName]);

Check warning on line 898 in src/AbstractActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractActiveRecord.php#L898

Added line #L898 was not covered by tests
} elseif (isset($this->related[$relationName]) && is_array($this->related[$relationName])) {
/** @psalm-var array<array-key, ActiveRecordInterface> $related */
$related = $this->related[$name];
$related = $this->related[$relationName];
foreach ($related as $a => $b) {
if ($arClass->getPrimaryKey() === $b->getPrimaryKey()) {
unset($this->related[$name][$a]);
unset($this->related[$relationName][$a]);
}
}
}
Expand All @@ -914,20 +915,19 @@ public function unlink(string $name, ActiveRecordInterface $arClass, bool $delet
*
* To destroy the relationship without removing records, make sure your keys can be set to `null`.
*
* @param string $name The case-sensitive name of the relationship, e.g., `orders` for a relation defined via
* `getOrders()` method.
* @param string $relationName The case-sensitive name of the relationship.
* @param bool $delete Whether to delete the model that contains the foreign key.
*
* @throws Exception
* @throws ReflectionException
* @throws StaleObjectException
* @throws Throwable
*/
public function unlinkAll(string $name, bool $delete = false): void
public function unlinkAll(string $relationName, bool $delete = false): void
{
$viaClass = null;
$viaTable = null;
$relation = $this->relationQuery($name);
$relation = $this->relationQuery($relationName);
$viaRelation = $relation->getVia();

if ($viaRelation !== null) {
Expand Down Expand Up @@ -1010,7 +1010,7 @@ public function unlinkAll(string $name, bool $delete = false): void
}
}

unset($this->related[$name]);
unset($this->related[$relationName]);
}

/**
Expand Down
24 changes: 10 additions & 14 deletions src/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ public function filterValidAliases(ActiveQuery $query): array;
*
* If this record is the result of a query and the property isn't loaded, `null` will be returned.
*
* @param string $name The property name.
* @param string $propertyName The property name.
*
* @return mixed The property value. `null` if the property isn't set or doesn't exist.
*
* @see hasProperty()
*/
public function get(string $name): mixed;
public function get(string $propertyName): mixed;

/**
* Returns property values.
Expand Down Expand Up @@ -255,8 +255,7 @@ public function isPrimaryKey(array $keys): bool;
/**
* Check whether the named relation has been populated with records.
*
* @param string $name The relation name, for example, `orders` for a relation defined via `getOrders()` method
* (case-sensitive).
* @param string $name The relation name, for example, `orders` (case-sensitive).
*
* @return bool Whether relation has been populated with records.
*
Expand All @@ -277,22 +276,20 @@ public function isRelationPopulated(string $name): bool;
*
* This method requires that the primary key value isn't `null`.
*
* @param string $name The case-sensitive name of the relationship, for example, `orders` for a relation defined via
* `getOrders()` method.
* @param string $relationName The relation name, for example, `orders` (case-sensitive).
* @param self $arClass The record to be linked with the current one.
* @param array $extraColumns More column values to be saved into the junction table. This parameter is only
* meaningful for a relationship involving a junction table (that's a relation set with
* {@see ActiveQueryInterface::via()}).
*/
public function link(string $name, self $arClass, array $extraColumns = []): void;
public function link(string $relationName, self $arClass, array $extraColumns = []): void;

/**
* Populates the named relation with the related records.
*
* Note that this method doesn't check if the relation exists or not.
*
* @param string $name The relation name, for example, `orders` for a relation defined via `getOrders()` method
* (case-sensitive).
* @param string $name The relation name, for example, `orders` (case-sensitive).
* @param array|self|null $records The related records to be populated into the relation.
*/
public function populateRelation(string $name, array|self|null $records): void;
Expand Down Expand Up @@ -383,11 +380,11 @@ public function save(array|null $propertyNames = null): bool;
/**
* Sets the named property value.
*
* @param string $name The property name.
* @param string $propertyName The property name.
*
* @throws InvalidArgumentException If the named property doesn't exist.
*/
public function set(string $name, mixed $value): void;
public function set(string $propertyName, mixed $value): void;

/**
* Saves the changes to this active record into the associated database table.
Expand Down Expand Up @@ -490,14 +487,13 @@ public function updateProperties(array $properties): int;
*
* Otherwise, the foreign key will be set `null` and the record will be saved without validation.
*
* @param string $name The case-sensitive name of the relationship, for example, `orders` for a relation defined via
* `getOrders()` method.
* @param string $relationName The relation name, for example, `orders` (case-sensitive).
* @param self $arClass The active record to be unlinked from the current one.
* @param bool $delete Whether to delete the active record that contains the foreign key.
* If false, the active record's foreign key will be set `null` and saved.
* If true, the active record containing the foreign key will be deleted.
*/
public function unlink(string $name, self $arClass, bool $delete = false): void;
public function unlink(string $relationName, self $arClass, bool $delete = false): void;

/**
* Returns the old property values.
Expand Down
1 change: 1 addition & 0 deletions src/Trait/ArrayAccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function offsetSet(mixed $offset, mixed $value): void
public function offsetUnset(mixed $offset): void
{
if ($this->hasProperty($offset) || property_exists($this, $offset)) {
$this->set($offset, null);
unset($this->$offset);

Check warning on line 125 in src/Trait/ArrayAccessTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Trait/ArrayAccessTrait.php#L123-L125

Added lines #L123 - L125 were not covered by tests
return;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Trait/MagicPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
* @method array getRelatedRecords()
* @see AbstractActiveRecord::getRelatedRecords()
*
* @method bool hasDependentRelations(string $name)
* @method bool hasDependentRelations(string $propertyName)
* @see AbstractActiveRecord::hasDependentRelations()
*
* @method bool isRelationPopulated(string $name)
* @see ActiveRecordInterface::isRelationPopulated()
*
* @method void resetDependentRelations(string $name)
* @method void resetDependentRelations(string $propertyName)
* @see AbstractActiveRecord::resetDependentRelations()
*
* @method void resetRelation(string $name)
Expand Down Expand Up @@ -156,12 +156,12 @@ public function hasProperty(string $name): bool
return isset($this->propertyValues[$name]) || in_array($name, $this->propertyNames(), true);
}

public function set(string $name, mixed $value): void
public function set(string $propertyName, mixed $value): void
{
if ($this->hasProperty($name)) {
parent::set($name, $value);
if ($this->hasProperty($propertyName)) {
parent::set($propertyName, $value);
} else {
throw new InvalidArgumentException(static::class . ' has no property named "' . $name . '".');
throw new InvalidArgumentException(static::class . ' has no property named "' . $propertyName . '".');
}
}

Expand Down

0 comments on commit 2e35860

Please sign in to comment.