Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP hint dynamic return type (based on $class param)

Is it possible that you hint the returned type of a PHP function/method, determined by one if it's arguments? Something like this:

/**
 * @param string $class
 * @param array $attributes
 * @return $class <- this doesn't work
 */
public function create($class, $attributes) {
    ... // finally returns object of type $class
}

I'm implementing a library that allows to create factory objects for unit tests. So I want to make it possible in the client code to have:

$user = $factory->create('Users', ['name' => 'John', 'email' => '[email protected]']);

And have autocompletion of object methods/attributes without having to add explicit PHPDoc comments in the client code like this:

/* @var $user Users */
$user = $factory->create('Users', ['name' => 'John', 'email' => '[email protected]']);

Any suggestions are most welcome! As long as I get it working in popular IDEs (PHPStorm, Eclipse, NetBeans), it's a valid solution.

like image 288
ddinchev Avatar asked Jan 07 '15 07:01

ddinchev


1 Answers

Try DynamicReturnTypePlugin. then you can add this configuration manually

like image 52
Yosef Tukachinsky Avatar answered Nov 19 '22 08:11

Yosef Tukachinsky