Tooling
Static analysis
Type the model of a resource for PHPStan and your IDE.
Declare the model of a resource with @extends JsonApiResource<User>: PHPStan and the IDEs then know it everywhere
the resource gives it.
app/Http/Resources/UserResource.php
/**
* @extends JsonApiResource<User>
*/
final class UserResource extends JsonApiResource
{
public function toAttributes(Request $request): iterable
{
return [
// $this->resource is a User; the descriptor is a StringSchema
'name' => $this->string(),
// the closure of a helper receives the User
'email' => $this->string(fn(User $user) => $user->email)
->when(fn(User $user) => $user->isAdmin()),
];
}
}
The hooks need no @return: they inherit that of the parent.
A value type of your own called as
$this->money() needs a @method MoneySchema money(...) on the resource.
Retiring revisions
With API revisions, Larastan is also the tool that finds every declaration left by a retired revision.