-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register forgotten NetteEntrypointProvider (#81)
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\DeadCode; | ||
|
||
use DirectoryIterator; | ||
use LogicException; | ||
use PHPStan\Testing\PHPStanTestCase; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use ShipMonk\PHPStan\DeadCode\Crate\Call; | ||
use ShipMonk\PHPStan\DeadCode\Crate\Kind; | ||
use ShipMonk\PHPStan\DeadCode\Provider\MethodEntrypointProvider; | ||
use ShipMonk\PHPStan\DeadCode\Provider\SimpleMethodEntrypointProvider; | ||
use function array_keys; | ||
use function array_merge; | ||
use function class_exists; | ||
use function implode; | ||
use function in_array; | ||
use function interface_exists; | ||
use function str_replace; | ||
|
||
class AllServicesInConfigTest extends PHPStanTestCase | ||
{ | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return array_merge( | ||
parent::getAdditionalConfigFiles(), | ||
[__DIR__ . '/../rules.neon'], | ||
); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$this->expectNotToPerformAssertions(); | ||
|
||
$directory = __DIR__ . '/../src'; | ||
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); | ||
$missingClassNames = []; | ||
$excluded = [ | ||
Call::class, | ||
Kind::class, | ||
MethodEntrypointProvider::class, | ||
SimpleMethodEntrypointProvider::class, | ||
]; | ||
|
||
/** @var DirectoryIterator $file */ | ||
foreach ($iterator as $file) { | ||
if (!$file->isFile() || $file->getExtension() !== 'php') { | ||
continue; | ||
} | ||
|
||
$className = $this->mapPathToClassName($file->getPathname()); | ||
|
||
if (in_array($className, $excluded, true)) { | ||
continue; | ||
} | ||
|
||
if (self::getContainer()->findServiceNamesByType($className) !== []) { | ||
continue; | ||
} | ||
|
||
$missingClassNames[$className] = true; | ||
} | ||
|
||
if ($missingClassNames !== []) { | ||
self::fail('Class ' . implode(', ', array_keys($missingClassNames)) . ' not registered in rules.neon'); | ||
} | ||
} | ||
|
||
/** | ||
* @return class-string | ||
*/ | ||
private function mapPathToClassName(string $pathname): string | ||
{ | ||
$namespace = 'ShipMonk\\PHPStan\\DeadCode\\'; | ||
$relativePath = str_replace(__DIR__ . '/../src/', '', $pathname); | ||
$classString = $namespace . str_replace('/', '\\', str_replace([__DIR__ . '/../src', '.php'], '', $relativePath)); | ||
|
||
if (!class_exists($classString) && !interface_exists($classString)) { | ||
throw new LogicException('Class ' . $classString . ' does not exist'); | ||
} | ||
|
||
return $classString; | ||
} | ||
|
||
} |