|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Classes; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Stmt\Class_; |
| 7 | +use PhpParser\Node\Stmt\TraitUse; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\ReflectionProvider; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
| 12 | +use Throwable; |
| 13 | +use function array_filter; |
| 14 | +use function array_merge; |
| 15 | +use function array_unique; |
| 16 | +use function basename; |
| 17 | +use function count; |
| 18 | +use function in_array; |
| 19 | +use function sprintf; |
| 20 | +use function str_replace; |
| 21 | + |
| 22 | +/** |
| 23 | + * Disallows redundant trait usage when a trait is already included via another trait. |
| 24 | + * |
| 25 | + * This rule checks classes that use multiple traits and ensures that |
| 26 | + * they don't use a trait that is already being used by another trait. |
| 27 | + * For example, if trait A uses trait B, then a class shouldn't use both A and B. |
| 28 | + * |
| 29 | + * @implements Rule<Class_> |
| 30 | + */ |
| 31 | +class NoRedundantTraitUseRule implements Rule |
| 32 | +{ |
| 33 | + |
| 34 | + private const ERROR_MESSAGE = 'Class uses trait "%s" redundantly as it is already included via trait "%s".'; |
| 35 | + |
| 36 | + private ReflectionProvider $reflectionProvider; |
| 37 | + |
| 38 | + public function __construct(ReflectionProvider $reflectionProvider) |
| 39 | + { |
| 40 | + $this->reflectionProvider = $reflectionProvider; |
| 41 | + } |
| 42 | + |
| 43 | + public function getNodeType(): string |
| 44 | + { |
| 45 | + return Class_::class; |
| 46 | + } |
| 47 | + |
| 48 | + public function processNode(Node $node, Scope $scope): array |
| 49 | + { |
| 50 | + $errors = []; |
| 51 | + |
| 52 | + // Get all trait use statements from the class. |
| 53 | + $traitUseNodes = array_filter($node->stmts, static fn ($stmt): bool => $stmt instanceof TraitUse); |
| 54 | + |
| 55 | + if (count($traitUseNodes) < 2) { |
| 56 | + // Need at least 2 traits to have redundancy. |
| 57 | + return []; |
| 58 | + } |
| 59 | + |
| 60 | + // Collect all directly used trait names with their resolved names. |
| 61 | + $directlyUsedTraits = []; |
| 62 | + foreach ($traitUseNodes as $traitUseNode) { |
| 63 | + foreach ($traitUseNode->traits as $trait) { |
| 64 | + $traitName = $scope->resolveName($trait); |
| 65 | + $directlyUsedTraits[] = $traitName; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Build a map of trait -> [traits it uses] with full resolution. |
| 70 | + $traitDependencies = []; |
| 71 | + foreach ($directlyUsedTraits as $traitName) { |
| 72 | + try { |
| 73 | + if ($this->reflectionProvider->hasClass($traitName)) { |
| 74 | + $traitReflection = $this->reflectionProvider->getClass($traitName); |
| 75 | + if ($traitReflection->isTrait()) { |
| 76 | + $traitDependencies[$traitName] = $this->getAllTraitsUsedByTrait($traitName, []); |
| 77 | + } |
| 78 | + } |
| 79 | + } catch (Throwable $e) { |
| 80 | + // Skip traits that can't be reflected. |
| 81 | + continue; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Check for redundancies. |
| 86 | + foreach ($directlyUsedTraits as $traitA) { |
| 87 | + foreach ($directlyUsedTraits as $traitB) { |
| 88 | + if ($traitA === $traitB) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + // Check if traitA uses traitB (directly or transitively). |
| 93 | + if (isset($traitDependencies[$traitA]) && in_array($traitB, $traitDependencies[$traitA], true)) { |
| 94 | + $shortNameA = basename(str_replace('\\', '/', $traitA)); |
| 95 | + $shortNameB = basename(str_replace('\\', '/', $traitB)); |
| 96 | + |
| 97 | + $errors[] = RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $shortNameB, $shortNameA)) |
| 98 | + ->line($node->getStartLine()) |
| 99 | + ->identifier('traits.redundantTraitUse') |
| 100 | + ->build(); |
| 101 | + |
| 102 | + // Only report each redundant trait once |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return $errors; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get all traits used by a given trait recursively. |
| 113 | + * |
| 114 | + * @param string $traitName The fully qualified trait name. |
| 115 | + * @param array<string> $visited Array to track visited traits (for cycle detection). |
| 116 | + * |
| 117 | + * @return array<string> Array of all trait names used by the given trait (directly and transitively). |
| 118 | + */ |
| 119 | + private function getAllTraitsUsedByTrait(string $traitName, array $visited = []): array |
| 120 | + { |
| 121 | + // Prevent infinite loops. |
| 122 | + if (in_array($traitName, $visited, true)) { |
| 123 | + return []; |
| 124 | + } |
| 125 | + |
| 126 | + $visited[] = $traitName; |
| 127 | + |
| 128 | + try { |
| 129 | + if (!$this->reflectionProvider->hasClass($traitName)) { |
| 130 | + return []; |
| 131 | + } |
| 132 | + |
| 133 | + $traitReflection = $this->reflectionProvider->getClass($traitName); |
| 134 | + if (!$traitReflection->isTrait()) { |
| 135 | + return []; |
| 136 | + } |
| 137 | + |
| 138 | + $allTraits = []; |
| 139 | + |
| 140 | + // Get direct traits used by this trait. |
| 141 | + foreach ($traitReflection->getTraits() as $trait) { |
| 142 | + $usedTraitName = $trait->getName(); |
| 143 | + $allTraits[] = $usedTraitName; |
| 144 | + |
| 145 | + // Recursively get traits used by the used trait. |
| 146 | + $nestedTraits = $this->getAllTraitsUsedByTrait($usedTraitName, $visited); |
| 147 | + $allTraits = array_merge($allTraits, $nestedTraits); |
| 148 | + } |
| 149 | + |
| 150 | + return array_unique($allTraits); |
| 151 | + } catch (Throwable $e) { |
| 152 | + return []; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments