此包已被 废弃 且不再维护。作者建议使用 sider/phinder 包代替。

PHP代码片段查找器


README

phinder logo

Phinder:PHP代码片段查找器

Latest Stable Version

Phinder是一个用于查找代码片段的工具。这个工具主要目的是加快你的代码审查过程,而不是静态错误检测。

假设你的项目有以下本地规则

  • 在调用 in_array 时显式指定第三个参数,以避免意外的比较结果。

如果你的项目代码遵循此规则,你可以在代码审查中检查它。但是,如果你忘记了检查呢?如果你的项目有几十条规则呢?你可能希望机器做这样的低级别检查。

Phinder是一个用于自动检查此类低级别问题的命令行工具。通过将以下yml保存为phinder.yml并在你的终端中运行phinder,Phinder会为你找到违规行为。

- id: in_array_without_3rd_param
  pattern: in_array(_, _)
  message: Specify the 3rd parameter explicitly when calling `in_array` to avoid unexpected comparison results.

安装

Phinder需要PHP >= 7.3。你可以使用Composer进行安装。

composer require --dev sider/phinder
vendor/bin/phinder -v

快速入门

第一步,你可以运行phinder init命令来创建一个示例phinder.yml

$ phinder init
`phinder.yml` has been created successfully

$ cat phinder.yaml
# Feel free to add your own project rules to this YAML file.
# The following example describes the rule syntax.
# See the documentation for more details: https://github.com/sider/phinder/tree/master/doc

- # The rule identifier. It must be unique in the YAML file.
  id: sample.var_dump
  # Pattern syntax. The `...` pattern matches variable length arguments or array pairs.
  # As a result, this pattern matches `var_dump` function call with any arguments.
  pattern: var_dump(...)
  # The message to display when code pieces are matched with the pattern.
  message: Do not use var_dump.
  # Exceptions that can ignore this violation.
  justification: Allowed when debugging

- id: sample.in_array_without_3rd_param
  # `_` pattern mattches any single expression.
  # This means the pattern always matches `in_array` function call with any two arguments.
  pattern: in_array(_, _)
  message: Specify 3rd parameter explicitly when calling in_array to avoid unexpected comparison results.
  # You can test whether your pattern works as expected with `phinder test`.
  test:
    # Code pieces that will match your pattern.
    # This means the following codes are bad as you expected.
    fail:
      - in_array(1, $arr)
      - in_array(2, $arr)
    # Code pieces that will NOT match your pattern.
    # This means the following codes are good as you expected.
    pass:
      - in_array(3, $arr, true)
      - in_array(4, $arr, false)

然后你可以运行phinder命令来对你的代码进行phind模式匹配。

$ phinder find

在了解了一个模式是如何匹配你的代码之后,让我们为你的项目添加更多有用的规则吧!

文档

贡献

欢迎在GitHub上提交错误报告、功能请求和拉取请求,网址为https://github.com/sider/phinder