|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\PHPStan\DeadCode\Provider; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\StaticCall; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Type\MixedType; |
| 10 | +use PHPStan\Type\Type; |
| 11 | +use PHPStan\Type\TypeCombinator; |
| 12 | +use PHPStan\Type\TypeUtils; |
| 13 | +use ReflectionEnum; |
| 14 | +use ReflectionEnumBackedCase; |
| 15 | +use ShipMonk\PHPStan\DeadCode\Graph\EnumCaseRef; |
| 16 | +use ShipMonk\PHPStan\DeadCode\Graph\EnumCaseUsage; |
| 17 | +use ShipMonk\PHPStan\DeadCode\Graph\UsageOrigin; |
| 18 | +use UnitEnum; |
| 19 | +use function array_filter; |
| 20 | +use function in_array; |
| 21 | +use function is_int; |
| 22 | +use function is_string; |
| 23 | + |
| 24 | +class EnumUsageProvider implements MemberUsageProvider |
| 25 | +{ |
| 26 | + |
| 27 | + private bool $enabled; |
| 28 | + |
| 29 | + public function __construct(bool $enabled) |
| 30 | + { |
| 31 | + $this->enabled = $enabled; |
| 32 | + } |
| 33 | + |
| 34 | + public function getUsages(Node $node, Scope $scope): array |
| 35 | + { |
| 36 | + if ($this->enabled === false) { |
| 37 | + return []; |
| 38 | + } |
| 39 | + |
| 40 | + if ($node instanceof StaticCall) { |
| 41 | + return $this->getTryFromUsages($node, $scope); |
| 42 | + } |
| 43 | + |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return list<EnumCaseUsage> |
| 49 | + */ |
| 50 | + private function getTryFromUsages(StaticCall $staticCall, Scope $scope): array |
| 51 | + { |
| 52 | + $methodNames = $this->getMethodNames($staticCall, $scope); |
| 53 | + $firstArgType = $this->getArgType($staticCall, $scope, 0); |
| 54 | + |
| 55 | + $callerType = $staticCall->class instanceof Expr |
| 56 | + ? $scope->getType($staticCall->class) |
| 57 | + : $scope->resolveTypeByName($staticCall->class); |
| 58 | + |
| 59 | + $typeNoNull = TypeCombinator::removeNull($callerType); // remove null to support nullsafe calls |
| 60 | + $typeNormalized = TypeUtils::toBenevolentUnion($typeNoNull); // extract possible calls even from Class|int |
| 61 | + $classReflections = $typeNormalized->getObjectTypeOrClassStringObjectType()->getObjectClassReflections(); |
| 62 | + |
| 63 | + $result = []; |
| 64 | + |
| 65 | + foreach ($methodNames as $methodName) { |
| 66 | + if (!in_array($methodName, ['tryFrom', 'from', 'cases'], true)) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + foreach ($classReflections as $classReflection) { |
| 71 | + if (!$classReflection->isEnum()) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + $valueToCaseMapping = $this->getValueToEnumCaseMapping($classReflection->getNativeReflection()); // @phpstan-ignore argument.type (https://github.com/phpstan/phpstan-src/pull/3925) |
| 76 | + $triedValues = $firstArgType->getConstantScalarValues() === [] |
| 77 | + ? [null] |
| 78 | + : array_filter($firstArgType->getConstantScalarValues(), static fn($value): bool => is_string($value) || is_int($value)); |
| 79 | + |
| 80 | + foreach ($triedValues as $value) { |
| 81 | + $enumCase = $value === null ? null : $valueToCaseMapping[$value] ?? null; |
| 82 | + $result[] = new EnumCaseUsage( |
| 83 | + UsageOrigin::createRegular($staticCall, $scope), |
| 84 | + new EnumCaseRef($classReflection->getName(), $enumCase), |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return $result; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return list<string|null> |
| 95 | + */ |
| 96 | + private function getMethodNames(StaticCall $call, Scope $scope): array |
| 97 | + { |
| 98 | + if ($call->name instanceof Expr) { |
| 99 | + $possibleMethodNames = []; |
| 100 | + |
| 101 | + foreach ($scope->getType($call->name)->getConstantStrings() as $constantString) { |
| 102 | + $possibleMethodNames[] = $constantString->getValue(); |
| 103 | + } |
| 104 | + |
| 105 | + return $possibleMethodNames === [] |
| 106 | + ? [null] // unknown method name |
| 107 | + : $possibleMethodNames; |
| 108 | + } |
| 109 | + |
| 110 | + return [$call->name->name]; |
| 111 | + } |
| 112 | + |
| 113 | + private function getArgType(StaticCall $staticCall, Scope $scope, int $position): Type |
| 114 | + { |
| 115 | + $args = $staticCall->getArgs(); |
| 116 | + |
| 117 | + if (isset($args[$position])) { |
| 118 | + return $scope->getType($args[$position]->value); |
| 119 | + } |
| 120 | + |
| 121 | + return new MixedType(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @param ReflectionEnum<UnitEnum> $enumReflection |
| 126 | + * @return array<array-key, string> |
| 127 | + */ |
| 128 | + private function getValueToEnumCaseMapping(ReflectionEnum $enumReflection): array |
| 129 | + { |
| 130 | + $mapping = []; |
| 131 | + |
| 132 | + foreach ($enumReflection->getCases() as $enumCaseReflection) { |
| 133 | + if (!$enumCaseReflection instanceof ReflectionEnumBackedCase) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + $mapping[$enumCaseReflection->getBackingValue()] = $enumCaseReflection->getName(); |
| 138 | + } |
| 139 | + |
| 140 | + return $mapping; |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments