|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Build; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Arg; |
| 7 | +use PhpParser\Node\ArrayItem; |
| 8 | +use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Expr\Array_; |
| 10 | +use PhpParser\Node\Expr\BinaryOp\BooleanOr; |
| 11 | +use PhpParser\Node\Expr\BinaryOp\Identical; |
| 12 | +use PhpParser\Node\Expr\ClassConstFetch; |
| 13 | +use PhpParser\Node\Expr\ConstFetch; |
| 14 | +use PhpParser\Node\Expr\FuncCall; |
| 15 | +use PhpParser\Node\Name; |
| 16 | +use PhpParser\Node\Scalar; |
| 17 | +use PhpParser\Node\Stmt\If_; |
| 18 | +use PHPStan\Analyser\Scope; |
| 19 | +use PHPStan\DependencyInjection\AutowiredParameter; |
| 20 | +use PHPStan\File\FileHelper; |
| 21 | +use PHPStan\Fixable\PhpPrinter; |
| 22 | +use PHPStan\Rules\IdentifierRuleError; |
| 23 | +use PHPStan\Rules\Rule; |
| 24 | +use PHPStan\Rules\RuleErrorBuilder; |
| 25 | +use function array_map; |
| 26 | +use function array_merge; |
| 27 | +use function array_shift; |
| 28 | +use function count; |
| 29 | +use function dirname; |
| 30 | +use function str_starts_with; |
| 31 | + |
| 32 | +/** |
| 33 | + * @implements Rule<If_> |
| 34 | + */ |
| 35 | +final class OrChainIdenticalComparisonToInArrayRule implements Rule |
| 36 | +{ |
| 37 | + |
| 38 | + public function __construct( |
| 39 | + #[AutowiredParameter] |
| 40 | + private PhpPrinter $printer, |
| 41 | + private FileHelper $fileHelper, |
| 42 | + private bool $skipTests = true, |
| 43 | + ) |
| 44 | + { |
| 45 | + } |
| 46 | + |
| 47 | + public function getNodeType(): string |
| 48 | + { |
| 49 | + return If_::class; |
| 50 | + } |
| 51 | + |
| 52 | + public function processNode(Node $node, Scope $scope): array |
| 53 | + { |
| 54 | + $errors = $this->processConditionNode($node->cond, $scope); |
| 55 | + foreach ($node->elseifs as $elseifCondNode) { |
| 56 | + $errors = array_merge($errors, $this->processConditionNode($elseifCondNode->cond, $scope)); |
| 57 | + } |
| 58 | + |
| 59 | + return $errors; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return list<IdentifierRuleError> |
| 64 | + */ |
| 65 | + public function processConditionNode(Expr $condNode, Scope $scope): array |
| 66 | + { |
| 67 | + $comparisons = $this->unpackOrChain($condNode); |
| 68 | + if (count($comparisons) < 2) { |
| 69 | + return []; |
| 70 | + } |
| 71 | + |
| 72 | + $firstComparison = array_shift($comparisons); |
| 73 | + if (!$firstComparison instanceof Identical) { |
| 74 | + return []; |
| 75 | + } |
| 76 | + |
| 77 | + $subjectAndValue = $this->getSubjectAndValue($firstComparison); |
| 78 | + if ($subjectAndValue === null) { |
| 79 | + return []; |
| 80 | + } |
| 81 | + |
| 82 | + if ($this->skipTests && str_starts_with($this->fileHelper->normalizePath($scope->getFile()), $this->fileHelper->normalizePath(dirname(__DIR__, 3) . '/tests'))) { |
| 83 | + return []; |
| 84 | + } |
| 85 | + |
| 86 | + $subjectNode = $subjectAndValue['subject']; |
| 87 | + $subjectStr = $this->printer->prettyPrintExpr($subjectNode); |
| 88 | + $values = [$subjectAndValue['value']]; |
| 89 | + |
| 90 | + foreach ($comparisons as $comparison) { |
| 91 | + if (!$comparison instanceof Identical) { |
| 92 | + return []; |
| 93 | + } |
| 94 | + |
| 95 | + $currentSubjectAndValue = $this->getSubjectAndValue($comparison); |
| 96 | + if ($currentSubjectAndValue === null) { |
| 97 | + return []; |
| 98 | + } |
| 99 | + |
| 100 | + if ($this->printer->prettyPrintExpr($currentSubjectAndValue['subject']) !== $subjectStr) { |
| 101 | + return []; |
| 102 | + } |
| 103 | + |
| 104 | + $values[] = $currentSubjectAndValue['value']; |
| 105 | + } |
| 106 | + |
| 107 | + $errorBuilder = RuleErrorBuilder::message('This chain of identical comparisons can be simplified using in_array().') |
| 108 | + ->line($condNode->getStartLine()) |
| 109 | + ->fixNode($condNode, static fn (Expr $node) => self::createInArrayCall($subjectNode, $values)) |
| 110 | + ->identifier('or.chainIdenticalComparison'); |
| 111 | + |
| 112 | + return [$errorBuilder->build()]; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return list<Expr> |
| 117 | + */ |
| 118 | + private function unpackOrChain(Expr $node): array |
| 119 | + { |
| 120 | + if ($node instanceof BooleanOr) { |
| 121 | + return [...$this->unpackOrChain($node->left), ...$this->unpackOrChain($node->right)]; |
| 122 | + } |
| 123 | + |
| 124 | + return [$node]; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @phpstan-assert-if-true Scalar|ClassConstFetch|ConstFetch $node |
| 129 | + */ |
| 130 | + private static function isSubjectNode(Expr $node): bool |
| 131 | + { |
| 132 | + return $node instanceof Scalar || $node instanceof ClassConstFetch || $node instanceof ConstFetch; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @return array{subject: Expr, value: Scalar|ClassConstFetch|ConstFetch}|null |
| 137 | + */ |
| 138 | + private function getSubjectAndValue(Identical $comparison): ?array |
| 139 | + { |
| 140 | + if (self::isSubjectNode($comparison->left) && !self::isSubjectNode($comparison->left)) { |
| 141 | + return ['subject' => $comparison->right, 'value' => $comparison->left]; |
| 142 | + } |
| 143 | + |
| 144 | + if (!self::isSubjectNode($comparison->left) && self::isSubjectNode($comparison->right)) { |
| 145 | + return ['subject' => $comparison->left, 'value' => $comparison->right]; |
| 146 | + } |
| 147 | + |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @param list<Scalar|ClassConstFetch|ConstFetch> $values |
| 153 | + */ |
| 154 | + private static function createInArrayCall(Expr $subjectNode, array $values): FuncCall |
| 155 | + { |
| 156 | + return new FuncCall(new Name('\in_array'), [ |
| 157 | + new Arg($subjectNode), |
| 158 | + new Arg(new Array_(array_map(static fn ($value)=> new ArrayItem($value), $values))), |
| 159 | + new Arg(new ConstFetch(new Name('true'))), |
| 160 | + ]); |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments