klesun / deep-assoc-completion

A phpstorm plugin for associative array key typing and completion
Other
267 stars 17 forks source link

Completion for params in factory with extends #204

Closed leiha closed 2 years ago

leiha commented 2 years ago

Hi first ty for the work :pray:

I try to have a completion in factory extended i tried many things but apparently nothing work :(

/**
 * @template T
 */
class SubFactory extends Factory {

    /**
     * @var array<T,bool>
     */
    protected array $ressources = [
        'index'  => true ,
    ];

    /**
     * @param T $name
     */
    public function allow( string $name ) : self {
        return parent::allow( $name );
    }   
}

There is a way to accomplish completion for param $name in allow method ?

klesun commented 2 years ago

Hi. Do you want the completion to be inferred from the $ressources field assigned value?

Afaik, PSALM type annotations don't provide type inference from value, not in the plugin at least.

What you could do, though, is to express the argument accepted string values through

@param $name = array_keys($this->ressources)[$any]
class SubFactory extends Factory {

    protected array $ressources = [
        'index'  => true ,
    ];

    /**
     * @param $name = array_keys($this->ressources)[$any]
     */
    public function allow( string $name ) : self {
        return parent::allow( $name );
    }
}

$subFactory = new SubFactory();
$subFactory->allow('');

image

klesun commented 2 years ago

You may find this document useful: https://github.com/klesun/deep-assoc-completion/issues/63

leiha commented 2 years ago

@klesun :pray: I had not understood how to work for my case despite the doc :( all examples had seemed for me "static" not really dynamic but maybe i'm blind ^^ it's works as you show me , perfect :)

leiha commented 2 years ago

@klesun i hope i'm not going to abuse :crossed_fingers: But can you also explain to me to have the right return type of the method ? ( With the doc i not understood again )

class SubFactory extends Factory {

    protected array $ressources = [
        'index'   => [ 'key1' => '' , 'key2' => ''   ] ,  
        'update' => Action\Update::class,            
    ];

    /**
     * @param $name = array_keys($this->ressources)[$any]
     * @return ??
     */
    public function allow( string $name ) : self {
        return parent::allow( $name );
    }
}

$subFactory = new SubFactory();
$subFactory->allow('');
klesun commented 2 years ago

Does it not work as is already? image

klesun commented 2 years ago

The : self seems to be sufficient for the plugin to understand that return value is the class

leiha commented 2 years ago

sorry my bad i modified example for reflect the idea

class SubFactory extends Factory {

    protected array $ressources = [
        'index'   => [ 'key1' => '' , 'key2' => ''   ] ,  
        'update' => Action\Update::class,            
    ];

    /**
     * @param $name = array_keys($this->ressources)[$any]
     * @return ??
     */
    public function get( string $name )  { // now the parent return item in array
        return parent::get( $name );
    }
}

$subFactory = new SubFactory();
$subFactory->allow('');
klesun commented 2 years ago

np

klesun commented 2 years ago

If you'll happen to want to use custom expressions in the return type, the syntax is following:

@return = array_values($this->ressources)[$any]

image

leiha commented 2 years ago

okay i'm idiot :pray: you're plugin is really usefull :+1: have nice day you come save mine ;)