从声纳分析中排除一些PHP代码 [英] Exclude some lines of php code from sonarqube analysis

查看:131
本文介绍了从声纳分析中排除一些PHP代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以从声纳分析中排除某些代码行而不排除整个源代码文件?

Is there a way to exclude some lines of code from sonarqube analysis without excluding the whole source code file? Maybe with an annotation or something like this?

我会给你一个具体的例子,我需要为未使用的函数参数排除某种代码味道:

I'll give you an concrete example, where i need to exclude a certain code smell for an unused function parameter:

我实现了这样的验证方法:

I implemented a validation method like this:

public function validateTruth(string $attribute, $value) : bool
{
    $acceptable = [
        '0',
        0,
        'false',
        false,
        '1',
        1,
        'true',
        true,
        'no',
        'yes',
    ];

    return in_array($value, $acceptable, true);
}

为了扩展照亮合同的有效性使用extend方法的工厂:

in order to extended the \Illuminate\Contracts\Validation\Factory with the extend method:

$validationFactory->extend(
    'truth',
    Validator::class . @validateTruth',
    'The :attribute field must be some kind of truth value.'
);

我的实现并没有使用所有必需的参数,但是我不能只留下第一个参数。没有$ attribute参数,扩展将无法与回调方法一起使用。因此,extend方法显然需要带有一组特定参数的回调,但是它不提供接口或类似的东西,因此丢失的参数将自动从声纳法中排除。

My implementation doesn't uses all the required parameters, but i cannot just leave out the first parameter. The extend wouldn't work with a callback method without the $attribute parameter. So the extend method clearly requires a callback with a certain set of parameters, but it doesn't provide an interface or something like this, so the missing parameter would be excluded automatically from sonarqube.

有没有一种方法可以只排除这种代码异味,而不能从声纳分析中排除整个函数或文件?我希望在源代码中提供一个解决方案,因为我们在开发生命周期中使用了不同的sonarqube实例,因此在Webfrontend中标记此类代码气味将更加难以维护。

Is there a way to exclude only this code smell, without excluding the whole function or file from sonarqube analysis? I would prefer a solution within the source code, because we use different instances of sonarqube in our development lifecycle, so flagging such code smells in the webfrontend would be harder to maintain.

推荐答案

您可以通过Web界面解决问题,或者,如果您愿意在代码中进行操作,请在代码中标记 NOSONAR 注释。

You can resolve the issue via the web-ui, or, if you prefer to do it in the code, mark it with a NOSONAR comment.

请参见官方常见问题解答以获取更多信息。

See the official FAQ for more information.

这篇关于从声纳分析中排除一些PHP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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