diff --git a/src/Rules/Methods/MissingMethodParameterTypehintRule.php b/src/Rules/Methods/MissingMethodParameterTypehintRule.php new file mode 100644 index 00000000..2c62248c --- /dev/null +++ b/src/Rules/Methods/MissingMethodParameterTypehintRule.php @@ -0,0 +1,66 @@ +isInClass()) { + throw new \PHPStan\ShouldNotHappenException(); + } + + $methodReflection = $scope->getClassReflection()->getNativeMethod($node->name); + + $messages = []; + + foreach ($methodReflection->getParameters() as $parameterReflection) { + $message = $this->checkMethodParameter($methodReflection, $parameterReflection); + if ($message === null) { + continue; + } + + $messages[] = $message; + } + + return $messages; + } + + private function checkMethodParameter(MethodReflection $methodReflection, ParameterReflection $parameterReflection): ?string + { + $parameterType = $parameterReflection->getType(); + + if ($parameterType instanceof MixedType && !$parameterType->isExplicitMixed()) { + return sprintf( + 'Method %s::%s() has parameter $%s with no typehint specified', + $methodReflection->getDeclaringClass()->getName(), + $methodReflection->getName(), + $parameterReflection->getName() + ); + } + + return null; + } + +} diff --git a/src/Rules/Methods/MissingMethodReturnTypehintRule.php b/src/Rules/Methods/MissingMethodReturnTypehintRule.php new file mode 100644 index 00000000..459476be --- /dev/null +++ b/src/Rules/Methods/MissingMethodReturnTypehintRule.php @@ -0,0 +1,60 @@ +isInClass()) { + throw new \PHPStan\ShouldNotHappenException(); + } + + $methodReflection = $scope->getClassReflection()->getNativeMethod($node->name); + + $messages = []; + + $message = $this->checkMethodReturnType($methodReflection); + if ($message !== null) { + $messages[] = $message; + } + + return $messages; + } + + private function checkMethodReturnType(MethodReflection $methodReflection): ?string + { + $returnType = $methodReflection->getReturnType(); + + if ($returnType instanceof MixedType && !$returnType->isExplicitMixed()) { + return sprintf( + 'Method %s::%s() has no return typehint specified', + $methodReflection->getDeclaringClass()->getName(), + $methodReflection->getName() + ); + } + + return null; + } + +} diff --git a/tests/Rules/Methods/MissingMethodParameterTypehintRuleTest.php b/tests/Rules/Methods/MissingMethodParameterTypehintRuleTest.php new file mode 100644 index 00000000..0b20e778 --- /dev/null +++ b/tests/Rules/Methods/MissingMethodParameterTypehintRuleTest.php @@ -0,0 +1,39 @@ +analyse([__DIR__ . '/data/missing-method-parameter-typehint.php'], [ + [ + 'Method MissingMethodParameterTypehint\FooInterface::getFoo() has parameter $p1 with no typehint specified', + 8, + ], + [ + 'Method MissingMethodParameterTypehint\FooParent::getBar() has parameter $p2 with no typehint specified', + 15, + ], + [ + 'Method MissingMethodParameterTypehint\Foo::getFoo() has parameter $p1 with no typehint specified', + 25, + ], + [ + 'Method MissingMethodParameterTypehint\Foo::getBar() has parameter $p2 with no typehint specified', + 33, + ], + [ + 'Method MissingMethodParameterTypehint\Foo::getBaz() has parameter $p3 with no typehint specified', + 42, + ], + ]); + } + +} diff --git a/tests/Rules/Methods/MissingMethodReturnTypehintRuleTest.php b/tests/Rules/Methods/MissingMethodReturnTypehintRuleTest.php new file mode 100644 index 00000000..f42fa912 --- /dev/null +++ b/tests/Rules/Methods/MissingMethodReturnTypehintRuleTest.php @@ -0,0 +1,35 @@ +analyse([__DIR__ . '/data/missing-method-return-typehint.php'], [ + [ + 'Method MissingMethodReturnTypehint\FooInterface::getFoo() has no return typehint specified', + 8, + ], + [ + 'Method MissingMethodReturnTypehint\FooParent::getBar() has no return typehint specified', + 15, + ], + [ + 'Method MissingMethodReturnTypehint\Foo::getFoo() has no return typehint specified', + 25, + ], + [ + 'Method MissingMethodReturnTypehint\Foo::getBar() has no return typehint specified', + 33, + ], + ]); + } + +} diff --git a/tests/Rules/Methods/data/missing-method-parameter-typehint.php b/tests/Rules/Methods/data/missing-method-parameter-typehint.php new file mode 100644 index 00000000..17b53af3 --- /dev/null +++ b/tests/Rules/Methods/data/missing-method-parameter-typehint.php @@ -0,0 +1,55 @@ +