|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\PHPStan\DeadCode\Provider; |
| 4 | + |
| 5 | +use Composer\InstalledVersions; |
| 6 | +use PhpParser\Node; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod; |
| 9 | +use PHPStan\Node\InClassNode; |
| 10 | +use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodRef; |
| 11 | +use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodUsage; |
| 12 | +use ShipMonk\PHPStan\DeadCode\Graph\UsageOrigin; |
| 13 | +use function strpos; |
| 14 | + |
| 15 | +class BehatUsageProvider implements MemberUsageProvider |
| 16 | +{ |
| 17 | + |
| 18 | + private bool $enabled; |
| 19 | + |
| 20 | + public function __construct(?bool $enabled) |
| 21 | + { |
| 22 | + $this->enabled = $enabled ?? InstalledVersions::isInstalled('behat/behat'); |
| 23 | + } |
| 24 | + |
| 25 | + public function getUsages( |
| 26 | + Node $node, |
| 27 | + Scope $scope |
| 28 | + ): array |
| 29 | + { |
| 30 | + if (!$this->enabled || !$node instanceof InClassNode) { // @phpstan-ignore phpstanApi.instanceofAssumption |
| 31 | + return []; |
| 32 | + } |
| 33 | + |
| 34 | + $classReflection = $node->getClassReflection(); |
| 35 | + |
| 36 | + if (!$classReflection->implementsInterface('Behat\Behat\Context\Context')) { |
| 37 | + return []; |
| 38 | + } |
| 39 | + |
| 40 | + $usages = []; |
| 41 | + $className = $classReflection->getName(); |
| 42 | + |
| 43 | + foreach ($classReflection->getNativeReflection()->getMethods() as $method) { |
| 44 | + $methodName = $method->getName(); |
| 45 | + |
| 46 | + if ($method->isConstructor()) { |
| 47 | + $usages[] = $this->createUsage($className, $methodName, 'Behat context constructor'); |
| 48 | + } elseif ($this->isBehatContextMethod($method)) { |
| 49 | + $usages[] = $this->createUsage($className, $methodName, 'Behat step definition or hook'); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return $usages; |
| 54 | + } |
| 55 | + |
| 56 | + private function isBehatContextMethod(ReflectionMethod $method): bool |
| 57 | + { |
| 58 | + return $this->hasAnnotation($method, '@Given') |
| 59 | + || $this->hasAnnotation($method, '@When') |
| 60 | + || $this->hasAnnotation($method, '@Then') |
| 61 | + || $this->hasAnnotation($method, '@BeforeScenario') |
| 62 | + || $this->hasAnnotation($method, '@AfterScenario') |
| 63 | + || $this->hasAnnotation($method, '@BeforeStep') |
| 64 | + || $this->hasAnnotation($method, '@AfterStep') |
| 65 | + || $this->hasAnnotation($method, '@BeforeSuite') |
| 66 | + || $this->hasAnnotation($method, '@AfterSuite') |
| 67 | + || $this->hasAnnotation($method, '@BeforeFeature') |
| 68 | + || $this->hasAnnotation($method, '@AfterFeature') |
| 69 | + || $this->hasAnnotation($method, '@Transform') |
| 70 | + || $this->hasAttribute($method, 'Behat\Step\Given') |
| 71 | + || $this->hasAttribute($method, 'Behat\Step\When') |
| 72 | + || $this->hasAttribute($method, 'Behat\Step\Then') |
| 73 | + || $this->hasAttribute($method, 'Behat\Hook\BeforeScenario') |
| 74 | + || $this->hasAttribute($method, 'Behat\Hook\AfterScenario') |
| 75 | + || $this->hasAttribute($method, 'Behat\Hook\BeforeStep') |
| 76 | + || $this->hasAttribute($method, 'Behat\Hook\AfterStep') |
| 77 | + || $this->hasAttribute($method, 'Behat\Hook\BeforeSuite') |
| 78 | + || $this->hasAttribute($method, 'Behat\Hook\AfterSuite') |
| 79 | + || $this->hasAttribute($method, 'Behat\Hook\BeforeFeature') |
| 80 | + || $this->hasAttribute($method, 'Behat\Hook\AfterFeature') |
| 81 | + || $this->hasAttribute($method, 'Behat\Transformation\Transform'); |
| 82 | + } |
| 83 | + |
| 84 | + private function hasAnnotation( |
| 85 | + ReflectionMethod $method, |
| 86 | + string $string |
| 87 | + ): bool |
| 88 | + { |
| 89 | + if ($method->getDocComment() === false) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + return strpos($method->getDocComment(), $string) !== false; |
| 94 | + } |
| 95 | + |
| 96 | + private function hasAttribute( |
| 97 | + ReflectionMethod $method, |
| 98 | + string $attributeClass |
| 99 | + ): bool |
| 100 | + { |
| 101 | + return $method->getAttributes($attributeClass) !== []; |
| 102 | + } |
| 103 | + |
| 104 | + private function createUsage( |
| 105 | + string $className, |
| 106 | + string $methodName, |
| 107 | + string $reason |
| 108 | + ): ClassMethodUsage |
| 109 | + { |
| 110 | + return new ClassMethodUsage( |
| 111 | + UsageOrigin::createVirtual($this, VirtualUsageData::withNote($reason)), |
| 112 | + new ClassMethodRef( |
| 113 | + $className, |
| 114 | + $methodName, |
| 115 | + false, |
| 116 | + ), |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments