Skip to content

Commit

Permalink
Readme: update limitations & problematic cases (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored Nov 4, 2024
1 parent 9636567 commit a3ddfe2
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,15 @@ parameters:
vendor/bin/phpstan analyse --error-format removeDeadCode
```

```php
// before
```diff
class UserFacade
{
public function deadMethod(): void
{
}
- public function deadMethod(): void
- {
- }
}
```

```php
// after
class UserFacade
{
}
```

## Calls over unknown types
- 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
Expand Down Expand Up @@ -176,13 +169,36 @@ Found 2 methods called over unknown type:
- It has almost no built-it library extensions
- It ignores trait methods
- Is lacks many minor features like class-string calls, dynamic method calls, array callbacks, nullsafe call chains etc
- It cannot detect dead cycles nor transitively dead methods
- It has no built-in dead code removal

## Limitations:
- Methods of anonymous classes are never reported as dead ([PHPStan limitation](https://github.com/phpstan/phpstan/issues/8410))
- Abstract trait methods are never reported as dead
- Most magic methods (e.g. `__get`, `__set` etc) are never reported as dead
- Only supported are: `__construct`, `__clone`

### Other problematic cases:

#### Constructors:
- For symfony apps & PHPStan extensions, we simplify the detection by assuming all DIC classes have used constructor.
- For other apps, you may get false-positives if services are created magically.
- To avoid those, you can easily disable consructor analysis with single ignore:

```neon
parameters:
ignoreErrors:
- '#^Unused .*?::__construct$#'
```

#### Private constructors:
- Those are never reported as dead as those are often used to deny class instantiation

- Only dead method calls are detected so far
- Including **constructors**, static methods, trait methods, interface methods, first class callables, clone, etc.
- Methods of anonymous classes are never reported as dead ([PHPStan limitation](https://github.com/phpstan/phpstan/issues/8410))
- Most magic methods (e.g. `__get`, `__set` etc) are never reported as dead

## Future scope:
- Dead class constant detection
- Dead class property detection
- Dead class detection

## Contributing
- Check your code by `composer check`
Expand Down

0 comments on commit a3ddfe2

Please sign in to comment.