struggle-for-php/sfp-deptrac-builtin

Deptrac 扩展,用于强制应用遵守框架规则。

0.2.1 2024-06-09 02:15 UTC

README

PHP Web 应用程序在框架中不应使用如 header() 这样的网络功能。但这种规则是 隐式 的!此扩展试图查找违反此类规则的违规行为。

示例

  • examples/src/Action/UserShowAction.php 包含
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        \header('Location: http://www.example.com/');
    }
$ cd examples/
$ ./vendor/bin/deptrac analyse
 107/107 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ----------- ----------------------------------------------------------------------------------
  Reason      Action
 ----------- ----------------------------------------------------------------------------------
  Violation   Foo\Action\UserShowAction must not depend on header() (Sending Raw HTTP)
              /home/sasezaki/dev/sfp-deptrac-builtin/examples/src/Action/UserShowAction.php:20
 ----------- ----------------------------------------------------------------------------------


 -------------------- -----
  Report
 -------------------- -----
  Violations           1
  Skipped violations   0
  Uncovered            24
  Allowed              48
  Warnings             0
  Errors               0
 -------------------- -----

问答

  • 问:为什么不使用 PHPCS 的 forbiddenFunctionNames

    • 答:在我看来,forbiddenFunctionNames 只应用于危险函数(自身)或别名函数。在应用程序中禁止使用 header() 的原因是层次问题。
  • 问:当使用 'Location: ... 调用 header() 时,是否应该在同一作用域中调用 exit()

    • 答:也许吧。

灵感来源

此扩展是在受到以下演讲的启发下开发的。

https://speakerdeck.com/asumikam/phpcon-2022