如何在PHP中执行静态代码分析? [英] How can I perform static code analysis in PHP?

查看:70
本文介绍了如何在PHP中执行静态代码分析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有用于PHP源文件的静态分析工具?

Is there a static analysis tool for PHP source files?

二进制文件本身可以检查语法错误,但是我正在寻找功能更多的东西,例如:

The binary itself can check for syntax errors, but I'm looking for something that does more, like:

  • 未使用的变量分配
  • 未先初始化就分配给它们的数组
  • 以及可能的代码样式警告
  • ...

推荐答案

从命令行以lint模式运行 php 以验证语法而无需执行:

Run php in lint mode from the command line to validate syntax without execution:

php -l文件名

高级静态分析器包括:

  • php-sat - Requires http://strategoxt.org/
  • PHP_Depend
  • PHP_CodeSniffer
  • PHP Mess Detector
  • PHPStan
  • PHP-CS-Fixer
  • phan

较低级别的分析器包括:

Lower-level analyzers include:

  • PHP_Parser
  • token_get_all (primitive function)

运行时分析器由于PHP的动态特性而在某些方面更有用,包括:

Runtime analyzers, which are more useful for some things due to PHP's dynamic nature, include:

  • Xdebug has code coverage and function traces.
  • My PHP Tracer Tool uses a combined static/dynamic approach, building on Xdebug's function traces.

文档库 phpdoc Graphviz 呈现漂亮的继承图.

The documentation libraries phpdoc and Doxygen perform a kind of code analysis. Doxygen, for example, can be configured to render nice inheritance graphs with Graphviz.

另一个选项是 xhprof ,它与Xdebug类似,但较轻,使其适合使用用于生产服务器.该工具包括一个基于PHP的界面.

Another option is xhprof, which is similar to Xdebug, but lighter, making it suitable for production servers. The tool includes a PHP-based interface.

这篇关于如何在PHP中执行静态代码分析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆