Giter Club home page Giter Club logo

symbol-parser's Introduction

composer-unused logo

composer-unused

A Composer tool to show unused Composer dependencies by scanning your code.

Created by Andreas Frömer and contributors, logo by Caneco.

⚠️ If you want to use this tool as composer-plugin head over to composer-unused-plugin.

Motivation

When working in a big repository, you sometimes lose track of your required Composer packages. There may be so many packages you can't be sure if they are actually used or not.

Unfortunately, the composer why command only gives you the information about why a package is installed in dependency to another package.

How do we check whether the provided symbols of a package are used in our code?

composer unused to the rescue!

example

Installation

⚠️ This tool heavily depends on certain versions of its dependencies. A local installation of this tool is not recommended as it might not work as intended or can't be installed correctly. We do recommened you download the .phar archive or use PHIVE to install it locally.

PHAR (PHP Archive) (recommended)

Install via phive or grab the latest composer-unused.phar from the latest release:

phive install composer-unused
curl -OL https://github.com/composer-unused/composer-unused/releases/latest/download/composer-unused.phar

Local

You can also install composer-unused as a local development dependency:

composer require --dev icanhazstring/composer-unused

Usage

Depending on the kind of your installation the command might differ.

Note: Packages must be installed via composer install or composer update prior to running composer-unused.

PHAR

The phar archive can be run directly in you project:

php composer-unused.phar

Local

Having composer-unused as a local dependency you can run it using the shipped binary:

vendor/bin/composer-unused

Exclude folders and packages

Sometimes you don't want to scan a certain directory or ignore a Composer package while scanning. In these cases, you can provide the --excludeDir or the --excludePackage option. These options accept multiple values as shown next:

php composer-unused.phar --excludeDir=config --excludePackage=symfony/console
php composer-unused.phar \
    --excludeDir=bin \
    --excludeDir=config \
    --excludePackage=symfony/assets \
    --excludePackage=symfony/console

Make sure the package is named exactly as in your composer.json

Configuration

You can configure composer-unused by placing a composer-unused.php beside the projects composer.json This configuration can look something like this: composer-unused.php

Ignore dependencies by name

To ignore dependencies by their name, add the following line to your configuration:

$config->addNamedFilter(NamedFilter::fromString('dependency/name'));

Ignore dependencies by pattern

To ignore dependencies by pattern, add the following line to your configuration

$config->addPatternFilter(PatternFilter::fromString('/dependency\/name/'));

You can ignore multiple dependencies by a single organization using PatternFilter e.g. /symfony\/.*/

Additional files to be parsed

Per default, composer-unused is using the composer.json autoload directive to determine where to look for files to parse. Sometimes dependencies don't have their composer.json correctly set up, or files get loaded in another way. Using this, you can define additional files on a per-dependency basis.

$config->setAdditionalFilesFor('dependency/name', [<list-of-file-paths>]);

Changelog

Please have a look at CHANGELOG.md.

Contributing

Please have a look at CONTRIBUTING.md.

Code of Conduct

Please have a look at CODE_OF_CONDUCT.md.

License

This package is licensed under the MIT License.

symbol-parser's People

Contributors

agustingomes avatar dependabot[bot] avatar eliashaeussler avatar icanhazstring avatar jean85 avatar leovie avatar llaville avatar samsonasik avatar simpod avatar vincentlanglet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

symbol-parser's Issues

AnnotationStrategy runs out of memory

Describe the bug

When trying to use composer-unused on a small(ish) package, it runs out of memory trying to allocate 17GB.

PHP Fatal error: Out of memory (allocated 17205039104) (tried to allocate 34359738376 bytes) in /home/username/vendor-name/package-name/vendor/composer-unused/symbol-parser/src/Parser/PHP/Strategy/AnnotationStrategy.php on line 165

Error dump

None created, and no extra output logged when using -vvv so I'm unsure what exactly is the cause.

Additional information

No memory limit when running on CLI I believe, but either way 17GB is extremely excessive, especially for this relatively small package.

[v0.2.2] `DefinedSymbolCollector` did not return traits and namespaces

Describe the bug

The DefinedSymbolCollector did not return user traits and namespaces

Additional information

With these data source files

data/interfaces.php

<?php
// @link https://www.php.net/manual/en/language.oop5.interfaces.php

interface A
{
    public function foo();
}

interface B extends A
{
    public function baz(Baz $baz);
}

// This will work
class C implements B
{
    public function foo()
    {
    }

    public function baz(Baz $baz)
    {
    }
}

// This will not work and result in a fatal error
class D implements B
{
    public function foo()
    {
    }

    public function baz(Foo $foo)
    {
    }
}

data/namespaces.php

<?php
// @link https://www.php.net/manual/en/language.namespaces.importing.php

namespace foo;

use My\Full\Classname as Another;

// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;

// importing a global class
use ArrayObject;

// importing a function
use function My\Full\functionName;

// aliasing a function
use function My\Full\functionName as func;

// importing a constant
use const My\Full\CONSTANT;

data/traits.php

<?php
// @link https://www.php.net/manual/en/language.oop5.traits.php

trait ezcReflectionReturnInfo {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}

class ezcReflectionMethod extends ReflectionMethod {
    use ezcReflectionReturnInfo;
    /* ... */
}

class ezcReflectionFunction extends ReflectionFunction {
    use ezcReflectionReturnInfo;
    /* ... */
}

And this script test :

<?php

use ComposerUnused\SymbolParser\Parser\PHP\ConsumedSymbolCollector;
use ComposerUnused\SymbolParser\Parser\PHP\DefinedSymbolCollector;
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\DefineStrategy;
use ComposerUnused\SymbolParser\Parser\PHP\SymbolNameParser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
use Symfony\Component\Finder\Finder;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/DefineStrategy.php';

$dataSource = __DIR__ . '/data';

$strategies = [
//    new DefineStrategy(),
];

$finder = new Finder();
$finder
    ->files()
    ->in($dataSource)
    ->name('/\\.(php|inc|phtml)$/');

if (empty($strategies)) {
    $symbolCollector = new DefinedSymbolCollector();
} else {
    $symbolCollector = new ConsumedSymbolCollector($strategies);
}

$symbolParser = new SymbolNameParser(
    (new ParserFactory())->createForNewestSupportedVersion(),
    new NameResolver(),
    $symbolCollector
);

foreach ($finder as $fileInfo) {
    $taskId = $fileInfo->getRelativePathname();
    $symbols = iterator_to_array(
        $symbolParser->parseSymbolNames($fileInfo->getContents())
    );
    printf('Task %s found %d symbol(s)'. PHP_EOL, $taskId, count($symbols));
    var_export($symbols);
    echo PHP_EOL;
}

Expected results :

Task interfaces.php found 4 symbol(s)
array (
  0 => 'A',
  1 => 'B',
  2 => 'C',
  3 => 'D',
)
Task traits.php found 3 symbol(s)
array (
  0 => 'ezcReflectionReturnInfo',
  1 => 'ezcReflectionMethod',
  2 => 'ezcReflectionFunction',
)
Task namespaces.php found 1 symbol(s)
array (
  0 => 'foo',
)

Current results :

Task interfaces.php found 4 symbol(s)
array (
  0 => 'A',
  1 => 'B',
  2 => 'C',
  3 => 'D',
)
Task traits.php found 2 symbol(s)
array (
  0 => 'ezcReflectionMethod',
  1 => 'ezcReflectionFunction',
)
Task namespaces.php found 0 symbol(s)
array (
)

NOTE : The DefineStrategy is the solution to this issue. But I can't open a Feature Request (see #135 )

Call to undefined method `PhpParser\Node\Expr\Closure::getParts()` in `DefinedSymbolCollector`

Describe the bug

I wanted to handle composer/xdebug-handler in PHP-CS-Fixer with custom config:

<?php

declare(strict_types=1);

use ComposerUnused\ComposerUnused\Configuration\Configuration;

return static function (Configuration $config): Configuration {
    $config
        ->setAdditionalFilesFor('composer/xdebug-handler', [
            __DIR__.'/../php-cs-fixer',
        ])
    ;

    return $config;
};

but it fails on php-cs-fixer entrypoint script:

Fatal error: Uncaught Error: Call to undefined method PhpParser\Node\Expr\Closure::getParts() in dev-tools/vendor/composer-unused/symbol-parser/src/Parser/PHP/DefinedSymbolCollector.php:65

Stack trace:
#0 dev-tools/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(200): ComposerUnused\SymbolParser\Parser\PHP\DefinedSymbolCollector->enterNode(Object(PhpParser\Node\Stmt\Expression))
#1 dev-tools/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(91): PhpParser\NodeTraverser->traverseArray(Array)
#2 dev-tools/vendor/composer-unused/symbol-parser/src/Parser/PHP/SymbolNameParser.php(55): PhpParser\NodeTraverser->traverse(Array)
#3 dev-tools/vendor/composer-unused/symbol-parser/src/Symbol/Provider/FileSymbolProvider.php(44): ComposerUnused\SymbolParser\Parser\PHP\SymbolNameParser->parseSymbolNames('#!/usr/bin/env ...')
#4 dev-tools/vendor/composer-unused/symbol-parser/src/Symbol/Loader/FileSymbolLoader.php(70): ComposerUnused\SymbolParser\Symbol\Provider\FileSymbolProvider->provide()
#5 dev-tools/vendor/composer-unused/symbol-parser/src/Symbol/Loader/CompositeSymbolLoader.php(29): ComposerUnused\SymbolParser\Symbol\Loader\FileSymbolLoader->load(Object(ComposerUnused\ComposerUnused\Composer\Package))
#6 [internal function]: ComposerUnused\SymbolParser\Symbol\Loader\CompositeSymbolLoader->load(Object(ComposerUnused\ComposerUnused\Composer\Package))
#7 dev-tools/vendor/composer-unused/symbol-parser/src/Symbol/SymbolList.php(23): iterator_to_array(Object(Generator))
#8 dev-tools/vendor/icanhazstring/composer-unused/src/Command/Handler/CollectRequiredDependenciesCommandHandler.php(60): ComposerUnused\SymbolParser\Symbol\SymbolList->addAll(Object(Generator))
#9 dev-tools/vendor/icanhazstring/composer-unused/src/Console/Command/UnusedCommand.php(182): ComposerUnused\ComposerUnused\Command\Handler\CollectRequiredDependenciesCommandHandler->collect(Object(ComposerUnused\ComposerUnused\Command\LoadRequiredDependenciesCommand))
#10 dev-tools/vendor/symfony/console/Command/Command.php(326): ComposerUnused\ComposerUnused\Console\Command\UnusedCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#11 dev-tools/vendor/symfony/console/Application.php(1078): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 dev-tools/vendor/symfony/console/Application.php(324): Symfony\Component\Console\Application->doRunCommand(Object(ComposerUnused\ComposerUnused\Console\Command\UnusedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 dev-tools/vendor/symfony/console/Application.php(175): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#14 dev-tools/vendor/icanhazstring/composer-unused/bin/composer-unused(66): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput))
#15 dev-tools/vendor/icanhazstring/composer-unused/bin/composer-unused(67): {closure}(Array)
#16 dev-tools/vendor/bin/composer-unused(119): include('/Volumes/Projec...')
#17 {main}
  thrown in dev-tools/vendor/composer-unused/symbol-parser/src/Parser/PHP/DefinedSymbolCollector.php on line 65

Originally posted by @Wirone in PHP-CS-Fixer/PHP-CS-Fixer#7536 (comment)

`ConsumedSymbolCollector` accept invalid strategies

Describe the bug

Class constructor of ConsumeSymbolCollector accept everything as input [1]

Additional information

The checks is provided only for IDE with the phpDoc @param array<StrategyInterface> $strategies [2]

Expected results

Display an empty symbol list, because no valid strategy provided.

Current results

Fatal error: Uncaught Error: Call to undefined method FooIsNotValidStrategy::canHandle() in /shared/backups/forks/symbol-parser/src/Parser/PHP/ConsumedSymbolCollector.php on line 43

Script to reproduce issue

<?php

use ComposerUnused\SymbolParser\Parser\PHP\ConsumedSymbolCollector;
use ComposerUnused\SymbolParser\Parser\PHP\SymbolNameParser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;

require_once __DIR__ . '/vendor/autoload.php';

class FooIsNotValidStrategy
{
}

$strategies = [
    new FooIsNotValidStrategy()
];

$symbolNameParser = new SymbolNameParser(
    (new ParserFactory())->createForNewestSupportedVersion(),
    new NameResolver(),
    new ConsumedSymbolCollector($strategies)
);

$code = <<<'CODE'
    <?php
    namespace My\Space;
CODE;

$symbols = iterator_to_array($symbolNameParser->parseSymbolNames($code));

var_dump($symbols);

[1] : https://github.com/composer-unused/symbol-parser/blob/0.2.2/src/Parser/PHP/ConsumedSymbolCollector.php#L31-L34
[2] : https://github.com/composer-unused/symbol-parser/blob/0.2.2/src/Parser/PHP/ConsumedSymbolCollector.php#L29

Undesired namespace consolidation

Describe the bug

If you have two namespaces that have common parts, they are treated like partial imports and are combined into one.

Additional information

This issue superseded to PR #102. Thanks to @ComiR for reporting by its unit tests !

Context

<?php
use My\Space\Using\ForeignUtility;
use ForeignUtility\SpecialClass;

Explains

Now, it's time to explains origin of this issue. (Thanks to git blame to help in search of origin)

This problem was related long time ago by composer-unused/composer-unused#287 and fixed by PR #26.

But consolidation made by PR 26 fixes is not acceptable if you use only UseStrategy in context above.

How To Fix

I'll propose a new PR that will continue to consolidate namespaces in context of composer-unused/composer-unused#287, but also fix this context (even if I don't think there is a real use case: @ComiR feel free to publish a full example where you have imports without classes/objects used)

My PR will apply this https://github.com/composer-unused/symbol-parser/blob/0.2.2/src/Parser/PHP/ConsumedSymbolCollector.php#L59-L88 resolution only if UseStrategy is combined with another one.

And it will apply this https://github.com/composer-unused/symbol-parser/blob/0.1.7/src/Parser/PHP/ConsumedSymbolCollector.php#L58 resolution in other combinations.

PR will follows soon.

Invesitage usage of phpstan to find used/provided symbols

At the moment, the symbol parsing is a custom implementation. This means most of the features PHP provides are not working.
For example:

  • Interfere symbol names from string
    $a = 'MyClass';
    $b = new $a;
    
  • Parse Docblocks

Using phpstan/phpstan we could move around that and use a well tested tool to actually find symbols.

Defined interfaces not recognized by DefinedSymbolCollector

Describe the bug

Object interfaces are not recognized by \ComposerUnused\SymbolParser\Parser\PHP\DefinedSymbolCollector as a symbol provided by a dependency.

Error dump

n/a

Additional information

Given a PHP file with the following content:

<?php

namespace Foo;

interface Template
{
}

Expected a SymbolNameParser with a DefinedSymbolCollector visitor to yield ['Foo\\Template'] on parseSymbolNames, [] returned instead.

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.