Skip to content

Incorrect behavior of DowngradeUnionTypeDeclarationRector on union implements interface #9121

@samsonasik

Description

@samsonasik

Bug Report

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

Minimal PHP Code Causing Issue

When method implements interface with union of of parent method, the union return removed, then after it, it cause fatal error

Fatal error: Declaration of Imp::run() must be compatible with DoX::run(): FooInterface 

see https://3v4l.org/4QVaj

this should copy method interface return instead.

See https://getrector.com/demo/80f293b5-ef36-4df0-bb0c-ede44636885a

<?php

interface FooInterface {}
class A implements FooInterface {}
class B implements FooInterface {}

interface DoX 
{
	public function run(): FooInterface;
}

class Imp implements DoX
{
    public function run(): A|B
    {
    	return new A();
    }
}

Responsible rules

  • DowngradeUnionTypeDeclarationRector

Expected Behavior

    /**
     * @return \A|\B
     */
-    public function run(): A|B
+    public function run(): FooInterface
    {
    	return new A();
    }

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