Skip to content

CompleteVarDocTypeProperty rector does not respect aliased, imported classes #2416

@alexanderschnitzler

Description

@alexanderschnitzler
Subject Details
Rector version Rector v0.6.0)
PHP version PHP 7.4.0 (cli) (built: Nov 28 2019 07:27:06) ( NTS ))
Full Command bin/rector process -s TypeDeclaration typo3/sysext/backend/Classes/Compatibility/SlotReplacement.php

Current Behaviour

Rector v0.6.0
Config file: rector.yml

[parsing] typo3/sysext/backend/Classes/Compatibility/SlotReplacement.php
[refactoring] typo3/sysext/backend/Classes/Compatibility/SlotReplacement.php
    [applying] Rector\TypeDeclaration\Rector\Property\PropertyTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector
[printing] typo3/sysext/backend/Classes/Compatibility/SlotReplacement.php
1 file with changes
===================

1) typo3/sysext/backend/Classes/Compatibility/SlotReplacement.php

    ---------- begin diff ----------
--- Original
+++ New
@@ -36,7 +36,7 @@
 class SlotReplacement
 {
     /**
-     * @var SignalSlotDispatcher
+     * @var \SignalSlotDispatcher
      */
     protected $signalSlotDispatcher;
    ----------- end diff -----------

Applied rules:

 * Rector\TypeDeclaration\Rector\Property\PropertyTypeDeclarationRector

To explain the issue, please have a look at the whole file here (https://github.com/TYPO3/TYPO3.CMS/blob/v10.2.0/typo3/sysext/backend/Classes/Compatibility/SlotReplacement.php) or take this short snippet that explains the issue quite well I suppose.

<?php
namespace TYPO3\CMS\Backend\Compatibility;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher as SignalSlotDispatcher;

class SlotReplacement
{
    /**
     * @var SignalSlotDispatcher
     */
    protected $signalSlotDispatcher;
    public function __construct(SignalSlotDispatcher $signalSlotDispatcher)
    {
        $this->signalSlotDispatcher = $signalSlotDispatcher;
    }
}

As one can see, the current doc block type is already correct. The TYPO3\CMS\Extbase\SignalSlot\Dispatcher has been aliased with SignalSlotDispatcher and that alias is used as type hint in the constructor and as type in the doc block.

When running the CompleteVarDocTypePropertyRector, the type hint in the doc block is prepended with a backslash.

Before:

/**
 * @var SignalSlotDispatcher
 */

After:

/**
 * @var \SignalSlotDispatcher
 */

Minimal PHP Code Causing Issue

I am putting a fixture in here because that is the easiest to show and test the issue.
Put it into packages/TypeDeclaration/tests/Rector/Property/CompleteVarDocTypePropertyRector/Fixture/ and run the unit tests and it will show the issue.

<?php

namespace Rector\TypeDeclaration\Tests\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;

use EventDispatcher as AliasedEventDispatcher;

final class PropertyAssignWithAliasesClassName
{
    /**
     * @var AliasedEventDispatcher
     */
    private $eventDispatcher;

    public function __construct(AliasedEventDispatcher $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }
}

?>
-----
<?php

namespace Rector\TypeDeclaration\Tests\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;

use EventDispatcher as AliasedEventDispatcher;

final class PropertyAssignWithAliasesClassName
{
    /**
     * @var AliasedEventDispatcher
     */
    private $eventDispatcher;

    public function __construct(AliasedEventDispatcher $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }
}

?>

Expected Behaviour

Expected behaviour is that the doc comment is left untouched as it's already correct.

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