PHP中的无效代码检测 [英] Dead code detection in PHP
问题描述
我有一个项目的代码非常混乱-到处都是重复和死代码.
I have a project with very messy code - lots of duplication and dead code here and there.
前一段时间,单元测试的代码覆盖率为零,但现在我们正尝试在T.D.D中编写所有新代码.并通过单元测试覆盖旧"代码(最后测试技术)来降低方式和降低技术负担.
Some time ago there was zero code coverage by unit-tests but now we're trying to write all new code in T.D.D. manner and lowering technical debt by covering "old" code by unit-tests as well(test-last technique).
业务逻辑的复杂度很高,有时没有人可以回答是否使用了某些方法.
Business logic's complexity is quite high and sometimes no one can answer whether some methods are used or not.
如何找到这种无效代码方法?广泛的日志记录?更高的测试覆盖率?(这不是很容易,因为客户希望推出新功能)
How this dead code methods can be found? Extensive logging? Higher test coverage?(It is not very easy because customers want new features to come out)
推荐答案
xdebug 的代码覆盖率工具可让您测试实际执行了哪些代码行,而无需在所有函数/方法中放入跟踪语句.
xdebug's code coverage tools allow you to test which lines of code are actually being executed, without needing to put trace statements in all of the functions/methods.
示例:
<?php
xdebug_start_code_coverage();
function a($a) {
echo $a * 2.5;
}
function b($count) {
for ($i = 0; $i < $count; $i++) {
a($i + 0.17);
}
}
b(6);
b(10);
var_dump(xdebug_get_code_coverage()); // array '/path/file.php' => array line_number => int 1 or 0.
?>
这篇关于PHP中的无效代码检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!