sethphat/eloquent-docs

为您的 Eloquent 模型生成 PHPDoc 范围(列、访问器等)

2.0.1 2024-05-13 03:13 UTC

README

codecov Latest Stable Version Total Downloads Build and test [MYSQL] Build and test [SQLite] Build and test [PostgreSQL] Build on specific PHP & Laravel versions

快速生成 Eloquent 模型的 phpDoc。让你的 Eloquent 与 IDE(例如 PHPStorm)更友好。

维护模型的 phpDoc 不再痛苦,而应该是

  • 有趣的
  • 快速的
  • 可靠的

欢迎来到 Eloquent phpDoc 生成器,它将帮助您实现以上三点 🎉

Eloquent phpDoc 生成器将生成什么?

  • 表名
  • 表列(包括正确的类型 - 包括铸件类型 😎)
  • 模型关系
  • 模型属性/访问器

可用性/要求

  • v2.x:Laravel 11,PHP 8.2+。
  • v1.x:Laravel 9/10,PHP 8.0,8.1。

安装

安装为开发依赖项就足够了,因为您只会在 local/development 环境中使用此命令。

composer require sethphat/eloquent-docs --dev

Laravel 自动发现将自动为您完成工作。

使用命令

php artisan eloquent:phpdoc "App\Models\User" # view only
php artisan eloquent:phpdoc "App\Models\User" --write # view & write to file
php artisan eloquent:phpdoc "App\Models\User" --short-class # new option - use short class instead of full namespace path

# from v1.2.0
php artisan eloquent:bulk-phpdoc "app/Models/*.php" # bulk generation (force write mode)

结果

====== Start PHPDOC scope of App\Models\User
/**
* Table: users
*
* === Columns ===
* @property int $id
* @property string $name
* @property string $email
* @property \Carbon\Carbon|null|null $email_verified_at
* @property string $password
* @property string|null $remember_token
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
*
* === Relationships ===
* @property-read \App\Models\Emails[]|\Illuminate\Database\Eloquent\Collection|null $emails
* @property-read \App\Models\UserDetails|null $userDetail
*
* === Accessors/Attributes ===
* @property-read string $full_name
* @property-read string $is_admin
* @property-read string $user_type
* @property-read int $total_salary
* @property-read mixed $levels
* @property-read mixed $first_name
* @property-read mixed $last_name
*/
====== End PHPDOC scope of App\Models\User
Wrote phpDoc scope to /<my-path>/app/Models/User.php
Thank you for using EloquentDocs!

最佳实践

  • 在您的模型中使用 $casts,以帮助 EloquentPhpDoc 为您生成更好的类型(数组、日期、日期时间等)
  • 对于 get*Attribute 访问器,始终声明返回类型

注意:对于 Eloquent 新的 Attribute 类,通过闭包访问数据,这使得获取返回类型变得不可能。在这种情况下,EloquentDocs 总是返回 mixed

版本日志

  • v1.0.0
    • 第一个版本
    • 一次查看和更新一个模型的 phpDoc
  • v1.1.0
    • --short-class
    • 修复了一些问题
  • v1.1.1
    • 修复了生成具有 enum 列的表的错误
  • v1.1.2
    • 修复了首次安装库导致 Laravel 发现错误的错误。
  • v1.1.3 & v1.1.4
    • 改进了缩进
    • 支持 Laravel 10
    • 弃用 Laravel 8
    • 弃用 PHP 8.0
  • v1.2.0
    • 新增命令,可以从给定的模型路径批量生成。
      • php artisan eloquent:bulk-phpdoc "app/Models/*.php"
    • 修复了访问器/属性被生成为 snake_case 的问题。应该是 camelCase。
  • v2.0.0
    • 支持 Laravel 11 + PHP 8.2/8.3
    • DBAL 作为必需依赖项
    • 更好的日期/日期时间解析(Carbon & Carbon immutable)
    • 更好的布尔解析

为库做出贡献

请随意分支此库并向这里发送 PR。

注意:所有贡献都需要遵循 PSR-12 并覆盖所有单元测试。

许可协议

MIT 许可证

(赞助内容) 介绍 RenderPDF.io - 在几秒钟内渲染您的 PDF

RenderPDF.io

RenderPDF.io 是一个将 PDF 作为服务的 Render-PDF,它提供对渲染现代 PDF 文件的开发人员友好的 API。

您只需获取 API 密钥并向 RenderPDF.io 发送 API 调用即可 🚀,无需设置基础设施,无需开销和繁琐的集成 🔋。

今天获取您的免费 API 密钥:在 RenderPDF.io 上注册