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

adding option to manually register psr0/psr4-like dependency autoloading #67

Open
wants to merge 6 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ phpdox.xml
clover.xml
example/test-data
phing-latest.phar
build/
build/
28 changes: 26 additions & 2 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ protected function configure()
'the composer.json of your package, that should be checked',
'./composer.json'
)
->addOption(
->addOption(
'ignore-parse-errors',
null,
InputOption::VALUE_NONE,
'this will cause ComposerRequireChecker to ignore errors when files cannot be parsed, otherwise'
. ' errors will be thrown'
)
->addOption(
'register-namespace',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'vendor/package:namespace:path as if it was psr4'
);
}

Expand All @@ -64,13 +70,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$options = $this->getCheckOptions($input);

$getPackageSourceFiles = new LocateComposerPackageSourceFiles();
$manualRegistered = $this->buildManualList($input, $composerJson);

$sourcesASTs = $this->getASTFromFilesLocator($input);

$definedVendorSymbols = (new LocateDefinedSymbolsFromASTRoots())->__invoke($sourcesASTs(
(new ComposeGenerators())->__invoke(
$getPackageSourceFiles($composerJson),
(new LocateComposerPackageDirectDependenciesSourceFiles())->__invoke($composerJson)
(new LocateComposerPackageDirectDependenciesSourceFiles())->__invoke($composerJson, $manualRegistered)
)
));

Expand Down Expand Up @@ -145,4 +152,21 @@ private function getASTFromFilesLocator(InputInterface $input): LocateASTFromFil
return $sourcesASTs;
}

/**
* @return array
*/
private function buildManualList(InputInterface $input, $composerJson)
{
if (!$input->hasOption('register-namespace')) {
return [];
}
$packageDir = dirname($composerJson);
$namespaces = [];
foreach ($input->getOption('register-namespace') as $option) {
if (preg_match('!^([^/:]+(/[^/:]+)+):([^:]+):([^:]+)$!i', $option, $matches)) {
$namespaces[$packageDir . '/vendor/' . $matches[1]][$matches[3]] = $matches[4];
}
}
return $namespaces;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class LocateComposerPackageDirectDependenciesSourceFiles
{
public function __invoke(string $composerJsonPath): Generator
public function __invoke(string $composerJsonPath, array $manual = []): Generator
{
$packageDir = dirname($composerJsonPath);

Expand All @@ -22,7 +22,10 @@ function (string $vendorName) use ($packageDir) {
continue;
}

yield from (new LocateComposerPackageSourceFiles())->__invoke($vendorDir . '/composer.json');
yield from (new LocateComposerPackageSourceFiles())->__invoke(
$vendorDir . '/composer.json',
$manual[$vendorDir] ?? []
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class LocateComposerPackageSourceFiles
{
public function __invoke(string $composerJsonPath): Generator
public function __invoke(string $composerJsonPath, array $manual = []): Generator
{
$packageDir = dirname($composerJsonPath);
$composerData = json_decode(file_get_contents($composerJsonPath), true);
Expand All @@ -29,6 +29,10 @@ public function __invoke(string $composerJsonPath): Generator
$this->getFilePaths($composerData['autoload']['psr-4'] ?? [], $packageDir),
$blacklist
);
yield from $this->locateFilesInFilesInFilesDefinitions(
$this->getFilePaths($manual, $packageDir),
$blacklist
);
}

private function getFilePaths(array $sourceDirs, string $packageDir): array
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace ComposerRequireCheckerTest\Cli;

use ComposerRequireChecker\Cli\CheckCommand;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionMethod;
use Symfony\Component\Console\Input\InputInterface;

class CheckCommandGetManualListTest extends TestCase
{
/**
* @return array
*/
public function provideBuildManualList():array
{
$method = (new ReflectionClass(CheckCommand::class))->getMethod('buildManualList');
$method->setAccessible(true);
return [
[$this->getInputMockForManualList(), $method, []],
[$this->getInputMockForManualList(
['abc', 'nope', 'abc/hi:Abc\\:src', 'abc/def/qoi:Qui\\:src/lib/ext', 'abc/def/:Qui\\:src/lib/ext']),
$method,
[__DIR__ . '/vendor/abc/def/qoi', __DIR__ . '/vendor/abc/hi']
],
];
}

/**
* @param array $return
* @return InputInterface
*/
private function getInputMockForManualList(array $return = []):InputInterface
{
$input = $this->getMockBuilder(InputInterface::class)->getMock();
$hasReturn = count($return) > 0;
$input->expects($this->once())
->method('hasOption')
->with(/** @scrutinizer ignore-type */'register-namespace')
->willReturn($hasReturn);
$input->expects($hasReturn ? $this->once() : $this->never())
->method('getOption')
->with(/** @scrutinizer ignore-type */'register-namespace')
->willReturn($return);
return $input;
}

/**
* @dataProvider provideBuildManualList
* Since the command does so much, there's no reasonable way to supply test data
* @test
*/
public function testBuildManualList(InputInterface $input, ReflectionMethod $method, array $expected)
{
$instance = new CheckCommand();
$result = $method->invoke($instance, $input, __FILE__);
$this->assertInternalType('array', $result);
$this->assertCount(count($expected), $result);
$this->assertCount(0, array_diff(array_keys($result), $expected));
}
}
3 changes: 2 additions & 1 deletion test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ComposerRequireCheckerTest\Cli;

use ComposerRequireChecker\Cli\Application;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

Expand All @@ -24,7 +25,7 @@ public function setUp()

public function testExceptionIfComposerJsonNotFound()
{
self::expectException(\InvalidArgumentException::class);
self::expectException(InvalidArgumentException::class);

$this->commandTester->execute([
'composer-json' => 'this-will-not-be-found.json'
Expand Down