Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] first draft to guess dependencies from composer's autoloader #91

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
- add symbol counts to check command for verbose output

### Changed
Expand Down
3 changes: 2 additions & 1 deletion 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 @@ -114,7 +115,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln("The following unknown symbols were found:");
$table = new Table($output);
$table->setHeaders(['unknown symbol', 'guessed dependency']);
$guesser = new DependencyGuesser();
$guesser = new DependencyGuesser(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 @@ -10,9 +10,12 @@ class DependencyGuesser
*/
private $guessers = [];

public function __construct()
public function __construct(GuesserInterface ...$guessers)
{
$this->guessers[] = new GuessFromLoadedExtensions();
foreach ($guessers as $guesser) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->guessers = array_merge([new GuessFromLoadedExtensions(), $guessers])

$this->guessers[] = $guesser;
}
}

public function __invoke($symbolName): \Generator
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace ComposerRequireChecker\DependencyGuesser;


use Composer\Autoload\ClassLoader;

class GuessFromComposerAutoloader implements GuesserInterface
{

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

private $configVendorDir;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type declaration


public function __construct(string $composerJsonPath)
{
$composerJson = json_decode(file_get_contents($composerJsonPath), true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to require the composer DSL to already be in array shape, instead of adding I/O and nullability in here

$this->configVendorDir = $this->normalizePath(dirname($composerJsonPath) . '/' . ($composerJson['config']['vendor-dir'] ?? 'vendor'));
$this->composerAutoloader = include $this->configVendorDir . '/autoload.php';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very risky, and it can lead to pre-loading analysed code. So far, the library limited itself to using the AST.

If you need to add composer-based file location, maybe check Roave/BackwardCompatibilityCheck#102, where I implemented an abstraction for PSR-4, PSR-0, classmap and files loading via BetterReflection. Still extremely slow, but since we need such an abstraction in BetterReflection itself, putting efforts there would be better.

}

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

private function normalizePath(string $path): string
{
return str_replace('\\', '/', $path);
}

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

namespace ComposerRequireCheckerTest\DependencyGuesser;


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

class GuessFromComposerAutoloaderTest extends TestCase
{

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

public function setUp()
{
$dir = dirname(__DIR__, 3);
$this->guesser = new DependencyGuesser(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);
}
}