-
-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(phpstan): foundation for usage in extensions (#3666)
* feat(phpstan): pick up extended model relations typings * feat(phpstan): pick up extended model date attributes * feat(core): introduce `castAttribute` extender Stops using `dates` as it's deprecated in laravel 8 * feat(phpstan): pick up extended model attributes through casts * fix: extenders not resolved when declared namespace * fix(phpstan): new model attributes are always nullable * chore(phpstan): add helpful cache clearing command * Apply fixes from StyleCI * chore: improve extend files provider logic * chore: rename `castAttribute` to just `cast` * chore: update phpstan package to detect `cast` method * Update framework/core/src/Extend/Model.php Signed-off-by: Sami Mazouz <[email protected]>
- Loading branch information
Showing
15 changed files
with
934 additions
and
28 deletions.
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') { | ||
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.