Skip to content

Commit

Permalink
Bleeding edge - add missing MissingTypehintCheck calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 24, 2024
1 parent 5a2d441 commit 3175c81
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/config.level2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ services:
class: PHPStan\Rules\Classes\MixinRule
arguments:
checkClassCaseSensitivity: %checkClassCaseSensitivity%
absentTypeChecks: %featureToggles.absentTypeChecks%
tags:
- phpstan.rules.rule

Expand Down
25 changes: 25 additions & 0 deletions src/Rules/Classes/MixinRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(
private MissingTypehintCheck $missingTypehintCheck,
private UnresolvableTypeHelper $unresolvableTypeHelper,
private bool $checkClassCaseSensitivity,
private bool $absentTypeChecks,
)
{
}
Expand Down Expand Up @@ -83,6 +84,30 @@ public function processNode(Node $node, Scope $scope): array
->build();
}

if ($this->absentTypeChecks) {
foreach ($this->missingTypehintCheck->getIterableTypesWithMissingValueTypehint($type) as $iterableType) {
$iterableTypeDescription = $iterableType->describe(VerbosityLevel::typeOnly());
$errors[] = RuleErrorBuilder::message(sprintf(
'%s %s has PHPDoc tag @mixin with no value type specified in iterable type %s.',
$classReflection->getClassTypeDescription(),
$classReflection->getDisplayName(),
$iterableTypeDescription,
))
->tip(MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP)
->identifier('missingType.iterableValue')
->build();
}

foreach ($this->missingTypehintCheck->getCallablesWithMissingSignature($type) as $callableType) {
$errors[] = RuleErrorBuilder::message(sprintf(
'%s %s has PHPDoc tag @mixin with no signature specified for %s.',
$classReflection->getClassTypeDescription(),
$classReflection->getDisplayName(),
$callableType->describe(VerbosityLevel::typeOnly()),
))->identifier('missingType.callable')->build();
}
}

foreach ($type->getReferencedClasses() as $class) {
if (!$this->reflectionProvider->hasClass($class)) {
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @mixin contains unknown class %s.', $class))
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Classes/MixinRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function getRule(): Rule
new MissingTypehintCheck(true, true, true, true, []),
new UnresolvableTypeHelper(),
true,
true,
);
}

Expand Down Expand Up @@ -97,6 +98,15 @@ public function testRule(): void
116,
'You can safely remove the call-site variance annotation.',
],
[
'Class MixinRule\NoIterableValue has PHPDoc tag @mixin with no value type specified in iterable type array.',
124,
MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP,
],
[
'Class MixinRule\NoCallableSignature has PHPDoc tag @mixin with no signature specified for callable.',
132,
],
]);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Classes/data/mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,19 @@ class Elit2
{

}

/**
* @mixin Dolor<array>
*/
class NoIterableValue
{

}

/**
* @mixin Dolor<callable>
*/
class NoCallableSignature
{

}

0 comments on commit 3175c81

Please sign in to comment.