feat(phpstan): foundation for usage in extensions - #3666
Conversation
b4f0f8c to
182aac5
Compare
Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
Stops using `dates` as it's deprecated in laravel 8 Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
023ce97 to
ffaf722
Compare
| private function findCastAttributeMethod(ClassReflection $classReflection, string $propertyName): ?MethodCall | ||
| { | ||
| foreach ($this->extendersResolver->getExtenders() as $extender) { | ||
| if (! $extender->isExtender('Model')) { |
There was a problem hiding this comment.
Maybe this string would make sense to put in a constant somewhere? Or could we reflect the model itself to get the unqualified class name?
| } | ||
|
|
||
| foreach (array_merge([$classReflection->getName()], $classReflection->getParentClassesNames()) as $className) { | ||
| if ($className === 'Flarum\Database\AbstractModel') { |
There was a problem hiding this comment.
Same here, might be cleaner to put all these strings in one place. Deriving it from the class would be nice, but I assume impossible since then phpstan ext would need to depend on core
| use PHPStan\Type\ObjectType; | ||
| use PHPStan\Type\UnionType; | ||
|
|
||
| class ModelDateAttributesExtension implements PropertiesClassReflectionExtension |
There was a problem hiding this comment.
There's a lot of code repetition; maybe we should consider some abstract class on top of PropertiesClassReflectionExtension for finding extenders?
There was a problem hiding this comment.
We can further iterate on this 👍🏼
| $this->paths = $paths; | ||
| } | ||
|
|
||
| public function getExtenderFiles(): array |
There was a problem hiding this comment.
Sometimes I wish PHP were Hack: https://docs.hhvm.com/hack/attributes/predefined-attributes#__memoize
Changes proposed in this pull request
In #3553, we added phpstan static code analysis, but only to core code. This PR aims to make the
flarum/phpstanpackage usable by extension repositories. Which means (at the moment) being able to pick up on new model relations and attributes.Static analysis is actually expanded to other extensions in a separate PR for ease of readability (#3667).
There are two crucial changes/additions introduced here.
Model::cast()ExtenderA new
cast(string $name, string $type)extender is introduced, which allows adding type casting to attributes added to existing models from extensions. This provides three advantages:dateAttributeextender, since laravel has deprecated the use of the$datesproperty in models for date attributes, and recommends using$castsinstead. I hope in another PR to also change model classes from directly using$datesproperty.Extending PHPStan
This PR also extends phpstan so that it can tell from
extend.phpfiles what relations and attributes are added to existing models, so that they aren't reported as unknown.extend.phpfiles are parsed and currently the package looks up calls tocastandhasOnebelongsTohasManybelongsToManymethod calls to inform the phpstan utility about them.Reviewers should focus on:
castmodel extender.dateAttributeextender.Follow-up Tasks
Necessity
Confirmed
composer test).