在Phinder中进行PHP目录检查

Phinder指的是什么?

这是一个可从Pull Request的源代码差异中找到特定字符串的Lint工具。
通过增加自定义规则,您可以自动化发现过时的编写方式和小错误。

引入法

只需提供一个选项进行汉语的同义改写:
①在具备php7.0以上版本并可使用Composer的环境中执行以下命令:
$ composer require –dev sider/phinder
$ vendor/bin/phinder -v

以「phinder.yml」檔名記錄規則。

使用“模式”和“消息”的组合来描述规则。

– id: in_array_without_3rd_param
pattern: in_array(_, _)
message: 调用`in_array`时请明确指定第三个参数,以避免意外的比较结果。

phinder.png

Phinder 网页正文翻译

为了那些阅读英文有困难的人,我为你总结翻译了这段原文(https://packagist.org/packages/tomokinakamaru/phinder)。

Phinder是一种用于寻找PHP代码片段的工具。这个工具主要旨在加速代码审查流程,而不是进行静态错误检查。

假设在项目中有以下本地规则。

为了避免意外的比较结果,调用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.

使用Composer可以安装Phinder,并确保您的PHP版本大于或等于7.0。
composer require –dev sider/phinder
vendor/bin/phinder -v

如果使用Docker的话

使用docker run命令,参数为–rm -t -v $(pwd):/workdir,镜像为sider/phinder。

先从输入”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`命令,以便将模式应用于您的代码。

¥ 找寻器

了解了模式如何与代码相匹配后,可以添加一些对实际项目有用的规则。可能性是无限的!

以下是 “ドキュメント・サンプルルールとその説明” 的中文表达:

https://github.com/tomokinakamaru/phinder/blob/HEAD/doc/rule.md
– 文件
– 示例规则及其说明

・模式语法
https://github.com/tomokinakamaru/phinder/blob/HEAD/doc/pattern-syntax.md

・命令行选项
https://github.com/tomokinakamaru/phinder/blob/HEAD/doc/command-line-options.md

我们欢迎您在GitHub(https://github.com/sider/phinder)寄送您的Bug报告、功能请求和Pull请求。

感谢辞

Phinder的灵感来自Querly、ast-grep和ASTsearch。实现主要依赖于PHP-Parser和kmyacc-forked。

最后

我們試驗的方法是在本地執行,但與Sider(https://sider.review/ja)結合使用,通常會在GUI上進行確認操作。

参考:
以下是一些资源链接,提供关于Phinder的更多信息。

– [博客文章](https://blog-ja.sideci.com/entry/2019/01/16/144242)
– [Phinder的包管理器](https://packagist.org/packages/tomokinakamaru/phinder)
– [技术博客文章](https://yourmystar-engineer.hatenablog.jp/entry/2018/10/02/121334)

bannerAds