How to Read / Improve C.R.A.P Index Calculated by PHP(如何阅读/改进 PHP 计算的 C.R.A.P 指数)
问题描述
我刚开始使用 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 offered a solid explanation. (+1 from me)
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.
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:
(You can get the .ods file here: https://www.dropbox.com/s/3bihb9thlp2fyg8/crap.ods?dl=1 )
这篇关于如何阅读/改进 PHP 计算的 C.R.A.P 指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何阅读/改进 PHP 计算的 C.R.A.P 指数


- Laravel 仓库 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01