Skip to content

Commit f5b9054

Browse files
authored
Register forgotten NetteEntrypointProvider (#81)
1 parent 397bbf5 commit f5b9054

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

phpstan.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ parameters:
4444
ShipMonk\PHPStan\DeadCode\Provider\MethodEntrypointProvider: EntrypointProvider
4545
enforceReadonlyPublicProperty:
4646
enabled: false # we support even PHP 7.4
47+
48+
ignoreErrors:
49+
-
50+
message: "#but it's missing from the PHPDoc @throws tag\\.$#" # allow uncatched exceptions in tests
51+
path: tests/*

rules.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ services:
3737
arguments:
3838
enabled: %shipmonkDeadCode.entrypoints.phpstan.enabled%
3939

40+
-
41+
class: ShipMonk\PHPStan\DeadCode\Provider\NetteEntrypointProvider
42+
tags:
43+
- shipmonk.deadCode.entrypointProvider
44+
arguments:
45+
enabled: %shipmonkDeadCode.entrypoints.nette.enabled%
46+
4047
-
4148
class: ShipMonk\PHPStan\DeadCode\Collector\MethodCallCollector
4249
tags:
@@ -72,6 +79,8 @@ parameters:
7279
enabled: null
7380
doctrine:
7481
enabled: null
82+
nette:
83+
enabled: null
7584

7685
parametersSchema:
7786
shipmonkDeadCode: structure([
@@ -91,5 +100,8 @@ parametersSchema:
91100
doctrine: structure([
92101
enabled: schema(bool(), nullable())
93102
])
103+
nette: structure([
104+
enabled: schema(bool(), nullable())
105+
])
94106
])
95107
])

tests/AllServicesInConfigTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)