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

Skip already visited files #178

Open
wants to merge 4 commits into
base: main
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
17 changes: 15 additions & 2 deletions src/Parser/PHP/AbstractCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ abstract class AbstractCollector extends NodeVisitorAbstract implements SymbolCo
{
private ?Closure $includeCallback = null;


/** @var array<string, bool> Index of files already visited. */
private static array $visited = [];

public function setFileIncludeCallback(Closure $includeCallback): void
{
$this->includeCallback = $includeCallback;
Expand All @@ -36,11 +40,20 @@ protected function followIncludes(Node $node): void

switch (get_class($expr)) {
case Node\Scalar\String_::class:
($this->includeCallback)(FileInclude::fromScalar($expr));
$fileInclude = FileInclude::fromScalar($expr);
break;
case Node\Expr\BinaryOp\Concat::class:
($this->includeCallback)(FileInclude::fromConcatOperation($expr));
$fileInclude = FileInclude::fromConcatOperation($expr);
break;
default:
return;
}

if (isset(self::$visited[$fileInclude->getPath()])) {
return;
}
self::$visited[$fileInclude->getPath()] = true;

($this->includeCallback)($fileInclude);
}
}
11 changes: 11 additions & 0 deletions tests/Integration/Symbol/Loader/FileSymbolLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FileSymbolLoaderTest extends AbstractIntegrationTestCase
private const ARRAY_NAMESPACE = __DIR__ . '/../../../assets/TestProjects/ArrayNamespace';
private const CLASSMAP_AUTOLOAD = __DIR__ . '/../../../assets/TestProjects/ClassmapAutoload';
private const MISSING_DIRECTORY = __DIR__ . '/../../../assets/TestProjects/MissingDirectory';
private const SELF_REFERENCING = __DIR__ . '/../../../assets/TestProjects/SelfReferencing';

/**
* @test
Expand Down Expand Up @@ -83,4 +84,14 @@ public function itSkipsNonExistingDirectories(): void

self::assertCount(0, $symbols);
}

/**
* @test
*/
public function itSkipsSelfReferencingFiles(): void
{
$symbols = $this->loadDefinedFileSymbols(self::SELF_REFERENCING, [AutoloadType::FILES]);

self::assertCount(0, $symbols);
}
}
44 changes: 44 additions & 0 deletions tests/Unit/Parser/PHP/SymbolNameParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace ComposerUnused\SymbolParser\Test\Unit\Parser\PHP;

use ComposerUnused\SymbolParser\Parser\PHP\DefinedSymbolCollector;
use ComposerUnused\SymbolParser\Parser\PHP\NameResolver;
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\ExtendsParseStrategy;
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\ImplementsParseStrategy;
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\UseStrategy;
use ComposerUnused\SymbolParser\Parser\PHP\SymbolNameParser;
use ComposerUnused\SymbolParser\Test\ParserTestCase;
use PhpParser\ParserFactory;

final class SymbolNameParserTest extends ParserTestCase
{
Expand Down Expand Up @@ -149,4 +153,44 @@ enum Suit
self::assertSame('ezcReflectionFunction', $symbols[2]);
self::assertSame('Suit', $symbols[3]);
}


/**
* @test
* @link https://github.com/composer-unused/symbol-parser/issues/136
*/
public function itSkipsAlreadyVisitedFiles(): void
{
/* @Given a symbol collector which follows includes. */
$includes = [];
$collector = new DefinedSymbolCollector();
$collector->setFileIncludeCallback(function ($file) use (&$includes) {
array_push($includes, $file->getPath());
});
$parser = new SymbolNameParser(
(new ParserFactory())->createForNewestSupportedVersion(),
new NameResolver(),
$collector
);

/* @When the source code includes the same file multiple times. */
$code = <<<CODE
<?php

include 'test1.php';
include 'test2.php';
include 'test3.php';
include 'test1.php'; // should get skipped
include 'test2.php'; // should get skipped
include 'test3.php'; // should get skipped
CODE;
iterator_to_array($parser->parseSymbolNames($code));

/* @And the symbol parser (+ collector) does multiple passes on the source code. */
iterator_to_array($parser->parseSymbolNames($code));

/* @Then the collector only followed each unique file once. */
self::assertCount(3, $includes);
self::assertEquals(['test1.php', 'test2.php', 'test3.php'], $includes);
}
}
7 changes: 7 additions & 0 deletions tests/assets/TestProjects/SelfReferencing/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "self/referencing",

"autoload": {
"files": ["index.php"]
}
}
3 changes: 3 additions & 0 deletions tests/assets/TestProjects/SelfReferencing/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

include 'index.php';