shipmonk-rnd / composer-dependency-analyser

🚀 Fast detection of composer dependency issues (unused dependencies, shadow dependencies, misplaced dependencies)
MIT License
400 stars 10 forks source link

Allow setting location of unknown functions file #147

Closed ruudk closed 3 months ago

ruudk commented 3 months ago

While using Symfony's PHP configuration, you can use their special functions that are automatically loaded.

These functions are defined in the ContainerConfigurator.

But when running the analyzer, it complains that these functions are unknown:

$ vendor/bin/composer-dependency-analyser
Using config composer-dependency-analyser.php

Unknown functions!
(those are not declared, so we cannot check them)

  • Symfony\Component\DependencyInjection\Loader\Configurator\env
    in config/packages/prod/sentry.php:12 (+ 210 more)

  • Symfony\Component\DependencyInjection\Loader\Configurator\inline_service
    in config/packages/httplug.php:37 (+ 275 more)

  • Symfony\Component\DependencyInjection\Loader\Configurator\param
    in config/packages/prod/framework.php:17 (+ 205 more)

  • Symfony\Component\DependencyInjection\Loader\Configurator\service
    in config/packages/httplug.php:39 (+ 498 more)

So I have to do this:

$config->ignoreUnknownFunctions([
    'Symfony\Component\DependencyInjection\Loader\Configurator\env',
    'Symfony\Component\DependencyInjection\Loader\Configurator\inline_service',
    'Symfony\Component\DependencyInjection\Loader\Configurator\param',
    'Symfony\Component\DependencyInjection\Loader\Configurator\service',
    'Symfony\Component\DependencyInjection\Loader\Configurator\service_locator',
]);

In order to ignore this. It works.

It would be great if I could say something like this:

$config->importUnknownFunctionsFromFile(__DIR__ . '/vendor/symfony/dependency-injection/Loader/Configurator/ContainerConfigurator.php');

That way, any new function that Symfony will provide, will automatically work too.

janedbal commented 3 months ago

Interesting approach of Symfony. Typically, packages declare files with function declarations to autoload.files section so that it get required during composer init. Then, such functions are available during runtime. I dont know the reason why those functions are not there, but I'd probably solve that just by requiring it manually in the PHP config:

<?php

use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;

require __DIR__ . '/vendor/symfony/dependency-injection/Loader/Configurator/ContainerConfigurator.php'; // function declarations inside

$config = new Configuration();

return $config->ignoreErrorsOnPath(...);

Implementing importUnknownFunctionsFromFile without actually requiring the file would be non-trivial. And implementing that the way that it would just require the file feels useless.

ruudk commented 3 months ago

@janedbal Why didn't I think of this. So simple and pragmatic. It works, thanks! 💙