The Problem
In Apivalk, request data is populated into bags. While you can access data using$request->body()->get('name') (which returns the Parameter), the bags also support magic getters like $request->body()->name that return the typed value or null. Without extra metadata, IDEs (like PhpStorm) won’t know that name exists or what type it is.
The Solution: Automated DocBlocks
TheDocBlockGenerator automates the creation of this metadata. It performs the following steps for every Request class in your project:
-
Discovery: Scans your API directories for classes extending
AbstractApivalkController. -
Extraction: Reads the Request class from the controller’s
__invoke()parameter, itsgetDocumentation(), and the controller’sgetRoute()(path properties, sorting, filtering, pagination). -
Shape Generation: Creates “Shape” interfaces named after the request class (e.g.,
GetUserRequestBodyShape,GetUserRequestSortingShape,GetUserRequestFilteringShape) in aShape/subdirectory next to the request class. They carry@property-readtags for your documented properties and the route-defined sorting/filtering. Filtering gets one extra interface per field (GetUserRequestFilterStatusShape), listing only the operators that field declares, so$request->filtering()->status->offersequalbut notlikewhen the route only allowsOperatorEnum::Eq. The filtering shape names those field shapes and carries theFilterBagAPI itself, which is why its@methodentry has noFilterBag|union: a union would resolve every field throughFilterBag::__get()as well and reportFilterInterface|nullnext to the field shape. -
Rewrite: Replaces the class docblock of the Request class file with
@methodtags pointing to these Shape interfaces. Anything you wrote in that docblock is lost, so keep notes elsewhere.
class declaration. A request declared final or readonly is not found: the generator prints an error for that controller and moves on. It reports every failing controller this way on standard output and never throws for it, so check the output (or the resulting git diff) in CI.
Example Result
Before running the generator, your Request class might look like this:DocBlockGenerator for a route with page pagination, it is automatically updated to:
paginator() line names the paginator class of the route’s pagination type and is left out when the route has none. A file() line is added when the request declares file properties.
Now, in your controller, you can type $request->body()-> and your IDE will suggest all defined properties with their correct types.
How to Run
You can trigger the generation process from your bootstrap or a CLI tool:Components
DocBlockGenerator: The main entry point that iterates over classes.DocBlockRequestGenerator: Converts anAbstractApivalkRequestinto aDocBlockRequestdata object.DocBlockRequest: Holds the metadata for the shapes (Body, Query, Path, Sorting, Filtering, File).DocBlockShape: Generates the PHP code for the Shape interfaces.DocBlockResourceGenerator/DocBlockResource: Generates@propertyannotations forAbstractResourcesubclasses so you get IDE autocompletion on$resource->name,$resource->status, etc.DocBlockResourceRequestGenerator/DocBlockResourceRequest: For each resource controller, generates a typed per-resource request (e.g.AnimalViewRequest,AnimalListRequest) with@methodannotations forpath(),sorting(),filtering()andpaginator()wired to shape interfaces derived from the route and the resource’savailableSortings()/availableFilters().
Resource DocBlock Generation
WhenDocBlockGenerator encounters an AbstractResourceController subclass, it produces two extra artifacts:
- Resource
@propertydocblock — written once per resource class so that$animal->name,$animal->type, etc. autocomplete in your IDE. - Per-resource request — for each resource controller, all five modes, a typed request class in a
Request/folder next to the controller, with its shapes inRequest/Shape/. Controllers inHttp/Controller/Animal/getHttp/Controller/Animal/Request/AnimalListRequest.phpand so on. If the file already exists, only the docblock is updated — your class body is preserved. If the controller’s__invoke()already type-hints a subclass ofResourceRequest, that class is updated where it lives instead.
$request->sorting()->status, $request->filtering()->createdAt, and $request->paginator() without writing the boilerplate yourself.