3

Is there any possibility in PhpStorm to map the usage of dynamically generated functions between its declaration and usage?

Assume I have the next code:

<?php

class TestExample {
    
    public function __construct($component) {
        $component_parts = $this->get_dynamic_component_part_list($component);
        $this->load_component_parts($component, $component_parts);
    }

    private function get_dynamic_component_part_list($component){
        //Complex logic to get attached parts by $component
        $component_parts = array('part1', 'part2');
        return $component_parts;
    }

    private function load_component_parts(&$component, $component_parts) {
        foreach ($component_parts as $component_part) {
            $component[$component_part] = $this->{'load_' . $component_part}($component['id']);
        }
    }

    private function load_part1($id) {
        //Loading and prepare condition from one source 
        $part1 = new Part1($id);
        // Complex algorithm
        return $part1;
    }

    private function load_part2($id) {
        //Loading and prepare condition from another source 
        $part2 = new Part2($id);
        // Complex algorithm
        return $part2;
    }
}

class Part1 {
    
} 

class Part2 {

} 

I want to see usage of load_part1 and load_part2.

Is there any way to do it by usage phpDoc or in some other way?

At this moment PhpStorm notice me that this function doesn't have usage, but really it used in load_component_parts method.

enter image description here

Pavlo Zhukov
  • 3,007
  • 3
  • 26
  • 43
  • I know no way -- you are assembling method name dynamically during runtime -- it cannot be detected/evaluated using static analysis. Make them protected if you do not wish to see such warning... or suppress disable/inspection that Inspection for this file. – LazyOne Apr 22 '17 at 09:36
  • 1
    *Is that possibly that in future developers add that feature?* Like add phpDoc for example `/** @possible_method $this->load_part1 */` or code be better such as `/** @possible_components ['part1','part2'] @possible_method $this->load_$component$ */`. Except current project I faced with this type of method calling in `OpenCart` for example and showing usage of method calling in this way would be useful – Pavlo Zhukov Apr 22 '17 at 09:52
  • 2
    I personally doubt about it -- little to no real benefits. In any case -- you know where the Issue Tracker is where you can leave you Feature Request ticket. – LazyOne Apr 22 '17 at 10:10
  • @LazyOne Ok. I'll do it for a while. Thanks for conversation! – Pavlo Zhukov Apr 22 '17 at 10:29
  • @LazyOne please see [this question](http://stackoverflow.com/questions/43583559/merge-live-templates) – Pavlo Zhukov Apr 24 '17 at 08:49

1 Answers1

4

You can use the phpDoc annotation @see.

For example:

$className = 'SomeClass';
$method = 'methodToCall';
$anArgument = 'bar';

/** @see SomeClass::someMethod() */
$foo = call_user_func([$className, $method], $anArgument);

This annotation will create at least a reference to this code, so that you know to come back here when you review SomeClass::someMethod() before throwing the "unused" method away.

Sneakyvv
  • 136
  • 5