Skip to content

AssertFuncCallToPHPUnitAssertRector uses $this->assert... in a static method #9482

@alies-dev

Description

@alies-dev

Bug Report

Subject Details
Rector version v2.2.7
---------- begin diff ----------
@@ @@
    /**
     * @param class-string<\Exception> $fqcn
     * @return string
     */
     private static function getExceptionMessage(string $fqcn): string
     {
         $exception = new $fqcn();
-        assert($exception instanceof \Exception);
+        $this->assertInstanceOf(\Exception::class, $exception);

         return $exception->getMessage();
     }
    ----------- end diff -----------

Applied rules:
 * AssertFuncCallToPHPUnitAssertRector

Minimal PHP Code Causing Issue

// inside PHPUnit's TestCase

    /**
     * @param class-string<\Exception> $fqcn
     * @return list<string>
     */
    private static function getExceptionMessage(string $fqcn): string
    {
        $exception = new $fqcn();
        assert($exception instanceof \Exception);

        return $exception->getMessage();
    }

Expected Behaviour

Highly Opinionated (sorry)

Do not change assert() calls into PHPUnit calls, as they have different goal: assert() calls often used as an extra validation/check layer and/or for static analysers, while PHPUnit assertions used for testing SUTs. Personally, I would disable AssertFuncCallToPHPUnitAssertRector rule on my projects.

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