Question

I'm using Kohana framework which allows for multiple class definitions (in application and system subfolders). I'm using phpstorm as an IDE which gives me messages multiple definitions exist for class . Is there any way to tell phpStorm which class definition is correct?

Was it helpful?

Solution 3

I've found a possible solution to my problem - I can mark file as plain

OTHER TIPS

Is there any way to tell PhpStorm which class definition is correct?

You cannot, unfortunately.

https://youtrack.jetbrains.com/issue/WI-17646 -- watch this ticket (star/vote/comment) to get notified on any progress.

ATM you either just ignore the under-waving .. or you can configure that inspection to not to report such cases (Settings/Preferences | Editor | Inspections | PHP | Undefined | Undefined class, it has Don't report multiple class declaration potential problems checkbox).

Even with that inspection configured, IDE will still ask you what class declaration to jump to (and this is correct behaviour as IDE does not know if you want to see the original implementation or implement your own).


The only other way is to ensure that there is only one class with the same name in the project. For that you may use:

  • Mark whole folder as excluded
  • Mark individual file as Plain Text

Both are available via content menu in Project View and applicable to project files only (e.g. will be unavailable or will do nothing useful if tried to apply in Library scope).

You should just ignore the complete cache folder.

  • Go to Settings > Directories
  • Choose var\cache
  • Set it to 'Excluded'

From: https://github.com/Haehnchen/idea-php-symfony2-plugin/issues/301

As variant you can turn off inspection only for specific class. Put cursor inside underwaved class name, then Alt+EnterInspection optionsSupress for statement

PHPStorm adds

/** @noinspection PhpUndefinedClassInspection */

above class declaration and class name is not underwaved anymore.

I've been around the web everywhere and not a single option worked for me... I've been struggling for months with it, and today I found a solution, so if none of the above works for you, try redefining the PHP Include Path List. under Settings > Languages & Frameworks > PHP, make sure only the folders containing paths to source used by your project or application is configured.

My scenario is that I do a lot of package development, and while my packages are all in one project, they are also "symlinked" in "vendor" in my composer configuration, so there is duplicated code found by PHPStorm, in the vendor folder and my packages folder. Similarly, if include paths are duplicated, or paths are configured to find code outside of your project, which is already part of the project, it will also find multiple definitions. So, excluding the symlinked folders in vendor, allows PHPStorm to only find one copy of the source to my packages, and if my packages contain vendor folders of their own, they will also show up as duplicated definitions. Remove anything in the Include Path list where it may find dipplication

Just some addition to the Andy White's comment:

Settings | Editor | Inspections | PHP | Undefined | Undefined class | Don't report multiple class declaration potential problems

I really couldn't find this config and thought it was no there now, but this is still there, but very well hidden)

It is a little counterintuitive and inconspicuous, but the needed checkbox is in the right panel, and appears only if you click on the Undefined class row: enter image description here

Prolog

Guess there are several ways to solve this problem. Actually, it's just a warning and it says that phpstorm can't provide you with autocompletion so you have to work a bit harder :D

I had the same problem as many others here and solved it by ignoring unwanted.


Scenario

Had a git project with a vendor-folder after composer install. Also, there is a my-project.phar in this project that also contains some vendor stuff and this caused my warnings.


Solution

File > Preferences|Settings > Directories There you have to possibility to exclude files and folders. In my case it's the .phar so it's a "file" and you can add it at the bottom of the settings-window.

sample

PHPStorm will no longer see duplicates.

This is very project-specific and I guess most people have to find their own solution but pointing to this may help to find the problem easier.

Hope this helps someone :)

Somewhere in your project there are multiple definitions for the same class. I discovered I had backup copies in my project which caused this warning. I removed the backups from my Project (a good idea anyway) and it fixed the error.

I don't know how you created the other definition, but if you or anyone has this issue due to calling class_alias(), then you can solve this issue quickly.

Consider

class_alias(
    'The\AliasClass',
    'My\RealClass',
    true
);

and

class_alias(
    'The\AliasClass',
    'My\Real'.'Class', // <-- break up the string
    true
);

With the latter, PhpStorm will not pick up the My\RealClass and your "multiple definition" warning will cease. This is an ancient JavaScript trick to embed HTML in a string literal, by the way.

This warning has annoyed me for a long time. I believe the answers here saying there is a duplicate file somewhere is correct. The reason I am getting the warnings are due to the autocomplete file to give phpStorm a hint on how to find codeIgniter functions. If you are doing this also that is the reason for some of the warnings. The autocomplete file makes phpStorm think there are two different definitions. However, I like autocomplete more than I dislike the warnings so I guess I have to live with them.

This is the autocomplete I'm referring to: IntelliJ IDEA 12 not finding CodeIgniter classes, throwing errors

Alternatives that work after a fashion, but aren't so good

  • marking the file B you don't want to be used by autocomplete as "plain" or excluded, leaving file A active: this will disable notifications in the file C, but will also make autocompletion no longer work for whatever is in file B. So if somewhere else you use something that's rightly in B, and maybe there you want to exclude A from autocompletion, you can't do that.
  • disable the inspection: this will also disable undefined class warnings, so if I make any typos in any class name, I'll only discover this after deployment (or from the fact that autocomplete stops working for that object).
  • "Don't report multiple class declaration potential problems" - this is very nearly good, but I don't like having "potential problems" ignored; what if I create a class with an unwittingly duplicated name that is in use somewhere else? Granted that I'll catch it (or phpunit will), but still.

The best I've found so far

The way to go for now, at least until a more focused configuration is available for PHPStorm (e.g. "Alternative Classes"), is to mark those notifications - and only those - as ignorable:

/* @noinspection PhpUndefinedClassInspection */
/**
 * Verify an existing contract. Requires agent and supervisor.
 *
 * @param array   $data
 * @param Cliente $cli
 * @param User    $age
 * @param User    $sup
 * @return Contratto
 */
private function contratto(
    array $data,
    /* @noinspection PhpUndefinedClassInspection */
    Cliente $cli,
    /* @noinspection PhpUndefinedClassInspection */
    User $age,
    /* @noinspection PhpUndefinedClassInspection */
    User $sup
) {

Note that to disable notifications in the PHPDoc comment I had to add a directive before the comment; this did not disable the notifications for the three parameters.

In the future, I wish to be able to specify those in PHPStorm as

 * @param array                 $data    raw data for the contract
 * @param \local\foobar\Cliente $cli     customer opening the contract

private function contratto(
    array $data,
    /*\local\foobar\*/Cliente $cli,

or better still, explicitly use a new PHPdoc tag such as "@replaces". So PHPStorm would know that my class is the one not replaced. I'll also have to decorate my use's to specify the class I'll be actually using.

And run a search for "@noinspection PhpUndefinedClassInspection" throughout my code.

Another way

The above problems stem from the fact that I have a "master" Customer class which is overridden by a "local" modification for the foobar client, whose Customers have (say) a special method.

The "correct" way of doing this should be to declare a FoobarCustomer which is only employed by foobar's code, and is a child class of Customer. Of course this is only possible if the child class is in my code, not in the framework's, and also I may need some methods in the parent class to be protected rather than private, which may make this solution either impossible or needful of Reflection:

/**
 * Verify an existing contract. Requires agent and supervisor.
 *
 * @param array         $data
 * @param FoobarCliente $cli
 * @param FoobarUser    $age
 * @param FoobarUser    $sup
 * @return Contratto
 */
private function contratto(array $data, FoobarCliente $cli, FoobarUser $age...

I had similar problem and it was quite annoying one. I was using Yii2 framework and as it turned out at the end I have accidentally created en extra "vendor" folder and composer.json in the root of the project (not in the root of the app) so I ended up with that warning as phpStorm was confused which extension folder is the right one. I've deleted extra vendor folder and it solved the problem.

Try delete duplicate declared libraries

Settings -> Languages & Frameworks -> PHP -> Include Path

enter image description here

I resolved this in my case by removing a more specific entry in my composer.json that included code by another more general entry

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top