How to manage traits and blind warnings

Hello

PHP langage cannot type a trait as a interface implementer.

It causes strange behaviour in the Phpstorm IDE, when perfect valid code has some alerts.

Here is an exemple. Let's assume we cannot refactor the code. Have you any workaround, tips (phpdoc, plugins... ? ) to solve this wrong alerts ?

----------------------------------------

interface Hi_Interface
{
   public function Hi_Process() ;
}

trait Hi_Trait
{
   //Unused element Hi_Process Phpstorm alert
   public function Hi_Process() {
      //Phpstorm alert : Expected element Hi_Interface, get Hi_Trait
      Logger::Log($this);
      print "Hi";
   }

}

class Hi_Implementation implements Hi_Interface {
   use Hi_Trait ;
}

class Logger {

   static public function Log(Hi_Interface $hi){
      print "Hi number ".spl_object_id($hi)." is gonna come";
   }
}

class Operator {

   public function Launch()
   {
      $a = new Hi_Implementation();
      $this->Operate($a);
   }

   private function Operate(Hi_Interface $a)
   {
      $a->Hi_Process();
   }

}

$o = new Operator();
$o->Launch();

0
3 comments

I know that it does not sound like a nice solution but you may suppress these alerts with 

/** @noinspection PhpParamsInspection */

construction before the statement.

0

Hello Vasily

First, a global question : how to paste code here in this forum ? I don't see any tag to have a clear formating like you did... sorry for mine

For the "Expected element Interface, get Trait" I have found something somewhere graceful : 

/* var @Hi_Interface $this */

to be inserted just before the problem line. I don't know if i'm exploiting a jetbrains bug here, but I think my solution is clear and clean.

For the unused element, I don't want to do a inspection bypass, I like inspection , and I don't think my team will agree too. I just want the inspection to be honest.

Do you think it's a Jetbrains bad behaviour, or do you think it's a PHP inherent issue ?

0

Hello Emmanuel,

Sorry, I have missed your comment.

For instance, you may paste code with "Code" style paragraph (the big T icon on a toolbar above).

Actually, there is a bunch of tickets where IDE is showing "expected N, get M" and each of those requires an additional investigation and it would be better to submit it on our YouTrack portal:

https://youtrack.jetbrains.com/newIssue

Here is one example:

https://youtrack.jetbrains.com/issue/WI-44718

0

Please sign in to leave a comment.