From 687ae1cc99db7a1af20f5b8421a268e4cbdbc5f5 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Sun, 17 Nov 2024 18:07:31 +0100 Subject: [PATCH] Remove TKey on Model and ActiveRecord --- Makefile | 2 +- lib/ActiveRecord.php | 34 ++++++++++--------- .../AbstractActiveRecordCache.php | 1 + lib/ActiveRecord/Model.php | 23 ++++++------- lib/ActiveRecord/ModelProvider.php | 2 +- lib/ActiveRecord/Query.php | 4 +-- lib/ActiveRecord/Relation.php | 2 +- lib/ActiveRecord/Schema/BelongsTo.php | 6 ++-- lib/ActiveRecord/StaticModelProvider.php | 2 +- phpstan.neon | 2 -- tests/Acme/ArticleQuery.php | 4 +-- tests/Acme/Brand.php | 3 -- tests/Acme/Car.php | 2 -- tests/Acme/Comment.php | 3 -- tests/Acme/Count.php | 3 -- tests/Acme/DanceSession.php | 2 -- tests/Acme/Driver.php | 3 -- tests/Acme/HasMany/Appointment.php | 2 -- tests/Acme/HasMany/Patient.php | 2 -- tests/Acme/HasMany/Physician.php | 2 -- tests/Acme/Location.php | 3 -- tests/Acme/Node.php | 3 -- tests/Acme/Person.php | 2 -- tests/Acme/Subscriber.php | 3 -- tests/Acme/Update.php | 3 -- tests/ActiveRecordTest.php | 2 +- 26 files changed, 42 insertions(+), 78 deletions(-) diff --git a/Makefile b/Makefile index 5331c6e..4dc9469 100644 --- a/Makefile +++ b/Makefile @@ -52,4 +52,4 @@ test-container-84: .PHONY: lint lint: @XDEBUG_MODE=off phpcs -s - @XDEBUG_MODE=off vendor/bin/phpstan + @XDEBUG_MODE=off vendor/bin/phpstan --memory-limit=-1 diff --git a/lib/ActiveRecord.php b/lib/ActiveRecord.php index 1c4438b..daa6af3 100644 --- a/lib/ActiveRecord.php +++ b/lib/ActiveRecord.php @@ -22,14 +22,15 @@ * * @method ValidationErrors validate() Validate the active record, returns an array of errors. * - * @see self::get_model() - * @property-read Model $model The model managing the active record. - * @see self::get_is_new() - * @property-read bool $is_new Whether the record is new or not. - * @see self::get_primary_key_value() - * @property-read TKey $primary_key_value The value of the primary key. - * - * @template TKey of scalar|scalar[] + * @property-read Model $model + * The model managing the active record. + * {@see self::get_model()} + * @property-read bool $is_new + * Whether the record is new or not. + * {@see self::get_is_new()} + * @property-read scalar|scalar[] $primary_key_value + * The value of the primary key. + * {@see self::get_primary_key_value()} */ abstract class ActiveRecord extends Prototyped { @@ -62,21 +63,22 @@ final public static function where(...$conditions_and_args): Query /** * Model managing the active record. * - * @var Model + * @var Model */ private Model $model; /** - * @return Model + * @return Model */ protected function get_model(): Model { + /** @var Model */ return $this->model ??= StaticModelProvider::model_for_record($this::class); } /** - * @return mixed&TKey + * @return scalar|scalar[] */ protected function get_primary_key_value(): mixed { @@ -97,9 +99,9 @@ protected function get_primary_key_value(): mixed } /** - * @param ?Model $model - * The model managing the active record. A {@link Model} instance can be specified as well as a model - * identifier. If `$model` is null, the model will be resolved with {@link StaticModelProvider} when required. + * @param ?Model $model + * The model managing the active record. A {@see Model} instance can be specified as well as a model + * identifier. If `$model` is null, the model will be resolved with {@see StaticModelProvider} when required. */ public function __construct(?Model $model = null) { @@ -109,9 +111,9 @@ public function __construct(?Model $model = null) } /** - * Removes the {@link $model} property. + * Removes the {@see $model} property. * - * Properties whose value are instances of the {@link ActiveRecord} class are removed from the + * Properties whose values are instances of the {@see ActiveRecord} class are removed from the * exported properties. * * @return array diff --git a/lib/ActiveRecord/ActiveRecordCache/AbstractActiveRecordCache.php b/lib/ActiveRecord/ActiveRecordCache/AbstractActiveRecordCache.php index a173322..4ec05b6 100644 --- a/lib/ActiveRecord/ActiveRecordCache/AbstractActiveRecordCache.php +++ b/lib/ActiveRecord/ActiveRecordCache/AbstractActiveRecordCache.php @@ -2,6 +2,7 @@ namespace ICanBoogie\ActiveRecord\ActiveRecordCache; +use ICanBoogie\ActiveRecord; use ICanBoogie\ActiveRecord\ActiveRecordCache; use ICanBoogie\ActiveRecord\Model; diff --git a/lib/ActiveRecord/Model.php b/lib/ActiveRecord/Model.php index 4399e93..e51bfdb 100644 --- a/lib/ActiveRecord/Model.php +++ b/lib/ActiveRecord/Model.php @@ -15,20 +15,19 @@ /** * Base class for activerecord models. * - * @template TKey of scalar|scalar[] - * @template TValue of ActiveRecord + * @template TRecord of ActiveRecord * * @property-read Model|null $parent Parent model. */ class Model extends Table { /** - * @var class-string + * @var class-string */ public readonly string $activerecord_class; /** - * @var class-string> + * @var class-string> */ public readonly string $query_class; @@ -68,7 +67,7 @@ private function resolve_parent(ModelProvider $models): ?Model * * @param int|non-empty-string ...$keys * - * @return TValue|TValue[] A record or a set of records. + * @return TRecord|TRecord[] A record or a set of records. * @throws RecordNotFound when the record, or one or more records of the records * set couldn't be found. */ @@ -88,13 +87,13 @@ public function find(int|string ...$keys) * * @param int|non-empty-string $key * - * @return ActiveRecord&TValue + * @return ActiveRecord&TRecord */ private function find_one(int|string $key): ActiveRecord { assert(is_string($this->primary)); - /** @var TValue|false $record */ + /** @var TRecord|false $record */ $record = $this->where([ $this->primary => $key ])->one; if (!$record) { @@ -112,7 +111,7 @@ private function find_one(int|string $key): ActiveRecord * * @param array $keys * - * @return array + * @return array */ private function find_many(array $keys): array { @@ -132,7 +131,7 @@ private function find_many(array $keys): array } } - /** @var array $records */ + /** @var array $records */ if ($missing) { if (count($missing) > 1) { @@ -157,7 +156,7 @@ private function find_many(array $keys): array /** * Returns a new query. * - * @return Query + * @return Query */ public function query(): Query { @@ -167,7 +166,7 @@ public function query(): Query /** * Returns a new query with the WHERE clause initialized with the provided conditions and arguments. * - * @return Query + * @return Query * * @see Query::where() */ @@ -183,7 +182,7 @@ public function where(mixed ...$conditions_and_args): Query * * @param array $properties Optional properties to instantiate the record with. * - * @retrun TValue + * @retrun TRecord */ public function new(array $properties = []): ActiveRecord { diff --git a/lib/ActiveRecord/ModelProvider.php b/lib/ActiveRecord/ModelProvider.php index 97619b2..32bc566 100644 --- a/lib/ActiveRecord/ModelProvider.php +++ b/lib/ActiveRecord/ModelProvider.php @@ -16,7 +16,7 @@ interface ModelProvider * * @param class-string $activerecord_class * - * @phpstan-return Model + * @phpstan-return Model */ public function model_for_record(string $activerecord_class): Model; } diff --git a/lib/ActiveRecord/Query.php b/lib/ActiveRecord/Query.php index b528b1b..fdc30ab 100644 --- a/lib/ActiveRecord/Query.php +++ b/lib/ActiveRecord/Query.php @@ -28,7 +28,7 @@ use const PHP_INT_MAX; /** - * @template TRecord of ActiveRecord + * @template-covariant TRecord of ActiveRecord * * @implements IteratorAggregate * @@ -201,7 +201,7 @@ private function get_args(): array private array $mode = []; /** - * @param Model $model The model to query. + * @param Model $model The model to query. */ public function __construct( public readonly Model $model diff --git a/lib/ActiveRecord/Relation.php b/lib/ActiveRecord/Relation.php index 03e3441..5d7adf8 100644 --- a/lib/ActiveRecord/Relation.php +++ b/lib/ActiveRecord/Relation.php @@ -73,7 +73,7 @@ protected function resolve_related_model(): Model * * @param class-string $activerecord_class * - * @return Model + * @return Model */ protected function model_for_activerecord(string $activerecord_class): Model { diff --git a/lib/ActiveRecord/Schema/BelongsTo.php b/lib/ActiveRecord/Schema/BelongsTo.php index ac2d5ba..c9ac845 100644 --- a/lib/ActiveRecord/Schema/BelongsTo.php +++ b/lib/ActiveRecord/Schema/BelongsTo.php @@ -35,10 +35,10 @@ public static function __set_state(array $an_array): self * @param class-string $associate * The associate ActiveRecord class. * @param non-empty-string|null $as - * The name of prototype getter for the association, by default it is build according to the column name e.g. - * `article_id` would result is a `article` getter. + * The name of prototype getter for the association, by default, it is built according to the column name e.g. + * `article_id` would result in an `article` getter. */ - public function __construct( // @phpstan-ignore-line + public function __construct( public string $associate, int $size = Integer::SIZE_REGULAR, bool $null = false, diff --git a/lib/ActiveRecord/StaticModelProvider.php b/lib/ActiveRecord/StaticModelProvider.php index 10ab398..48a628f 100644 --- a/lib/ActiveRecord/StaticModelProvider.php +++ b/lib/ActiveRecord/StaticModelProvider.php @@ -63,7 +63,7 @@ public static function reset(): void * * @param class-string $activerecord_class * - * @phpstan-return Model + * @return Model **/ public static function model_for_record(string $activerecord_class): Model { diff --git a/phpstan.neon b/phpstan.neon index 611da2a..cc52d0c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,8 +4,6 @@ parameters: - lib ignoreErrors: - "#get_.*is unused#" - - "#ActiveRecord but does not specify its types#" - - "#generic class .+ActiveRecord does not specify its types#" - "#generic class .+Model does not specify its types#" - "#generic class .+Model but does not specify its types#" - "#generic class .+Query but does not specify its types#" diff --git a/tests/Acme/ArticleQuery.php b/tests/Acme/ArticleQuery.php index 63deaea..7d2bf4a 100644 --- a/tests/Acme/ArticleQuery.php +++ b/tests/Acme/ArticleQuery.php @@ -8,8 +8,8 @@ * @extends Query
* * @property-read self $ordered - * @uses self::ordered - * @uses self::get_ordered + * {@see self::ordered()} + * {@see self::get_ordered()} */ class ArticleQuery extends Query { diff --git a/tests/Acme/Brand.php b/tests/Acme/Brand.php index 89a9512..e494e7c 100644 --- a/tests/Acme/Brand.php +++ b/tests/Acme/Brand.php @@ -7,9 +7,6 @@ use ICanBoogie\ActiveRecord\Schema\Id; use ICanBoogie\ActiveRecord\Schema\Serial; -/** - * @extends ActiveRecord - */ class Brand extends ActiveRecord { #[Id, Serial] diff --git a/tests/Acme/Car.php b/tests/Acme/Car.php index 44095b6..49192a1 100644 --- a/tests/Acme/Car.php +++ b/tests/Acme/Car.php @@ -11,8 +11,6 @@ /** * @property Brand $brand * @property Driver $driver - * - * @extends ActiveRecord */ class Car extends ActiveRecord { diff --git a/tests/Acme/Comment.php b/tests/Acme/Comment.php index 38164f7..00dec42 100644 --- a/tests/Acme/Comment.php +++ b/tests/Acme/Comment.php @@ -8,9 +8,6 @@ use ICanBoogie\ActiveRecord\Schema\Serial; use ICanBoogie\ActiveRecord\Schema\Text; -/** - * @extends ActiveRecord - */ class Comment extends ActiveRecord { #[Id, Serial] diff --git a/tests/Acme/Count.php b/tests/Acme/Count.php index 972050d..ac37f2e 100644 --- a/tests/Acme/Count.php +++ b/tests/Acme/Count.php @@ -8,9 +8,6 @@ use ICanBoogie\ActiveRecord\Schema\Id; use ICanBoogie\ActiveRecord\Schema\Serial; -/** - * @extends ActiveRecord - */ class Count extends ActiveRecord { #[Id, Serial] diff --git a/tests/Acme/DanceSession.php b/tests/Acme/DanceSession.php index 985ef69..5f34e7f 100644 --- a/tests/Acme/DanceSession.php +++ b/tests/Acme/DanceSession.php @@ -10,8 +10,6 @@ /** * @property-read ActiveRecord\Query $people - * - * @extends ActiveRecord */ #[HasMany(Person::class)] final class DanceSession extends ActiveRecord diff --git a/tests/Acme/Driver.php b/tests/Acme/Driver.php index 17f539d..04cccb5 100644 --- a/tests/Acme/Driver.php +++ b/tests/Acme/Driver.php @@ -7,9 +7,6 @@ use ICanBoogie\ActiveRecord\Schema\Id; use ICanBoogie\ActiveRecord\Schema\Serial; -/** - * @extends ActiveRecord - */ class Driver extends ActiveRecord { #[Id, Serial] diff --git a/tests/Acme/HasMany/Appointment.php b/tests/Acme/HasMany/Appointment.php index 30d0e16..75a3c62 100644 --- a/tests/Acme/HasMany/Appointment.php +++ b/tests/Acme/HasMany/Appointment.php @@ -11,8 +11,6 @@ /** * @property-read Physician $physician * @property-read Patient $patient - * - * @extends ActiveRecord */ class Appointment extends ActiveRecord { diff --git a/tests/Acme/HasMany/Patient.php b/tests/Acme/HasMany/Patient.php index 97329f3..d9f6f68 100644 --- a/tests/Acme/HasMany/Patient.php +++ b/tests/Acme/HasMany/Patient.php @@ -12,8 +12,6 @@ /** * @property-read Query $physicians * @property-read Query $appointments - * - * @extends ActiveRecord */ #[HasMany(Appointment::class, foreign_key: 'patient_id')] #[HasMany(Physician::class, through: Appointment::class)] diff --git a/tests/Acme/HasMany/Physician.php b/tests/Acme/HasMany/Physician.php index 12edfc8..4ef724c 100644 --- a/tests/Acme/HasMany/Physician.php +++ b/tests/Acme/HasMany/Physician.php @@ -12,8 +12,6 @@ /** * @property-read Query $appointments * @property-read Query $patients - * - * @extends ActiveRecord */ #[HasMany(Appointment::class, foreign_key: 'physician_id')] #[HasMany(Patient::class, through: Appointment::class)] diff --git a/tests/Acme/Location.php b/tests/Acme/Location.php index 66c00ee..2ad0a25 100644 --- a/tests/Acme/Location.php +++ b/tests/Acme/Location.php @@ -4,9 +4,6 @@ use ICanBoogie\ActiveRecord; -/** - * @extends ActiveRecord - */ class Location extends ActiveRecord { } diff --git a/tests/Acme/Node.php b/tests/Acme/Node.php index 45e9af8..0cfb3d9 100644 --- a/tests/Acme/Node.php +++ b/tests/Acme/Node.php @@ -7,9 +7,6 @@ use ICanBoogie\ActiveRecord\Schema\Id; use ICanBoogie\ActiveRecord\Schema\Serial; -/** - * @extends ActiveRecord - */ class Node extends ActiveRecord { #[Id, Serial] diff --git a/tests/Acme/Person.php b/tests/Acme/Person.php index 8643fe6..ae3dcfc 100644 --- a/tests/Acme/Person.php +++ b/tests/Acme/Person.php @@ -14,8 +14,6 @@ * @property-read Skill $hire_skill * @property-read Skill $summon_skill * @property-read Skill $teach_skill - * - * @extends ActiveRecord */ #[HasMany(Equipment::class, through: PersonEquipment::class)] class Person extends ActiveRecord diff --git a/tests/Acme/Subscriber.php b/tests/Acme/Subscriber.php index ff6d583..583ab3c 100644 --- a/tests/Acme/Subscriber.php +++ b/tests/Acme/Subscriber.php @@ -7,9 +7,6 @@ use ICanBoogie\ActiveRecord\Schema\Id; use ICanBoogie\ActiveRecord\Schema\Serial; -/** - * @extends ActiveRecord - */ class Subscriber extends ActiveRecord { #[Id, Serial] diff --git a/tests/Acme/Update.php b/tests/Acme/Update.php index 8f4c23c..22cdc35 100644 --- a/tests/Acme/Update.php +++ b/tests/Acme/Update.php @@ -9,9 +9,6 @@ use ICanBoogie\ActiveRecord\Schema\Id; use ICanBoogie\ActiveRecord\Schema\Serial; -/** - * @extends ActiveRecord - */ class Update extends ActiveRecord { #[Id, Serial] diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 0b65729..ac0abaf 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -136,7 +136,7 @@ public function test_validate(): void try { $record->save(); } catch (RecordNotValid $e) { - $errors = $e->errors; + $errors = $e->errors->to_array(); $this->assertArrayNotHasKey('id', $errors); $this->assertArrayHasKey('name', $errors);