Skip to content

feat: type route entity through repository, service and BaseRequest - #317

Open
vitgrams wants to merge 5 commits into
masterfrom
vitgrams/resource-from-routes-find-twice
Open

vitgrams wants to merge 5 commits into
masterfrom
vitgrams/resource-from-routes-find-twice

Conversation

@vitgrams

Copy link
Copy Markdown
Contributor

What changes

  • Model generics. EntityControlTrait, BaseRepository and EntityService declare @template TModel of Model = Model (PHPDoc only, no runtime signature changes). A project binds the model once per class with @extends BaseRepository<Event> / @extends EntityService<Event>. find(), first(), findBy(), last(), update() return TModel|null; create(), firstOrCreate(), updateOrCreate() return TModel. Collection-returning methods are untouched.
  • Service chains. EntityService carries the type through @mixin BaseRepository<TModel> and declares @method $this for with(), withCount(), force(), withTrashed(), onlyTrashed(), so $service->with(...)->find() resolves to the bound model and the chain stays typed as the service.
  • BaseRequest::loadRouteEntity(). A new protected method with a method-level template, meant to be called from init(): resolves the service, applies relations, finds the model by a route parameter (id by default) exactly once, and throws NotFoundHttpException with the :entity does not exist. message (translatable through the project JSON file) when the parameter is missing or no row matches. The result is typed as the bound model without null.
  • getEntityName() is now public in EntityControlTrait, so the request gets the default entity name through the service proxy.
  • PHPStan added as a dev dependency, with phpstan.neon (level 8) scoped to the type fixtures only.
  • Tests: assertType fixtures for the repository, the service and the request; behavior tests in BaseRequestRouteEntityTest.
  • Docs: documentation/services.md (binding and the new request -> controller -> Action flow), readme.md, and a new Model generics section in documentation/migration.md.

Why

Requests used to look the route entity up in init()/beforeAuthorization() into a nullable property, and the Action searched for it again by id. Now the entity is loaded once, a missing resource produces 404 before authorization runs, and the model is non-nullable for PHPStan without @var at the call site.

How it was verified

  • vendor/bin/phpunit — OK (501 tests, 1868 assertions)
  • vendor/bin/pint --test — PASS 136 files
  • vendor/bin/phpstan analyse -c phpstan.neon (level 8) — No errors
  • Template default check: without = Model PHPStan reports extends generic class ... but does not specify its types: TModel on classes without @extends; with the default it is clean.
  • Tried on a consumer project under larastan (Laravel 13): one endpoint moved to loadRouteEntity(), its 29 tests pass without fixture changes, no missing generics errors on classes without @extends.

Known limitations / notes for consumers

  • Projects with a PHPStan baseline: entries mentioning Model|null stop matching (the type is more precise now), regenerate the baseline.
  • A relation loaded in the request for authorize() stays on the model and reaches the resource through whenLoaded(); use withoutRelations() if needed.
  • A non-numeric route parameter value with an integer key on PostgreSQL causes a query layer error (as before with find($this->route('id'))); constrain the route with whereNumber().
  • PhpStorm does not infer TModel from class-string<EntityService<TModel>> through the child's @extends and shows "Incompatible types: Expected ..., Model provided"; PHPStan/larastan infer the type correctly.
  • getEntityName() became public: a protected override in a project causes a fatal error, make the override public.

https://claude.ai/code/session_01SSJ49hkfqDc6bsDwk83JBA

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 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-15T09:54:33.679082Z d3eb92b 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.

@vitgrams vitgrams self-assigned this Nov 14, 2026

@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: c08ad6da30

ℹ️ 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 +14 to +18
* @method $this with(array|string $relations)
* @method $this withCount(array|string $relations)
* @method $this force($value = true)
* @method $this withTrashed($enable = true)
* @method $this onlyTrashed($enable = true)

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 Preserve service type for search chains

When a typed service uses common fluent SearchTrait calls such as $service->searchQuery(...)->filterBy(...), these declarations do not cover them, so PHPStan falls back to @mixin BaseRepository<TModel> and infers the repository rather than the actual service. At runtime __call() replaces a repository self-return with the service, so the incorrect type prevents chaining service-specific methods; add $this declarations for the remaining self-returning search/filter/order methods and cover one such chain in the inference tests.

Useful? React with 👍 / 👎.

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.

1 participant