Giter Site home page Giter Site logo

dead-code-detector's Introduction

Dead code detector

PHPStan rules to find dead code in your project.

Installation:

composer require --dev shipmonk/dead-code-detector

Use official extension-installer or just load the rules:

includes:
    - vendor/shipmonk/dead-code-detector/rules.neon

Configuration:

  • All entrypoints of your code (controllers, consumers, commands, ...) need to be known to the detector to get proper results
  • By default, all overridden methods which declaration originates inside vendor are considered entrypoints
  • Also, there are some built-in providers for some magic calls that occur in doctrine, symfony, phpstan and phpunit
  • For everything else, you can implement your own entrypoint provider, just tag it with shipmonk.deadCode.entrypointProvider and implement ShipMonk\PHPStan\DeadCode\Provider\EntrypointProvider
# phpstan.neon.dist
services:
    -
        class: App\MyEntrypointProvider
        tags:
            - shipmonk.deadCode.entrypointProvider
use ReflectionMethod;
use ShipMonk\PHPStan\DeadCode\Provider\EntrypointProvider;

class MyEntrypointProvider implements EntrypointProvider
{

    public function isEntrypoint(ReflectionMethod $method): bool
    {
        return $method->getDeclaringClass()->implementsInterface(ApiOutput::class));
    }
}

Limitations:

  • Only method calls are detected
    • Including static methods, trait methods, interface methods, first class callables, etc.
    • Any calls on mixed types are not detected, e.g. $unknownClass->method()
    • Expression method calls are not detected, e.g. $this->$methodName()
    • Anonymous classes are ignored
    • Does not check magic methods
    • No transitive check is performed (dead method called only from dead method)
    • No dead cycles are detected (e.g. dead method calling itself)

Contributing

  • Check your code by composer check
  • Autofix coding-style by composer fix:cs
  • All functionality must be tested

Supported PHP versions

  • PHP 7.4 - 8.3

dead-code-detector's People

Contributors

dependabot[bot] avatar janedbal avatar mabar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

staabm mabar

dead-code-detector's Issues

comparison with TomasVotruba/unused-public

first let me say thank you for this repo... dead code is a major problem, a lot of people miss :-).

we are currently using https://github.com/TomasVotruba/unused-public which is pretty similar.
it would be great to see if there is a major difference between this 2 libs...?


one thing I saw by reading the docs: you are using a NEON based whitelist, while unused-public allows whitelisting by adding a @api annotation at class and/or method level. maybe thats something useful for you as well

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.