Automatic API documentation
and TypeScript types for PHP projects

PHP autodoc analyzes your actual PHP code to generate accurate API documentation and TypeScript types, keeping your frontend and backend in sync without manual work.

Get Started
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rules\Enum;

class OrderController
{
    public function update(Request $request, Order $order): JsonResponse
    {
        $validated = $request->validate([
            'status' => ['required', new Enum(OrderStatus::class)],
        ]);

        if ($order->status === OrderStatus::Completed) {
            return response()->json([
                'status_updated' => false,
                'message' => 'Completed orders can’t be changed',
                'order' => array_filter($order->toArray()),
            ], 400);
        }

        $order->update([
            'status' => $validated['status'],
        ]);

        return response()->json([
            'status_updated' => true,
            'products' => $order->products->map(fn ($product) => [
                'id' => $product->id,
                'name' => $product->name,
                'updated_at' => $product->updated_at,
            ]),
        ]);
    }
}

PATCH/api/orders/{order}

Parameters

Path

orderinteger
required

Request Body

application/json
statusinteger

1234
required

Responses

Bodyapplication/json
status_updatedboolean
required
productsarray<object>
required
idinteger
required
namestring
required
updated_atstring<date-time> or null
required
Bodyapplication/json
status_updatedboolean
required
messagestring
Completed orders can’t be changed
required
orderobject
required
idinteger
required
statusinteger

1234
required
created_atstring<date-time>
updated_atstring<date-time>

Introduction

PHP API Documentation and TypeScript Type Generator

PHP autodoc automatically generates up-to-date OpenAPI 3.1.0 documentation and TypeScript types directly from your PHP code. PHPDoc annotations are supported if present, but completely optional.

For Laravel projects, autodoc-laravel offers seamless integration with routes, request validation, database models, API resources, and more.

TypeScript export

Define your types once in PHP and let PHP autodoc generate the matching TypeScript types automatically to keep your frontend and backend types in sync.

PHP autodoc supports exporting API request/response structures, classes, enums, PHPStan type aliases and custom PHPDoc type expressions as TypeScript types. PHP autodoc-laravel extends this functionality by offering support for Laravel models and other Laravel-specific structures that need custom handling.

See TypeScript section for more info.

How it works

PHP autodoc uses PHP parser and PHPDoc parser to analyze your code and convert it into AutoDoc\DataTypes\Type objects. These objects can be used in your custom PHP autodoc extensions and are utilized in generating OpenAPI 3.1.0 schemas and TypeScript types. If you are using a static analysis tool like PHPStan, you will find that autodoc works well without any changes to your codebase.

Tips to improve the generated documentation
  1. Enable debug mode in autodoc configuration to show any errors that may occur during code analysis. By default, debug mode is disabled and errors are silently ignored.
  2. Increase max_depth setting in autodoc configuration to allow for deeper analysis of your codebase.
  3. Use a tool like PHPStan to analyze your codebase and fix any issues that may occur. This will improve the quality of your code and help autodoc generate more accurate documentation.
  4. Create custom extensions to handle domain-specific structures that PHP autodoc may not recognize by default.