Skip to content

Commit

Permalink
Allow to pass an array of files instead of a single file
Browse files Browse the repository at this point in the history
Generally we have multiple changed files, so this makes it easier to
use that.
  • Loading branch information
BackEndTea committed Aug 10, 2019
1 parent c985187 commit f424926
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $finder = new DependencyFinder([__DIR__.'/src/', './vendor/psr/container/src',

$finder->build();

$deps = $finder->getAllFilesDependingOn('./tests/Fixtures/Circular/A.php');
$deps = $finder->getAllFilesDependingOn(['./tests/Fixtures/Circular/A.php']);

foreach ($deps as $dep) {
var_dump($dep);
Expand Down
11 changes: 8 additions & 3 deletions src/Dependency/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use function array_key_exists;
use function array_push;
use function realpath;

class DependencyResolver
{
Expand All @@ -32,17 +33,21 @@ class DependencyResolver
private $declareMap = [];

/**
* @param array<string> $fileNames
* @param array<string, array<string>> $declareMap
* @param array<string, array<string>> $dependantMap
*
* @return array<string>
*/
public function resolve(string $fileName, array $declareMap, array $dependantMap) : array
public function resolve(array $fileNames, array $declareMap, array $dependantMap) : array
{
$this->declareMap = $declareMap;
$this->dependantMap = $dependantMap;
foreach ($this->declareMap[$fileName]?? [] as $class) {
$this->resolveClass($class);
foreach ($fileNames as $fileName) {
$fileName = realpath($fileName);
foreach ($this->declareMap[$fileName]?? [] as $class) {
$this->resolveClass($class);
}
}

return $this->knownDependencies;
Expand Down
12 changes: 4 additions & 8 deletions src/DependencyFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\Finder\SplFileInfo;
use function array_unique;
use function file_get_contents;
use function realpath;

class DependencyFinder
{
Expand Down Expand Up @@ -84,16 +83,13 @@ public function build() : void
}

/**
* @param array<string> $fileNames
*
* @return array<string>
*/
public function getAllFilesDependingOn(string $fileName) : array
public function getAllFilesDependingOn(array $fileNames) : array
{
$path = realpath($fileName);
if ($path === false) {
return [];
}

return(new DependencyResolver())->resolve($path, $this->declareMap, $this->dependantMap);
return(new DependencyResolver())->resolve($fileNames, $this->declareMap, $this->dependantMap);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/DependencyFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testItCanDealWithCircularDependencies() : void
$finder = new DependencyFinder([__DIR__ . '/Fixtures/Circular']);

$finder->build();
$deps = $finder->getAllFilesDependingOn(__DIR__ . '/Fixtures/Circular/A.php');
$deps = $finder->getAllFilesDependingOn([__DIR__ . '/Fixtures/Circular/A.php']);

$this->assertCount(2, $deps);
}
Expand All @@ -24,7 +24,7 @@ public function testAWrongFileReturnsNoDependencies() : void
$finder = new DependencyFinder([__DIR__ . '/Fixtures/Circular']);
$finder->build();

$deps = $finder->getAllFilesDependingOn(__FILE__ . 'asdf');
$deps = $finder->getAllFilesDependingOn([__FILE__ . 'asdf']);
$this->assertCount(0, $deps);
}
}
50 changes: 50 additions & 0 deletions tests/PHPParser/Visitor/DeclarationCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Depend\Test\PHPParser\Visitor;

use Depend\PHPParser\Visitor\DeclarationCollector;
use Depend\PHPParser\Visitor\NameCollector;
use Depend\PHPParser\Visitor\ParentConnectorVisitor;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;

class DeclarationCollectorTest extends TestCase
{

public function testItFindsAllDeclaredNames(): void
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$nodes = $parser->parse(<<<'PHP'
<?php
namespace F;
class Foo
{
public function __construct(Bar\Baz $a) {}
public function hello(\Full\Name $b): string
{
return false;
}
}
interface B{}
PHP
);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor(new ParentConnectorVisitor());
$declareCollector = new DeclarationCollector();
$traverser->addVisitor($declareCollector);
$traverser->traverse($nodes);

$this->assertCount(2, $declareCollector->declared);
$this->assertSame([
'F\Foo',
'F\B',
], $declareCollector->declared);
}

}

0 comments on commit f424926

Please sign in to comment.