|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\PHPStan\DeadCode; |
| 4 | + |
| 5 | +use PHPStan\Testing\PHPStanTestCase; |
| 6 | +use function array_merge; |
| 7 | +use function file_get_contents; |
| 8 | +use function is_array; |
| 9 | +use function strpos; |
| 10 | + |
| 11 | +class ParamsArePassedToServicesTest extends PHPStanTestCase |
| 12 | +{ |
| 13 | + |
| 14 | + /** |
| 15 | + * @return string[] |
| 16 | + */ |
| 17 | + public static function getAdditionalConfigFiles(): array |
| 18 | + { |
| 19 | + return array_merge( |
| 20 | + parent::getAdditionalConfigFiles(), |
| 21 | + [__DIR__ . '/../rules.neon'], |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + public function test(): void |
| 26 | + { |
| 27 | + $parameters = self::getContainer()->getParameters(); |
| 28 | + self::assertArrayHasKey('shipmonkDeadCode', $parameters); |
| 29 | + self::assertIsArray($parameters['shipmonkDeadCode']); |
| 30 | + |
| 31 | + $paths = $this->getArrayPaths($parameters['shipmonkDeadCode'], 'shipmonkDeadCode'); |
| 32 | + |
| 33 | + $neonContents = file_get_contents(__DIR__ . '/../rules.neon'); |
| 34 | + self::assertNotFalse($neonContents); |
| 35 | + |
| 36 | + foreach ($paths as $path) { |
| 37 | + self::assertTrue( |
| 38 | + strpos($neonContents, "%$path%") !== false, |
| 39 | + 'Usage of parameter %' . $path . '% not found in rules.neon. It should probably be passed to some service.', |
| 40 | + ); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param array<mixed> $input |
| 46 | + * @return list<string> |
| 47 | + */ |
| 48 | + private function getArrayPaths(array $input, string $currentPath = ''): array |
| 49 | + { |
| 50 | + $resultPaths = []; |
| 51 | + |
| 52 | + foreach ($input as $key => $value) { |
| 53 | + $newPathSegment = $currentPath === '' ? (string) $key : $currentPath . '.' . $key; |
| 54 | + |
| 55 | + if (is_array($value)) { |
| 56 | + $resultPaths = array_merge($resultPaths, $this->getArrayPaths($value, $newPathSegment)); |
| 57 | + } else { |
| 58 | + $resultPaths[] = $newPathSegment; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return $resultPaths; |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments