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

processing includes as proposed in #67 #68

Open
wants to merge 21 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
45 changes: 45 additions & 0 deletions src/ComposerRequireChecker/ASTLocator/FileAST.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace ComposerRequireChecker\ASTLocator;

use PhpParser\Node;

class FileAST
{
/**
* @var string
*/
private $file;

/**
* @var array
*/
private $ast;

/**
* @param string $file
* @param array|null $ast
*
*/
public function __construct(string $file, ?array $ast)
{
$this->file = $file;
$this->ast = $ast ?? [];
}

/**
* @return string
*/
public function getFile(): string
{
return $this->file;
}

/**
* @return Node[]
*/
public function getAst(): array
{
return $this->ast;
}
}
4 changes: 2 additions & 2 deletions src/ComposerRequireChecker/ASTLocator/LocateASTFromFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public function __construct(Parser $parser, ?ErrorHandler $errorHandler)
/**
* @param Traversable|string[] $files
*
* @return Traversable|array[] a series of AST roots, one for each given file
* @return Traversable|FileAST[] a series of AST roots, one for each given file
*/
public function __invoke(Traversable $files): Traversable
{
foreach ($files as $file) {
yield $this->parser->parse(file_get_contents($file), $this->errorHandler);
yield new FileAST($file, $this->parser->parse(file_get_contents($file), $this->errorHandler));
}
}
}
28 changes: 23 additions & 5 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
(new LocateComposerPackageDirectDependenciesSourceFiles())->__invoke($composerJson)
)
));
while (count($definedVendorSymbols->getIncludes())) {
(new LocateDefinedSymbolsFromASTRoots())->__invoke($sourcesASTs($definedVendorSymbols->getIncludes()), $definedVendorSymbols);
}

$definedExtensionSymbols = (new LocateDefinedSymbolsFromExtensions())->__invoke(
(new DefinedExtensionsResolver())->__invoke($composerJson, $options->getPhpCoreExtensions())
Expand All @@ -85,13 +88,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
throw new \LogicException('There were no symbols found, please check your configuration.');
}

$unknownSymbols = array_diff(
$usedSymbols,
$definedVendorSymbols,
$definedExtensionSymbols,
$options->getSymbolWhitelist()
return $this->handleResult(
array_diff(
$usedSymbols,
$definedVendorSymbols->getSymbols(),
$definedExtensionSymbols,
$options->getSymbolWhitelist()
),
$output
);
}

/**
* @param array $unknownSymbols
* @param OutputInterface $output
* @return int
*/
private function handleResult(?array $unknownSymbols, OutputInterface $output): int
{
if (!$unknownSymbols) {
$output->writeln("There were no unknown symbols found.");
return 0;
Expand All @@ -113,6 +127,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return ((int)(bool)$unknownSymbols);
}

/**
* @param InputInterface $input
* @return Options
*/
private function getCheckOptions(InputInterface $input): Options
{
$fileName = $input->getOption('config-file');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ComposerRequireChecker\DefinedSymbolsLocator;

use ComposerRequireChecker\NodeVisitor\DefinedSymbolCollector;
use ComposerRequireChecker\NodeVisitor\IncludeCollector;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use Traversable;
Expand All @@ -11,25 +12,31 @@ final class LocateDefinedSymbolsFromASTRoots
{
/**
* @param Traversable|array[] $ASTs a series of AST roots
* @param LocatedSymbolsAndIncludes|null $located previously found data if exists
*
* @return string[] all the found symbols
* @return LocatedSymbolsAndIncludes
*/
public function __invoke(Traversable $ASTs): array
public function __invoke(Traversable $ASTs, ?LocatedSymbolsAndIncludes $located = null): LocatedSymbolsAndIncludes
{
// note: dependency injection is not really feasible for these two, as they need to co-exist in parallel
$traverser = new NodeTraverser();

$traverser->addVisitor(new NameResolver());
$traverser->addVisitor($collector = new DefinedSymbolCollector());
$traverser->addVisitor($includes = new IncludeCollector());

$astSymbols = [];
$additionalFiles = [];

foreach ($ASTs as $astRoot) {
$traverser->traverse($astRoot);

$traverser->traverse($astRoot->getAst());
$astSymbols[] = $collector->getDefinedSymbols();
$additionalFiles = array_merge($additionalFiles, $includes->getIncluded($astRoot->getFile()));
}
$located = $located ?? new LocatedSymbolsAndIncludes();

return array_values(array_unique(array_merge([], ...$astSymbols)));
return $located
->addSymbols($astSymbols)
->setIncludes($additionalFiles);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace ComposerRequireChecker\DefinedSymbolsLocator;

use ArrayIterator;
use Traversable;

class LocatedSymbolsAndIncludes
{
/**
* @var string[]
*/
private $symbols = [];

/**
* @var string[]
*/
private $includes = [];

/**
* @var string[]
*/
private $previousIncludes = [];

/**
* @return string[]
*/
public function getSymbols(): array
{
return $this->symbols;
}

/**
* @return Traversable|string[]
*/
public function getIncludes(): Traversable
{
return new ArrayIterator($this->includes);
}

/**
* @param string[] $symbols
* @return LocatedSymbolsAndIncludes
*/
public function addSymbols(array $symbols): LocatedSymbolsAndIncludes
{
$this->symbols = $this->arrayMergeUnique($this->symbols, $symbols);
return $this;
}

/**
* @param string[] $includes
* @return LocatedSymbolsAndIncludes
*/
public function setIncludes(array $includes): LocatedSymbolsAndIncludes
{
$this->includes = array_diff($includes, $this->previousIncludes);
$this->previousIncludes = $this->arrayMergeUnique($this->previousIncludes, [$includes]);
return $this;
}

/**
* @param array $into
* @param array $add
* @return array
*/
private function arrayMergeUnique(array $into, array $add): array
{
if (empty($add)) {
return $into;
}
return array_values(array_unique(array_merge($into, ...$add)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private function filterFilesByExtension(Traversable $files, string $fileExtensio

/* @var $file \SplFileInfo */
foreach ($files as $file) {
if ($blacklist && preg_match('{('.implode('|', $blacklist).')}', $file->getPathname())) {
if ($blacklist && preg_match('{(' . implode('|', $blacklist) . ')}', $file->getPathname())) {
continue;
}

Expand Down
149 changes: 149 additions & 0 deletions src/ComposerRequireChecker/NodeVisitor/IncludeCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace ComposerRequireChecker\NodeVisitor;

use FilesystemIterator;
use InvalidArgumentException;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\MagicConst\Dir;
use PhpParser\Node\Scalar\MagicConst\File;
use PhpParser\Node\Scalar\String_;
use PhpParser\NodeVisitorAbstract;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

final class IncludeCollector extends NodeVisitorAbstract
{
/**
* @var Expr[]
*/
private $included = [];

/**
* {@inheritDoc}
*/
public function beforeTraverse(array $nodes)
{
$this->included = [];
return parent::beforeTraverse($nodes);
}

/**
* @param string $file
* @return string[]
*/
public function getIncluded(string $file): array
{
$included = [];
foreach ($this->included as $exp) {
try {
$this->computePath($included, $this->processIncludePath($exp, $file), $file);
} catch (InvalidArgumentException $exception) {
// not sure there's anything sensible to do here
}
}
return $included;
}

/**
* @param array $included
* @param string $path
* @param string $self
* @return void
*/
private function computePath(array &$included, string $path, string $self)
{
if (!preg_match('#^([A-Z]:)?/#i', str_replace('\\', '/', $path))) {
$path = dirname($self) . '/' . $path;
}
if (false === strpos($path, '{var}')) {
$included[] = $path;
return;
}
$regex = $this->pathWithVarToRegex($path);
$self = str_replace('\\', '/', $self);
foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
explode('{var}', $path)[0],
FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS
)
) as $file) {
$rfile = str_replace('\\', '/', $file);
if ($rfile !== $self && preg_match('/\\.php$/i', $rfile) && preg_match($regex, $rfile)) {
$included[] = $file;
}
}
}

/**
* @param string $path
* @return string
*/
private function pathWithVarToRegex(string $path): string
{
$parts = explode('{var}', $path);
$regex = [];
foreach ($parts as $part) {
$regex[] = preg_quote(str_replace('\\', '/', $part), '/');
}
return '/^' . implode('.+', $regex) . '$/';
}

/**
* @param string|Expr $exp
* @param string $file
* @return string
* @throws InvalidArgumentException
*/
private function processIncludePath($exp, string $file): string
{
if (is_string($exp)) {
return $exp;
}
if ($exp instanceof String_) {
return $exp->value;
}
if ($exp instanceof Concat) {
return $this->processIncludePath($exp->left, $file) . $this->processIncludePath($exp->right, $file);
}
return $this->replaceInIncludePath($exp, $file);
}

/**
* @param Expr $exp
* @param string $file
* @return string
* @throws InvalidArgumentException
*/
private function replaceInIncludePath($exp, string $file)
{
if ($exp instanceof Dir) {
return dirname($file);
}
if ($exp instanceof File) {
return $file;
}
if ($exp instanceof ConstFetch && "$exp->name" === 'DIRECTORY_SEPARATOR') {
return DIRECTORY_SEPARATOR;
}
if ($exp instanceof Variable || $exp instanceof ConstFetch) {
return '{var}';
}
throw new InvalidArgumentException('can\'t yet handle ' . $exp->getType());
}

/**
* {@inheritDoc}
*/
public function enterNode(Node $node)
{
if ($node instanceof Include_) {
$this->included[] = $node->expr;
}
}
}
Loading