klesun / deep-assoc-completion

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

Consider current class' context for cross-referencing properties #194

Open 1234ru opened 2 years ago

1234ru commented 2 years ago

Let's say, we have an abstract class with two propertes.

First one's structure is undefined. It completely depends on particular future child class.

Second one's structure is defined and relies on the first one.

In that class we can declare a relation between these two properties. But unfortunately it doesn't recognize first property's structure if it is declared in a child class:

<?php

abstract class A {

    public $client;

    /**
     * @var = [ 
     *  'client' => static::$client
     * ] 
     */
    public $order;
}

class B extends A {
    /**
     * @var = [
     *     'name' => '',
     *     'phone' => '',
     *     'email' => '',
     * ]
     */
    public $client;
}

$obj = new B();

$obj->order['client']['']; // Doesn't suggest 'name', 'phone' or 'email' :(

Is it possible to fix that?

klesun commented 2 years ago

Hm, can't you achieve what you want with Psalm's @template type parameters? A moment...

/**
 * @template T
 */
abstract class A {
    /**
     * @var T
     */
    public $client;

    /**
     * @var array{
     *     client: T
     * }
     */
    public $order;
}

/**
 * @extends A<array{
 *     name: '',
 *     phone: '',
 *     email: ''
 * }>
 */
class B extends A {
}
klesun commented 2 years ago

Hm, it seems to be possible in Psalm's notation, but deep-assoc-completion does not support the @extends tag sadly.

PR welcome

The code responsible for psalm types resolution is located at PsalmRes.java