shipmonk-rnd / composer-dependency-analyser

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

Question: Can it be that it has problem if non-standard directories #149

Open tomasnorre opened 5 days ago

tomasnorre commented 5 days ago

Hi,

I'm the maintainer of the TYPO3 Crawler, which used non-standard vendor-dir. Everything is in a .Build directory.

When running the tool. I get

PHP Warning:  foreach() argument must be of type array|object, null given in /home/tomas/Projects/Private/crawler/.Build/vendor/shipmonk/composer-dependency-analyser/src/Analyser.php on line 410

Which indicates from code, that no class loading is found https://github.com/shipmonk-rnd/composer-dependency-analyser/blob/master/src/Analyser.php#L410

My composer.json looks like this for the autload part.

"autoload": {
        "psr-4": {
            "AOE\\Crawler\\": "Classes"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "AOE\\Crawler\\Tests\\": "Tests",
            "TYPO3\\CMS\\Core\\Tests\\": ".Build/vendor/typo3/cms-core/Tests"
        }
    },

Can it be that this tool have problems with projects with non-standard directory structure?

janedbal commented 5 days ago

That PHP Warning is definitelly a bug when no composer autoloader is found (fixed in #150).


But since you have regular composer autoload sections, it should not happen (composer autoloader should be present). Few questions to verify the state:

  1. How did you install this tool and how do you run it?
  2. Did you setup custom vendor dir in your composer.json?
  3. Does this tool output No composer class loader detected! (to stderr) ?
janedbal commented 5 days ago

I assume you are mentioning this repository, right?

Looks like you are using custom classloader instead of composer's one, which currently is not supported. I'll try to add support, but I'm currently getting some class not found failures, so I need to check even that.

PHP Fatal error:  Uncaught Error: Class "AcceptanceTester" not found in /home/honza/Development/tmp-typo3/Tests/Acceptance/Support/Step/Acceptance/Admin.php:23
Stack trace:
#0 /home/honza/Development/tmp-typo3/.Build/vendor/composer/ClassLoader.php(571): include()
#1 /home/honza/Development/tmp-typo3/.Build/vendor/composer/ClassLoader.php(428): Composer\Autoload\includeFile()
#2 /home/honza/Development/tmp-typo3/.Build/vendor/typo3/class-alias-loader/src/ClassAliasLoader.php(137): Composer\Autoload\ClassLoader->loadClass()
#3 /home/honza/Development/tmp-typo3/.Build/vendor/typo3/class-alias-loader/src/ClassAliasLoader.php(125): TYPO3\ClassAliasLoader\ClassAliasLoader->loadClass()
#4 [internal function]: TYPO3\ClassAliasLoader\ClassAliasLoader->loadClassWithAlias()
#5 /home/honza/Development/tmp-typo3/.Build/vendor/shipmonk/composer-dependency-analyser/src/Analyser.php(424): ReflectionClass->__construct()
#6 /home/honza/Development/tmp-typo3/.Build/vendor/shipmonk/composer-dependency-analyser/src/Analyser.php(396): ShipMonk\ComposerDependencyAnalyser\Analyser->detectFileByReflection()
#7 /home/honza/Development/tmp-typo3/.Build/vendor/shipmonk/composer-dependency-analyser/src/Analyser.php(149): ShipMonk\ComposerDependencyAnalyser\Analyser->getSymbolPath()
#8 /home/honza/Development/tmp-typo3/.Build/vendor/shipmonk/composer-dependency-analyser/bin/composer-dependency-analyser(43): ShipMonk\ComposerDependencyAnalyser\Analyser->run()
#9 /home/honza/Development/tmp-typo3/.Build/bin/composer-dependency-analyser(117): include('...')
janedbal commented 5 days ago

Can you test #151? With that, I was able to make the tool work over your repository, but I'm not certain if autoloading is working correctly there. I'll check that more deeply tomorrow.

janedbal commented 4 days ago

The main problem is that the autoloader the package uses (ClassAliasLoader) is broken and is unable to load classes referenced in the codebase (failing on Fatal Error).


You can exclude paths that references those broken symbols to make it work, but it will definitelly affect the results:


use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;

$config = new Configuration();

return $config->addPathsToExclude([
    __DIR__ . '/.Build/vendor/typo3/cms-core/Tests',
    __DIR__ . '/Tests/Acceptance',
]);

Another idea is to enable composer's classloader for the analysis (which seems to work):

use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;

/** @var \Composer\Autoload\ClassLoader $classLoader */
$classLoader = require __DIR__ . '/.Build/vendor/autoload.php';
$classLoader->register();

return new Configuration();
tomasnorre commented 4 days ago

Thanks for all you information. Will try to set it up according to your advice.