Skip to content

Commit 86d884e

Browse files
committed
Introduce dead-code-detector
1 parent 11e0ab6 commit 86d884e

File tree

4 files changed

+151
-2
lines changed

4 files changed

+151
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Build;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\ParametersAcceptorSelector;
9+
use PHPStan\Type\Type;
10+
use PHPStan\Type\TypeCombinator;
11+
use ShipMonk\PHPStan\DeadCode\Provider\SimpleMethodEntrypointProvider;
12+
13+
final class ApiPhpDocEntrypointProvider extends SimpleMethodEntrypointProvider
14+
{
15+
16+
public function isEntrypointMethod(\ReflectionMethod $method): bool
17+
{
18+
$reflectionClass = $method->getDeclaringClass();
19+
$methodName = $method->getName();
20+
21+
if ($this->isApiClass($reflectionClass)) {
22+
return true;
23+
}
24+
25+
do {
26+
if ($this->isApiMethod($reflectionClass, $methodName)) {
27+
return true;
28+
}
29+
30+
foreach ($reflectionClass->getInterfaces() as $interface) {
31+
if ($this->isApiClass($interface)) {
32+
return true;
33+
}
34+
35+
if ($this->isApiMethod($interface, $methodName)) {
36+
return true;
37+
}
38+
}
39+
40+
$reflectionClass = $reflectionClass->getParentClass();
41+
} while ($reflectionClass !== false);
42+
43+
return false;
44+
}
45+
46+
private function isApiClass(\ReflectionClass $reflection): bool
47+
{
48+
$phpDoc = $reflection->getDocComment();
49+
if ($phpDoc !== false && str_contains($phpDoc, '@api')) {
50+
return true;
51+
}
52+
53+
return false;
54+
}
55+
56+
private function isApiMethod(\ReflectionClass $reflectionClass, string $methodName): bool
57+
{
58+
if (!$reflectionClass->hasMethod($methodName)) {
59+
return false;
60+
}
61+
62+
$phpDoc = $reflectionClass->getMethod($methodName)->getDocComment();
63+
if ($phpDoc !== false && str_contains($phpDoc, '@api')) {
64+
return true;
65+
}
66+
67+
return false;
68+
}
69+
}

build/phpstan.neon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ includes:
44
- ../vendor/phpstan/phpstan-phpunit/extension.neon
55
- ../vendor/phpstan/phpstan-phpunit/rules.neon
66
- ../vendor/phpstan/phpstan-strict-rules/rules.neon
7+
- ../vendor/shipmonk/dead-code-detector/rules.neon
78
- ../conf/bleedingEdge.neon
89
- ../phpstan-baseline.neon
910
- ../phpstan-baseline.php
@@ -91,6 +92,10 @@ parameters:
9192
path: ../src/Diagnose/PHPStanDiagnoseExtension.php
9293
- '#^Parameter \#1 \$offsetType of class PHPStan\\Type\\Accessory\\HasOffsetType constructor expects PHPStan\\Type\\Constant\\ConstantIntegerType\|PHPStan\\Type\\Constant\\ConstantStringType#'
9394
- '#^Short ternary operator is not allowed#'
95+
96+
- '#^Unused .*?Factory::create$#' # likely used in DIC
97+
- '#^Unused .*?::__construct$#' # disable dead construct analysis, DIC services might not be autowired
98+
9499
reportStaticMethodSignatures: true
95100
tmpDir: %rootDir%/tmp
96101
stubFiles:
@@ -111,3 +116,7 @@ services:
111116
class: PHPStan\Internal\ContainerDynamicReturnTypeExtension
112117
tags:
113118
- phpstan.broker.dynamicMethodReturnTypeExtension
119+
-
120+
class: PHPStan\Build\ApiPhpDocEntrypointProvider
121+
tags:
122+
- shipmonk.deadCode.entrypointProvider

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"phpstan/phpstan-strict-rules": "^1.6",
6565
"phpunit/phpunit": "^9.5.4",
6666
"shipmonk/composer-dependency-analyser": "^1.5",
67+
"shipmonk/dead-code-detector": "dev-master",
6768
"shipmonk/name-collision-detector": "^2.0"
6869
},
6970
"config": {

composer.lock

Lines changed: 72 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)