扩展 phpcodesniffer 以根据错误代码过滤报告

发布于 2024-11-16 23:22:12 字数 1011 浏览 5 评论 0原文

我正在尝试扩展 PHPCodeSniffer。我想要实现的是使用错误代码过滤报告。

为了解释这一点,可以说我有一条错误消息,例如“错误代码:630,函数不兼容”

当我从命令行运行 PHPCS 时,我应该能够传递参数“错误代码”,以便根据它。(仅显示错误代码的结果,例如 630)

例如

 $ phpcs --standard=mystanderd /path/to/code/myfile.php --errorcode=603

,输出将是

FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR   | 603 | function is  not compatible      
 20 | ERROR   | 603 | function is  not compatible      
 51 | ERROR   | 603 | function is  not compatible      
 88 | ERROR   | 603 | function is  not compatible      
--------------------------------------------------------------------------------

实现它的最佳方法是什么?据我所知,我们只能根据严重性进行过滤,因为它有内置的支持。

我想避免修改 PHPCodeSniffer 的核心。我想做的是编写一个包装器脚本,它将接受来自 CLI 的参数并执行 PHPCS 捕获 o/p 并在抛出到控制台之前对其进行操作。但是,我认为这不是最好的解决方案。

I am trying to extend PHPCodeSniffer.What I am trying to achive is to filter the report using error codes.

To explain this lets say I have an error message like "error code : 630 , function is not compatible"

When I run PHPCS from command line , I shoudl be able to pass an argument "error code" so that the report is filtered based on it.(only show result for error code say 630)

e.g.

 $ phpcs --standard=mystanderd /path/to/code/myfile.php --errorcode=603

and output will be

FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR   | 603 | function is  not compatible      
 20 | ERROR   | 603 | function is  not compatible      
 51 | ERROR   | 603 | function is  not compatible      
 88 | ERROR   | 603 | function is  not compatible      
--------------------------------------------------------------------------------

what is the best way to achive it ? as far as what I have understood we can filter only based on seviority as it have inbuilt support.

I would like to avoid modifying the core of PHPCodeSniffer. What I am thinking to do is to write a wrapper script which will accept the argument from CLI and execute PHPCS the capture the o/p and manipulate it before throwing out to the console.However, I don't think it is a best solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我想到了一个利用 grep 和 wc 的 bash 脚本。

a bash script utilising grep and wc comes to mind.

筑梦 2024-11-23 23:22:12

您还可以使用这样的 PHP 脚本(假设这称为 my_wrapper.php):

<?php

$legal_codes = array(
    '603' => true
);

$f = fopen('php://stdin', 'r');
while ($line = fgets($f)) {
    if (preg_match("/^\s*(\d+)\s*\|\s*([A-Z]+)\s*\|\s*(\d+)\s*\|\s*(.*)/", $line, $match)) {
        $code = trim($match[3]);
        if (!isset($legal_codes[$code])) {
            continue;
        }
    }
    echo $line;
}

?>

当这样调用时:

php my_wrapper.php < cs_out.txt

使用这样的 cs_out.txt:

FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR   | 601 | function is  not compatible
 20 | ERROR   | 602 | function is  not compatible
 51 | ERROR   | 603 | function is  not compatible
 88 | ERROR   | 604 | function is  not compatible
--------------------------------------------------------------------------------

生成如下输出:

FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
 51 | ERROR   | 603 | function is  not compatible      
--------------------------------------------------------------------------------

Making the keys of the $legal_codes array可通过命令行指定my_wrapper.php 的参数留给读者作为练习。

You could also use a PHP script like this (let's say this is called my_wrapper.php):

<?php

$legal_codes = array(
    '603' => true
);

$f = fopen('php://stdin', 'r');
while ($line = fgets($f)) {
    if (preg_match("/^\s*(\d+)\s*\|\s*([A-Z]+)\s*\|\s*(\d+)\s*\|\s*(.*)/", $line, $match)) {
        $code = trim($match[3]);
        if (!isset($legal_codes[$code])) {
            continue;
        }
    }
    echo $line;
}

?>

Which when called like this:

php my_wrapper.php < cs_out.txt

With cs_out.txt like this:

FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR   | 601 | function is  not compatible
 20 | ERROR   | 602 | function is  not compatible
 51 | ERROR   | 603 | function is  not compatible
 88 | ERROR   | 604 | function is  not compatible
--------------------------------------------------------------------------------

Produces output like this:

FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
 51 | ERROR   | 603 | function is  not compatible      
--------------------------------------------------------------------------------

Making the keys of the $legal_codes array specifiable via command line parameter to my_wrapper.php is left as an exercise for the reader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文