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?
请先登录再写评论。
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?
Hi there,
Please provide some code that illustrates such issue -- I cannot figure out what is wrong from your description alone.
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();
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:
P.S.
I've put those 2 traits in one file (in case if it matters)
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.
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.
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]))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:
You guessed right. That is exactly what needed to be done! Thanks!
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.
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.
Thanks for replying Andriy, it was a user error after all. I edited my original post with the solution some minutes ago.