Skip to content

Commit

Permalink
Round two!
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Aug 7, 2019
1 parent e237550 commit a78abe9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 58 deletions.
4 changes: 2 additions & 2 deletions bin/composer-require-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

$foundAutoloadFile = true;
$autoloader = require $autoloadFileLocation;
require $autoloadFileLocation;
break;
}

Expand All @@ -28,5 +28,5 @@

use ComposerRequireChecker\Cli\Application;

$application = new Application($autoloader);
$application = new Application();
$application->run();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"ext-phar": "*",
"nikic/php-parser": "^4.0",
"symfony/console": "^3.4.17 | ^4.1.6",
"webmozart/glob": "^4.1"
"webmozart/glob": "^4.1",
"roave/better-reflection": "^3.5"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
Expand Down
4 changes: 2 additions & 2 deletions src/ComposerRequireChecker/Cli/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

class Application extends AbstractApplication
{
public function __construct(ClassLoader $autoloader)
public function __construct()
{
parent::__construct('ComposerRequireChecker', $this->getPackageVersion());

$check = new CheckCommand($autoloader);
$check = new CheckCommand();
$this->add($check);
$this->setDefaultCommand($check->getName());
}
Expand Down
94 changes: 42 additions & 52 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

namespace ComposerRequireChecker\Cli;

use Composer\Autoload\ClassLoader;
use ComposerRequireChecker\ASTLocator\LocateASTFromFiles;
use ComposerRequireChecker\DefinedExtensionsResolver\DefinedExtensionsResolver;
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromASTRoots;
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromExtensions;
use ComposerRequireChecker\DependencyGuesser\DependencyGuesser;
use ComposerRequireChecker\FileLocator\LocateComposerPackageDirectDependenciesSourceFiles;
use ComposerRequireChecker\FileLocator\LocateComposerPackageSourceFiles;
use ComposerRequireChecker\FileLocator\LocateFilesByGlobPattern;
use ComposerRequireChecker\GeneratorUtil\ComposeGenerators;
use ComposerRequireChecker\JsonLoader;
use ComposerRequireChecker\UsedSymbolsLocator\LocateUsedSymbolsFromASTRoots;
use PhpParser\ErrorHandler\Collecting as CollectingErrorHandler;
use PhpParser\ParserFactory;
use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\ConstantReflector;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use Roave\BetterReflection\Reflector\FunctionReflector;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\MakeLocatorForComposerJsonAndInstalledJson;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -26,16 +29,6 @@

class CheckCommand extends Command
{
/** @var ClassLoader */
private $classLoader;

public function __construct(ClassLoader $autoloader)
{
$this->classLoader = $autoloader;

parent::__construct();
}

protected function configure()
{
$this
Expand Down Expand Up @@ -91,59 +84,56 @@ protected function execute(InputInterface $input, OutputInterface $output): int
));
$this->verbose("found " . count($usedSymbols) . " symbols.", $output, true);


$this->verbose("Collecting defined extension symbols... ", $output);
$definedExtensionSymbols = (new LocateDefinedSymbolsFromExtensions())->__invoke(
(new DefinedExtensionsResolver())->__invoke($composerJson, $options->getPhpCoreExtensions())
);
$this->verbose("found " . count($definedExtensionSymbols) . " symbols.", $output, true);

$this->verbose("Collecting defined vendor symbols phase 1... ", $output);
$locator = new MakeLocatorForComposerJsonAndInstalledJson();
$astLocator = (new BetterReflection())->astLocator();
$composerLocator = $locator(dirname($composerJson), $astLocator);
$classReflector = new ClassReflector($composerLocator);
$functionReflector = new FunctionReflector($composerLocator, $classReflector);
$constantReflector = new ConstantReflector($composerLocator, $classReflector);
$whiteList = $options->getSymbolWhitelist();
$unknownSymbols = [];
foreach ($usedSymbols as $usedSymbol) {
if (in_array($usedSymbol, $whiteList)) {
continue;
}

$definedVendorSymbols = (new LocateDefinedSymbolsFromASTRoots())->__invoke($sourcesASTs(
$getAdditionalSourceFiles(iterator_to_array((function ($usedSymbols): iterable{
foreach ($usedSymbols as $usedSymbol) {
$file = $this->classLoader->findFile($usedSymbol);
if (in_array($usedSymbol, $definedExtensionSymbols)) {
continue;
}

if ($file !== false) {
yield $file;
}
}
})($usedSymbols)), ''),
));
try {
$classReflector->reflect($usedSymbol);

$this->verbose("found " . count($definedVendorSymbols) . " symbols.", $output, true);
continue;
} catch (IdentifierNotFound $ignore) {
// void
}

$unknownSymbols = array_diff(
$usedSymbols,
$definedVendorSymbols,
$definedExtensionSymbols,
$options->getSymbolWhitelist()
);
try {
$functionReflector->reflect($usedSymbol);

if (count($unknownSymbols) > 0) {
$this->verbose("Collecting defined vendor symbols phase 2... ", $output);
continue;
} catch (IdentifierNotFound $ignore) {
// void
}

$definedVendorSymbols = (new LocateDefinedSymbolsFromASTRoots())->__invoke($sourcesASTs(
(new ComposeGenerators())->__invoke(
$getAdditionalSourceFiles($options->getScanFiles(), dirname($composerJson)),
$getPackageSourceFiles($composerData, dirname($composerJson)),
(new LocateComposerPackageDirectDependenciesSourceFiles())->__invoke($composerJson)
)
));
try {
$constantReflector->reflect($usedSymbol);

$this->verbose("found " . count($definedVendorSymbols) . " symbols.", $output, true);
}
if (!count($usedSymbols)) {
throw new \LogicException('There were no symbols found, please check your configuration.');
}
continue;
} catch (IdentifierNotFound $ignore) {
// void
}

$this->verbose("Checking for unknown symbols... ", $output, true);
$unknownSymbols = array_diff(
$usedSymbols,
$definedVendorSymbols,
$definedExtensionSymbols,
$options->getSymbolWhitelist()
);
$unknownSymbols[] = $usedSymbol;
}

if (!$unknownSymbols) {
$output->writeln("There were no unknown symbols found.");
Expand Down
2 changes: 1 addition & 1 deletion test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testVerboseSelfCheckShowsCounts()
'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
]);

$this->assertRegExp('/Collecting defined vendor symbols... found \d+ symbols./', $this->commandTester->getDisplay());
// $this->assertRegExp('/Collecting defined vendor symbols... found \d+ symbols./', $this->commandTester->getDisplay());
$this->assertRegExp('/Collecting defined extension symbols... found \d+ symbols./', $this->commandTester->getDisplay());
$this->assertRegExp('/Collecting used symbols... found \d+ symbols./', $this->commandTester->getDisplay());
}
Expand Down

0 comments on commit a78abe9

Please sign in to comment.