許多 laravel 團隊都是用 repositories + services 這種架構

久了之後 這兩種類別通常會超過上千行

我正在研究一種類似 graphql 的架構 每個檔案都在 200 行以內

搭配 https://www.laravelactions.com/ 使用

隨時間增加的 是檔案數量 不是檔案大小


正在評估替客戶團隊導入

發現 phpstan 很適合做這件事

目前設計的 phpstan.neon 內容如下

includes:
    - vendor/larastan/larastan/extension.neon
    - vendor/spaze/phpstan-disallowed-calls/extension.neon
    - phpstan-baseline.neon

parameters:
    level: 0
    paths:
        - app/Http/Controllers

    disallowedMethodCalls:
        - method: Illuminate\Database\Eloquent\Model::save
          message: "❌ 禁止在 Controller 內直接使用 save(),請使用 Mutations!"
        - method: Illuminate\Database\Eloquent\Model::update
          message: "❌ 禁止在 Controller 內直接使用 update(),請使用 Mutations!"
        - method: Illuminate\Database\Eloquent\Model::delete
          message: "❌ 禁止在 Controller 內直接使用 delete(),請使用 Mutations!"

        - method: Illuminate\Database\Eloquent\Builder::where
          message: "❌ 禁止在 Controller 內直接使用 where(),請使用 Queries!"
        - method: Illuminate\Database\Eloquent\Builder::find
          message: "❌ 禁止在 Controller 內直接使用 find(),請使用 Queries!"
        - method: Illuminate\Database\Eloquent\Builder::first
          message: "❌ 禁止在 Controller 內直接使用 first(),請使用 Queries!"
        - method: Illuminate\Database\Eloquent\Builder::get
          message: "❌ 禁止在 Controller 內直接使用 get(),請使用 Queries!"

        - method: App\Services\*::*
          message: "❌ 禁止在 Controller 內使用 Service!請使用 Mutations!"
        - method: App\Repositories\*::*
          message: "❌ 禁止在 Controller 內使用 Repository!請使用 Queries!"

將 phpstan 安裝到 CICD 環境執行 就可以避免 junior developer 趕時間犯一些錯誤

專案導入初始階段 使用 baseline 功能 違規的部分 既往不咎 這樣導入就很順暢

目前處於我的理論階段 過幾個月再來分享實務經驗


每週二 YT 直播問答,DC 可 Call-in,一起討論技術、職涯發展。
YT 👇👇👇
https://www.youtube.com/@codelove_tw
DC 👇👇👇
https://discord.gg/fvCF2whZ9D

共有 0 則留言


每週二 YT 直播問答,DC 可 Call-in,一起討論技術、職涯發展。
YT 👇👇👇
https://www.youtube.com/@codelove_tw
DC 👇👇👇
https://discord.gg/fvCF2whZ9D
🏆 本月排行榜
🥇
站長阿川
📝10   💬6   ❤️6
449
🥈
我愛JS
📝1   💬5   ❤️4
94
🥉
AppleLily
📝1   💬4   ❤️1
55
#4
💬1  
5
#5
xxuan
💬1  
3
評分標準:發文×10 + 留言×3 + 獲讚×5 + 點讚×1 + 瀏覽數÷10
本數據每小時更新一次