如何阅读/改进PHP计算的C.R.A.P指数 [英] How to Read / Improve C.R.A.P Index Calculated by PHP
问题描述
我刚刚开始使用PHPUnit及其丰富多彩的代码覆盖率报告.我了解所有数字和百分数都保存一个:C.R.A.P索引.谁能为我提供扎实的含义,如何分析以及如何降低它的解释?
I just started working with PHPUnit and its colorful code coverage reports. I understand all the numbers and percentages save one: The C.R.A.P index. Can anyone offer me a solid explanation of what it means, how to analyze it and how to lower it?
推荐答案
@Toader Mihai提供了扎实的解释.(向我+1)
编写不太复杂的代码或编写经过更好测试的代码. (请参见下图)
Write less complex code OR write better tested code. (See the graph below)
测试更好的代码?
在这种情况下,这仅意味着:更高的代码覆盖率,通常会导致编写更多测试.
In this context this just means: A higher code coverage and usually results in writing more tests.
少复杂的代码吗?
例如:将您的方法重构为较小的方法:
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)
首先,让我提供一些其他资源:
First off let me provide some additional resources:
以防万一:解释了循环复杂性. PHP_CodeSniffer和PHPMD之类的工具会在您想知道的情况下告诉您该数字.
just in case: Cyclomatic complexity explained. Tools like PHP_CodeSniffer and PHPMD will tell you that number in case you want to know.
虽然您可以决定哪个数字是可以的",但经常建议使用的数字(即小小的高恕我直言)是 30 的废话指数,其结果如下图所示:
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:
(您可以在此处获取.ods文件: https://www.dropbox .com/s/3bihb9thlp2fyg8/crap.ods?dl = 1 )
(You can get the .ods file here: https://www.dropbox.com/s/3bihb9thlp2fyg8/crap.ods?dl=1 )
这篇关于如何阅读/改进PHP计算的C.R.A.P指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!