Skip to content

Wrong doc node type detection #9388

@simonschaufi

Description

@simonschaufi

Bug Report

Subject Details
Rector version 2.1.7

I have a strange scenario in TYPO3 Rector where I want to convert the @TYPO3\CMS\Extbase\Annotation\Validate into a php attribute: #[\TYPO3\CMS\Extbase\Annotation\Validate(['validator' => 'NotEmpty'])]. The migration works well if the validate annotation is the only one but once you add @var string to it, it doesn't work anymore. The issue is this one: sabbelasichon/typo3-rector/issues/4625

I've tried my best to find the exact location where the error is but I couldn't find it as there is so much going on in the PhpDocInfoFactory and especially the BetterPhpDocParser that I got lost on the way.

Minimal PHP Code Causing Issue

The following code leads to an invalid result:

<?php

class HelloWorld
{
    /**
     * @var string
     * @TYPO3\CMS\Extbase\Annotation\Validate("NotEmpty")
     */
    protected $hash = '';
}

results into (note the missing \ in the beginning):

<?php

class HelloWorld
{
    /**
     * @var string
     */
    #[TYPO3\CMS\Extbase\Annotation\Validate(['validator' => 'NotEmpty'])]
    protected $name = '';
}

This code works:

<?php

class HelloWorld
{
    /**
     * @TYPO3\CMS\Extbase\Annotation\Validate("NotEmpty")
     */
    protected $hash = '';
}

and results in:

<?php

class HelloWorld
{
    #[\TYPO3\CMS\Extbase\Annotation\Validate(['validator' => 'NotEmpty'])]
    protected $name = '';
}

Expected Behaviour

The applied rule https://github.com/sabbelasichon/typo3-rector/blob/main/rules/TYPO312/v0/ExtbaseAnnotationToAttributeRector.php does it's job correctly

The most important line in the rector rule is:

if (str_starts_with($doctrineTagValueNode->identifierTypeNode->name, '@TYPO3\CMS')) {

If that is true, the rest works. In the case of the additional @var string annotation, this is false.

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