Skip to content

AssertCompareOnCountableWithMethodToAssertCountRector should not apply to Generators #9508

@theofidry

Description

@theofidry

Bug Report

Subject Details
Rector version 2.2.4
 PHPUnit version  11.5.42

The rule propose invalid code.

Minimal PHP Code Causing Issue

This rule suggest the following change:

-        $this->assertSame(10, iterator_count($executedProcesses));
+        $this->assertCount(10, $executedProcesses);

Expected Behaviour

Looking at the API, it looks fine. However, PHPUnit implements ::assertCount() as follows:

    final public static function assertCount(int $expectedCount, Countable|iterable $haystack, string $message = ''): void
    {
        if ($haystack instanceof Generator) {
            throw GeneratorNotSupportedException::fromParameterName('$haystack');
        }

        self::assertThat(
            $haystack,
            new Count($expectedCount),
            $message,
        );
    }

So when $executedProcesses is a Generator, this rule ends up creating invalid code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions