Skip to content

feat(repositories): add generic model type annotations to the base repository - #316

Open
vitgrams wants to merge 9 commits into
masterfrom
vitgrams/repository-template-annotation
Open

vitgrams wants to merge 9 commits into
masterfrom
vitgrams/repository-template-annotation

Conversation

@vitgrams

@vitgrams vitgrams commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What

  • @template TModel of Model = Model on BaseRepository, EntityControlTrait and SearchTrait.
  • @use bindings: BaseRepository binds EntityControlTrait<TModel>, EntityControlTrait binds SearchTrait<TModel>.
  • Return annotations:
    • TModel: create(), updateOrCreate(), firstOrCreate()
    • TModel|null: update(), first(), last(), findBy(), find()
    • Collection<int, TModel>: all(), get(), getByList()
    • LengthAwarePaginator<int, TModel>: getSearchResults()
    • $this: every fluent method with a native self return type
  • Query builder: Query<TModel> on getQuery(), getSearchQuery() and on the $query property, so custom repository methods built on the builder get the concrete model. The malformed @property Query query tag is replaced with a @var annotation.
  • @var TModel on the $model property declaration instead of the class-level @property tag, so the model is typed for classes that use EntityControlTrait directly as well.
  • @param class-string<TModel> on setModel(), so a model class that does not match @extends is reported.

Why

PHPStan/Larastan did not know the concrete model of a repository, so every result was Model and level 5 analysis produced noise on model properties and methods.

How to use

/**
 * @extends BaseRepository<User>
 */
class UserRepository extends BaseRepository

Backward compatibility

PHPDoc only: native types and method bodies are unchanged. The template defaults to Model, so repositories without @extends and classes that use the traits without @use are still inferred as Model and get no missingType.generics errors on PHPStan level 6 and above.

How verified

  • vendor/bin/phpunit: OK (491 tests, 1812 assertions).
  • Pint: PASS.
  • Temporary PHPStan 2.3 and Larastan 3.15 runs at levels 5 and 6 with dumpType (not committed):
    • a repository with @extends BaseRepository<TestModel>: $this->query and getQuery() -> Builder<TestModel>, getSearchQuery()->first() -> TestModel|null, a custom method returning getQuery()->where()->first() as ?TestModel has no errors, setModel() with another model class is reported
    • a repository without @extends and a class using the traits directly: no missingType.generics errors, find() -> Model|null
  • A fresh Laravel 13 app with Larastan 3.15 and @extends BaseRepository<Building>:
    • find() / with()->find() -> Building|null
    • create() -> Building
    • get() -> Collection<int, Building>
    • getSearchResults() -> LengthAwarePaginator<int, Building>
    • fluent methods -> the concrete repository
    • ->id with no errors
    • a repository without @extends -> Model
  • Not verified in PhpStorm.

refs: #320

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T10:34:34.774994Z bff763b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c2a2053d8a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Repositories/BaseRepository.php Outdated
use RonasIT\Support\Traits\EntityControlTrait;

/**
* @template TModel of Model

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Provide a default for the model template

Existing applications that extend BaseRepository without an @extends annotation will now get PHPStan/Larastan missingType.generics errors at stricter analysis levels, rather than transparently retaining Model as claimed. Declare the template with a Model default (and likewise default the two public trait templates) so repositories and direct trait consumers written before this change remain valid.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c619b68: the template is declared as @template TModel of Model = Model on BaseRepository, EntityControlTrait and SearchTrait. Checked with PHPStan at level 6: a repository without @extends and a class using the traits directly no longer report missingType.generics, and find() is still inferred as Model|null.

@vitgrams vitgrams self-assigned this Nov 14, 2026
…generic

Annotate getQuery() and the search query property as Query<TModel>, so
custom repository methods get the concrete model from the builder.

refs: https://app.clickup.com/t/PRD-3225

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3cea84be21

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +63 to +65
/**
* @return $this
*/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bind the model class argument to TModel

When a repository is declared as @extends BaseRepository<User> but its constructor accidentally calls setModel(Post::class), PHPStan accepts the mismatch because $modelClass is untyped, while the newly annotated methods are advertised as returning User even though they return Post. Add an @param class-string<TModel> $modelClass annotation so the generic contract is checked where the runtime model is selected.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 92435f7: setModel() is annotated with @param class-string<TModel> $modelClass. Checked with PHPStan at level 6: setModel(RelationModel::class) in a repository with @extends BaseRepository<TestModel> reports expects class-string<TestModel>, and a repository without @extends accepts any model class.

Repositories and trait consumers without @extends or @use annotations
no longer get missingType.generics errors on PHPStan level 6 and above.

refs: https://app.clickup.com/t/PRD-3225

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c619b68c01

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +22 to 23
/** @var Query<TModel> */
protected $query;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Type the public query accessor with TModel

For a repository declared as @extends BaseRepository<User>, this property is typed internally, but getSearchQuery() still declares an unparameterized Query; expressions such as searchQuery($filters)->getSearchQuery()->first() therefore lose User and degrade to the generic bound or mixed type. Add an @return Query<TModel> annotation to the public accessor so callers receive the concrete model type.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5de5631: getSearchQuery() is annotated with @return Query<TModel>. Checked with PHPStan at level 6: for a repository with @extends BaseRepository<TestModel>, searchQuery([])->getSearchQuery() is Builder<TestModel> and ->first() is TestModel|null; a repository without @extends still gets Model|null.

@vitgrams

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 92435f7acb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Traits/EntityControlTrait.php Outdated
* @property Model model
* @template TModel of Model = Model
*
* @property TModel $model

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Type the declared model property directly

When a generic repository implements a custom method that reads $this->model directly, PHPStan still sees the declared protected $model property as mixed: a class-level @property tag describes a virtual property and does not type an actual property declaration with the same name. Move this annotation to the declaration as @var TModel so this common repository extension point receives the concrete model type.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in bff763b, with one correction to the premise. For classes that extend BaseRepository the property was already typed: PHPStan resolved $this->model as the concrete model with @extends and as Model without it. It was mixed only for classes that use EntityControlTrait directly.

The annotation is now @var TModel on the property declaration. Moving it alone made $this->model an unresolved TModel in BaseRepository subclasses, because the duplicate @property TModel $model tag in SearchTrait took over, so that tag is removed as well; SearchTrait does not read the model.

Checked with PHPStan and Larastan at level 6: $this->model is the concrete model in a subclass with @extends and in a class with @use EntityControlTrait<...>, and Model in a subclass without @extends.

…neric

Move the annotation from the class-level @Property tag to the property
declaration, so the model is typed for classes that use the trait
directly as well. Drop the duplicate tag from SearchTrait, which does
not read the model.

refs: #320
@vitgrams
vitgrams requested a review from artengin November 23, 2026 11:22
@vitgrams vitgrams removed their assignment Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants