|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Analyser; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PHPStan\DependencyInjection\AutowiredService; |
| 8 | +use PHPStan\Node\RedundantVarTagNode; |
| 9 | +use PHPStan\PhpDoc\Tag\VarTag; |
| 10 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 11 | +use PHPStan\Type\ErrorType; |
| 12 | +use PHPStan\Type\MixedType; |
| 13 | +use PHPStan\Type\NeverType; |
| 14 | +use PHPStan\Type\Type; |
| 15 | +use PHPStan\Type\TypeCombinator; |
| 16 | +use PHPStan\Type\TypeTraverser; |
| 17 | +use function is_string; |
| 18 | +use function spl_object_id; |
| 19 | + |
| 20 | +/** |
| 21 | + * Finds the inline @var tags that change nothing about the variable or |
| 22 | + * expression they re-type, for the opt-in reportRedundantVarTag. |
| 23 | + * |
| 24 | + * The walk applies a tag every time it walks the tag's statement: in every |
| 25 | + * pass over a loop body, in the observation pass of template arguments, in |
| 26 | + * every iteration over a closure with by-reference uses. Its own type can flow |
| 27 | + * back into what it re-types in a later pass, so a tag is redundant only when |
| 28 | + * none of these applications changed anything. The engine records each one, |
| 29 | + * and emits the redundant tags once it has processed the statement. |
| 30 | + * |
| 31 | + * Only the type and the certainty are compared: an assignment's tag also makes |
| 32 | + * the native type mixed and forgets what the assigned value narrowed, and |
| 33 | + * without the tag both would only be more precise. |
| 34 | + */ |
| 35 | +#[AutowiredService] |
| 36 | +final class RedundantVarTagDetector implements PerFileAnalysisResettable |
| 37 | +{ |
| 38 | + |
| 39 | + private const EXPLICIT_MIXED = 1; |
| 40 | + private const IMPLICIT_MIXED = 2; |
| 41 | + private const EXPLICIT_NEVER = 4; |
| 42 | + private const IMPLICIT_NEVER = 8; |
| 43 | + |
| 44 | + /** @var array<int, array{Node\Stmt, array<int|string, array{VarTag, bool}>}> statement id => [statement, tag key => [tag, changed nothing so far]] */ |
| 45 | + private array $applications = []; |
| 46 | + |
| 47 | + public function resetFileAnalysisState(): void |
| 48 | + { |
| 49 | + $this->applications = []; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Records the application of $varTag to a variable, which gets the tag's |
| 54 | + * type, and becomes certainly defined when $defines is true. $narrowedByForeach |
| 55 | + * is true for the tag of a foreach's iteratee applied again on entering the |
| 56 | + * loop body, where the loop may have narrowed the variable to a non-empty |
| 57 | + * array the tag widens again. |
| 58 | + * |
| 59 | + * @param int|string $key the tag's key in ResolvedPhpDocBlock::getVarTags() |
| 60 | + */ |
| 61 | + public function recordVariable(Node\Stmt $stmt, $key, VarTag $varTag, MutatingScope $scope, string $variableName, bool $defines, bool $narrowedByForeach = false): void |
| 62 | + { |
| 63 | + $certainty = $scope->hasVariableType($variableName); |
| 64 | + if ($certainty->no() || ($defines && !$certainty->yes())) { |
| 65 | + $this->record($stmt, $key, $varTag, false); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $originalType = $scope->getVariableType($variableName); |
| 70 | + $tagType = $varTag->getType(); |
| 71 | + $this->record( |
| 72 | + $stmt, |
| 73 | + $key, |
| 74 | + $varTag, |
| 75 | + $this->isSameType($originalType, $tagType) |
| 76 | + || ($narrowedByForeach && $this->isSameType($originalType, TypeCombinator::remove($tagType, new ConstantArrayType([], [])))), |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Records the application of the variable-less $varTag to $expr, $scope |
| 82 | + * being the scope before the tag re-typed it. |
| 83 | + */ |
| 84 | + public function recordExpression(NodeScopeResolver $nodeScopeResolver, Node\Stmt $stmt, VarTag $varTag, Expr $expr, MutatingScope $scope): void |
| 85 | + { |
| 86 | + // priced again from scratch: the walk's results of the expression and |
| 87 | + // of its parts saw the tag's type, re-typing $a['k'] narrows $a as well |
| 88 | + $originalType = $nodeScopeResolver->processExprOnDemand($expr, $scope, new ExpressionResultStorage())->getType(); |
| 89 | + $this->record($stmt, 0, $varTag, $this->isSameType($originalType, $varTag->getType())); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Emits a RedundantVarTagNode for each tag above $stmt that changed |
| 94 | + * nothing in any of its applications so far. |
| 95 | + * |
| 96 | + * @param callable(Node $node, Scope $scope): void $nodeCallback |
| 97 | + */ |
| 98 | + public function emitNodes(NodeScopeResolver $nodeScopeResolver, MutatingScope $scope, ExpressionResultStorage $storage, Node\Stmt $stmt, callable $nodeCallback): void |
| 99 | + { |
| 100 | + $id = spl_object_id($stmt); |
| 101 | + if (!isset($this->applications[$id]) || $this->applications[$id][0] !== $stmt) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + foreach ($this->applications[$id][1] as $key => [$varTag, $changedNothing]) { |
| 106 | + if (!$changedNothing) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + $nodeScopeResolver->callNodeCallback($nodeCallback, new RedundantVarTagNode($stmt, $varTag, is_string($key) ? $key : null), $scope, $storage); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @param int|string $key |
| 116 | + */ |
| 117 | + private function record(Node\Stmt $stmt, $key, VarTag $varTag, bool $changedNothing): void |
| 118 | + { |
| 119 | + $id = spl_object_id($stmt); |
| 120 | + if (!isset($this->applications[$id]) || $this->applications[$id][0] !== $stmt) { |
| 121 | + // the id of a statement freed since is reused |
| 122 | + $this->applications[$id] = [$stmt, []]; |
| 123 | + } |
| 124 | + |
| 125 | + $changedNothingBefore = $this->applications[$id][1][$key][1] ?? true; |
| 126 | + $this->applications[$id][1][$key] = [$varTag, $changedNothingBefore && $changedNothing]; |
| 127 | + } |
| 128 | + |
| 129 | + private function isSameType(Type $originalType, Type $tagType): bool |
| 130 | + { |
| 131 | + if ( |
| 132 | + // equals() is one-way for a type extending another one, like a |
| 133 | + // template type or a benevolent union, and does not compare |
| 134 | + // whether a constant array is a list |
| 135 | + !$originalType->equals($tagType) |
| 136 | + || !$tagType->equals($originalType) |
| 137 | + || !$originalType->isList()->equals($tagType->isList()) |
| 138 | + ) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + // equals() also ignores whether mixed and never are explicit, which |
| 143 | + // decides what is reported about them |
| 144 | + $kinds = $this->getMixedAndNeverKinds($originalType); |
| 145 | + |
| 146 | + return $kinds !== null && $kinds === $this->getMixedAndNeverKinds($tagType); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Which kinds of mixed and never the type contains; null when it contains an |
| 151 | + * error type, or both kinds of mixed or of never, whose positions are not compared. |
| 152 | + */ |
| 153 | + private function getMixedAndNeverKinds(Type $type): ?int |
| 154 | + { |
| 155 | + $kinds = 0; |
| 156 | + $containsError = false; |
| 157 | + TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$kinds, &$containsError): Type { |
| 158 | + if ($type instanceof ErrorType) { |
| 159 | + $containsError = true; |
| 160 | + return $type; |
| 161 | + } |
| 162 | + |
| 163 | + if ($type instanceof MixedType) { |
| 164 | + $kinds |= $type->isExplicitMixed() ? self::EXPLICIT_MIXED : self::IMPLICIT_MIXED; |
| 165 | + } elseif ($type instanceof NeverType) { |
| 166 | + $kinds |= $type->isExplicit() ? self::EXPLICIT_NEVER : self::IMPLICIT_NEVER; |
| 167 | + } |
| 168 | + |
| 169 | + return $traverse($type); |
| 170 | + }); |
| 171 | + |
| 172 | + if ( |
| 173 | + $containsError |
| 174 | + || ($kinds & (self::EXPLICIT_MIXED | self::IMPLICIT_MIXED)) === (self::EXPLICIT_MIXED | self::IMPLICIT_MIXED) |
| 175 | + || ($kinds & (self::EXPLICIT_NEVER | self::IMPLICIT_NEVER)) === (self::EXPLICIT_NEVER | self::IMPLICIT_NEVER) |
| 176 | + ) { |
| 177 | + return null; |
| 178 | + } |
| 179 | + |
| 180 | + return $kinds; |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments