From 2e35860e4a35fa8ae110a551f1b8ada3db27a41b Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 12 Sep 2024 17:45:08 +0700 Subject: [PATCH] Rename arguments `$name` to `$propertyName` or `$relationName` --- src/AbstractActiveRecord.php | 76 +++++++++++++++--------------- src/ActiveRecordInterface.php | 24 ++++------ src/Trait/ArrayAccessTrait.php | 1 + src/Trait/MagicPropertiesTrait.php | 12 ++--- 4 files changed, 55 insertions(+), 58 deletions(-) diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index f11b31ca9..ed0253928 100644 --- a/src/AbstractActiveRecord.php +++ b/src/AbstractActiveRecord.php @@ -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 @@ -128,15 +128,15 @@ 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; } /** @@ -144,14 +144,14 @@ public function oldValue(string $name): mixed * * 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; @@ -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) { @@ -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) { @@ -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; } } } @@ -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); } /** @@ -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 . '".'); } } @@ -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 @@ -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) { @@ -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]); + } elseif (isset($this->related[$relationName]) && is_array($this->related[$relationName])) { /** @psalm-var array $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]); } } } @@ -914,8 +915,7 @@ 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 @@ -923,11 +923,11 @@ public function unlink(string $name, ActiveRecordInterface $arClass, bool $delet * @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) { @@ -1010,7 +1010,7 @@ public function unlinkAll(string $name, bool $delete = false): void } } - unset($this->related[$name]); + unset($this->related[$relationName]); } /** diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index b1e7801d3..6fd9e97b5 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -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. @@ -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. * @@ -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; @@ -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. @@ -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. diff --git a/src/Trait/ArrayAccessTrait.php b/src/Trait/ArrayAccessTrait.php index 705582c94..9e9fef60d 100644 --- a/src/Trait/ArrayAccessTrait.php +++ b/src/Trait/ArrayAccessTrait.php @@ -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); return; } diff --git a/src/Trait/MagicPropertiesTrait.php b/src/Trait/MagicPropertiesTrait.php index 0d4490cdd..79e632cdb 100644 --- a/src/Trait/MagicPropertiesTrait.php +++ b/src/Trait/MagicPropertiesTrait.php @@ -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) @@ -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 . '".'); } }