定制 algirdasc/laravel-openapi-tools 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

algirdasc/laravel-openapi-tools

Composer 安装命令:

composer require algirdasc/laravel-openapi-tools

包简介

README 文档

README

This toolkit provides rules for PHPStan and utilities for working with OpenAPI specifications in Laravel framework.

It should help you maintain documentation of your codebase more easily and lower data duplication as much as possible.

Toolkit uses PHPStan to statically check code for errors, leading to Swagger/OpenAPI specification errors.

!! Only OpenApi attributes with named arguments is supported and checked !!

Requirements

  • PHP 8.3 or higher
  • PHPStan
  • Laravel 11.x or higher
  • Composer

Installation

Install the package via composer:

composer require algirdasc/laravel-openapi-tools

Add rules to phpstan.neon config:

includes:
    - vendor/algirdasc/laravel-openapi-tools/extension.neon

Run phpstan to analyze your project:

vendor/bin/phpstan

Helper classes

Toolkit includes helper class to ease and minimize duplication while defining API documentation in Laravel framework. Usage of them is completely optional, although is recomended.

Rules

Quick overview of rules provided in toolkit and their functions. List of incomplete and suspect for changes.

Schemas - OA\Schema

  • OpenApiTools\PHPStan\Rules\OpenApi\Schema\PropertiesRule
    • Validates property type
    • Validates property format
    • Validates property case
    • Validates property date type & format
    • Validated property ref class to have schema
  • OpenApiTools\PHPStan\Rules\OpenApi\Schema\RequiredPropertiesRule
    • Validates if required properties defined in properties list
  • OpenApiTools\PHPStan\Rules\OpenApi\Schema\SchemaNameRule
    • Validates naming convention

Operation - OA\Get, OA\Post,OA\Put,OA\Patch,OA\Delete

  • OpenApiTools\PHPStan\Rules\OpenApi\Operation\ControllerInvokeMethodRule
    • Validates __invoke method has correct schema scope
  • OpenApiTools\PHPStan\Rules\OpenApi\Operation\ControllerMethodParametersRule
    • Validates if operation attributes has methods
    • Validates method parameter types
    • Validates method parameters vs. operation path parameters
  • OpenApiTools\PHPStan\Rules\OpenApi\Operation\ControllerMethodsRule
    • Validates controller method count if __invoke is used
  • OpenApiTools\PHPStan\Rules\OpenApi\Operation\DescriptionRule
    • Validates description length
  • OpenApiTools\PHPStan\Rules\OpenApi\Operation\PathRule
    • Validates path leading & trailing slash
    • Validates whether path parameters defined in operation schema parameters
  • OpenApiTools\PHPStan\Rules\OpenApi\Operation\RequestBodyReferenceRule
    • Validates whether requestBody is set when FormRequest instance provided in method parameters
  • OpenApiTools\PHPStan\Rules\OpenApi\Operation\ResponsesRule
    • Validates if Success response is provided
    • Validates if Error response is provided
    • Validates if Authorization response is provided
    • Validates if Unprocessable response is provided when FormRequest instance provided in method parameters
  • OpenApiTools\PHPStan\Rules\OpenApi\Operation\SummaryRule
    • Validates summary optimal length
  • OpenApiTools\PHPStan\Rules\OpenApi\Operation\TagCountRule
    • Validates tags count

Form Request - Illuminate\Foundation\Http\FormRequest

  • OpenApiTools\PHPStan\Rules\Laravel\FormRequest\EnumRule
    • Validates whether enum parameter is set in OA\Property, depending on validation rules
  • OpenApiTools\PHPStan\Rules\Laravel\FormRequest\MissingSchemaPropertiesRule
    • Validates whether properties set
  • OpenApiTools\PHPStan\Rules\Laravel\FormRequest\MissingSchemaRule
    • Validates whether OA\Schema attribute is set
  • OpenApiTools\PHPStan\Rules\Laravel\FormRequest\NullableRule
    • Validates whether nullable parameter is set in OA\Property, depending on validation rules
  • OpenApiTools\PHPStan\Rules\Laravel\FormRequest\RequiredRule
    • Validates whether required parameter is set in OA\Property, depending on validation rules

Json Resource - Illuminate\Http\Resources\Json\JsonResource

  • OpenApiTools\PHPStan\Rules\Laravel\Resource\BooleanPropertyRule
    • Validates boolean property to match isset/haser naming convetion - is_something, has_something
  • OpenApiTools\PHPStan\Rules\Laravel\Resource\MissingReturnPropertyRule
    • Validates whether OA\Schema property returned in toArray() method (plain array return only)
  • OpenApiTools\PHPStan\Rules\Laravel\Resource\MissingSchemaPropertiesRule
    • Validates whether OA\Schema contains properties parameter (plain array return only)
  • OpenApiTools\PHPStan\Rules\Laravel\Resource\MissingSchemaPropertyRule
    • Validates whether returned property is in OA\Schema properties (plain array return only)

Customizations

Ignoring rules

You can ignore a specific rule one time, all you need to do is to add a doc comment where this error happens. Example how to ignore openApiTools.missingRequestSchemaAttribute in a specific file:

<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @phpstan-ignore openApiTools.missingJsonResourceSchemaAttribute
 */
class MyResource extends JsonResource
{
    public function toArray(mixed $request): array
    {
        return [
            'some_property' => $this->resource->some_property,
        ]);
    }
}
...
#[OA\Schema(
    schema: 'Resources.Books.BookResource',
    properties: [
        /** @phpstan-ignore openApiTools.booleanInJsonResourceMustStartWithIs */
        new OA\Property('truncated', type: 'boolean', example: true),
    ],
)]
class BookResource extends JsonResource
...

If you want to ignore rules to specific files or file pattern, add ignoreErrors to your phpstan.neon. Example, how to ignore openApiTools.missingRequestSchemaAttribute errors:

    ignoreErrors:
        -
          identifier: openApiTools.missingRequestSchemaAttribute
          paths:
            - app/Path/To/Directory/*

More on error ignoring - PHPStan documentation

Schema naming rule override

By default, schema names should be named by namespaces separated by dot, for example Laravel resource App\Http\Resources\SomeController\SomeResource should be named Resources.SomeController.SomeResource, but if you want to change to your customized rule, you can do so by specifying class, implementing SchemaNameGeneratorInterface in your phpstan.neon. For example:

...
services:
  schemaNameGenerator:
    class: OpenApiTools\PHPStan\Generators\LaravelSchemaNameGenerator
...

Property naming rule override

By default, property names should be named using snake_case naming convention, but if you want to change to camelCase or your customized rule, you can do so by specifying class, implementing PropertyNameGeneratorInterface in your phpstan.neon. For example:

...
services:
  propertyNameGenerator:
    class: OpenApiTools\PHPStan\Generators\CamelCasePropertyNameGenerator
...

algirdasc/laravel-openapi-tools 适用场景与选型建议

algirdasc/laravel-openapi-tools 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 algirdasc/laravel-openapi-tools 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 algirdasc/laravel-openapi-tools 我们能提供哪些服务?
定制开发 / 二次开发

基于 algirdasc/laravel-openapi-tools 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2025-05-11