Skip to content

Commit 90ed7f0

Browse files
authored
Ease first setup, introduce entrypoint providers for vendor & phpunit & symfony (#31)
1 parent 3fc29fd commit 90ed7f0

16 files changed

+1041
-166
lines changed

.github/workflows/checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
name: Setup PHP
1919
uses: shivammathur/setup-php@v2
2020
with:
21-
php-version: 8.2
21+
php-version: 8.3
2222
-
2323
name: Install dependencies
2424
run: composer install --no-progress --prefer-dist --no-interaction

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
-
1818
name: Install dependencies
19-
run: composer install --no-progress --no-interaction
19+
run: composer update --no-progress --no-interaction
2020

2121
-
2222
name: Lint

README.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,48 @@ includes:
1717

1818

1919
## Configuration:
20-
- You need to mark all entrypoints of your code to get proper results.
21-
- This is typically long whitelist of all code that is called by your framework and libraries.
20+
- All entrypoints of your code (controllers, consumers, commands, ...) need to be known to the detector to get proper results
21+
- By default, all overridden methods which declaration originates inside vendor are considered entrypoints
22+
- Also, there are some basic entrypoint providers for `symfony` and `phpunit`
23+
- For everything else, you can implement your own entrypoint provider, just tag it with `shipmonk.deadCode.entrypointProvider`
2224

2325
```neon
26+
parameters:
27+
deadCode:
28+
entrypoints:
29+
vendor:
30+
enabled: true # enabled by default
31+
symfony:
32+
enabled: true
33+
phpunit:
34+
enabled: true
35+
2436
services:
2537
-
26-
class: App\SymfonyEntrypointProvider
38+
class: App\MyEntrypointProvider
2739
tags:
2840
- shipmonk.deadCode.entrypointProvider
2941
```
3042
```php
3143

3244
use ReflectionMethod;
33-
use PHPStan\Reflection\ReflectionProvider;
3445
use ShipMonk\PHPStan\DeadCode\Provider\EntrypointProvider;
3546

36-
class SymfonyEntrypointProvider implements EntrypointProvider
47+
class MyEntrypointProvider implements EntrypointProvider
3748
{
3849

39-
public function __construct(
40-
private ReflectionProvider $reflectionProvider
41-
) {}
42-
4350
public function isEntrypoint(ReflectionMethod $method): bool
4451
{
45-
$methodName = $method->getName();
46-
$reflection = $this->reflectionProvider->getClass($method->getDeclaringClass()->getName());
47-
48-
return $reflection->is(\Symfony\Bundle\FrameworkBundle\Controller\AbstractController::class)
49-
|| $reflection->is(\Symfony\Component\EventDispatcher\EventSubscriberInterface::class)
50-
|| $reflection->is(\Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface::class)
51-
|| ($reflection->is(\Symfony\Component\Console\Command\Command::class) && in_array($methodName, ['execute', 'initialize', ...], true)
52-
// and many more
52+
return $method->getDeclaringClass()->implementsInterface(ApiOutput::class));
5353
}
5454
}
5555
```
5656

57-
## Limitations
58-
This project is currently a working prototype (we are using it since 2022) with limited functionality:
57+
## Limitations:
5958

6059
- Only method calls are detected
6160
- Including static methods, trait methods, interface methods, first class callables, etc.
62-
- Callbacks like `[$this, 'method']` are mostly not detected
61+
- Callbacks like `[$this, 'method']` are mostly not detected; prefer first class callables `$this->method(...)`
6362
- Any calls on mixed types are not detected, e.g. `$unknownClass->method()`
6463
- Expression method calls are not detected, e.g. `$this->$methodName()`
6564
- Anonymous classes are ignored

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"require": {
1515
"php": "^7.4 || ^8.0",
16-
"phpstan/phpstan": "^1.10.30"
16+
"phpstan/phpstan": "^1.11.0"
1717
},
1818
"require-dev": {
1919
"editorconfig-checker/editorconfig-checker": "^10.3.0",
@@ -22,8 +22,11 @@
2222
"phpstan/phpstan-strict-rules": "^1.2.3",
2323
"phpunit/phpunit": "^9.5.20",
2424
"shipmonk/name-collision-detector": "^2.0.0",
25-
"shipmonk/phpstan-rules": "^2.11",
26-
"slevomat/coding-standard": "^8.0.1"
25+
"shipmonk/phpstan-rules": "^3.1",
26+
"slevomat/coding-standard": "^8.0.1",
27+
"symfony/contracts": "^2.5 || ^3.0",
28+
"symfony/event-dispatcher": "^5.4 || ^6.0 || ^7.0",
29+
"symfony/routing": "^5.4 || ^6.0 || ^7.0"
2730
},
2831
"autoload": {
2932
"psr-4": {
@@ -65,7 +68,7 @@
6568
"check:composer": "composer normalize --dry-run --no-check-lock --no-update-lock",
6669
"check:cs": "phpcs",
6770
"check:ec": "ec src tests",
68-
"check:tests": "phpunit -vvv tests",
71+
"check:tests": "phpunit tests",
6972
"check:types": "phpstan analyse -vvv --ansi",
7073
"fix:cs": "phpcbf"
7174
}

0 commit comments

Comments
 (0)