Skip to content

Commit

Permalink
Register forgotten NetteEntrypointProvider (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored Sep 13, 2024
1 parent 397bbf5 commit f5b9054
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ parameters:
ShipMonk\PHPStan\DeadCode\Provider\MethodEntrypointProvider: EntrypointProvider
enforceReadonlyPublicProperty:
enabled: false # we support even PHP 7.4

ignoreErrors:
-
message: "#but it's missing from the PHPDoc @throws tag\\.$#" # allow uncatched exceptions in tests
path: tests/*
12 changes: 12 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ services:
arguments:
enabled: %shipmonkDeadCode.entrypoints.phpstan.enabled%

-
class: ShipMonk\PHPStan\DeadCode\Provider\NetteEntrypointProvider
tags:
- shipmonk.deadCode.entrypointProvider
arguments:
enabled: %shipmonkDeadCode.entrypoints.nette.enabled%

-
class: ShipMonk\PHPStan\DeadCode\Collector\MethodCallCollector
tags:
Expand Down Expand Up @@ -72,6 +79,8 @@ parameters:
enabled: null
doctrine:
enabled: null
nette:
enabled: null

parametersSchema:
shipmonkDeadCode: structure([
Expand All @@ -91,5 +100,8 @@ parametersSchema:
doctrine: structure([
enabled: schema(bool(), nullable())
])
nette: structure([
enabled: schema(bool(), nullable())
])
])
])
90 changes: 90 additions & 0 deletions tests/AllServicesInConfigTest.php
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;
}

}

0 comments on commit f5b9054

Please sign in to comment.