Rector: (Probably) Coding standard causes Cannot declare class X because the name is already in use, while trying to import FQN of classes

Created on 11 Nov 2019  ยท  14Comments  ยท  Source: rectorphp/rector

| Subject | Details |
| :------------- | :----------------------------------------------------------- |
| Rector version | v0.5.21 |
| PHP version | 7.3.4 |
| Full Command | vendor/bin/rector p src/Test/Exception.php --set=dead-code -n |

Current Behaviour

-class Exception extends \Exception
+use Exception;
+class Exception extends Exception

These changes results in PHP Fatal error: Cannot declare class Test\Exception because the name is already in use, whenever code is processed again by rector or that class is used in another file (i encountered this fatal error when rector was processing next file using that class and it was a bit tricky to find out what is wrong ๐Ÿ˜„ )

Minimal PHP Code Causing Issue

<?php declare (strict_types=1);

namespace Test;

final class Exception extends \Exception { }

Expected Behaviour

I expect rector to do nothing with this code.

bug

Most helpful comment

Hi guys, I have same problem and fix it by this because typo:
PHP Fatal error: Cannot declare class AppBundleEntityKeExtendedProductNames, because the name is already in use in...

    /**
     * @var \KeExtendedProductNames
     *
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\OneToOne(targetEntity="KeExtendedProductNames")

change to

    /**
     * @var KeExtendedProductNames
     *
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\OneToOne(targetEntity="KeExtendedProductNames")

All 14 comments

Here is another situation:

<?php declare (strict_types=1);

namespace Klarka\Test;

final class Memcached
{
    /**
     * @var \Memcached
     */
    public $memcached;
}

Command: vendor/bin/rector p src/Test/Memcached.php --set=dead-code -n:

โฏ vendor/bin/rector p src/Test/Memcached.php --set=dead-code -n
Rector v0.5.21
Config file: rector.yaml

 2/3 [โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘]  66%PHP Fatal error:  Cannot declare class Klarka\Test\Memcached, because the name is already in use in /Users/janmikes/Sites/klarka/src/Test/Memcached.php on line 5

Fatal error: Cannot declare class Klarka\Test\Memcached, because the name is already in use in /Users/janmikes/Sites/klarka/src/Test/Memcached.php on line 5

How it runs on master? There've been similar issues in last 6 days, that are now fixed

Damn, got conflict in composer, one of our dependencies needs nikic/php-parser 4.2.* but rector master 4.3.* :-).

Anyways on master still broken:

โฏ vendor/bin/rector p src/Test/Memcached.php --set=dead-code -n
Rector dev-master@44e6747
Config file: rector.yaml

 2/3 [โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘]  66%PHP Fatal error:  Cannot declare class Klarka\Test\Memcached, because the name is already in use in /Users/janmikes/Sites/klarka/src/Test/Memcached.php on line 5

Fatal error: Cannot declare class Klarka\Test\Memcached, because the name is already in use in /Users/janmikes/Sites/klarka/src/Test/Memcached.php on line 5

This is with the memcached example above.

Damn, got conflict in composer, one of our dependencies needs nikic/php-parser 4.2.*

Which one? Just for future fixes :)

https://github.com/rectorphp/rector/issues/2307#issue-521025580 should be fixed. Can you confirm?

I recall the problem might persist for docblocks - https://github.com/rectorphp/rector/issues/2307#issuecomment-552489132
Same error, but 2 different issues

https://packagist.org/packages/voda/gettext-extractor#v4.0.1, it is pretty old though, i will check for compatibility and send PR so we are not stuck on 4.2.

Unfortunately even the class Exception extends \Exception persists.

auto_import_names: false in config will solve the Exception extends \Exception situation.

With this change (and on master), the memcached problem still persists.

Ah :/ that sucks. That package has native support in SymfonyTranslation for years

Could you paste here exact version of Rector? vendor/bin/rector --version

Minimal code required to break things up:

<?php

namespace Klarka\Test;

final class Exception
{
    /**
     * @var \Exception
     */
    public $exception;
}

Command: vendor/bin/rector p src/Test/Exception.php --set=dead-code

Version: Rector dev-master@44e6747

One more update, i had Rector\Renaming\Rector\Class_\RenameClassRector in my services in rector.yaml. After removed and running set only, it works.

It is the following config that is breaking it:

parameters:
    auto_import_names: false

services:
    Rector\Renaming\Rector\Class_\RenameClassRector:
        $oldToNewClasses:
            A: B

I recall the problem might persist for docblocks

This issue still persists, i just rebased to master and triggered build: https://travis-ci.org/rectorphp/rector/jobs/611986871?utm_medium=notification&utm_source=github_status

Shall i create separate issue, or?

It was not clear from your report.
That's why #2320 is not closed yet. I still need to explore the other file

https://github.com/rectorphp/rector/issues/2307#issuecomment-552502919 ok i see, i should have updated the original post ๐Ÿ˜‰

Hi guys, I have same problem and fix it by this because typo:
PHP Fatal error: Cannot declare class AppBundleEntityKeExtendedProductNames, because the name is already in use in...

    /**
     * @var \KeExtendedProductNames
     *
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\OneToOne(targetEntity="KeExtendedProductNames")

change to

    /**
     * @var KeExtendedProductNames
     *
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\OneToOne(targetEntity="KeExtendedProductNames")

Failing test case is needed.

Was this page helpful?
0 / 5 - 0 ratings