Skip to content

Commit

Permalink
first draft to guess dependencies from composer's autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
maglnet committed Oct 11, 2018
1 parent 20a7a3b commit 326308f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased]
### Added
- guess dependencies from composer's autoloader

### Changed

Expand Down
2 changes: 2 additions & 0 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromASTRoots;
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromExtensions;
use ComposerRequireChecker\DependencyGuesser\DependencyGuesser;
use ComposerRequireChecker\DependencyGuesser\GuessFromComposerAutoloader;
use ComposerRequireChecker\FileLocator\LocateComposerPackageDirectDependenciesSourceFiles;
use ComposerRequireChecker\FileLocator\LocateComposerPackageSourceFiles;
use ComposerRequireChecker\FileLocator\LocateFilesByGlobPattern;
Expand Down Expand Up @@ -108,6 +109,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$table = new Table($output);
$table->setHeaders(['unknown symbol', 'guessed dependency']);
$guesser = new DependencyGuesser();
$guesser->addGuesser(new GuessFromComposerAutoloader($composerJson));
foreach ($unknownSymbols as $unknownSymbol) {
$guessedDependencies = [];
foreach ($guesser($unknownSymbol) as $guessedDependency) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public function __construct()
$this->guessers[] = new GuessFromLoadedExtensions();
}

public function addGuesser(GuesserInterface $guesser): void
{
$this->guessers[] = $guesser;
}

public function __invoke($symbolName): \Generator
{
foreach ($this->guessers as $guesser) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace ComposerRequireChecker\DependencyGuesser;


use Composer\Autoload\ClassLoader;

class GuessFromComposerAutoloader implements GuesserInterface
{

/**
* @var ClassLoader
*/
private $composerAutoloader;

private $configVendorDir;

public function __construct(string $composerJsonPath)
{
$composerJson = json_decode(file_get_contents($composerJsonPath), true);
$this->configVendorDir = dirname($composerJsonPath) . '/' . ($composerJson['config']['vendor-dir'] ?? 'vendor');
$this->composerAutoloader = include $this->configVendorDir . '/autoload.php';
}

public function __invoke(string $symbolName): \Generator
{
$fullFileName = $this->composerAutoloader->findFile(ltrim($symbolName, '\\/ '));
if ($fullFileName) {
$fileName = ltrim(substr(realpath($fullFileName), strlen($this->configVendorDir)), '\\/');
$packageName = preg_replace('/^([^\/]+\/[^\/]+).*/', '$1', $fileName);
yield $packageName;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace ComposerRequireCheckerTest\DependencyGuesser;


use ComposerRequireChecker\DependencyGuesser\GuessFromComposerAutoloader;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;

class GuessFromComposerAutoloaderTest extends TestCase
{

/**
* @var GuessFromComposerAutoloader
*/
private $guesser;

public function setUp()
{
parent::setUp();
$dir = dirname(__DIR__, 3);
$this->guesser = new GuessFromComposerAutoloader($dir . '/composer.json');
}

public function testClassWillBeFound()
{
$quessedDependencies = $this->guesser->__invoke(ParserFactory::class);
$guessedDependencies = iterator_to_array($quessedDependencies);

$this->assertCount(1, $guessedDependencies);
$this->assertContains('nikic/php-parser', $guessedDependencies);
}
}

0 comments on commit 326308f

Please sign in to comment.