-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
88883b2
feat(phpstan): pick up extended model relations typings
SychO9 c952bc6
feat(phpstan): pick up extended model date attributes
SychO9 a94cda6
feat(core): introduce `castAttribute` extender
SychO9 bb9b6fa
feat(phpstan): pick up extended model attributes through casts
SychO9 9531e7e
fix: extenders not resolved when declared namespace
SychO9 0468f6e
fix(phpstan): new model attributes are always nullable
SychO9 7d4f021
chore(phpstan): add helpful cache clearing command
SychO9 52d805e
Apply fixes from StyleCI
StyleCIBot 2e464d3
chore: improve extend files provider logic
SychO9 1ae3346
chore: rename `castAttribute` to just `cast`
SychO9 ffaf722
chore: update phpstan package to detect `cast` method
SychO9 ba01e31
Update framework/core/src/Extend/Model.php
SychO9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* For detailed copyright and license information, please view the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarum\PHPStan\Attributes; | ||
|
||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\PropertyReflection; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\Type; | ||
|
||
class AttributeProperty implements PropertyReflection | ||
{ | ||
/** @var ClassReflection */ | ||
private $classReflection; | ||
/** @var Type */ | ||
private $type; | ||
|
||
public function __construct(ClassReflection $classReflection, Type $type) | ||
{ | ||
$this->classReflection = $classReflection; | ||
$this->type = $type; | ||
} | ||
|
||
public function getDeclaringClass(): ClassReflection | ||
{ | ||
return $this->classReflection; | ||
} | ||
|
||
public function isStatic(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isPrivate(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isPublic(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getDocComment(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function getReadableType(): Type | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function getWritableType(): Type | ||
{ | ||
return $this->getReadableType(); | ||
} | ||
|
||
public function canChangeTypeAfterAssignment(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isReadable(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function isWritable(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function isDeprecated(): TrinaryLogic | ||
{ | ||
return TrinaryLogic::createNo(); | ||
} | ||
|
||
public function getDeprecatedDescription(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function isInternal(): TrinaryLogic | ||
{ | ||
return TrinaryLogic::createNo(); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
php-packages/phpstan/src/Attributes/ModelCastAttributeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* For detailed copyright and license information, please view the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarum\PHPStan\Attributes; | ||
|
||
use Carbon\Carbon; | ||
use Flarum\PHPStan\Extender\MethodCall; | ||
use Flarum\PHPStan\Extender\Resolver; | ||
use PHPStan\PhpDoc\TypeStringResolver; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\PropertiesClassReflectionExtension; | ||
use PHPStan\Reflection\PropertyReflection; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\UnionType; | ||
|
||
class ModelCastAttributeExtension implements PropertiesClassReflectionExtension | ||
{ | ||
/** @var Resolver */ | ||
private $extendersResolver; | ||
/** @var TypeStringResolver */ | ||
private $typeStringResolver; | ||
|
||
public function __construct(Resolver $extendersResolver, TypeStringResolver $typeStringResolver) | ||
{ | ||
$this->extendersResolver = $extendersResolver; | ||
$this->typeStringResolver = $typeStringResolver; | ||
} | ||
|
||
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool | ||
{ | ||
return $this->findCastAttributeMethod($classReflection, $propertyName) !== null; | ||
} | ||
|
||
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection | ||
{ | ||
return $this->resolveCastAttributeProperty($this->findCastAttributeMethod($classReflection, $propertyName), $classReflection); | ||
} | ||
|
||
private function findCastAttributeMethod(ClassReflection $classReflection, string $propertyName): ?MethodCall | ||
{ | ||
foreach ($this->extendersResolver->getExtenders() as $extender) { | ||
if (! $extender->isExtender('Model')) { | ||
continue; | ||
} | ||
|
||
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 commentThe 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 |
||
break; | ||
} | ||
|
||
if ($extender->extends($className)) { | ||
if ($methodCalls = $extender->findMethodCalls('cast')) { | ||
foreach ($methodCalls as $methodCall) { | ||
if ($methodCall->arguments[0]->value === $propertyName) { | ||
return $methodCall; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function resolveCastAttributeProperty(MethodCall $methodCall, ClassReflection $classReflection): PropertyReflection | ||
{ | ||
$typeName = $methodCall->arguments[1]->value; | ||
$type = $this->typeStringResolver->resolve("$typeName|null"); | ||
|
||
if (str_contains($typeName, 'date') || $typeName === 'timestamp') { | ||
$type = new UnionType([ | ||
new ObjectType(Carbon::class), | ||
new NullType(), | ||
]); | ||
} | ||
|
||
return new AttributeProperty($classReflection, $type); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?