search
Homephp教程php手册Xdebug文档(五) 代码覆盖分析

代码覆盖分析能在请求时让你知道脚本哪一行(或哪一段)在执行。

相关设置

xdebug.coverage_enable

类型: boolean, 默认值: 1, 始于 

Xdebug >= 2.2

如果设为0, Xdebug不会设置内置结构来允许代码分析,这能加速xdebug快些,不过代码覆盖分析将不会工作。

相关函数

boolean xdebug_code_coverage_started()

返回布尔值查看代码覆盖分析是否已经开始。

Example:

<span style="color: #000000;">php
    </span><span style="color: #008080;">var_dump</span><span style="color: #000000;">(xdebug_code_coverage_started());

    xdebug_start_code_coverage();

    </span><span style="color: #008080;">var_dump</span><span style="color: #000000;">(xdebug_code_coverage_started());
</span>?>  

 

Returns:

bool(false)
bool(true)
 

 

array xdebug_get_code_coverage()

返回一结构包含脚本哪一行(包括引用文件)在执行。以下展示代码覆盖指定某一文件示例:

Example:

<span style="color: #000000;">php
    xdebug_start_code_coverage();

    </span><span style="color: #0000ff;">function</span> a(<span style="color: #800080;">$a</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span> * 2.5<span style="color: #000000;">;
    }

    </span><span style="color: #0000ff;">function</span> b(<span style="color: #800080;">$count</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $count; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) {
            a(</span><span style="color: #800080;">$i</span> + 0.17<span style="color: #000000;">);
        }
    }

    b(</span>6<span style="color: #000000;">);
    b(</span>10<span style="color: #000000;">);

    </span><span style="color: #008080;">var_dump</span><span style="color: #000000;">(xdebug_get_code_coverage());
</span>?>  

 

Returns:

array

  '/home/httpd/html/test/xdebug/docs/xdebug_get_code_coverage.php' =>

    array

      5 => int 1

      6 => int 1

      7 => int 1

      9 => int 1

      10 => int 1

      11 => int 1

      12 => int 1

      13 => int 1

      15 => int 1

      16 => int 1

      18 => int 1

void xdebug_start_code_coverage( [int options] )

开始代码覆盖分析

该函数开始收集代码覆盖信息。该信息由二维数组形成,一维索引为执行文件名二级索引为行号。元素值表示该行是否被执行或它拥有未能到达的行。

每行返回的值:

  • 1: 该行已执行过
  • -1:      该行未被执行
  • -2:      该行没有可执行的代码

 -1 值只有在 XDEBUG_CC_UNUSED  开启后才会返回,而 -2 值只有XDEBUG_CC_UNUSED 和 XDEBUG_CC_DEAD_CODE 同时开启才会返回。

函数有两个以枚举值的选项:

XDEBUG_CC_UNUSED

开启检测代码并说明哪行为可执行代码。无此项则返回的数组只包含实际执行过的行号。

XDEBUG_CC_DEAD_CODE

开始额外的分析说明哪些代码能被执行。

如果开启这些选项执行大幅减慢代码覆盖分析速度。

以下示例展示如何使用选项:

Example:

<span style="color: #000000;">php
xdebug_start_code_coverage( XDEBUG_CC_UNUSED </span>|<span style="color: #000000;"> XDEBUG_CC_DEAD_CODE );
</span>?>

 

void xdebug_stop_code_coverage( [int cleanup=true] )

停止代码覆盖分析

该函数停止收集信息,该信息在内存中会被清除。如果你传递“false”参数,则代码覆盖信息不会被清除而能让你再次使用 xdebug_start_code_coverage() 恢复信息收集。

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment