Using @var type hinting with properties instantiated by Traits

The use of PHPdoc's @var comments plays an important role in PhpStorm type inspection.  Code warnings for missing methods and incorrect variable usage are triggered based on the type hints provided by @var.  It would be valuable to be able to access the same level of type hinting/checking when the variable in question is instantiated from a Php Trait (mixin type language macro).  Q:  is that possible with the current version of PhpStorm?

Playing around with it, the best I was able to achieve was turning off warnings with a generic Object declaration like so

trait StaticInterfaceFace {

/* @var Object $keyObject */ static $keyObject

}

class Something
{
use StaticInterface;
// $keyObject is always going to be a SomethingSpecial in class Something
static public function setKeyObject(SomethingSpecial $newObj)
{
self::$keyObject = $newObj
}

static public callASpecialMethod()
{
//I want type check as SpecialObject required here:
self::$keyObject->specialMethod();
}

Is there a way to do that?

 

Trait

0
<?php

trait StaticInterface
{
/**
* @var specialObject $keyObject
*/
static $keyObject;

}

class Something1
{
use StaticInterface;

// $keyObject is always going to be a SomethingSpecial in class Something

static public function setKeyObject(SomethingSpecial $newObj)
{

self::$keyObject = $newObj;
}

static public function callASpecialMethod()
{
//I want type check as SpecialObject required here:
self::$keyObject->specialMethod();
}
}

class SomethingSpecial{}

class specialObject{
public function specialMethod()
{

}
}

Did I got you right? That does work, you can test it on your end.

0
Avatar
Permanently deleted user

I want to use the trait in different classes and have "SpecialObject" be a different kind/class of object in different usages, but only one 1 different kind in each class where it is used - it is SpecialObject<T>, in C++/Java generics notation.  My use case is similar to the root "facade" object in Laravel "facades".

 

0

请先登录再写评论。