简体   繁体   English

如何阅读/改进PHP计算的CRAP指数

[英]How to Read / Improve C.R.A.P Index Calculated by PHP

I just started working with PHPUnit and its colorful code coverage reports. 我刚刚开始使用PHPUnit及其丰富多彩的代码覆盖率报告。 I understand all the numbers and percentages save one: The CRAP index. 我知道所有数字和百分比都保存一个:CRAP索引。 Can anyone offer me a solid explanation of what it means, how to analyze it and how to lower it? 谁能对我的意思,如何分析和降低它提供扎实的解释?

@Toader Mihai offered a solid explanation. @Toader Mihai提供了扎实的解释。 (+1 from me) (我的+1)

How to lower it: 如何降低它:

Write less complex code OR write better tested code. 编写不太复杂的代码或编写更好的经过测试的代码。 (See the graph below) (请参见下图)

Better tested Code ? 经过更好测试的代码?

In this context this just means: A higher code coverage and usually results in writing more tests. 在这种情况下,这仅意味着:更高的代码覆盖率,通常会导致编写更多测试。

Less complex code ? 不太复杂的代码?

For example: Refactor your methods into smaller ones: 例如:将方法重构为较小的方法:

// Complex
function doSomething() {
    if($a) {
        if($b) {
        }
        if($c) {
        }
    } else {
        if($b) {
        }
        if($c) {
        }
    }
}

// 3 less complex functions
function doSomething() {
    if($a) {
        doA();
    } else {
        doNotA();
    }
}

function doA() {
    if($b) {
    }
    if($c) {
    }
}

function doNotA() {
    if($b) {
    }
    if($c) {
    }
}

(just a trivial example, you'll find more resources for that i'm sure) (只是一个简单的例子,我敢肯定会找到更多资源)

Additional resources: 其他资源:

First off let me provide some additional resources: 首先,让我提供一些其他资源:

Creators blog post about the crap index 创作者博客文章关于垃圾索引

just in case: Cyclomatic complexity explained . 以防万一: 解释了圈复杂度 Tools like PHP_CodeSniffer and PHPMD will tell you that number in case you want to know. 诸如PHP_CodeSniffer和PHPMD之类的工具会告诉您该数字,以备不时之需。

And while it is for you to decide what number is "ok" one often suggested number (that is a litte high imho) is a crap index of 30 resulting in a Graph like this: 当您决定什么数字是“ ok”时,一个经常被建议的数字(即小高的恕我直言)是废话指数为30的图形,如下所示:

替代文字 (You can get the .ods file here: https://www.dropbox.com/s/3bihb9thlp2fyg8/crap.ods?dl=1 ) (您可以在此处获取.ods文件: https ://www.dropbox.com/s/3bihb9thlp2fyg8/crap.ods?dl =1

Basically it wants to be a predictor of the risk of change for a method. 基本上,它希望成为方法更改风险的预测指标。

It has two factors in it: 它有两个因素:

  • code complexity of the method ( cyclomatic complexity ) aka how many decisions paths exists in said method: comp(m) . 该方法的代码复杂度( cyclomatic complexity ),即该方法中存在多少个决策路径: comp(m)
  • how testable is that method (via automated tests, provided by a code coverage tool). 该方法的可测试性如何(通过代码覆盖率工具提供的自动测试)。 Basically this measures how many decisions in said code are automatically testable. 基本上,这测量了所述代码中的多少个决定是可自动测试的。

If the method has 100% coverage than the risk of change is considered to be equivalent only with the complexity of the method: CRAP(m) = comp(m) . 如果该方法具有100%的覆盖率,则更改风险仅与该方法的复杂性等效: CRAP(m) = comp(m)

If the method has 0% coverage than the risk of change is considered to be a second degree polinomial in the complexity measure (reasoning being that if you can't test a code path changing it increases risk of breakage): CRAP(m) = comp(m)^2 + comp(m) 如果该方法的覆盖率为0%,则更改风险被认为是复杂性度量中的二级政策(原因是,如果您无法测试更改代码路径,则会增加破损风险): CRAP(m) = comp(m)^2 + comp(m)

Hopefully this will help you. 希望这会对您有所帮助。

I just noticed that I only provide the half answer (the read part). 我只是注意到我只提供一半的答案(阅读部分)。 The how to improve it should be pretty clear if you understand the reasoning of the index. 如果您了解索引的原因,应该非常清楚如何改进它。 But a much more clear explanation is given in @edorian's answer . 但是@edorian的答案给出了更清晰的解释。

The short story is: write tests until you have near 100% coverage and after that refactor the methods to decrease the cyclomatic complexity. 简短的故事是:编写测试,直到覆盖率接近100%,然后重构这些方法以降低循环复杂度。 You can try to refactor before having tests but depending on the actual method complexity you risk introducing breakage if you can't reason (because of the complexity involved) all the consequences of the change you are doing. 您可以在进行测试之前尝试进行重构,但是如果您不能(由于所涉及的复杂性)无法推理出所做更改的所有后果,则有可能会导致破坏,这取决于实际方法的复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM