Skip to content

Commit

Permalink
Remove TKey on Model and ActiveRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Nov 17, 2024
1 parent ff24d22 commit 687ae1c
Show file tree
Hide file tree
Showing 26 changed files with 42 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 18 additions & 16 deletions lib/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<static> $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
{
Expand Down Expand Up @@ -62,21 +63,22 @@ final public static function where(...$conditions_and_args): Query
/**
* Model managing the active record.
*
* @var Model<TKey, static>
* @var Model<static>
*/
private Model $model;

/**
* @return Model<TKey, static>
* @return Model<static>
*/
protected function get_model(): Model
{
/** @var Model<static> */
return $this->model
??= StaticModelProvider::model_for_record($this::class);
}

/**
* @return mixed&TKey
* @return scalar|scalar[]
*/
protected function get_primary_key_value(): mixed
{
Expand All @@ -97,9 +99,9 @@ protected function get_primary_key_value(): mixed
}

/**
* @param ?Model<TKey, static> $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<static> $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)
{
Expand All @@ -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<non-empty-string, mixed>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ICanBoogie\ActiveRecord\ActiveRecordCache;

use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\ActiveRecordCache;
use ICanBoogie\ActiveRecord\Model;

Expand Down
23 changes: 11 additions & 12 deletions lib/ActiveRecord/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TValue>
* @var class-string<TRecord>
*/
public readonly string $activerecord_class;

/**
* @var class-string<Query<TValue>>
* @var class-string<Query<TRecord>>
*/
public readonly string $query_class;

Expand Down Expand Up @@ -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.
*/
Expand All @@ -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) {
Expand All @@ -112,7 +111,7 @@ private function find_one(int|string $key): ActiveRecord
*
* @param array<int|non-empty-string> $keys
*
* @return array<int|non-empty-string, TValue>
* @return array<int|non-empty-string, TRecord>
*/
private function find_many(array $keys): array
{
Expand All @@ -132,7 +131,7 @@ private function find_many(array $keys): array
}
}

/** @var array<int|non-empty-string, TValue> $records */
/** @var array<int|non-empty-string, TRecord> $records */

if ($missing) {
if (count($missing) > 1) {
Expand All @@ -157,7 +156,7 @@ private function find_many(array $keys): array
/**
* Returns a new query.
*
* @return Query<TValue>
* @return Query<TRecord>
*/
public function query(): Query
{
Expand All @@ -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<TValue>
* @return Query<TRecord>
*
* @see Query::where()
*/
Expand All @@ -183,7 +182,7 @@ public function where(mixed ...$conditions_and_args): Query
*
* @param array<string, mixed> $properties Optional properties to instantiate the record with.
*
* @retrun TValue
* @retrun TRecord
*/
public function new(array $properties = []): ActiveRecord
{
Expand Down
2 changes: 1 addition & 1 deletion lib/ActiveRecord/ModelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface ModelProvider
*
* @param class-string<T> $activerecord_class
*
* @phpstan-return Model<scalar|scalar[], T>
* @phpstan-return Model<T>
*/
public function model_for_record(string $activerecord_class): Model;
}
4 changes: 2 additions & 2 deletions lib/ActiveRecord/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use const PHP_INT_MAX;

/**
* @template TRecord of ActiveRecord
* @template-covariant TRecord of ActiveRecord
*
* @implements IteratorAggregate<TRecord>
*
Expand Down Expand Up @@ -201,7 +201,7 @@ private function get_args(): array
private array $mode = [];

/**
* @param Model<int|non-empty-string, TRecord> $model The model to query.
* @param Model<TRecord> $model The model to query.
*/
public function __construct(
public readonly Model $model
Expand Down
2 changes: 1 addition & 1 deletion lib/ActiveRecord/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function resolve_related_model(): Model
*
* @param class-string<T> $activerecord_class
*
* @return Model<scalar|scalar[], T>
* @return Model<T>
*/
protected function model_for_activerecord(string $activerecord_class): Model
{
Expand Down
6 changes: 3 additions & 3 deletions lib/ActiveRecord/Schema/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public static function __set_state(array $an_array): self
* @param class-string<ActiveRecord> $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,
Expand Down
2 changes: 1 addition & 1 deletion lib/ActiveRecord/StaticModelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function reset(): void
*
* @param class-string<T> $activerecord_class
*
* @phpstan-return Model<scalar|scalar[], T>
* @return Model<T>
**/
public static function model_for_record(string $activerecord_class): Model
{
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
4 changes: 2 additions & 2 deletions tests/Acme/ArticleQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* @extends Query<Article>
*
* @property-read self $ordered
* @uses self::ordered
* @uses self::get_ordered
* {@see self::ordered()}
* {@see self::get_ordered()}
*/
class ArticleQuery extends Query
{
Expand Down
3 changes: 0 additions & 3 deletions tests/Acme/Brand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use ICanBoogie\ActiveRecord\Schema\Id;
use ICanBoogie\ActiveRecord\Schema\Serial;

/**
* @extends ActiveRecord<int>
*/
class Brand extends ActiveRecord
{
#[Id, Serial]
Expand Down
2 changes: 0 additions & 2 deletions tests/Acme/Car.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
/**
* @property Brand $brand
* @property Driver $driver
*
* @extends ActiveRecord<int>
*/
class Car extends ActiveRecord
{
Expand Down
3 changes: 0 additions & 3 deletions tests/Acme/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use ICanBoogie\ActiveRecord\Schema\Serial;
use ICanBoogie\ActiveRecord\Schema\Text;

/**
* @extends ActiveRecord<int>
*/
class Comment extends ActiveRecord
{
#[Id, Serial]
Expand Down
3 changes: 0 additions & 3 deletions tests/Acme/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use ICanBoogie\ActiveRecord\Schema\Id;
use ICanBoogie\ActiveRecord\Schema\Serial;

/**
* @extends ActiveRecord<int>
*/
class Count extends ActiveRecord
{
#[Id, Serial]
Expand Down
2 changes: 0 additions & 2 deletions tests/Acme/DanceSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

/**
* @property-read ActiveRecord\Query<Person> $people
*
* @extends ActiveRecord<int>
*/
#[HasMany(Person::class)]
final class DanceSession extends ActiveRecord
Expand Down
3 changes: 0 additions & 3 deletions tests/Acme/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use ICanBoogie\ActiveRecord\Schema\Id;
use ICanBoogie\ActiveRecord\Schema\Serial;

/**
* @extends ActiveRecord<int>
*/
class Driver extends ActiveRecord
{
#[Id, Serial]
Expand Down
2 changes: 0 additions & 2 deletions tests/Acme/HasMany/Appointment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
/**
* @property-read Physician $physician
* @property-read Patient $patient
*
* @extends ActiveRecord<int>
*/
class Appointment extends ActiveRecord
{
Expand Down
2 changes: 0 additions & 2 deletions tests/Acme/HasMany/Patient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
/**
* @property-read Query<Physician> $physicians
* @property-read Query<Appointment> $appointments
*
* @extends ActiveRecord<int>
*/
#[HasMany(Appointment::class, foreign_key: 'patient_id')]
#[HasMany(Physician::class, through: Appointment::class)]
Expand Down
2 changes: 0 additions & 2 deletions tests/Acme/HasMany/Physician.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
/**
* @property-read Query<Appointment> $appointments
* @property-read Query<Patient> $patients
*
* @extends ActiveRecord<int>
*/
#[HasMany(Appointment::class, foreign_key: 'physician_id')]
#[HasMany(Patient::class, through: Appointment::class)]
Expand Down
3 changes: 0 additions & 3 deletions tests/Acme/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use ICanBoogie\ActiveRecord;

/**
* @extends ActiveRecord<int>
*/
class Location extends ActiveRecord
{
}
3 changes: 0 additions & 3 deletions tests/Acme/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use ICanBoogie\ActiveRecord\Schema\Id;
use ICanBoogie\ActiveRecord\Schema\Serial;

/**
* @extends ActiveRecord<int>
*/
class Node extends ActiveRecord
{
#[Id, Serial]
Expand Down
2 changes: 0 additions & 2 deletions tests/Acme/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* @property-read Skill $hire_skill
* @property-read Skill $summon_skill
* @property-read Skill $teach_skill
*
* @extends ActiveRecord<int>
*/
#[HasMany(Equipment::class, through: PersonEquipment::class)]
class Person extends ActiveRecord
Expand Down
3 changes: 0 additions & 3 deletions tests/Acme/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use ICanBoogie\ActiveRecord\Schema\Id;
use ICanBoogie\ActiveRecord\Schema\Serial;

/**
* @extends ActiveRecord<int>
*/
class Subscriber extends ActiveRecord
{
#[Id, Serial]
Expand Down
3 changes: 0 additions & 3 deletions tests/Acme/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
use ICanBoogie\ActiveRecord\Schema\Id;
use ICanBoogie\ActiveRecord\Schema\Serial;

/**
* @extends ActiveRecord<int>
*/
class Update extends ActiveRecord
{
#[Id, Serial]
Expand Down
Loading

0 comments on commit 687ae1c

Please sign in to comment.