Skip to content

Commit 0ebe2c7

Browse files
Fix ActiveRecordInterface::class. (#263)
1 parent 7d4b9e6 commit 0ebe2c7

3 files changed

Lines changed: 80 additions & 66 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"require-dev": {
2828
"maglnet/composer-require-checker": "^4.2",
2929
"phpunit/phpunit": "^9.6|^10.0",
30-
"rector/rector": "^0.15",
30+
"rector/rector": "^0.16",
3131
"roave/infection-static-analysis-plugin": "^1.16",
3232
"spatie/phpunit-watcher": "^1.23",
3333
"vimeo/psalm": "^4.8|^5.8",

src/ActiveRecordInterface.php

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Yiisoft\Db\Exception\Exception;
1010
use Yiisoft\Db\Exception\InvalidArgumentException;
1111
use Yiisoft\Db\Exception\InvalidConfigException;
12+
use Yiisoft\Db\Exception\NotSupportedException;
1213
use Yiisoft\Db\Exception\StaleObjectException;
1314

1415
interface ActiveRecordInterface
@@ -36,7 +37,7 @@ public function attributes(): array;
3637
*
3738
* @return false|int The number of rows deleted, or `false` if the deletion is unsuccessful for some reason.
3839
*
39-
* Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
40+
* Note that it's possible the number of rows deleted is 0, even though the deletion execution is successful.
4041
*/
4142
public function delete(): false|int;
4243

@@ -50,7 +51,7 @@ public function delete(): false|int;
5051
* $customer->deleteAll('status = 3');
5152
* ```
5253
*
53-
* > Warning: If you do not specify any condition, this method will delete **all** rows in the table.
54+
* > Warning: If you don't specify any condition, this method will delete **all** rows in the table.
5455
*
5556
* ```php
5657
* $customerQuery = new ActiveQuery(Customer::class, $this->db);
@@ -77,7 +78,7 @@ public function deleteAll(array $condition = []): int;
7778
* Returns a value indicating whether the given active record is the same as the current one.
7879
*
7980
* The comparison is made by comparing the table names and the primary key values of the two active records. If one
80-
* of the records {@see isNewRecord|is new} they are also considered not equal.
81+
* of the records {@see isNewRecord|is new} they're also considered not equal.
8182
*
8283
* @param self $record Record to compare to.
8384
*
@@ -94,7 +95,7 @@ public function equals(self $record): bool;
9495
public function fields(): array;
9596

9697
/**
97-
* Filters array condition before it is assigned to a Query filter.
98+
* Filters array condition before it's assigned to a Query filter.
9899
*
99100
* This method will ensure that an array condition only filters on existing table columns.
100101
*
@@ -110,7 +111,7 @@ public function fields(): array;
110111
public function filterCondition(array $condition, array $aliases = []): array;
111112

112113
/**
113-
* Returns table aliases which are not the same as the name of the tables.
114+
* Returns table aliases which aren't the same as the name of the tables.
114115
*
115116
* @throws InvalidArgumentException
116117
* @throws InvalidConfigException
@@ -120,16 +121,31 @@ public function filterValidAliases(ActiveQuery $query): array;
120121
/**
121122
* Returns the named attribute value.
122123
*
123-
* If this record is the result of a query and the attribute is not loaded, `null` will be returned.
124+
* If this record is the result of a query and the attribute isn't loaded, `null` will be returned.
124125
*
125126
* @param string $name The attribute name.
126127
*
127-
* @return mixed The attribute value. `null` if the attribute is not set or does not exist.
128+
* @return mixed The attribute value. `null` if the attribute isn't set or doesn't exist.
128129
*
129130
* {@see hasAttribute()}
130131
*/
131132
public function getAttribute(string $name): mixed;
132133

134+
/**
135+
* Returns attribute values.
136+
*
137+
* @param array|null $names List of attributes whose value needs to be returned. Defaults to null, meaning all
138+
* attributes listed in {@see attributes()} will be returned.
139+
* If it's an array, only the attributes in the array will be returned.
140+
* @param array $except List of attributes whose value shouldn't be returned.
141+
*
142+
* @throws InvalidConfigException
143+
* @throws Exception
144+
*
145+
* @return array Attribute values (name => value).
146+
*/
147+
public function getAttributes(array $names = null, array $except = []): array;
148+
133149
/**
134150
* Returns a value indicating whether the current record is new (not saved in the database).
135151
*
@@ -140,14 +156,14 @@ public function getIsNewRecord(): bool;
140156
/**
141157
* Returns the old primary key value(s).
142158
*
143-
* This refers to the primary key value that is populated into the record after executing a find method (e.g.
159+
* This refers to the primary key value that's populated into the record after executing a find method (for example
144160
* findOne()).
145161
*
146162
* The value remains unchanged even if the primary key attribute is manually assigned with a different value.
147163
*
148164
* @param bool $asArray Whether to return the primary key value as an array. If true, the return value will be an
149165
* array with column name as key and column value as value. If this is `false` (default), a scalar value will be
150-
* returned for non-composite primary key.
166+
* returned for a non-composite primary key.
151167
*
152168
* @return mixed The old primary key value. An array (column name => column value) is returned if the primary key
153169
* is composite or `$asArray` is true. A string is returned otherwise (`null` will be returned if the key value is
@@ -174,9 +190,9 @@ public function getPrimaryKey(bool $asArray = false): mixed;
174190
* A relation is defined by a getter method which returns an object implementing the {@see ActiveQueryInterface}
175191
* (normally this would be a relational {@see ActiveQuery} object).
176192
*
177-
* @param string $name The relation name, e.g. `orders` for a relation defined via `getOrders()` method
193+
* @param string $name The relation name, for example `orders` for a relation defined via `getOrders()` method
178194
* (case-sensitive).
179-
* @param bool $throwException Whether to throw exception if the relation does not exist.
195+
* @param bool $throwException Whether to throw exception if the relation doesn't exist.
180196
*
181197
* @return ActiveQueryInterface|null The relational query object.
182198
*/
@@ -210,9 +226,9 @@ public function hasAttribute(string $name): bool;
210226
/**
211227
* Inserts a row into the associated database table using the attribute values of this record.
212228
*
213-
* Only the {@see dirtyAttributes|changed attribute values} will be inserted into database.
229+
* Only the {@see dirtyAttributes|changed attribute values} will be inserted into a database.
214230
*
215-
* If the table's primary key is auto-incremental and is `null` during insertion, it will be populated with the
231+
* If the table's primary key is auto incremental and is `null` during insertion, it will be populated with the
216232
* actual value after insertion.
217233
*
218234
* For example, to insert a customer record:
@@ -246,7 +262,7 @@ public function isPrimaryKey(array $keys): bool;
246262
/**
247263
* Check whether the named relation has been populated with records.
248264
*
249-
* @param string $name The relation name, e.g. `orders` for a relation defined via `getOrders()` method
265+
* @param string $name The relation name, for example `orders` for a relation defined via `getOrders()` method
250266
* (case-sensitive).
251267
*
252268
* @return bool Whether relation has been populated with records.
@@ -261,18 +277,18 @@ public function isRelationPopulated(string $name): bool;
261277
* The relationship is established by setting the foreign key value(s) in one record to be the corresponding primary
262278
* key value(s) in the other record.
263279
*
264-
* The record with the foreign key will be saved into database without performing validation.
280+
* The record with the foreign key will be saved into a database without performing validation.
265281
*
266282
* If the relationship involves a junction table, a new row will be inserted into the junction table which contains
267283
* the primary key values from both records.
268284
*
269-
* This method requires that the primary key value is not `null`.
285+
* This method requires that the primary key value isn't `null`.
270286
*
271-
* @param string $name The case-sensitive name of the relationship, e.g. `orders` for a relation defined via
287+
* @param string $name The case-sensitive name of the relationship, for example `orders` for a relation defined via
272288
* `getOrders()` method.
273289
* @param self $arClass The record to be linked with the current one.
274-
* @param array $extraColumns Additional column values to be saved into the junction table. This parameter is only
275-
* meaningful for a relationship involving a junction table (i.e., a relation set with
290+
* @param array $extraColumns More column values to be saved into the junction table. This parameter is only
291+
* meaningful for a relationship involving a junction table (that's a relation set with
276292
* {@see ActiveQueryInterface::via()}).
277293
*/
278294
public function link(string $name, self $arClass, array $extraColumns = []): void;
@@ -282,7 +298,7 @@ public function link(string $name, self $arClass, array $extraColumns = []): voi
282298
*
283299
* This method is required by the SPL interface {@see ArrayAccess}.
284300
*
285-
* It is implicitly called when you use something like `$value = $model[$offset];`.
301+
* It's implicitly called when you use something like `$value = $model[$offset];`.
286302
*
287303
* @param mixed $offset the offset to retrieve element.
288304
*
@@ -293,9 +309,9 @@ public function offsetGet(mixed $offset): mixed;
293309
/**
294310
* Populates the named relation with the related records.
295311
*
296-
* Note that this method does not check if the relation exists or not.
312+
* Note that this method doesn't check if the relation exists or not.
297313
*
298-
* @param string $name The relation name, e.g. `orders` for a relation defined via `getOrders()` method
314+
* @param string $name The relation name, for example `orders` for a relation defined via `getOrders()` method
299315
* (case-sensitive).
300316
* @param array|self|null $records The related records to be populated into the relation.
301317
*/
@@ -304,13 +320,13 @@ public function populateRelation(string $name, array|self|null $records): void;
304320
/**
305321
* Returns the primary key name(s) for this AR class.
306322
*
307-
* The default implementation will return the primary key(s) as declared in the DB table that is associated with
323+
* The default implementation will return the primary key(s) as declared in the DB table that's associated with
308324
* this AR class.
309325
*
310-
* If the DB table does not declare any primary key, you should override this method to return the attributes that
326+
* If the DB table doesn't declare any primary key, you should override this method to return the attributes that
311327
* you want to use as primary keys for this AR class.
312328
*
313-
* Note that an array should be returned even for a table with single primary key.
329+
* Note that an array should be returned even for a table with a single primary key.
314330
*
315331
* @throws Exception
316332
* @throws InvalidConfigException
@@ -337,7 +353,7 @@ public function primaryKey(): array;
337353
* @param array|null $attributeNames List of attribute names that need to be saved. Defaults to `null`,
338354
* meaning all attributes that are loaded from DB will be saved.
339355
*
340-
* @return bool Whether the saving succeeded (i.e. no validation errors occurred).
356+
* @return bool Whether the saving succeeded (that's no validation errors occurred).
341357
*/
342358
public function save(array $attributeNames = null): bool;
343359

@@ -347,14 +363,14 @@ public function save(array $attributeNames = null): bool;
347363
* @param string $name The attribute name.
348364
* @param mixed $value The attribute value.
349365
*
350-
* @throws InvalidArgumentException If the named attribute does not exist.
366+
* @throws InvalidArgumentException If the named attribute doesn't exist.
351367
*/
352368
public function setAttribute(string $name, mixed $value): void;
353369

354370
/**
355371
* Saves the changes to this active record into the associated database table.
356372
*
357-
* Only the {@see dirtyAttributes|changed attribute values} will be saved into database.
373+
* Only the {@see dirtyAttributes|changed attribute values} will be saved into a database.
358374
*
359375
* For example, to update a customer record:
360376
*
@@ -365,8 +381,9 @@ public function setAttribute(string $name, mixed $value): void;
365381
* $customer->update();
366382
* ```
367383
*
368-
* Note that it is possible the update does not affect any row in the table. In this case, this method will return
369-
* 0. For this reason, you should use the following code to check if update() is successful or not:
384+
* Note that it's possible the update doesn't affect any row in the table.
385+
* In this case, this method will return 0.
386+
* For this reason, you should use the following code to check if update() is successful or not:
370387
*
371388
* ```php
372389
* if ($customer->update() !== false) {
@@ -398,7 +415,7 @@ public function update(array $attributeNames = null): false|int;
398415
* $customer->updateAll(['status' => 1], 'status = 2');
399416
* ```
400417
*
401-
* > Warning: If you do not specify any condition, this method will update **all** rows in the table.
418+
* > Warning: If you don't specify any condition, this method will update **all** rows in the table.
402419
*
403420
* ```php
404421
* $customerQuery = new ActiveQuery(Customer::class, $db);
@@ -417,21 +434,43 @@ public function update(array $attributeNames = null): false|int;
417434
* @param array $params The parameters (name => value) to be bound to the query.
418435
*
419436
* @throws InvalidConfigException
420-
* @throws Throwable if the models cannot be unlinked.
437+
* @throws Throwable if the models can't be unlinked.
421438
* @throws Exception
422439
*
423440
* @return int The number of rows updated.
424441
*/
425442
public function updateAll(array $attributes, array|string $condition = [], array $params = []): int;
426443

444+
/**
445+
* Updates the specified attributes.
446+
*
447+
* This method is a shortcut to {@see update()} when data validation isn't needed and only a small set attributes
448+
* need to be updated.
449+
*
450+
* You may specify the attributes to be updated as name list or name-value pairs.
451+
* If the latter, the corresponding attribute values will be modified so.
452+
*
453+
* The method will then save the specified attributes into a database.
454+
*
455+
* Note that this method will **not** perform data validation and will **not** trigger events.
456+
*
457+
* @param array $attributes The attributes (names or name-value pairs) to be updated.
458+
*
459+
* @throws Exception
460+
* @throws NotSupportedException
461+
*
462+
* @return int The number of rows affected.
463+
*/
464+
public function updateAttributes(array $attributes): int;
465+
427466
/**
428467
* Destroys the relationship between two records.
429468
*
430469
* The record with the foreign key of the relationship will be deleted if `$delete` is true.
431470
*
432471
* Otherwise, the foreign key will be set `null` and the record will be saved without validation.
433472
*
434-
* @param string $name The case-sensitive name of the relationship, e.g. `orders` for a relation defined via
473+
* @param string $name The case-sensitive name of the relationship, for example `orders` for a relation defined via
435474
* `getOrders()` method.
436475
* @param self $arClass The active record to be unlinked from the current one.
437476
* @param bool $delete Whether to delete the active record that contains the foreign key.
@@ -451,7 +490,8 @@ public function getOldAttributes(): array;
451490
* Populates an active record object using a row of data from the database/storage.
452491
*
453492
* This is an internal method meant to be called to create active record objects after fetching data from the
454-
* database. It is mainly used by {@see ActiveQuery} to populate the query results into active records.
493+
* database.
494+
* It's mainly used by {@see ActiveQuery} to populate the query results into active records.
455495
*
456496
* @param array|object $row Attribute values (name => value).
457497
*
@@ -461,7 +501,7 @@ public function getOldAttributes(): array;
461501
public function populateRecord(array|object $row): void;
462502

463503
/**
464-
* Serializes the active record into its array implementation with attribute name as key and it value as... value.
504+
* Serializes the active record into its array implementation with attribute name as a key, and it values as value.
465505
*/
466506
public function toArray(): array;
467507
}

src/BaseActiveRecord.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,6 @@ public function getAttribute(string $name): mixed
129129
return $this->attributes[$name] ?? null;
130130
}
131131

132-
/**
133-
* Returns attribute values.
134-
*
135-
* @param array|null $names List of attributes whose value needs to be returned. Defaults to null, meaning all
136-
* attributes listed in {@see attributes()} will be returned. If it is an array, only the attributes in the array
137-
* will be returned.
138-
* @param array $except List of attributes whose value should NOT be returned.
139-
*
140-
* @return array Attribute values (name => value).
141-
*/
142132
public function getAttributes(array $names = null, array $except = []): array
143133
{
144134
$values = [];
@@ -236,6 +226,10 @@ public function getOldAttributes(): array
236226
return $this->oldAttributes ?? [];
237227
}
238228

229+
/**
230+
* @throws InvalidConfigException
231+
* @throws Exception
232+
*/
239233
public function getOldPrimaryKey(bool $asArray = false): array|string|null
240234
{
241235
$keys = $this->primaryKey();
@@ -751,26 +745,6 @@ public function updateAll(array $attributes, array|string $condition = [], array
751745
return $command->execute();
752746
}
753747

754-
/**
755-
* Updates the specified attributes.
756-
*
757-
* This method is a shortcut to {@see update()} when data validation is not needed and only a small set attributes
758-
* need to be updated.
759-
*
760-
* You may specify the attributes to be updated as name list or name-value pairs. If the latter, the corresponding
761-
* attribute values will be modified accordingly.
762-
*
763-
* The method will then save the specified attributes into database.
764-
*
765-
* Note that this method will **not** perform data validation and will **not** trigger events.
766-
*
767-
* @param array $attributes The attributes (names or name-value pairs) to be updated.
768-
*
769-
* @throws Exception
770-
* @throws NotSupportedException
771-
*
772-
* @return int The number of rows affected.
773-
*/
774748
public function updateAttributes(array $attributes): int
775749
{
776750
$attrs = [];

0 commit comments

Comments
 (0)