Skip to content

Update getBehavior annotation to allow better code analyse on applica… #18323

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

Merged
merged 4 commits into from
Apr 13, 2025

Conversation

rochamarcelo
Copy link
Contributor

Related to CakeDC/cakephp-phpstan#44 and #18318

Add annotation to improve static analyse.

Application models will be able to use a phpdoc to set possible return types for Table::getBehavior

/**
 * Usuarios Model
 *
 * @extends \Cake\ORM\Table<array{Register: \CakeDC\Users\Model\Behavior\RegisterBehavior, Filterable: \PlumSearch\Model\Behavior\FilterableBehavior, MyC: \App\Model\Behavior\PasswordBehavior, Tree: \App\Model\Behavior\TreeBehavior}>
 */
class UsersTable extends Table
{
    use DefaultOrderTrait;

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->addBehavior('CakeDC/Users.Register');
        $this->addBehavior('MyC', ['className' => 'Password']);
        $this->addBehavior('Tree');
    }

    public function register()
    {
          //This will not result in phpstan error
          debug($this->getBehavior('Filterable')->filters());
    }
}

@rochamarcelo
Copy link
Contributor Author

rochamarcelo commented Apr 13, 2025

At model I've used @phpstan-extends instead of @extends, since we have that in Cake\ORM\Table class

@rochamarcelo
Copy link
Contributor Author

Using @phpstan-return still doens't work perfect with PHPStorm. Psalm also complains about return type.

@dereuromark
Copy link
Member

For PHPStorm the IdeHelper and meta file helps.
Does it work for PHPStan so far? I would say we can then ignore the psalm one, that tool should be decommissioned anyway.

@markstory markstory added this to the 5.2.2 milestone Apr 13, 2025
@ADmad
Copy link
Member

ADmad commented Apr 13, 2025

Using @phpstan-return still doens't work perfect with PHPStorm. Psalm also complains about return type.

Pretty much all popular stan tools and IDEs understand annotation template vars. So using un-prefixed annotations is fine. Also for consistency TBehaviors<TName> instead TBehaviors[TName].

@ADmad
Copy link
Member

ADmad commented Apr 13, 2025

We already have multiple use of @template in our codebase.

@dereuromark
Copy link
Member

Using @phpstan-return still doens't work perfect with PHPStorm. Psalm also complains about return type.

Pretty much all popular stan tools and IDEs understand annotation template vars. So using un-prefixed annotations is fine. Also for consistency TBehaviors<TName> instead TBehaviors[TName].

This is the first indirect case (trick for getBehavior).
And in general it would still be nice if we knew the direct types.

@rochamarcelo
Copy link
Contributor Author

rochamarcelo commented Apr 13, 2025

Using @phpstan-return still doens't work perfect with PHPStorm. Psalm also complains about return type.

Pretty much all popular stan tools and IDEs understand annotation template vars. So using un-prefixed annotations is fine. Also for consistency TBehaviors<TName> instead TBehaviors[TName].

TBehaviors[TName] is used to match defined behaviors, can't be replaced with TBehaviors<TName>

@rochamarcelo
Copy link
Contributor Author

Please let me know if we should use phpstan- prefix

@dereuromark
Copy link
Member

Not for template, and for the others also not if it doesnt work any other way.
If it works already keeping the old param/return and adding phpstan-param/phpstan-return on top, that would be perfect and increases compatibility in general.

@rochamarcelo
Copy link
Contributor Author

At PHP Storm both ways have the same behavior. It can't autcomplete methods from concrete behavior class only from \Cake\ORM\Behavior.

@dereuromark
Copy link
Member

PHPStorm can itself also autocomplete, through the meta file, thats a PHPStorm internal separate topic. at least that works for sure, if we keep the old param/return.

So right now "working" I mean the new PHPStan introspection, that ideally works with prefixed param/return then.
And psalm doesnt work either way, so we can ignore that one (see first commit).

@rochamarcelo
Copy link
Contributor Author

Updated. Tested at localhost with phpstan and tag generated with cakephp-ide-helper.

@dereuromark
Copy link
Member

OK, I needed to run phpstan with --debug to clear the cache
Then it works with

 * @param string $name The behavior alias to get from the registry.
 * @return \Cake\ORM\Behavior
 * @template TName of key-of<TBehaviors>
 * @phpstan-param TName $name The behavior alias to get from the registry.
 * @phpstan-return TBehaviors[TName]

Just fine for me

I also verified that PHPStorm still can autocomplete through meta file, also works

override(
		\Cake\ORM\Table::getBehavior(),
		map([
			...
		]),
);

Takes care of that

Copy link
Member

@dereuromark dereuromark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably silence the psalm issue.
Or add a psalm-* tag on top that specifically sets it for psalm to the expected behavior.

@rochamarcelo
Copy link
Contributor Author

psalm tests passed with @psalm-return \Cake\ORM\Behavior

@dereuromark
Copy link
Member

dereuromark commented Apr 13, 2025

dereuromark/cakephp-ide-helper#374
is also done.

It would by default add the annotations only for 5.2.2+ (since before it would throw errors)
you can silence the old mixins or display both, ... full options if needed.

* @template TName of key-of<TBehaviors>
* @phpstan-param TName $name The behavior alias to get from the registry.
* @phpstan-return TBehaviors[TName]
* @psalm-return \Cake\ORM\Behavior
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psalm-return looks the same as return.

Copy link
Member

@dereuromark dereuromark Apr 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but see the commits above. If not added on top, psalm goes all ballistic on us.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed because psalm reads @phpstan- prefixed annotations too (and vice versa) and it doesn't understand TBehaviors[TName], hence @psalm-return is also required to override the @phpstan-return.

@markstory markstory merged commit 417b769 into cakephp:5.x Apr 13, 2025
13 checks passed
@rochamarcelo rochamarcelo deleted the feature/get-behavior-annotation branch April 14, 2025 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants