C
C
Cat Anton2018-05-08 00:32:53
phpstorm
Cat Anton, 2018-05-08 00:32:53

How to teach PhpStorm to understand that fields and methods are used dynamically?

If a variable, field or method in a class is used dynamically ( how is that? ), then PhpStorm will not be able to determine this in any way, as a result, for example, functions such as Find usages for a field in a class do not work.
Sample code snippet:

<?php

class Example {
    public $property1;
    public $property2;
    public $property3;
}

$obj = new Example();

// Случайным образом зададим значение
// в одном из трех полей (property1 / property2 / property3)
$name = 'property' . rand(1, 3);
$obj->{$name} = 'value';

If you then do Find usages for any of the properties of the Example class, then PhpStorm will not find anything.
Maybe there is some legal way, abstract or hack to teach him this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2018-05-08
@27cm

Option 1 . List all properties in a @seecomment

<?php

class Example {
    public $property1;
    public $property2;
    public $property3;
}

$obj = new Example();

/** @see Example::property1 */
/** @see Example::property2 */
/** @see Example::property3 */
$name = 'property' . rand(1, 3);
$obj->{$name} = 'value';

Option 2 . Use a function that does nothing.
<?php

function ide_usage(...$args) {
    // Функция ничего не делает, используется для тех редких случаев,
    // когда нужно показать IDE, что тут неявно используется переменная, свойство или метод
}

class Example {
    public $property1;
    public $property2;
    public $property3;
}

$obj = new Example();

ide_usage($obj->property1, $obj->property2, $obj->property3);

$name = 'property' . rand(1, 3);
$obj->{$name} = 'value';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question