简体   繁体   中英

PhpStorm code completion based on parameters

I am wondering, if it's possible to have code completion based on parameters passed to a method.

Example:

class One { public function foo(){} }
class Two { public function bar(){} }
class DI {
    public function get($name) {
        if ($name === 'foo') {
            return new One();
        } elseif ($name === 'bar') {
            return new Two();
        }
    }
}

(new DI())->get('one')-> offers me both foo and bar

is there a way, how to tell PhpStorm, that if name is one, I well get class One ?

Sure -- have a look at Advanced Metadata support .

Please note that this kind of metadata will only work since PhpStorm v9 (just released yesterday) as v8 only supports static factories.

For example:

<?php
namespace PHPSTORM_META {
    $STATIC_METHOD_TYPES = [
        \DI::get('') => [
            "foo" instanceof \One,
            "bar" instanceof \Two,
        ]
    ];
}

PS

In your code (inside method get() ) you are using foo and bar as keys to define what class to create .. but when using actual DI->get() you are using class names as keys (eg one ). I'm sure it's just a typo .. but still -- next time try to provide valid code as an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM