Phpstan: PHPStan emits errors for Trait properties

Created on 17 Jun 2017  Β·  2Comments  Β·  Source: phpstan/phpstan

First, I want to say that I'm really impressed and thankful for your work on PHPStan! I'm starting to use it in my projects, it already helped me find a bunch of genuine issues. So far there is just one issue that I have found in code that I believe is perfectly fine. Consider the following example:

// traitbug/Awesome.php
namespace traitbug;

class Awesome
{
    public $someProp = 'some-prop-indeed';
}

// traitbug/AwesomeTrait.php
namespace traitbug;

/**
 * @property Awesome $awesome
 */
trait AwesomeTrait
{

}

// traitbug/TraitedClass.php
namespace traitbug;

class TraitedClass
{
    use AwesomeTrait;

    public function getTraitProp()
    {
        return $this->awesome->someProp;
    }
}

$ vendor/bin/phpstan analyse traitbug
 3/3 [β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“] 100%

 ------ ------------------------------------------------------------------
  Line   traitbug/TraitedClass.php
 ------ ------------------------------------------------------------------
  11     Access to an undefined property traitbug\TraitedClass::$awesome.
 ------ ------------------------------------------------------------------


 [ERROR] Found 1 error

If however the $awesome property doc is added to the TraitedClass, it would work fine.

/**
 * @property Awesome $awesome
 */
class TraitedClass
{
    use AwesomeTrait;

    public function getTraitProp()
    {
        return $this->awesome->someProp;
    }
}

// all is green now

In my opinion, PHPStan should consider the trait property PHP docs as if they were added to the class using the trait itself. PHPStorm respects trait property docs correctly and the code works exactly the same.

I also would be happy to contribute the fix if it's accepted as such (bug or incorrect behavior) and if roughly pointed to the changes required.

Most helpful comment

I agree, will fix it.

On Sat, 17 Jun 2017 at 14:55, Dimitar Dinchev notifications@github.com
wrote:

Consider the following example:

// traitbug/Awesome.phpnamespace traitbug;class Awesome{ public $someProp = 'some-prop-indeed';}// traitbug/AwesomeTrait.phpnamespace traitbug;/** * @property Awesome $awesome */trait AwesomeTrait{}// traitbug/TraitedClass.phpnamespace traitbug;class TraitedClass{ use AwesomeTrait; public function getTraitProp() { return $this->awesome->someProp; }}$ vendor/bin/phpstan analyse traitbug 3/3 [β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“] 100% ------ ------------------------------------------------------------------ Line traitbug/TraitedClass.php ------ ------------------------------------------------------------------ 11 Access to an undefined property traitbugTraitedClass::$awesome. ------ ------------------------------------------------------------------ [ERROR] Found 1 error

In my opinion, PHPStan should consider the trait property PHP docs as if
they were added to the class using the trait itself. PHPStorm respects them
and the code works as expected.

β€”
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/phpstan/phpstan/issues/340, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuIrHZvKwbNOElPaC_m-09LuuI4K4ks5sE8y2gaJpZM4N9N96
.

>

OndΕ™ej Mirtes

All 2 comments

I agree, will fix it.

On Sat, 17 Jun 2017 at 14:55, Dimitar Dinchev notifications@github.com
wrote:

Consider the following example:

// traitbug/Awesome.phpnamespace traitbug;class Awesome{ public $someProp = 'some-prop-indeed';}// traitbug/AwesomeTrait.phpnamespace traitbug;/** * @property Awesome $awesome */trait AwesomeTrait{}// traitbug/TraitedClass.phpnamespace traitbug;class TraitedClass{ use AwesomeTrait; public function getTraitProp() { return $this->awesome->someProp; }}$ vendor/bin/phpstan analyse traitbug 3/3 [β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“] 100% ------ ------------------------------------------------------------------ Line traitbug/TraitedClass.php ------ ------------------------------------------------------------------ 11 Access to an undefined property traitbugTraitedClass::$awesome. ------ ------------------------------------------------------------------ [ERROR] Found 1 error

In my opinion, PHPStan should consider the trait property PHP docs as if
they were added to the class using the trait itself. PHPStorm respects them
and the code works as expected.

β€”
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/phpstan/phpstan/issues/340, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuIrHZvKwbNOElPaC_m-09LuuI4K4ks5sE8y2gaJpZM4N9N96
.

>

OndΕ™ej Mirtes

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vhenzl picture vhenzl  Β·  3Comments

mitelg picture mitelg  Β·  3Comments

hrach picture hrach  Β·  3Comments

jiripudil picture jiripudil  Β·  3Comments

olivernybroe picture olivernybroe  Β·  3Comments