Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model relation option keys autocomplete #346

Open
dereuromark opened this issue Feb 25, 2024 · 2 comments
Open

Model relation option keys autocomplete #346

dereuromark opened this issue Feb 25, 2024 · 2 comments

Comments

@dereuromark
Copy link
Owner

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
Copy link
Owner Author

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

@dereuromark
Copy link
Owner Author

dereuromark commented Feb 26, 2024

     * @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'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant