Closed dereuromark closed 8 months ago
About the $data['price'] = $articleSet->price; // Doesnt recognize it, PHPStorm does (ArticleSet|EntityInterface)
I think phpstan should complain since EntityInterface does not have price property only ArticleSet
About the relations annotation, I expect to not have issue with Intersection types, the doc says it is supported https://phpstan.org/blog/union-types-vs-intersection-types .
In a perfect world would be nice to have a phpdoc saying it is a HasMany object containing ArticlesTable object, we have to think of a good solution.
We know that get() on that table with that entity should return the concrete object We should make PHPStan believe that too, it should not error as the pure amount of needed inline annotations is already crazy and we shouldnt be forced to add more where the results are obvious IMO As such, we should make sure that get() only returns the concrete object as already somewhat specified in annotation.
I mean if you have an annotation saying it may return ArticleSet|EntityInterface phpstan won't know that EntityInterface.price exists.
We could ignore the annotation and add a custom logic to detect the type as we do to detect if a custom table class exists, keep in mind that we probably can't cover the cases when someone use a non-convetional class name for entity.
Addittionally I was researching about generics annotation with phpstan, maybe it can improve the CakePHP core annotations with generics for entity class, and table class for associations, then we could have these in ours models.
@template Entity of \App\Model\Entity\Article
/
@property HasMany<\App\Model\Table\ArticlesTable> $Articles
Anyway, these are just some ideas, I don't have proof that adding generics annotation will work.
Custom logic sounds fair enough for now
I prepared https://github.com/dereuromark/cakephp-ide-helper/pull/335
What implications does this have on existing code and PHPStans ability to understand it?
This should fix issues related to Symplify\PHPStanRules\Rules\Explicit\NoMixedPropertyFetcherRule
and cover cases:
$user = $this->Notes->Users->get($note->user_id);
$user->role = 'user';
$this->Notes->User->myCustomFunction();
Now you mentioned cakephp-ide-helper I noticed that PHPStorm autocomplete is partially working, it works fine for custom methods inside your table but fail to understand the custom @method annotations and with entity properties. This is odd because some time it shows the correct method and sometime show multiple method.
Here it shows the custom @method save defined in AppUsersTable but PHPStorm fails to understand $user->role:
Tested on PhpStorm 2023.2.4 I believe I started to get errors after Invalidated cache. Could you check if PHPStorm autocomplete works fine for you @dereuromark ?
Yes, the new syntax would break IDE autocomplete, so it seems like rather a too strong issue compared to the small benefit of less inline annotations for PHPStorm. I wonder if PHPStorm is already aware of it.
Yes. Trait supports was add on the latest version 2023.2 (It is not available on 2023.1) It seems the feature is still in development. I've just an issue report
I am on 2023.2.3
I can confirm that the phpstan run is now OK, once I used my branch of IdeHelper to auto-change all to new syntax. But then I lose all IDE compatibility, so its a no go for now.
We should open a ticket with PHPStorm to fast track this there.
@dereuromark have this one https://youtrack.jetbrains.com/issue/WI-75239/Autocomplete-and-go-to-declaration-does-not-work-properly-with-mixin-and-generics ~5 days ago and no updated from them , I think they prioritize the ones with high votes, so please vote
Upvoted
I merged https://github.com/dereuromark/cakephp-ide-helper/commit/33d8f43327f9666cf2e4acb53ef79bb7d810779d in, but with a feature flag to manually enable, as this is probably not usable as is for now.
PHPStorm 2023.3 was just released a few days ago. We should check if things got better.
PS: There is also https://youtrack.jetbrains.com/issue/WI-63261/Generics-support-by-passing-template-TValue-into-template-implements-IteratorAggregateint-TValue which seems a bit related to.
At https://github.com/CakeDC/cakephp-phpstan/blob/feature/cakephp-rules-001 I've added an 'extension' to fix the bad intersection phpDoc to the new one (generic Association
I added the following rules to more clearly see where PHPStan doesnt see the proper object type:
Turns out almost none of the annotations actually work out
Also the relations annotations dont seem to work yet:
Makes it not find it anymore
Fails with
It only works if we remove the relation part and only keep the table name:
Should we then annotate it without the relation part?