侧边/发现者

PHP 代码片段查找工具

该包的官方仓库似乎已不存在,因此该包已被冻结。

v0.9.4 2021-12-30 04:45 UTC

README

phinder logo

Phinder:PHP 代码片段查找工具

Latest Stable Version

Phinder 是一个查找代码片段的工具。此工具的主要目的是加速您的代码审查过程,而不是静态错误检测。

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

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

如果您的项目代码遵循此规则,则可以在代码审查中进行检查。但如果您忘记检查呢?如果您的项目有几十条规则呢?您可能希望机器进行这样的底层检查。

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