-
-
Notifications
You must be signed in to change notification settings - Fork 38
Add generic type annotations #578
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,6 @@ | |
| * | ||
| * See {@see ActiveRecord} for a concrete implementation. | ||
| * | ||
| * @psalm-import-type ModelClass from ActiveQuery | ||
| * @psalm-import-type RawFrom from QueryInterface | ||
| */ | ||
| abstract class AbstractActiveRecord implements ActiveRecordInterface | ||
|
|
@@ -61,6 +60,12 @@ | |
| /** @var string[][] */ | ||
| private array $relationsDependencies = []; | ||
|
|
||
| /** | ||
| * @template T as ActiveRecordInterface | ||
| * @template TModelClass as T|class-string<T>|null | ||
| * @psalm-param TModelClass $modelClass | ||
| * @psalm-return ActiveQueryInterface<(TModelClass is null ? static : T), null> | ||
| */ | ||
| public function createQuery(ActiveRecordInterface|string|null $modelClass = null): ActiveQueryInterface | ||
| { | ||
| $modelClass ??= $this; | ||
|
|
@@ -340,7 +345,7 @@ | |
| } else { | ||
| $link = $relation->getLink(); | ||
| $p1 = $linkModel->isPrimaryKey(array_keys($link)); | ||
| $p2 = $this->isPrimaryKey(array_values($link)); | ||
|
Check warning on line 348 in src/AbstractActiveRecord.php
|
||
|
|
||
| if ($p1 && $p2) { | ||
| if ($this->isNew() && $linkModel->isNew()) { | ||
|
|
@@ -348,7 +353,7 @@ | |
| } | ||
|
|
||
| if ($this->isNew()) { | ||
| $this->bindModels(array_flip($link), $this, $linkModel); | ||
|
Check warning on line 356 in src/AbstractActiveRecord.php
|
||
| } else { | ||
| $this->bindModels($link, $linkModel, $this); | ||
| } | ||
|
|
@@ -419,8 +424,15 @@ | |
| $this->related[$name] = $records; | ||
| } | ||
|
|
||
| /** | ||
| * @template T as ActiveRecordInterface | ||
| * @template TModelClass as T|class-string<T>|null | ||
| * @psalm-param TModelClass $modelClass | ||
| * @psalm-return ActiveQuery<(TModelClass is null ? static : T), null> | ||
| */ | ||
|
Tigrov marked this conversation as resolved.
|
||
| public static function query(ActiveRecordInterface|string|null $modelClass = null): ActiveQueryInterface | ||
| { | ||
| /** @psalm-var ActiveQuery<(TModelClass is null ? static : T), null> */ | ||
| return new ActiveQuery($modelClass ?? static::class); | ||
| } | ||
|
|
||
|
|
@@ -453,7 +465,7 @@ | |
|
|
||
| public function resetRelation(string $name): void | ||
| { | ||
| foreach ($this->relationsDependencies as &$relationNames) { | ||
|
Check warning on line 468 in src/AbstractActiveRecord.php
|
||
| unset($relationNames[$name]); | ||
| } | ||
|
|
||
|
|
@@ -474,7 +486,7 @@ | |
| { | ||
| if ( | ||
| isset($this->relationsDependencies[$propertyName]) | ||
| && ($value === null || $this->get($propertyName) !== $value) | ||
|
Check warning on line 489 in src/AbstractActiveRecord.php
|
||
| ) { | ||
| $this->resetDependentRelations($propertyName); | ||
| } | ||
|
|
@@ -484,7 +496,7 @@ | |
|
|
||
| public function populateProperties(array $values): void | ||
| { | ||
| $values = array_intersect_key($values, array_flip($this->propertyNames())); | ||
|
Check warning on line 499 in src/AbstractActiveRecord.php
|
||
| foreach ($values as $name => $value) { | ||
| $this->populateProperty($name, $value); | ||
| } | ||
|
|
@@ -650,7 +662,7 @@ | |
| } | ||
| } | ||
|
|
||
| if (!$relation->isMultiple()) { | ||
|
Check warning on line 665 in src/AbstractActiveRecord.php
|
||
| unset($this->related[$relationName]); | ||
| } elseif (isset($this->related[$relationName]) && is_array($this->related[$relationName])) { | ||
| /** @psalm-var array<array-key, ActiveRecordInterface> $related */ | ||
|
|
@@ -822,17 +834,20 @@ | |
| * | ||
| * @return ActiveQueryInterface The relational query object. | ||
| * | ||
| * @psalm-param ModelClass $modelClass | ||
| * @template T as ActiveRecordInterface | ||
| * @psalm-param T|class-string<T> $modelClass | ||
| * @psalm-param array<string, string> $link | ||
| * @psalm-return ActiveQueryInterface<T, null> | ||
| * | ||
|
Tigrov marked this conversation as resolved.
|
||
| * @see AbstractActiveRecord::hasOne() | ||
| * @see AbstractActiveRecord::hasMany() | ||
| */ | ||
| protected function createRelationQuery( | ||
|
Check warning on line 845 in src/AbstractActiveRecord.php
|
||
| ActiveRecordInterface|string $modelClass, | ||
| array $link, | ||
| bool $multiple, | ||
| ): ActiveQueryInterface { | ||
| /** @psalm-var ActiveQueryInterface<T, null> */ | ||
| return $this->createQuery($modelClass)->primaryModel($this)->link($link)->multiple($multiple); | ||
| } | ||
|
|
||
|
|
@@ -844,7 +859,7 @@ | |
| * | ||
| * @see AbstractActiveRecord::delete() | ||
| */ | ||
| protected function deleteInternal(): int | ||
|
Check warning on line 862 in src/AbstractActiveRecord.php
|
||
| { | ||
| /** | ||
| * We don't check the return value of deleteAll() because it is possible the record is already deleted in | ||
|
|
@@ -915,7 +930,7 @@ | |
| * | ||
| * @see AbstractActiveRecord::refresh() | ||
| */ | ||
| protected function refreshInternal(array|ActiveRecordInterface|null $record = null): bool | ||
|
Check warning on line 933 in src/AbstractActiveRecord.php
|
||
| { | ||
| if ($record === null || is_array($record)) { | ||
| return false; | ||
|
|
@@ -942,7 +957,7 @@ | |
| * | ||
| * @see AbstractActiveRecord::update() | ||
| */ | ||
| protected function updateInternal(?array $properties = null): int | ||
|
Check warning on line 960 in src/AbstractActiveRecord.php
|
||
| { | ||
| if ($this->isNew()) { | ||
| throw new InvalidCallException('The record is new and cannot be updated.'); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.