|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\PHPStan\DeadCode; |
| 4 | + |
| 5 | +use DirectoryIterator; |
| 6 | +use LogicException; |
| 7 | +use PHPStan\Testing\PHPStanTestCase; |
| 8 | +use RecursiveDirectoryIterator; |
| 9 | +use RecursiveIteratorIterator; |
| 10 | +use ShipMonk\PHPStan\DeadCode\Crate\Call; |
| 11 | +use ShipMonk\PHPStan\DeadCode\Crate\Kind; |
| 12 | +use ShipMonk\PHPStan\DeadCode\Provider\MethodEntrypointProvider; |
| 13 | +use ShipMonk\PHPStan\DeadCode\Provider\SimpleMethodEntrypointProvider; |
| 14 | +use function array_keys; |
| 15 | +use function array_merge; |
| 16 | +use function class_exists; |
| 17 | +use function implode; |
| 18 | +use function in_array; |
| 19 | +use function interface_exists; |
| 20 | +use function str_replace; |
| 21 | + |
| 22 | +class AllServicesInConfigTest extends PHPStanTestCase |
| 23 | +{ |
| 24 | + |
| 25 | + /** |
| 26 | + * @return string[] |
| 27 | + */ |
| 28 | + public static function getAdditionalConfigFiles(): array |
| 29 | + { |
| 30 | + return array_merge( |
| 31 | + parent::getAdditionalConfigFiles(), |
| 32 | + [__DIR__ . '/../rules.neon'], |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function test(): void |
| 37 | + { |
| 38 | + $this->expectNotToPerformAssertions(); |
| 39 | + |
| 40 | + $directory = __DIR__ . '/../src'; |
| 41 | + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); |
| 42 | + $missingClassNames = []; |
| 43 | + $excluded = [ |
| 44 | + Call::class, |
| 45 | + Kind::class, |
| 46 | + MethodEntrypointProvider::class, |
| 47 | + SimpleMethodEntrypointProvider::class, |
| 48 | + ]; |
| 49 | + |
| 50 | + /** @var DirectoryIterator $file */ |
| 51 | + foreach ($iterator as $file) { |
| 52 | + if (!$file->isFile() || $file->getExtension() !== 'php') { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + $className = $this->mapPathToClassName($file->getPathname()); |
| 57 | + |
| 58 | + if (in_array($className, $excluded, true)) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + if (self::getContainer()->findServiceNamesByType($className) !== []) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + $missingClassNames[$className] = true; |
| 67 | + } |
| 68 | + |
| 69 | + if ($missingClassNames !== []) { |
| 70 | + self::fail('Class ' . implode(', ', array_keys($missingClassNames)) . ' not registered in rules.neon'); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return class-string |
| 76 | + */ |
| 77 | + private function mapPathToClassName(string $pathname): string |
| 78 | + { |
| 79 | + $namespace = 'ShipMonk\\PHPStan\\DeadCode\\'; |
| 80 | + $relativePath = str_replace(__DIR__ . '/../src/', '', $pathname); |
| 81 | + $classString = $namespace . str_replace('/', '\\', str_replace([__DIR__ . '/../src', '.php'], '', $relativePath)); |
| 82 | + |
| 83 | + if (!class_exists($classString) && !interface_exists($classString)) { |
| 84 | + throw new LogicException('Class ' . $classString . ' does not exist'); |
| 85 | + } |
| 86 | + |
| 87 | + return $classString; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments