Skip to content

[TypedPropertyRector] Compile Error: Type of Child::$name must not be defined (as in class VendorParent) #2506

@ruudk

Description

@ruudk
Subject Details
Rector version v0.6.3
PHP version PHP 7.4
Full Command vendor/bin/rector process --dry-run file.php

Problem

There is a problem with TypedPropertyRector. When Child extends VendorParent (package coming from Composer) and overwrites a property with a default value, Rector will try to apply the string property type.

When running the code, this fails:

 Compile Error: Type of Child::$name must not be defined (as in class VendorParent)

The reason is that the parent is not changed, so the parent has no type, but the child introduces a type. That's not allowed in PHP 7.4.

Failing test scenario:

vendor/rector/rector/packages/Php74/tests/Rector/Property/TypedPropertyRector/Fixture/parent_has_untyped_property.php.inc:

<?php

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Fixture;

class VendorParent
{
    /**
     * @var string
     */
    protected $name;
}

final class Child extends VendorParent
{
    /**
     * @var string
     */
    protected $name = 'child';
}

?>
-----
<?php

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Fixture;

class VendorParent
{
    /**
     * @var string
     */
    protected $name;
}

final class Child extends VendorParent
{
    /**
     * @var string
     */
    protected $name = 'child';
}

?>

result:

Failed asserting that string matches format description.
--- Expected
+++ Actual
@@ @@
     /**
      * @var string
      */
-    protected $name;
+    protected string $name;
 }
 
 final class Child extends VendorParent
@@ @@
     /**
      * @var string
      */
-    protected $name = 'child';
+    protected string $name = 'child';
 }
 
 ?>

(it also tries to update the VendorParent, but that's because I didn't exclude this one. Should propably be done in the test somewhere, not sure where).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions