PHP Class properties not recognized

As of newest PhpSorm update 2021.2, class properties with phpdoc @property comments are not recognized. Usually class properties are colored in different color.

Maybe it is because now IDE thinks it is two different properties?

 

4 comments
Comment actions Permalink

Hi there,

PHPDoc's @property tag is meant for magic (not real / dynamic) fields only (that are getting accessed via __get() / __set() methods)

If you need to typehint your $bar real property then do it using appropriate @var tag: https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-tags.md#517-var

/** @var string My cool bar property */
public $bar;

P.S. Yes, it's a fix since 2021.2 version.

0
Comment actions Permalink

Thanks, 

So it seems, that I used @property tag incorrectly. It was handy that IDE changed attribute name color and was really easy to spot incorrect attribute names.

0
Comment actions Permalink

The IDE already tells you about wrong / non-existing properties:

<?php

/**
* @property string $meow
*/
class Foo4 {
public string $bark;
}

$a = new Foo4();
$a->bark = 'yes';
$a->meow = 'nope';
$a->oink = 4;

It will highlight such place with the notice (see the screenshot below).

You can increase the visibility by changing the severity of that inspection to a more noticeable warning (or even an error) level if needed. The style / how noticeable it is depends on your Color Scheme / Theme used.

 

BTW: If https://wiki.php.net/rfc/deprecate_dynamic_properties will be accepted then PHP 8.2+ will throw warnings during runtime on such dynamic property access (without __get/__set in place).

0
Comment actions Permalink

Thanks, I will change inspection severity to error.

0

Please sign in to leave a comment.