Skip to content

Incorrect behavior of TypedPropertyFromCreateMockAssignRector #9126

@ThoWi-de

Description

@ThoWi-de

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/e9e1beb3-c466-4ee6-9a82-78e89e9e567b

<?php

declare(strict_types=1);

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

interface SomeInterface {
    public function someMethod(): void;
}

final class SomeClass
{
    public function foo(SomeInterface $bar): int
    {
        $bar->someMethod();
        return 42;
    }
}

final class SomeClassTest extends TestCase
{
    private MockObject&SomeInterface $someProperty;

    protected function setUp(): void
    {
        $this->someProperty = $this->createMock(SomeInterface::class);
    }

    public function testFoo(): void
    {
        $object = new SomeClass();
        $this->assertSame(42, $object->foo($this->someProperty));
    }
}

Responsible rules

  • TypedPropertyFromCreateMockAssignRector

Expected Behavior

Rector should not make any changes.

Rector will make this change

 final class SomeClassTest extends TestCase
 {
-    private MockObject&SomeInterface $someProperty;
+    private MockObject $someProperty;

     protected function setUp(): void
     {

and using PHPStan will show this error

 ------ --------------------------------------------------------------------------------------------------------------------
  Line   test.php
 ------ --------------------------------------------------------------------------------------------------------------------
  33     Parameter #1 $bar of method SomeClass::foo() expects SomeInterface, PHPUnit\Framework\MockObject\MockObject given.
         🪪  argument.type
 ------ --------------------------------------------------------------------------------------------------------------------

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