Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing PHPDoc with phpdoc return typehint duplicates Annotation #7020

Closed
shyim opened this issue Feb 23, 2022 · 1 comment · Fixed by rectorphp/rector-src#1862
Closed
Labels

Comments

@shyim
Copy link
Contributor

shyim commented Feb 23, 2022

Bug Report

Subject Details
Rector version bc0eac3

Minimal PHP Code Causing Issue

Code

<?php

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

class Test {
    /**
     * @Since("6.4.0.1")
     * @Route("/api/_admin/reset-excluded-search-term", name="api.admin.reset-excluded-search-term", methods={"POST"})
     *
     * @return JsonResponse
     */
    public function resetExcludedSearchTerm(Context $context)
    {

    }
}

Rector

<?php

namespace Shopware\Core\DevOps\Rector;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
use Rector\Core\Rector\AbstractRector;
use Rector\Php80\NodeFactory\AttrGroupsFactory;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class TestRector extends AbstractRector
{
    public function __construct(
        private AttrGroupsFactory $attrGroupsFactory,
        private PhpDocTagRemover $phpDocTagRemover,
    ) {
    }

    public function getRuleDefinition(): RuleDefinition
    {
        return new RuleDefinition('uff', []);
    }

    public function getNodeTypes(): array
    {
        return [
            ClassMethod::class,
        ];
    }

    public function refactor(Node $node)
    {
        $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
        if (!$phpDocInfo instanceof PhpDocInfo) {
            return null;
        }

        $route = $phpDocInfo->getByName('Route');

        $defaults = $route->value->getValue('defaults');

        if ($defaults === null) {
            $defaults = new CurlyListNode();
        }

        $route->value->changeValue('defaults', $defaults);
    }
}

Expected Behaviour

The route just gets an additional "defaults" and not be duplicated

1) class.php:7

    ---------- begin diff ----------
@@ @@
 class Test {
     /**
      * @Since("6.4.0.1")
+     * @Route("/api/_admin/reset-excluded-search-term", name="api.admin.reset-excluded-search-term", methods={"POST"}, defaults={})
      * @Route("/api/_admin/reset-excluded-search-term", name="api.admin.reset-excluded-search-term", methods={"POST"})
      *
      * @return JsonResponse
    ----------- end diff -----------

                                                                                                                        
 [OK] 1 file would have changed (dry-run) by Rector                                                                     
                                                                                                                        

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants