Code Monkey home page Code Monkey logo

unused-scanner's Introduction

Project scanner for detect unused composer dependencies

unused-scanner

Versions

Use 1.3.x@dev versions for projects with php 5.6, 7.0

Use 2.x versions for projects with php >= 7.1

ChangeLog

see CHANGELOG.md

Installation

composer global require insolita/unused-scanner

Ensure that your ~/.composer/vendor/bin directory declared in $PATH

echo $PATH

if not - you should add it in ~/.bashrc or ~/.profile

Update

composer global update

Usage

prepare configuration file, see scanner_config.example.php

put it in project root (or other place)

run composer dumpautoload in your project directory

run unused_scanner /path/to/configuration/file/scanner_config.php

since 1.1 you can run it without argument, if scanner_config.php existed in current working directory, it will be used by default

For auto-testing:

Add --silent option for skip progress output and return exit code = 16, when unused packages detected

run unused_scanner --silent /path/to/configuration/file/scanner_config.php

Docker:

https://github.com/juanmrad/docker-unused-scanner

Demo screenshot

Licence

This project uses the MIT licence.

unused-scanner's People

Contributors

andreybolonin avatar bitgandtter avatar disc avatar dmaroulidis avatar george-zakharov avatar insolita avatar peter279k avatar tekkcraft avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unused-scanner's Issues

::class references not detected

Under one of the directories included in the scanDirectories setting, if a PHP file returning an array contains a use of ::class, usage of the referenced class will not be detected. In my case, the reference was Opauth\OpenID\Strategy\OpenID::class and the associated library was opauth/openid.

Feature request: Usage log

It would be nice if the scanner was able to log instances of usage that it found per dependency (i.e. the name of the dependency and paths to the source files that use it) so that dependencies with minimal usage could be removed or replaced.

False positive

I have following code and scanner still reports matthiasmullie/minify is not used.
If I replace use with use MatthiasMullie\Minify\JS and call with new JS then problem gone.
But IMO this still should be fixed as example is valid php code.

<?php

declare(strict_types=1);

namespace Sp\Minifier;

use MatthiasMullie\Minify;

class MinifyJSMinifier implements MinifierInterface
{
    public function minify(string $pathFrom, string $pathTo)
    {
        $minifier = new Minify\JS($pathFrom);
        $minifier->minify($pathTo);
    }
}

skipPackages Not working

Php version:

7.2

Os:

Docker on Mac

Composer packages with detection troubles:

ramsey/uuid

Code example of package classes usage that not detected:

The package is a suggested package by one library I use. since it is not require composer won't install it. I have installed it using composer require, but then I am not using it on my project directly. I tried adding it as such:

    'skipPackages' => [
        'ramsey/uuid',
    ],

on my config, but it is still reporting it as unused. is this how it is supposed to work? or is there any other way to configure it.

Failed to open Stream

Php version: 7.1.14

Os: MacOS High Sierra

Composer packages with detection troubles:

Which type of autoload it uses?

Code example of package classes usage that not detected:

unused_scanner /path/to/configuration/file/scanner_config.php --silent
PHP Warning: require_once(/path/to/configuration/file/scanner_config.php): failed to open stream: No such file or directory in /Users/enz/.composer/vendor/insolita/unused-scanner/Lib/Runner.php on line 86
PHP Fatal error: require_once(): Failed opening required '/path/to/configuration/file/scanner_config.php' (include_path='.:') in /Users/enz/.composer/vendor/insolita/unused-scanner/Lib/Runner.php on line 86

Usage of classes without declared namespaces not detected

One of the dependencies of my project is datadog/php-datadogstatsd. This library uses a classmap autoloader and references a directory containing a single class file that does not include a namespace directive.

Under one of the directories included in the scanDirectories setting, I have a class that extends from this library's class. This class usage is not detected.

problem with composer >= 2.0.9

hi there,

my apologies in case I am totally wrong here.
i am using this package with version with "^1.3" inside my composer.json with PHP 5.6.9 as fixed PHP version. (yeah i know, super old).
i was wondering, why composer now takes minutes instead of seconds when just using composer with -h - so with nothing.
turned out, that this package uses nesbot/carbon (>=1.22) and this package requires kylekatarnls/update-helper (^1.1).
and the helper is scanning all my files, while composer <= 2.0.8 is not.

i don´t know what really causes that behavior, but in case it has something to do with this package i wanted to let you know.

thanks !

composer/composer#9760

It's not working on windows

  1. outputs only first dir from defined $scanDirectories
  2. scans very very fast (no files I guess?)
  3. always says Unused dependencies not found!

I tried to use DIRECTECTORY_SEPARATOR in config with no luck. Same config works good on linux.

Support php 7.0

Php version: 7.0

Maintaining old code base with php 7.0, would be great to use this tool if backward compatibility is not too hard.

[Feature Request] Config file optional

Would it be possible to make the parameter for the config file optional? Meaning that if the standard 'scanner_config.php' is present in the projects' root, allowing the tool to run without the config parameter. If the config filename is different, then of course it can still be used with the parameter.

Not a big request, but saves some typing :)

Cheers! Sacha

Packages with only usage of Symfony services/config in .yml are detected as unused

Php version: 7.2

Os: Debian Stretch

Composer packages with detection troubles: any that are used only in Symfony .yml definitions

Which type of autoload it uses? -

Code example of package classes usage that not detected: -

E.g. if the only reference to willdurand/geocoder-bundle is - '@bazinga_geocoder.geocoder' as a DI argument for a service and in my service I construct it with a class from a different package as I have to (use Geocoder\ProviderAggregator;), it's not detected.

Unused-scanner scans only first directory from the list and founds nothing

Php version: 7.2.7 on Laravel 5.7.19

Os: Win10 x64

Composer packages with detection troubles: all

I tried to test unused-scanner on my Laravel project. Following the manual, I've installed scanner via composer, created scanner_config.php in root directory and...

It does not work. After preparing stage (less than a second), scanner takes first directory from $scanDirectories array, returns "No unused depedencies found" and quits.

D:\htest\test>unused_scanner scanner_config.php

  • config prepared

  • search patterns prepared

  • Scan D:\htest\test\app
    100%[============================================================>]
    No unused dependencies found!

My scanner_config.php:

<?php $projectPath = __DIR__ ; //Declare directories which contains php code $scanDirectories = [ $projectPath . '/app/', $projectPath . '/config/', $projectPath . '/resources/views/', $projectPath. '/routes/' ]; //Optionally declare standalone files $scanFiles = [ $projectPath . '/AuthenticatesUsers.php', ]; return [ 'composerJsonPath' => $projectPath . '/composer.json', 'vendorPath' => $projectPath . '/vendor/', 'scanDirectories' => $scanDirectories, 'scanFiles'=>$scanFiles ];

Feature request: Distribute as phar

Do you have any plans to distribute this project as phar? Having to composer install it globally or on project scope might introduce dependency conflicts. Also, installing this project as a phar would simplify the install process.

Many other similar projects do recommend that they are installed as phar - Composer Require Checker, Symfony Security Checker, Infection, etc.

I'd be happy to contribute if you think this would be something you are willing to accept.

unused_scanner command not found

Php version:7.4

Os:Ubuntu 20.04 LTS

I followed the instructions mentioned in the read me file but it did not work and gave me the command not found.

Error when `scanDirectories` contains a directory that does not exist.

In case a configuration file is used with an entry in scanDirectories that points to a directory that does not exist an error is returned.

I believe this happens because relpath returns false after which DIRECTORY_SEPARATOR is appended here:

return realpath(rtrim($path, DIRECTORY_SEPARATOR)) . DIRECTORY_SEPARATOR;

It should append the DIRECTORY_SEPARATOR to the output of rtrim(), not realpath(). Further, there should also be a check or filter to exclude directories that don't exist (cases when realpath() returns false).

Laravel autodiscovery

Is it somehow possible to support Laravels autodicovery feature?

I tried to dig into Laravel to find the relevant code but I only found the place where the extra gets loaded, not where it's actually stored.

Php version:

8.0 (but doesn't really matter)

Os:

macOS / Ubunutu / Alpine

Composer packages with detection troubles:

All Laravel autodiscovered packages, e.g. https://github.com/spatie/laravel-json-api-paginate

Which type of autoload it uses?

Autodiscovery via the package's composer.json extra.
See: https://laravelpackage.com/03-service-providers.html#autoloading

composer.json of the package that is not detected:

{
  ...,

  "autoload": { ... },

  "extra": {
      "laravel": {
          "providers": [
              "JohnDoe\\BlogPackage\\BlogPackageServiceProvider"
          ]
      }
  }
}

Code example of package classes usage that not detected:

The methods are magically injected, you call the package methods on your own models without any use.

YourModel::jsonPaginate();

See: https://github.com/spatie/laravel-json-api-paginate#usage

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.