$this->request is red underlined in Trait

Hello,

In a Laravel 5.2 project I have a BaseController like this:

class BaseController extends Controller
{
protected $messages;
protected $request;

public function __construct(Request $request)
{
$this->request = $request;
}
}

All other Controllers inherit from this

Then I have a Trait that I use in some Controller(s)

trait SortableTrait
{
public function _sortable($default_sort, $allowed_sort, $default_order = 'asc')
{
$allowed_order = ['asc', 'desc'];
$sort = in_array($this->request->get('sort'), $allowed_sort) ? $this->request->get('sort') : $default_sort;
$order = in_array($this->request->get('order'), $allowed_order) ? $this->request->get('order') : $default_order;
$params = compact('sort', 'order'); // create the array
$this->request->merge($params); // for default icon on load
return $params;
}
}

Well, the part:

$this->request->...

is treated as a error in PHPStorm (I get a 'Member has protected access' warning when I hover the 'request' word) and the file / folder are red underlined. Anyway, the application works as expected.

Problem is gone when I change:

protected $request;

to

public $request;

but I would avoid this...

So, is this a PHPStorm bug? Is there a way to avoid the annoying red underline?

Thanks

 

5 comments

I have the same issue using EAP PhpStorm-162.646.18

A trait can expose members (properties and methods) to the class that uses it, and also can use the methods and properties from the class.

Looks like the problem is that the trait can only access public members and not protected or private.

This is a case that shows the problem.
(1) TSomeOne shows the error until is used inside AFoo
(2) TSomeTwo shows the error until the annotation @mixin is set

<?php

interface ISome
{
public function dosome();
}

abstract class AbsSome implements ISome
{
protected $varProtected;
protected function methodProtected() { echo __METHOD__, "\n"; }
}

trait TSomeOne
{
public function dosome()
{
echo __METHOD__, "\n";
$this->methodProtected(); // member has protected access
return $this->varProtected; // member has protected access
}
}

class AFoo extends AbsSome
{
use TSomeOne;

public function othersome()
{
echo __METHOD__, "\n";
$this->methodProtected(); // member access is OK
return $this->varProtected; // member access is OK
}
}

$foo = new AFoo();
$foo->othersome();
$foo->dosome();

/* This is the return, no errors were found

AFoo::othersome
AbsSome::methodProtected
TSomeOne::dosome
AbsSome::methodProtected
*/

/** @mixin AbsSome */
trait TSomeTwo
{
public function dosome()
{
$this->methodProtected(); // member has protected access
return $this->varProtected; // member has protected access
}
}
0

This is https://youtrack.jetbrains.com/issue/WI-30855

Please vote for it to move it in a queue and receive notifications regarding the progress of the issue.

0

Has anyone solved this issue? 

0

Just found that adding a docblock with `@property Type $var` solves ("hides") the inspection error.

0

Please sign in to leave a comment.