You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+48-2Lines changed: 48 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,7 @@
7
7
- ✅ **PHPStan** extension
8
8
- ♻️ **Dead cycles** detection
9
9
- 🔗 **Transitive dead** member detection
10
+
- 🧪 **Dead tested code** detection
10
11
- 🧹 **Automatic removal** of unused code
11
12
- 📚 **Popular libraries** support
12
13
- ✨ **Customizable** usage providers
@@ -65,7 +66,6 @@ All those libraries are autoenabled when found within your composer dependencies
65
66
If you want to force enable/disable some of them, you can:
66
67
67
68
```neon
68
-
# phpstan.neon.dist
69
69
parameters:
70
70
shipmonkDeadCode:
71
71
usageProviders:
@@ -85,12 +85,26 @@ parameters:
85
85
86
86
Those providers are enabled by default, but you can disable them if needed.
87
87
88
+
## Excluding usages in tests:
89
+
- By default, all usages within scanned paths can mark members as used
90
+
- But that might not be desirable if class declared in `src` is **only used in `tests`**
91
+
- You can exclude those usages by enabling `tests` usage excluder:
92
+
93
+
```neon
94
+
parameters:
95
+
shipmonkDeadCode:
96
+
usageExcluders:
97
+
tests:
98
+
enabled: true
99
+
devPaths: # optional, autodetects from autoload-dev sections of composer.json when omitted
100
+
- %currentWorkingDirectory%/tests
101
+
```
102
+
88
103
## Customization:
89
104
- If your application does some magic calls unknown to this library, you can implement your own usage provider.
90
105
- Just tag it with `shipmonk.deadCode.memberUsageProvider` and implement `ShipMonk\PHPStan\DeadCode\Provider\MemberUsageProvider`
91
106
92
107
```neon
93
-
# phpstan.neon.dist
94
108
services:
95
109
-
96
110
class: App\ApiOutputUsageProvider
@@ -176,6 +190,36 @@ class DeserializationUsageProvider implements MemberUsageProvider
176
190
}
177
191
```
178
192
193
+
### Excluding usages:
194
+
195
+
You can exclude any usage based on custom logic, just implement `MemberUsageExcluder` and register it with `shipmonk.deadCode.memberUsageExcluder` tag:
196
+
197
+
```php
198
+
199
+
use ShipMonk\PHPStan\DeadCode\Excluder\MemberUsageExcluder;
200
+
201
+
class MyUsageExcluder implements MemberUsageExcluder
202
+
{
203
+
204
+
public function shouldExclude(ClassMemberUsage $usage, Node $node, Scope $scope): bool
205
+
{
206
+
// ...
207
+
}
208
+
209
+
}
210
+
```
211
+
212
+
```neon
213
+
# phpstan.neon.dist
214
+
services:
215
+
-
216
+
class: App\MyUsageExcluder
217
+
tags:
218
+
- shipmonk.deadCode.memberUsageExcluder
219
+
```
220
+
221
+
The same interface is used for exclusion of test-only usages, see above.
222
+
179
223
## Dead cycles & transitively dead methods
180
224
- This library automatically detects dead cycles and transitively dead methods (methods that are only called from dead methods)
181
225
- By default, it reports only the first dead method in the subtree and the rest as a tip:
@@ -221,6 +265,8 @@ class UserFacade
221
265
}
222
266
```
223
267
268
+
- If you are excluding tests usages (see above), this will not cause the related tests to be removed alongside.
269
+
224
270
225
271
## Calls over unknown types
226
272
- In order to prevent false positives, we support even calls over unknown types (e.g. `$unknown->method()`) by marking all methods named `method` as used
Copy file name to clipboardExpand all lines: composer.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
{
2
2
"name": "shipmonk/dead-code-detector",
3
-
"description": "Dead code detector to find unused PHP code via PHPStan extension. Can automatically remove dead PHP code. Supports libraries like Symfony, Doctrine, PHPUnit etc. Detects dead cycles.",
3
+
"description": "Dead code detector to find unused PHP code via PHPStan extension. Can automatically remove dead PHP code. Supports libraries like Symfony, Doctrine, PHPUnit etc. Detects dead cycles. Can detect dead code that is tested.",
0 commit comments