dereuromark / cakephp-ide-helper

IDE Helper plugin for CakePHP
MIT License
186 stars 39 forks source link

Model relation option keys autocomplete #346

Open dereuromark opened 9 months ago

dereuromark commented 9 months ago

Any idea on how to allow IDE (PHPStorm?) to autocomplete the 2nd arg for options array, the keys?

        $this->hasMany('ADmad/SocialAuth.SocialProfiles', [
            'foreignKey' => 'user_id',
           ...
        ]);

These are the possible keys per type:

/**
 * @var array<string, string>
 */
protected array $optionKeys = [
    '\\' . self::CLASS_TABLE . '::belongsTo' => [
        'className',
        'foreignKey',
        'bindingKey',
        'conditions',
        'joinType',
        'dependent',
        'cascadeCallbacks',
        'propertyName',
        'strategy',
        'finder',
    ],
    '\\' . self::CLASS_TABLE . '::hasOne' => [
        'className',
        'foreignKey',
        'bindingKey',
        'conditions',
        'joinType',
        'propertyName',
        'strategy',
        'finder',
    ],
    '\\' . self::CLASS_TABLE . '::hasMany' => [
        'className',
        'foreignKey',
        'bindingKey',
        'conditions',
        'sort',
        'dependent',
        'cascadeCallbacks',
        'propertyName',
        'strategy',
        'saveStrategy',
        'finder',
    ],
    '\\' . self::CLASS_TABLE . '::belongToMany' => [
        'className',
        'joinTable',
        'foreignKey',
        'bindingKey',
        'targetForeignKey',
        'conditions',
        'sort',
        'dependent',
        'through',
        'cascadeCallbacks',
        'propertyName',
        'strategy',
        'saveStrategy',
        'finder',
    ],
];
dereuromark commented 9 months ago

Maybe a custom IDE plugin could help? With e.g. similar syntax for array shapes PHPStan/Psalm use?

dereuromark commented 9 months ago
     * @param array{
     *   className: string,
     *   targetTable: string,
     *   foreignKey: string,
     *   webroot: string,
     *   dependent: bool,
     *   cascadeCallbacks: bool,
     *   conditions: array,
     *   sort: array,
     *   saveStrategy: string,
     *   finder: string,
     *   sourceTable: string
     * } $options List of options to configure the association definition
     */
    public function hasMany(string $associated, array $options = []): HasMany

seems to work for autocomplete, but that list makes it finite in options afaik, and there could always be more (valid) keys. That syntax doesnt then quite work I guess.

It also makes the docblock line super loong, Would be nice if there was a way outside of the param docblock line directly.

Also, PHPStan doesnt like it:

1161 Default value of the parameter # 2 $options (array{}) of method Cake\ORM\Table::hasMany() is incompatible with type array{className: string, targetTable: string, foreignKey: string, webroot: string, dependent: bool,
cascadeCallbacks: bool, conditions: array, sort: array, ...}.
💡 Array does not have offset 'className'.