Declare strict_types in the global helper files

未關閉
#712 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
68/100
Issue 類型
重構
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
php

研究方向

Start with the 26 helper files listed in the issue and the helper tests that cover coercion-dependent inputs; for each affected helper, pin its current return value before making conversions explicit. Add strict_types declarations throughout src/*/Helpers and remove that directory's skip in rector.php. Run the helper tests and composer rector:check; done when all checks pass and the helpers retain their existing results.

由索引模型根據 Issue 內容生成。

描述

refactoring tech debt

Summary

Every class file in src declares declare(strict_types=1);, but most of the global helper files in src/*/Helpers do not: 26 of 30 lack it. Only db.php, debugger.php, transformer.php and view.php declare it. Add it to all of them, so the whole of src runs with the same type rules, and let Rector guard it.

Problem

  • Inconsistent type rules. A call made inside a helper file without strict_types silently converts scalars: a helper that passes '587' to an int parameter, or 1 to a string parameter, works there and throws a TypeError in a class file. The same code behaves differently depending on which file it is in.
  • Rector skips the helpers. Since Rector 2.9 removed DeclareStrictTypesRector (#710), rector.php uses SafeDeclareStrictTypesRector with a skip for src/*/Helpers/*, so nothing checks the helper files.

strict_types only affects calls made inside the file that declares it. An application calling config(), cache() or env() from its own code behaves the same either way.

Proposed change

  1. 13 helper files Rector finds type-safe (no scalar coercion inside): add declare(strict_types=1); as Rector would.
    App/Helpers/app.php, App/Helpers/uuid.php, Auth/Helpers/auth.php, Cache/Helpers/cache.php, Captcha/Helpers/captcha.php, Cron/Helpers/cron.php, HttpClient/Helpers/http_client.php, Lang/Helpers/lang.php, Logger/Helpers/logger.php, Mailer/Helpers/mailer.php, Router/Helpers/router.php, Session/Helpers/session.php, Storage/Helpers/fs.php
  2. 13 helper files Rector does not add it to: find each call that relies on a coercion, make the conversion explicit so the helper returns what it returns today, then add declare(strict_types=1);.
    App/Helpers/dir.php, App/Helpers/misc.php, Asset/Helpers/asset.php, Config/Helpers/config.php, Cookie/Helpers/cookie.php, Csrf/Helpers/csrf.php, Encryption/Helpers/encryption.php, Environment/Helpers/env.php, Environment/Helpers/server.php, Event/Helpers/event.php, Http/Helpers/http.php, Model/Helpers/model.php, Service/Helpers/service.php
  3. rector.php: remove the src/*/Helpers/* skip, so SafeDeclareStrictTypesRector covers the helpers too.

No public function changes its signature or result, so nothing breaks for applications.

Tests

  • Before each change in step 2, a test pins what the helper returns for the input that relied on the coercion, and passes before and after.
  • The existing helper tests pass unchanged.
  • composer rector:check passes without the skip.

Acceptance criteria

  • every file in src/*/Helpers declares declare(strict_types=1);
  • rector.php has no skip for the helpers
  • no helper changes what it returns for the inputs it accepts today
  • all checks and tests pass
主要語言
PHP
星號
44
分支
22
平均合併
2 小時 57 分鐘
30 天內合併 PR
52

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

quantum-php/framework 的其他 Issue

查看 quantum-php/framework 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。