Skip to content

Commit

Permalink
Modernized InClassMethodNode rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 25, 2023
1 parent e864b24 commit c3da6a9
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 78 deletions.
15 changes: 3 additions & 12 deletions src/Rules/Classes/UnusedConstructorParametersRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\UnusedFunctionParametersCheck;
use PHPStan\ShouldNotHappenException;
Expand Down Expand Up @@ -37,15 +36,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}

$method = $scope->getFunction();
if (!$method instanceof MethodReflection) {
return [];
}

$method = $node->getMethodReflection();
$originalNode = $node->getOriginalNode();
if (strtolower($method->getName()) !== '__construct' || $originalNode->stmts === null) {
return [];
Expand All @@ -57,9 +48,9 @@ public function processNode(Node $node, Scope $scope): array

$message = sprintf(
'Constructor of class %s has an unused parameter $%%s.',
SprintfHelper::escapeFormatString($scope->getClassReflection()->getDisplayName()),
SprintfHelper::escapeFormatString($node->getClassReflection()->getDisplayName()),
);
if ($scope->getClassReflection()->isAnonymous()) {
if ($node->getClassReflection()->isAnonymous()) {
$message = 'Constructor of an anonymous class has an unused parameter $%s.';
}

Expand Down
6 changes: 1 addition & 5 deletions src/Rules/Generics/MethodSignatureVarianceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use function sprintf;
Expand All @@ -28,10 +27,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$method = $scope->getFunction();
if (!$method instanceof MethodReflection) {
return [];
}
$method = $node->getMethodReflection();

return $this->varianceCheck->checkParametersAcceptor(
ParametersAcceptorSelector::selectSingle($method->getVariants()),
Expand Down
9 changes: 1 addition & 8 deletions src/Rules/Methods/ConsistentConstructorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
use PHPStan\Reflection\MethodPrototypeReflection;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use function strtolower;

/** @implements Rule<InClassMethodNode> */
Expand All @@ -31,12 +29,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$method = $scope->getFunction();

if (! $method instanceof PhpMethodFromParserNodeReflection) {
throw new ShouldNotHappenException();
}

$method = $node->getMethodReflection();
if (strtolower($method->getName()) !== '__construct') {
return [];
}
Expand Down
13 changes: 2 additions & 11 deletions src/Rules/Methods/ExistingClassesInTypehintsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\FunctionDefinitionCheck;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use function sprintf;

/**
Expand All @@ -29,15 +27,8 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$methodReflection = $scope->getFunction();
if (!$methodReflection instanceof PhpMethodFromParserNodeReflection) {
throw new ShouldNotHappenException();
}
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}

$className = SprintfHelper::escapeFormatString($scope->getClassReflection()->getDisplayName());
$methodReflection = $node->getMethodReflection();
$className = SprintfHelper::escapeFormatString($node->getClassReflection()->getDisplayName());
$methodName = SprintfHelper::escapeFormatString($methodReflection->getName());

return $this->check->checkClassMethod(
Expand Down
7 changes: 1 addition & 6 deletions src/Rules/Methods/FinalPrivateMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
Expand All @@ -28,11 +27,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$method = $scope->getFunction();
if (!$method instanceof PhpMethodFromParserNodeReflection) {
return [];
}

$method = $node->getMethodReflection();
if (!$this->phpVersion->producesWarningForFinalPrivateMethods()) {
return [];
}
Expand Down
7 changes: 1 addition & 6 deletions src/Rules/Methods/MethodSignatureRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Reflection\ParameterReflectionWithPhpDocs;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\TrinaryLogic;
Expand Down Expand Up @@ -46,11 +45,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$method = $scope->getFunction();
if (!$method instanceof PhpMethodFromParserNodeReflection) {
return [];
}

$method = $node->getMethodReflection();
$methodName = $method->getName();
if ($methodName === '__construct') {
return [];
Expand Down
6 changes: 1 addition & 5 deletions src/Rules/Methods/MissingMethodParameterTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$methodReflection = $scope->getFunction();
if (!$methodReflection instanceof MethodReflection) {
return [];
}

$methodReflection = $node->getMethodReflection();
$messages = [];

foreach (ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getParameters() as $parameterReflection) {
Expand Down
7 changes: 1 addition & 6 deletions src/Rules/Methods/MissingMethodReturnTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\Rule;
Expand All @@ -32,11 +31,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$methodReflection = $scope->getFunction();
if (!$methodReflection instanceof MethodReflection) {
return [];
}

$methodReflection = $node->getMethodReflection();
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();

if ($returnType instanceof MixedType && !$returnType->isExplicitMixed()) {
Expand Down
8 changes: 1 addition & 7 deletions src/Rules/Methods/OverridingMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Reflection\MethodPrototypeReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\VerbosityLevel;
use function array_merge;
use function count;
Expand Down Expand Up @@ -42,11 +40,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$method = $scope->getFunction();
if (!$method instanceof PhpMethodFromParserNodeReflection) {
throw new ShouldNotHappenException();
}

$method = $node->getMethodReflection();
$prototype = $method->getPrototype();
if ($prototype->getDeclaringClass()->getName() === $method->getDeclaringClass()->getName()) {
if (strtolower($method->getName()) === '__construct') {
Expand Down
7 changes: 1 addition & 6 deletions src/Rules/PhpDoc/MethodAssertRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use function count;

/**
Expand All @@ -26,11 +25,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$method = $scope->getFunction();
if ($method === null) {
throw new ShouldNotHappenException();
}

$method = $node->getMethodReflection();
$variants = $method->getVariants();
if (count($variants) !== 1) {
return [];
Expand Down
7 changes: 1 addition & 6 deletions src/Rules/PhpDoc/MethodConditionalReturnTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use function count;

/**
Expand All @@ -26,11 +25,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$method = $scope->getFunction();
if ($method === null) {
throw new ShouldNotHappenException();
}

$method = $node->getMethodReflection();
$variants = $method->getVariants();
if (count($variants) !== 1) {
return [];
Expand Down

0 comments on commit c3da6a9

Please sign in to comment.