|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\DeadCode; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Node\NoopExpressionNode; |
| 8 | +use PHPStan\Node\Printer\ExprPrinter; |
| 9 | +use PHPStan\Rules\Rule; |
| 10 | +use PHPStan\Rules\RuleErrorBuilder; |
| 11 | +use function sprintf; |
| 12 | + |
| 13 | +/** |
| 14 | + * @implements Rule<NoopExpressionNode> |
| 15 | + */ |
| 16 | +class BetterNoopRule implements Rule |
| 17 | +{ |
| 18 | + |
| 19 | + public function __construct(private ExprPrinter $exprPrinter) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + public function getNodeType(): string |
| 24 | + { |
| 25 | + return NoopExpressionNode::class; |
| 26 | + } |
| 27 | + |
| 28 | + public function processNode(Node $node, Scope $scope): array |
| 29 | + { |
| 30 | + $expr = $node->getOriginalExpr(); |
| 31 | + if ($expr instanceof Node\Expr\BinaryOp\LogicalXor) { |
| 32 | + return [ |
| 33 | + RuleErrorBuilder::message( |
| 34 | + 'Unused result of "xor" operator.', |
| 35 | + )->line($expr->getStartLine()) |
| 36 | + ->tip('This operator has unexpected precedence, try disambiguating the logic with parentheses ().') |
| 37 | + ->identifier('logicalXor.resultUnused') |
| 38 | + ->build(), |
| 39 | + ]; |
| 40 | + } |
| 41 | + if ($expr instanceof Node\Expr\BinaryOp\LogicalAnd || $expr instanceof Node\Expr\BinaryOp\LogicalOr) { |
| 42 | + $identifierType = $expr instanceof Node\Expr\BinaryOp\LogicalAnd ? 'logicalAnd' : 'logicalOr'; |
| 43 | + |
| 44 | + return [ |
| 45 | + RuleErrorBuilder::message(sprintf( |
| 46 | + 'Unused result of "%s" operator.', |
| 47 | + $expr->getOperatorSigil(), |
| 48 | + ))->line($expr->getStartLine()) |
| 49 | + ->tip('This operator has unexpected precedence, try disambiguating the logic with parentheses ().') |
| 50 | + ->identifier(sprintf('%s.resultUnused', $identifierType)) |
| 51 | + ->build(), |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + if ($expr instanceof Node\Expr\BinaryOp\BooleanAnd || $expr instanceof Node\Expr\BinaryOp\BooleanOr) { |
| 56 | + $identifierType = $expr instanceof Node\Expr\BinaryOp\BooleanAnd ? 'booleanAnd' : 'booleanOr'; |
| 57 | + |
| 58 | + return [ |
| 59 | + RuleErrorBuilder::message(sprintf( |
| 60 | + 'Unused result of "%s" operator.', |
| 61 | + $expr->getOperatorSigil(), |
| 62 | + ))->line($expr->getStartLine()) |
| 63 | + ->identifier(sprintf('%s.resultUnused', $identifierType)) |
| 64 | + ->build(), |
| 65 | + ]; |
| 66 | + } |
| 67 | + |
| 68 | + if ($expr instanceof Node\Expr\Ternary) { |
| 69 | + return [ |
| 70 | + RuleErrorBuilder::message('Unused result of ternary operator.') |
| 71 | + ->line($expr->getStartLine()) |
| 72 | + ->identifier('ternary.resultUnused') |
| 73 | + ->build(), |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + if ( |
| 78 | + $expr instanceof Node\Expr\FuncCall |
| 79 | + || $expr instanceof Node\Expr\NullsafeMethodCall |
| 80 | + || $expr instanceof Node\Expr\MethodCall |
| 81 | + || $expr instanceof Node\Expr\New_ |
| 82 | + || $expr instanceof Node\Expr\StaticCall |
| 83 | + ) { |
| 84 | + // handled by *WithoutSideEffectsRule rules |
| 85 | + return []; |
| 86 | + } |
| 87 | + |
| 88 | + if ( |
| 89 | + $expr instanceof Node\Expr\Assign |
| 90 | + || $expr instanceof Node\Expr\AssignOp |
| 91 | + || $expr instanceof Node\Expr\AssignRef |
| 92 | + ) { |
| 93 | + return []; |
| 94 | + } |
| 95 | + |
| 96 | + if ($expr instanceof Node\Expr\Closure) { |
| 97 | + return []; |
| 98 | + } |
| 99 | + |
| 100 | + return [ |
| 101 | + RuleErrorBuilder::message(sprintf( |
| 102 | + 'Expression "%s" on a separate line does not do anything.', |
| 103 | + $this->exprPrinter->printExpr($expr), |
| 104 | + ))->line($expr->getStartLine()) |
| 105 | + ->identifier('expr.resultUnused') |
| 106 | + ->build(), |
| 107 | + ]; |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments