Search code examples
composer-phpphpstorm

How to resolve PHPStorm alert "Multiple definitions" for a class


I use PHPStorm and have the following message "Multiple definitions" for the PHPUnit\Framework\TestCase. I am not sure exactly but it looks like PHPStorm used compose autoload system. So I tried to find directories where unnecessary class definitions live and exclude them.

I found that I can use exclude-from-classmap property in autoload. So I added the following to it:

"vendor/cloudinary/cloudinary_php/tests/",
"vendor/bin/.phpunit/phpunit-5.7/src/ForwardCompatibility/"

These are directories where unnecessary class definitions exist. Then I ran composer dump-autoload. Nothing changed. Restarted PHPStrom - nothing.

Here is my whole autoload section:

"autoload": {
        "psr-4": { "": "src/" },
        "classmap": [ "app/AppKernel.php", "app/AppCache.php" ],
        "files": [
            "src/helpers/helpers.php"
        ],
        "exclude-from-classmap": [
            "vendor/cloudinary/cloudinary_php/tests/",
            "vendor/bin/.phpunit/phpunit-5.7/src/ForwardCompatibility/"
        ]
    },

I noticed related questions but they do not meet my requirements. I know that the way to exclude folder from composer.


Solution

  • PhpStorm doesn't care too much about your composer.json*. It founds the class defined two times in the files of your project and this is the reason it warns you.

    PHP is not bothered about the class being defined two times. If it loads the class from one file (through the autoloader generated by Composer), it never reads the other file.

    After you detect the duplicates and decide which one you want PhpStorm to ignore, right-click on its directory in Project View and choose "Mark Directory as" -> "Excluded" from the menu that appears.


    * This is not entirely true. PhpStorm by default is configured to add as libraries the packages it finds in the Composer files in the project. But it warns about duplicate definitions of classes even in the absence of Composer files in the project.