Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/Rules/PhpDoc/RequireExtendsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\PhpDoc\Tag\RequireExtendsTag;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\ClassNameCheck;
use PHPStan\Rules\ClassNameNodePair;
use PHPStan\Rules\ClassNameUsageLocation;
use PHPStan\Rules\IdentifierRuleError;
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 sort;
use function sprintf;
use function strtolower;

Expand All @@ -26,6 +24,7 @@ final class RequireExtendsCheck
{

public function __construct(
private ReflectionProvider $reflectionProvider,
private ClassNameCheck $classCheck,
#[AutowiredParameter]
private bool $checkClassCaseSensitivity,
Expand Down Expand Up @@ -59,12 +58,8 @@ public function checkExtendsTags(Scope $scope, Node $node, array $extendsTags):
continue;
}

sort($classNames);
$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) {
if (!$this->reflectionProvider->hasClass($class)) {
$errorBuilder = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-extends contains unknown class %s.', $class))
->identifier('class.notFound');

Expand All @@ -76,16 +71,17 @@ public function checkExtendsTags(Scope $scope, Node $node, array $extendsTags):
continue;
}

if ($referencedClassReflection->isInterface()) {
$reflection = $this->reflectionProvider->getClass($class);
if ($reflection->isInterface()) {
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-extends cannot contain an interface %s, expected a class.', $class))
->tip('If you meant an interface, use @phpstan-require-implements instead.')
->identifier('requireExtends.interface')
->build();
} elseif (!$referencedClassReflection->isClass()) {
} elseif (!$reflection->isClass()) {
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-extends cannot contain non-class type %s.', $class))
->identifier(sprintf('requireExtends.%s', strtolower($referencedClassReflection->getClassTypeDescription())))
->identifier(sprintf('requireExtends.%s', strtolower($reflection->getClassTypeDescription())))
->build();
} elseif ($referencedClassReflection->isFinal()) {
} elseif ($reflection->isFinal()) {
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-extends cannot contain final class %s.', $class))
->identifier('requireExtends.finalClass')
->build();
Expand Down
12 changes: 4 additions & 8 deletions src/Rules/PhpDoc/RequireImplementsDefinitionTraitRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
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;
Expand Down Expand Up @@ -66,11 +64,8 @@ public function processNode(Node $node, Scope $scope): array
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) {
if (!$this->reflectionProvider->hasClass($class)) {
$errorBuilder = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-implements contains unknown class %s.', $class))
->identifier('class.notFound');

Expand All @@ -82,9 +77,10 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if (!$referencedClassReflection->isInterface()) {
$reflection = $this->reflectionProvider->getClass($class);
if (!$reflection->isInterface()) {
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-implements cannot contain non-interface type %s.', $class))
->identifier(sprintf('requireImplements.%s', strtolower($referencedClassReflection->getClassTypeDescription())))
->identifier(sprintf('requireImplements.%s', strtolower($reflection->getClassTypeDescription())))
->build();
} else {
$errors = array_merge(
Expand Down
9 changes: 3 additions & 6 deletions src/Rules/PhpDoc/SealedDefinitionClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\InClassNode;
use PHPStan\Reflection\ReflectionProvider;
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;
Expand All @@ -27,6 +26,7 @@ final class SealedDefinitionClassRule implements Rule
{

public function __construct(
private ReflectionProvider $reflectionProvider,
private ClassNameCheck $classCheck,
#[AutowiredParameter]
private bool $checkClassCaseSensitivity,
Expand Down Expand Up @@ -69,11 +69,8 @@ public function processNode(Node $node, Scope $scope): array
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) {
if (!$this->reflectionProvider->hasClass($class)) {
$errorBuilder = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-sealed contains unknown class %s.', $class))
->identifier('class.notFound');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getRule(): Rule
$container = self::getContainer();
return new RequireExtendsDefinitionClassRule(
new RequireExtendsCheck(
$reflectionProvider,
new ClassNameCheck(
new ClassCaseSensitivityCheck($reflectionProvider, true),
new ClassForbiddenNameCheck($container),
Expand Down Expand Up @@ -86,11 +87,6 @@ public function testRule(): void
183,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
[
'PHPDoc tag @phpstan-require-extends contains unknown class IncompatibleRequireExtends\SomeClass.',
183,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function getRule(): Rule
return new RequireExtendsDefinitionTraitRule(
$reflectionProvider,
new RequireExtendsCheck(
$reflectionProvider,
new ClassNameCheck(
new ClassCaseSensitivityCheck($reflectionProvider, true),
new ClassForbiddenNameCheck($container),
Expand Down Expand Up @@ -56,11 +57,6 @@ public function testRule(): void
192,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
[
'PHPDoc tag @phpstan-require-extends contains unknown class IncompatibleRequireExtends\SomeClass.',
192,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function testRule(): void
'PHPDoc tag @phpstan-require-implements contains non-object type *NEVER*.',
34,
],
[
'PHPDoc tag @phpstan-require-implements contains unknown class IncompatibleRequireImplements\TypeDoesNotExist.',
175,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
];

$this->analyse([__DIR__ . '/data/incompatible-require-implements.php'], $expectedErrors);
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/SealedDefinitionClassRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected function getRule(): Rule

$container = self::getContainer();
return new SealedDefinitionClassRule(
$reflectionProvider,
new ClassNameCheck(
new ClassCaseSensitivityCheck($reflectionProvider, true),
new ClassForbiddenNameCheck($container),
Expand Down Expand Up @@ -50,6 +51,11 @@ public function testRule(): void
26,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
[
'PHPDoc tag @phpstan-sealed contains unknown class IncompatibleSealed\UnknownClass.',
46,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,8 @@ trait ValidPsalmTrait {}
new class {
use ValidPsalmTrait;
};

/**
* @phpstan-require-implements RequiredInterface|TypeDoesNotExist
*/
trait InvalidTraitWithUnknown {}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/incompatible-sealed.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ interface ValidInterface {}
* @phpstan-sealed SomeInterface
*/
interface ValidInterface2 {}

/**
* @phpstan-sealed UnknownClass|SomeClass
*/
class InvalidClassWithUnion {}
Loading