struggle-for-php / sfp-deptrac-builtin
Deptrac 扩展,用于强制应用遵守框架规则。
    0.2.1
    2024-06-09 02:15 UTC
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- qossmic/deptrac-shim: ^1.0.2
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8
- laminas/laminas-coding-standard: ^2.4
- nikic/php-parser: ^4.13.2
- phpunit/phpunit: ^9.6
- psalm/plugin-phpunit: ^0.18.4
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^5
This package is auto-updated.
Last update: 2024-09-09 02:47:44 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()?- 答:也许吧。
 
灵感来源
此扩展是在受到以下演讲的启发下开发的。