Skip to main content

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

The DocBlockGenerator automates the creation of this metadata. It performs the following steps for every Request class in your project:
  1. Discovery: Scans your API directories for classes extending AbstractApivalkController.
  2. Extraction: Reads the Request class from the controller’s __invoke() parameter, its getDocumentation(), and the controller’s getRoute() (path properties, sorting, filtering, pagination).
  3. Shape Generation: Creates “Shape” interfaces named after the request class (e.g., GetUserRequestBodyShape, GetUserRequestSortingShape, GetUserRequestFilteringShape) in a Shape/ subdirectory next to the request class. They carry @property-read tags 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-> offers equal but not like when the route only allows OperatorEnum::Eq. The filtering shape names those field shapes and carries the FilterBag API itself, which is why its @method entry has no FilterBag| union: a union would resolve every field through FilterBag::__get() as well and report FilterInterface|null next to the field shape.
  4. Rewrite: Replaces the class docblock of the Request class file with @method tags pointing to these Shape interfaces. Anything you wrote in that docblock is lost, so keep notes elsewhere.
The rewrite looks for a plain 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:
After running DocBlockGenerator for a route with page pagination, it is automatically updated to:
The 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 an AbstractApivalkRequest into a DocBlockRequest data 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 @property annotations for AbstractResource subclasses 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 @method annotations for path(), sorting(), filtering() and paginator() wired to shape interfaces derived from the route and the resource’s availableSortings() / availableFilters().

Resource DocBlock Generation

When DocBlockGenerator encounters an AbstractResourceController subclass, it produces two extra artifacts:
  1. Resource @property docblock — written once per resource class so that $animal->name, $animal->type, etc. autocomplete in your IDE.
  2. 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 in Request/Shape/. Controllers in Http/Controller/Animal/ get Http/Controller/Animal/Request/AnimalListRequest.php and 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 of ResourceRequest, that class is updated where it lives instead.
Every resource request gets a path, sorting and filtering shape, the last two stay empty outside list mode. A list request gets one more shape per filter field. This gives you full IDE autocompletion on $request->sorting()->status, $request->filtering()->createdAt, and $request->paginator() without writing the boilerplate yourself.