Add proper generic type annotations (fixes missingType.generics)#19200
Add proper generic type annotations (fixes missingType.generics)#19200dereuromark wants to merge 13 commits intocakephp:5.xfrom
Conversation
…enerics This is a work-in-progress to properly specify generic types instead of ignoring the missingType.generics errors in PHPStan. Changes include: - Add @template annotations to Helper, View subclasses, Cache classes - Add proper generic type parameters to method signatures - Fix Collection, ORM, Event system generic types Progress: ~210 of 405 errors fixed (52%) Remaining work needed for complete fix.
Complete the fix for generic type annotations by: - Add generic types to Association.php methods (attachTo, find, etc.) - Add @phpstan-ignore for self-referential generics in EventDispatcherTrait - Fixes all remaining missingType.generics errors Remaining 69 errors are different types (return.type, argument.type, generics.interfaceConflict) not related to the original missingType.generics issue.
| * @phpstan-ignore missingType.generics | ||
| */ | ||
| public function dispatchEvent(string $name, array $data = [], ?object $subject = null): EventInterface | ||
| public function dispatchEvent(string $name, array $data = [], ?object $subject = null): EventInterface // @phpstan-ignore missingType.generics |
There was a problem hiding this comment.
I'd rather add these kind of ignore statements via the basline file instead of doing them inline
There was a problem hiding this comment.
I wanted to make them visible first, maybe we can fix some of them up.
Then we can still move them to baseline.
- Add @phpstan-ignore comments for Collection return type mismatches - Add path-specific ignores for generics.interfaceConflict in TreeIterator/TreePrinter - Add path-specific ignores for argument.type covariance issues in Cache/ORM - Use consistent SelectQuery generic types in Association.php
- Cache.php: inline ignore for NullEngine fallback - TableRegistry.php: inline ignore for TableLocator - bootstrap.php: inline ignore for TableLocator generics.interfaceConflict must remain in config (PHPStan limitation for class-level interface conflicts)
|
@rochamarcelo what do you think? |
src/Console/Command/HelpCommand.php
Outdated
| /** | ||
| * Print out command list | ||
| * | ||
| * @extends \Cake\Console\BaseCommand<\Cake\Console\Command\HelpCommand> |
There was a problem hiding this comment.
This is a bit strange, but if it works , it works.
|
It looks good. Autocomplete works for collections on PHPStorm when using the result of I got this with SelectQuery: ' variable $query contains generic class Cake\ORM\Query\SelectQuery but does not specify its types: TSubject' |
- Remove unused import EntityInterface from EagerLoader - Remove @inheritdoc when combined with @param/@return (coding standard violation) - Add proper type hints and @param annotations
f195061 to
6650705
Compare
|
Fixed! Using |
|
Okay, I didn't expected the trait requires to override Looks good. |
|
@rochamarcelo To fix the PHPStan error about /** @var \Cake\ORM\Query\SelectQuery<\App\Model\Entity\YourEntity> $query */
$query = $this->YourTable->find();Or for array results (when using /** @var \Cake\ORM\Query\SelectQuery<array> $query */The |
|
So, how do we move forward with the inline ignores? |
|
I prefer a baseline file but no hard opnion on this. Btw, about dispatchEvent not sure if is relevant but in any class we can use any type as subject not only the main class, for example in a command I could have |
…view Suggestions for generic usage on view
|
@rochamarcelo Thx, merged in. |
|
@dereuromark I forgot to update command dereuromark#2 |
* use static instead of self used generic template * use static instead of self used generic template
|
@ADmad How shall we proceed here? |
|
I'd say if we get the CI green this is fine to merge since its a major step into better static analysis |
We can merge once CI is green. |
- Resolve merge conflict in CollectionTrait.php by keeping both generic templates and @require-implements annotation - Fix Association::find() return type to SelectQuery<EntityInterface|array> to be compatible with BelongsToMany::find() override
Summary
This PR adds proper generic type annotations to fix PHPStan
missingType.genericserrors instead of ignoring them globally.Progress
The 69 remaining errors are different types (return.type, argument.type, generics.interfaceConflict) not related to the original
missingType.genericsignore rule.Files Modified
40 files across these subsystems:
Approach
For each generic class/interface:
@templateannotation to define type parametersClassName<TemplateType>in PHPDoc@param,@return,@var@extends/@implementsannotations to subclasses@phpstan-ignore missingType.genericsonly for truly self-referential casesStatus
Ready for review. All
missingType.genericserrors are fixed, allowing removal of the global ignore rule fromphpstan.neon.dist.