Method not found in class when using traits

If a class uses a trait that has a method accessible through that class when the trait is used, is there a way to get PHPStorm v8 to detect that method through the trait so I don't get the "Method not found in class" hover error for Intellisense?

12 comments
Comment actions Permalink

This support was added in phpDocumenter2:
https://github.com/mvriel/phpDocumentor2/commit/a56f7f2fa2bf2fefc49d03b4ba14ccefdc7a5673

Is there a way to get it to work in PHPStorm the same way?

0
Comment actions Permalink

Hi there,

Please provide some code that illustrates such issue -- I cannot figure out what is wrong from your description alone.

0
Comment actions Permalink

For instance, suppose I have these two traits each in their own files:

namespace My\Place\Traits;

trait Foo

{

public function someThing() { /*...*/}

}

namespace My\Place\Traits;

trait Bar

{

public function someThingElse() { /*...*/}

}

And I have this class in it's own file:

namespace My\Place;

use My\Place\Traits\Foo;

use My\Place\Traits\Bar;

class Baz

{

use Foo, Bar;

public function someMethod() { /* ... */ }

}

Then if I type out the following I get code hinting and other IDE magic:

$baz = new Baz;

$baz->someMethod();

However, I get no code hinting on either trait based method etc when I do this:

$baz = new Baz;

$baz->someThing();

$baz->someThingElse();

0
Comment actions Permalink

1) What is your IDE version exactly (Help | About)

2) Please show a screenshot for the same code of how it looks on your computer. It's a simple code example and it works fine here:

screen01.png

P.S.
I've put those 2 traits in one file (in case if it matters)

0
Comment actions Permalink

This was my error. I just noticed that the class is dynamically assigned and that is why the IDE can't detect the traits methods because it doesn't have a clear picture of what the class is going to be.

0
Comment actions Permalink

OK


In any case: please provide some simple example -- maybe there could be something done (via extra PHPDoc comment or something) ... just try it on your computer first.

0
Comment actions Permalink

The object is created inside a loop based on a dynamic value determined at the time of a database query. How would I add a PHPdoc comment to something like this within a method:

     if (!empty($syncs[$region]))
$Obj = $syncs[$region]; // This is always the same class, pre-initialized elsewhere in the site, but the init values just differ.
0
Comment actions Permalink

Since I do not have a clear picture I will be guessing on what is going on.

But generally speaking:
/** @var MyClass $myVariable */

I assume you need to typehint (give a hint for IDE) $Obj? If so:

if (!empty($syncs[$region])) {
    /** @var MyClass $Obj */

    $Obj = $syncs[$region];
}

0
Comment actions Permalink

You guessed right. That is exactly what needed to be done! Thanks!

0
Comment actions Permalink

I'm having this same problem but with Upsource.

PhpStorm seems to be code-completing and recognizing trait use just fine.

But Upsource code intelligence keeps complaining `Method 'get_integration_instance' not found in \My\Namespace\MyClass`. It can clearly locate `MyClass` correctly, but it can't find the trait?

Is this an Upsource bug to file? We're running Build 2019.1.1432, which is one step behind latest.

FIXED it turns out I had to add the right project to Upsource project's dependency list! Trait was coming from a library that was a theme dependency, so I had to remove library and add the theme instead.

0
Comment actions Permalink

Leho

TBH -- no idea (not using it myself).

If it would be me, I would submit Support Ticket first (see Submit a Request link at the top of this page) and then Upsource Issue Tracker if needed.

0
Comment actions Permalink

Thanks for replying Andriy, it was a user error after all. I edited my original post with the solution some minutes ago.

1

Please sign in to leave a comment.