Skip to content

Commit

Permalink
Bleeding edge - validate overriding methods in stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 5, 2021
1 parent 3c3ea2f commit c98d0a4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ parameters:
apiRules: true
deepInspectTypes: true
neverInGenericReturnType: true
validateOverridingMethodsInStubs: true
stubFiles:
- ../stubs/arrayFunctions.stub
6 changes: 5 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ parameters:
apiRules: false
deepInspectTypes: false
neverInGenericReturnType: false
validateOverridingMethodsInStubs: false
fileExtensions:
- php
checkAlwaysTrueCheckTypeFunctionCall: false
Expand Down Expand Up @@ -215,7 +216,8 @@ parametersSchema:
preciseExceptionTracking: bool(),
apiRules: bool(),
deepInspectTypes: bool(),
neverInGenericReturnType: bool()
neverInGenericReturnType: bool(),
validateOverridingMethodsInStubs: bool()
])
fileExtensions: listOf(string())
checkAlwaysTrueCheckTypeFunctionCall: bool()
Expand Down Expand Up @@ -418,6 +420,8 @@ services:

-
class: PHPStan\PhpDoc\StubValidator
arguments:
validateOverridingMethods: %featureToggles.validateOverridingMethodsInStubs%

-
class: PHPStan\Analyser\Analyser
Expand Down
23 changes: 20 additions & 3 deletions src/PhpDoc/StubValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Broker\Broker;
use PHPStan\DependencyInjection\Container;
use PHPStan\DependencyInjection\DerivativeContainerFactory;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\Reflection\ReflectionProvider;
Expand All @@ -33,8 +34,10 @@
use PHPStan\Rules\Generics\TraitTemplateTypeRule;
use PHPStan\Rules\Generics\VarianceCheck;
use PHPStan\Rules\Methods\ExistingClassesInTypehintsRule;
use PHPStan\Rules\Methods\MethodSignatureRule;
use PHPStan\Rules\Methods\MissingMethodParameterTypehintRule;
use PHPStan\Rules\Methods\MissingMethodReturnTypehintRule;
use PHPStan\Rules\Methods\OverridingMethodRule;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\PhpDoc\IncompatiblePhpDocTypeRule;
use PHPStan\Rules\PhpDoc\IncompatiblePropertyPhpDocTypeRule;
Expand All @@ -52,11 +55,15 @@ class StubValidator

private \PHPStan\DependencyInjection\DerivativeContainerFactory $derivativeContainerFactory;

private bool $validateOverridingMethods;

public function __construct(
DerivativeContainerFactory $derivativeContainerFactory
DerivativeContainerFactory $derivativeContainerFactory,
bool $validateOverridingMethods
)
{
$this->derivativeContainerFactory = $derivativeContainerFactory;
$this->validateOverridingMethods = $validateOverridingMethods;
}

/**
Expand Down Expand Up @@ -127,7 +134,7 @@ private function getRuleRegistry(Container $container): Registry
$missingTypehintCheck = $container->getByType(MissingTypehintCheck::class);
$unresolvableTypeHelper = $container->getByType(UnresolvableTypeHelper::class);

return new Registry([
$rules = [
// level 0
new ExistingClassesInClassImplementsRule($classCaseSensitivityCheck, $reflectionProvider),
new ExistingClassesInInterfaceExtendsRule($classCaseSensitivityCheck, $reflectionProvider),
Expand Down Expand Up @@ -165,7 +172,17 @@ private function getRuleRegistry(Container $container): Registry
new MissingMethodParameterTypehintRule($missingTypehintCheck),
new MissingMethodReturnTypehintRule($missingTypehintCheck),
new MissingPropertyTypehintRule($missingTypehintCheck),
]);
];

if ($this->validateOverridingMethods) {
$rules[] = new OverridingMethodRule(
$container->getByType(PhpVersion::class),
new MethodSignatureRule(true, true),
true
);
}

return new Registry($rules);
}

}

0 comments on commit c98d0a4

Please sign in to comment.