-
Notifications
You must be signed in to change notification settings - Fork 516
Add phpstan-sealed support #4095
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\PhpDoc\Tag; | ||
|
||
use PHPStan\Type\Type; | ||
|
||
/** | ||
* @api | ||
*/ | ||
final class SealedTypeTag implements TypedTag | ||
{ | ||
|
||
public function __construct(private Type $type) | ||
{ | ||
} | ||
|
||
public function getType(): Type | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function withType(Type $type): self | ||
{ | ||
return new self($type); | ||
} | ||
|
||
} |
This file contains hidden or 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
36 changes: 36 additions & 0 deletions
36
src/Reflection/Php/SealedAllowedSubTypesClassReflectionExtension.php
This file contains hidden or 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,36 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Reflection\Php; | ||
|
||
use PHPStan\DependencyInjection\AutowiredService; | ||
use PHPStan\Reflection\AllowedSubTypesClassReflectionExtension; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Type\UnionType; | ||
use function count; | ||
|
||
#[AutowiredService] | ||
final class SealedAllowedSubTypesClassReflectionExtension implements AllowedSubTypesClassReflectionExtension | ||
{ | ||
|
||
public function supports(ClassReflection $classReflection): bool | ||
{ | ||
return count($classReflection->getSealedTags()) > 0; | ||
} | ||
|
||
public function getAllowedSubTypes(ClassReflection $classReflection): array | ||
{ | ||
$types = []; | ||
|
||
foreach ($classReflection->getSealedTags() as $sealedTag) { | ||
$type = $sealedTag->getType(); | ||
if ($type instanceof UnionType) { | ||
$types = $type->getTypes(); | ||
} else { | ||
$types = [$type]; | ||
} | ||
} | ||
|
||
return $types; | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,100 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PhpDoc; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\DependencyInjection\AutowiredParameter; | ||
use PHPStan\DependencyInjection\RegisteredRule; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\ClassNameCheck; | ||
use PHPStan\Rules\ClassNameNodePair; | ||
use PHPStan\Rules\ClassNameUsageLocation; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function array_column; | ||
use function array_map; | ||
use function array_merge; | ||
use function count; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
#[RegisteredRule(level: 2)] | ||
final class SealedDefinitionClassRule implements Rule | ||
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. Should be level 2. That's where PHPDocs are being checked. |
||
{ | ||
|
||
public function __construct( | ||
private ClassNameCheck $classCheck, | ||
#[AutowiredParameter] | ||
private bool $checkClassCaseSensitivity, | ||
#[AutowiredParameter(ref: '%tips.discoveringSymbols%')] | ||
private bool $discoveringSymbolsTip, | ||
) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $node->getClassReflection(); | ||
$sealedTags = $classReflection->getSealedTags(); | ||
|
||
if (count($sealedTags) === 0) { | ||
return []; | ||
} | ||
|
||
if ($classReflection->isEnum()) { | ||
return [ | ||
RuleErrorBuilder::message('PHPDoc tag @phpstan-sealed is only valid on class or interface.') | ||
->identifier('sealed.onEnum') | ||
->build(), | ||
]; | ||
} | ||
|
||
$errors = []; | ||
foreach ($sealedTags as $sealedTag) { | ||
$type = $sealedTag->getType(); | ||
$classNames = $type->getObjectClassNames(); | ||
if (count($classNames) === 0) { | ||
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-sealed contains non-object type %s.', $type->describe(VerbosityLevel::typeOnly()))) | ||
->identifier('sealed.nonObject') | ||
->build(); | ||
continue; | ||
} | ||
|
||
$referencedClassReflections = array_map(static fn ($reflection) => [$reflection, $reflection->getName()], $type->getObjectClassReflections()); | ||
$referencedClassReflectionsMap = array_column($referencedClassReflections, 0, 1); | ||
foreach ($classNames as $class) { | ||
$referencedClassReflection = $referencedClassReflectionsMap[$class] ?? null; | ||
if ($referencedClassReflection === null) { | ||
$errorBuilder = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-sealed contains unknown class %s.', $class)) | ||
->identifier('class.notFound'); | ||
|
||
if ($this->discoveringSymbolsTip) { | ||
$errorBuilder->discoveringSymbolsTip(); | ||
} | ||
|
||
$errors[] = $errorBuilder->build(); | ||
continue; | ||
} | ||
|
||
$errors = array_merge( | ||
$errors, | ||
$this->classCheck->checkClassNames($scope, [ | ||
new ClassNameNodePair($class, $node), | ||
], ClassNameUsageLocation::from(ClassNameUsageLocation::PHPDOC_TAG_SEALED), $this->checkClassCaseSensitivity), | ||
); | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
This file contains hidden or 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,54 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PhpDoc; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\DependencyInjection\RegisteredRule; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function count; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\Trait_> | ||
*/ | ||
#[RegisteredRule(level: 0)] | ||
final class SealedDefinitionTraitRule implements Rule | ||
{ | ||
|
||
public function __construct( | ||
private ReflectionProvider $reflectionProvider, | ||
) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Trait_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ( | ||
$node->namespacedName === null | ||
|| !$this->reflectionProvider->hasClass($node->namespacedName->toString()) | ||
) { | ||
return []; | ||
} | ||
|
||
$traitReflection = $this->reflectionProvider->getClass($node->namespacedName->toString()); | ||
$sealedTags = $traitReflection->getSealedTags(); | ||
|
||
if (count($sealedTags) === 0) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message('PHPDoc tag @phpstan-sealed is only valid on class or interface.') | ||
->identifier('sealed.onTrait') | ||
->build(), | ||
]; | ||
} | ||
|
||
} |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.