Skip to content

Commit

Permalink
Missing typehints should be consistently checked on level 6
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 9, 2024
1 parent 052f6b1 commit e3e80f6
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -919,17 +919,20 @@ services:
class: PHPStan\Rules\Classes\MethodTagCheck
arguments:
checkClassCaseSensitivity: %checkClassCaseSensitivity%
checkMissingTypehints: %checkMissingTypehints%

-
class: PHPStan\Rules\Classes\MixinCheck
arguments:
checkClassCaseSensitivity: %checkClassCaseSensitivity%
absentTypeChecks: %featureToggles.absentTypeChecks%
checkMissingTypehints: %checkMissingTypehints%

-
class: PHPStan\Rules\Classes\PropertyTagCheck
arguments:
checkClassCaseSensitivity: %checkClassCaseSensitivity%
checkMissingTypehints: %checkMissingTypehints%

-
class: PHPStan\Rules\Comparison\ConstantConditionRuleHelper
Expand Down
4 changes: 2 additions & 2 deletions src/PhpDoc/StubValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ private function getRuleRegistry(Container $container): RuleRegistry
if ((bool) $container->getParameter('featureToggles')['absentTypeChecks']) {
$rules[] = new MissingMethodSelfOutTypeRule($missingTypehintCheck);

$methodTagCheck = new MethodTagCheck($reflectionProvider, $classNameCheck, $genericObjectTypeCheck, $missingTypehintCheck, $unresolvableTypeHelper, true);
$methodTagCheck = new MethodTagCheck($reflectionProvider, $classNameCheck, $genericObjectTypeCheck, $missingTypehintCheck, $unresolvableTypeHelper, true, true);
$rules[] = new MethodTagRule($methodTagCheck);
$rules[] = new MethodTagTraitRule($methodTagCheck, $reflectionProvider);
$rules[] = new MethodTagTraitUseRule($methodTagCheck);

$propertyTagCheck = new PropertyTagCheck($reflectionProvider, $classNameCheck, $genericObjectTypeCheck, $missingTypehintCheck, $unresolvableTypeHelper, true);
$propertyTagCheck = new PropertyTagCheck($reflectionProvider, $classNameCheck, $genericObjectTypeCheck, $missingTypehintCheck, $unresolvableTypeHelper, true, true);
$rules[] = new PropertyTagRule($propertyTagCheck);
$rules[] = new PropertyTagTraitRule($propertyTagCheck, $reflectionProvider);
$rules[] = new PropertyTagTraitUseRule($propertyTagCheck);
Expand Down
5 changes: 5 additions & 0 deletions src/Rules/Classes/MethodTagCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function __construct(
private MissingTypehintCheck $missingTypehintCheck,
private UnresolvableTypeHelper $unresolvableTypeHelper,
private bool $checkClassCaseSensitivity,
private bool $checkMissingTypehints,
)
{
}
Expand Down Expand Up @@ -161,6 +162,10 @@ public function checkInTraitUseContext(
*/
private function checkMethodTypeInTraitDefinitionContext(ClassReflection $classReflection, string $methodName, string $description, Type $type): array
{
if (!$this->checkMissingTypehints) {
return [];
}

$errors = [];
foreach ($this->missingTypehintCheck->getNonGenericObjectTypesWithGenericClass($type) as [$innerName, $genericTypeNames]) {
$errors[] = RuleErrorBuilder::message(sprintf(
Expand Down
5 changes: 5 additions & 0 deletions src/Rules/Classes/MixinCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(
private UnresolvableTypeHelper $unresolvableTypeHelper,
private bool $checkClassCaseSensitivity,
private bool $absentTypeChecks,
private bool $checkMissingTypehints,
)
{
}
Expand Down Expand Up @@ -68,6 +69,10 @@ public function checkInTraitDefinitionContext(ClassReflection $classReflection):
continue;
}

if (!$this->checkMissingTypehints) {
continue;
}

foreach ($this->missingTypehintCheck->getIterableTypesWithMissingValueTypehint($type) as $iterableType) {
$iterableTypeDescription = $iterableType->describe(VerbosityLevel::typeOnly());
$errors[] = RuleErrorBuilder::message(sprintf(
Expand Down
5 changes: 5 additions & 0 deletions src/Rules/Classes/PropertyTagCheck.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 $checkMissingTypehints,
)
{
}
Expand Down Expand Up @@ -141,6 +142,10 @@ private function getTypesAndTagName(PropertyTag $propertyTag): array
*/
private function checkPropertyTypeInTraitDefinitionContext(ClassReflection $classReflection, string $propertyName, string $tagName, Type $type): array
{
if (!$this->checkMissingTypehints) {
return [];
}

$errors = [];

foreach ($this->missingTypehintCheck->getNonGenericObjectTypesWithGenericClass($type) as [$innerName, $genericTypeNames]) {
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Classes/MethodTagRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function getRule(): TRule
new MissingTypehintCheck(true, true, true, true, []),
new UnresolvableTypeHelper(),
true,
true,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Classes/MethodTagTraitRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function getRule(): TRule
new MissingTypehintCheck(true, true, true, true, []),
new UnresolvableTypeHelper(),
true,
true,
),
$reflectionProvider,
);
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Classes/MethodTagTraitUseRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function getRule(): TRule
new MissingTypehintCheck(true, true, true, true, []),
new UnresolvableTypeHelper(),
true,
true,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Classes/MixinRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function getRule(): Rule
new UnresolvableTypeHelper(),
true,
true,
true,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Classes/MixinTraitRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function getRule(): Rule
new UnresolvableTypeHelper(),
true,
true,
true,
),
$reflectionProvider,
);
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Classes/MixinTraitUseRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function getRule(): Rule
new UnresolvableTypeHelper(),
true,
true,
true,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Classes/PropertyTagRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function getRule(): TRule
new MissingTypehintCheck(true, true, true, true, []),
new UnresolvableTypeHelper(),
true,
true,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Classes/PropertyTagTraitRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function getRule(): TRule
new MissingTypehintCheck(true, true, true, true, []),
new UnresolvableTypeHelper(),
true,
true,
),
$reflectionProvider,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function getRule(): TRule
new MissingTypehintCheck(true, true, true, true, []),
new UnresolvableTypeHelper(),
true,
true,
),
);
}
Expand Down

0 comments on commit e3e80f6

Please sign in to comment.