Skip to content

Coverage of early return in Generators #392

@Gisleburt

Description

@Gisleburt

There seems to be an issue where code that should be marked as "Dead Code", is marked as "Uncovered" when using an early return in a generator.

composer.json

{
    "require": {
        "phpunit/phpunit": "5.0.8"
    }
}

test.php

<?php

require 'vendor/autoload.php';

function gen(&$output, $branch = false)
{
    yield;

    if($branch) {
        $output = 'branched';
        return;
    } // This line is never covered.
    $output = 'did not branch';

}

class GenTest extends \PHPUnit_Framework_TestCase
{

    public function testGen()
    {
        $output = '';
        $gen = gen($output);

        while($gen->valid()) {
            $gen->next();
        }

        $this->assertSame(
            'did not branch',
            $output
        );

        $output = '';
        $gen = gen($output, true);

        while($gen->valid()) {
            $gen->next();
        }

        $this->assertSame(
            'branched',
            $output
        );
    }

}

Reproduce

$ vendor/bin/phpunit --coverage-html reports --whitelist test.php test.php

Result

image

Version

$ php --version
PHP 5.6.14-1+deb.sury.org~trusty+1 (cli)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Xdebug v2.3.2, Copyright (c) 2002-2015, by Derick Rethans

$ composer show -i
doctrine/instantiator             1.0.5  A small, lightweight utility to instantiate objects in PHP without invoking their constructors
myclabs/deep-copy                 1.4.0  Create deep copies (clones) of your objects
phpdocumentor/reflection-docblock 2.0.4
phpspec/prophecy                  v1.5.0 Highly opinionated mocking framework for PHP 5.3+
phpunit/php-code-coverage         3.0.1  Library that provides collection, processing, and rendering functionality for PHP code coverage information.
phpunit/php-file-iterator         1.4.1  FilterIterator implementation that filters files based on a list of suffixes.
phpunit/php-text-template         1.2.1  Simple template engine.
phpunit/php-timer                 1.0.7  Utility class for timing
phpunit/php-token-stream          1.4.8  Wrapper around PHP's tokenizer extension.
phpunit/phpunit                   5.0.8  The PHP Unit Testing framework.
phpunit/phpunit-mock-objects      3.0.3  Mock Object library for PHPUnit
sebastian/comparator              1.2.0  Provides the functionality to compare PHP values for equality
sebastian/diff                    1.3.0  Diff implementation
sebastian/environment             1.3.2  Provides functionality to handle HHVM/PHP environments
sebastian/exporter                1.2.1  Provides the functionality to export PHP variables for visualization
sebastian/global-state            1.1.1  Snapshotting of global state
sebastian/recursion-context       1.0.1  Provides functionality to recursively process PHP variables
sebastian/resource-operations     1.0.0  Provides a list of PHP built-in functions that operate on resources
sebastian/version                 1.0.6  Library that helps with managing the version number of Git-hosted PHP projects
symfony/yaml                      v2.7.5 Symfony Yaml Component

This bug also reported here: http://bugs.xdebug.org/view.php?id=1192

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions