-
Notifications
You must be signed in to change notification settings - Fork 71
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
base: master
Are you sure you want to change the base?
Changes from all commits
326308f
266391f
dbe33eb
99535e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to require the composer DSL to already be in |
||
$this->configVendorDir = $this->normalizePath(dirname($composerJsonPath) . '/' . ($composerJson['config']['vendor-dir'] ?? 'vendor')); | ||
$this->composerAutoloader = include $this->configVendorDir . '/autoload.php'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
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); | ||
} | ||
} |
There was a problem hiding this comment.
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])