sebastianbergmann / php-token-stream

Wrapper around PHP's tokenizer extension.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Class with method that uses anonymous class is not processed correctly

t0mmy742 opened this issue · comments

Q A
php-code-coverage version 8.0.1
PHP version 7.4.5
Driver Xdebug (pcov also)
Xdebug version (if used) 2.9.4
Installation Method Composer
Usage Method PHPUnit
PHPUnit version (if used) 9.1.4

src/PhpAnonym.php

class PhpAnonym
{
    private $field;

    public function __construct(int $field)
    {
        $this->field = $field;
    }

    public function funcOne(): int
    {
        return $this->field;
    }

    public function funcTwo(): int
    {
        $cls = new class(42) {
            private $num;

            public function __construct(int $num)
            {
                $this->num = $num;
            }

            public function getNum(): int
            {
                return $this->num;
            }
        };

        return $cls->getNum();
    }
}

src/PhpAnonymTest.php

class PhpAnonymTest extends \PHPUnit\Framework\TestCase
{
    public function testFuncOne(): void
    {
        $phpAnonym = new PhpAnonym(42);
        $this->assertSame(42, $phpAnonym->funcOne());
    }

    public function testFuncTwo(): void
    {
        $phpAnonym = new PhpAnonym(1234);
        $this->assertSame(42, $phpAnonym->funcTwo());
    }
}

Coverage HTML

1
2

If funcTwo is the first function of the class, it seems like there is no problem.