-
-
Notifications
You must be signed in to change notification settings - Fork 836
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
feat(phpstan): foundation for usage in extensions #3666
Conversation
b4f0f8c
to
182aac5
Compare
Signed-off-by: Sami Mazouz <[email protected]>
Signed-off-by: Sami Mazouz <[email protected]>
Stops using `dates` as it's deprecated in laravel 8 Signed-off-by: Sami Mazouz <[email protected]>
Signed-off-by: Sami Mazouz <[email protected]>
Signed-off-by: Sami Mazouz <[email protected]>
Signed-off-by: Sami Mazouz <[email protected]>
Signed-off-by: Sami Mazouz <[email protected]>
Signed-off-by: Sami Mazouz <[email protected]>
Signed-off-by: Sami Mazouz <[email protected]>
Signed-off-by: Sami Mazouz <[email protected]>
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can further iterate on this 👍🏼
$this->paths = $paths; | ||
} | ||
|
||
public function getExtenderFiles(): array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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/phpstan
package 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:dateAttribute
extender, since laravel has deprecated the use of the$dates
property in models for date attributes, and recommends using$casts
instead. I hope in another PR to also change model classes from directly using$dates
property.Extending PHPStan
This PR also extends phpstan so that it can tell from
extend.php
files what relations and attributes are added to existing models, so that they aren't reported as unknown.extend.php
files are parsed and currently the package looks up calls tocast
andhasOne
belongsTo
hasMany
belongsToMany
method calls to inform the phpstan utility about them.Reviewers should focus on:
cast
model extender.dateAttribute
extender.Follow-up Tasks
Necessity
Confirmed
composer test
).