Skip to content

Commit

Permalink
feat: 解析する深さを制限できるようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokinoue committed Feb 26, 2024
1 parent d572d65 commit 43c2ff8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
'excludeFromAnalysis' => [
'PhpParser\Node\\',
],
'maxDepth' => 5,
];
7 changes: 7 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Config
private static array $endOfAnalysis = [];
/** @var string[] */
private static array $excludeFromAnalysis = [];
private static int $maxDepth = 5;

public static function initialize(string $baseDir): void
{
Expand All @@ -25,6 +26,7 @@ public static function initialize(string $baseDir): void
self::$memoryLimit = $config['memoryLimit'] ?? '1024M';
self::$endOfAnalysis = $config['endOfAnalysis'] ?? [];
self::$excludeFromAnalysis = $config['excludeFromAnalysis'] ?? [];
self::$maxDepth = $config['maxDepth'] ?? 5;
}

public static function memoryLimit(): string
Expand All @@ -47,4 +49,9 @@ public static function excludeFromAnalysis(): array
{
return self::$excludeFromAnalysis;
}

public static function maxDepth(): int
{
return self::$maxDepth;
}
}
2 changes: 1 addition & 1 deletion src/DiagramUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function ancestors(): array {

public function shouldStopTraverse(): bool
{
return $this->isCirculating || $this->isEndOfAnalysis();
return $this->isCirculating || $this->isEndOfAnalysis() || $this->layer === Config::maxDepth();
}

private function isEndOfAnalysis(): bool
Expand Down
24 changes: 24 additions & 0 deletions tests/Visitor/ClassVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Hirokinoue\DependencyVisualizer\Tests\Visitor;

use _HumbugBox72e598a46208\RdKafka\Conf;
use Hirokinoue\DependencyVisualizer\ClassManipulator\ClassLikeNodeFinder;
use Hirokinoue\DependencyVisualizer\ClassManipulator\ClassLikeWrapper;
use Hirokinoue\DependencyVisualizer\ClassManipulator\ClassLoader;
use Hirokinoue\DependencyVisualizer\Config\Config;
use Hirokinoue\DependencyVisualizer\DiagramUnit;
use Hirokinoue\DependencyVisualizer\Exporter\StringExporter;
use Hirokinoue\DependencyVisualizer\Logger;
use Hirokinoue\DependencyVisualizer\Visitor\ClassVisitor;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
Expand Down Expand Up @@ -134,6 +136,28 @@ public function data指定された名前空間を解析しないこと(): array
];
}

/**
* @noinspection NonAsciiCharacters
*/
public function test指定された深さ以上解析しないこと(): void
{
// given
Config::initialize(__DIR__ . '/../data/Visitor/Config/maxDepth');
$sut = new ClassVisitor(new DiagramUnit(
'\Foo',
['\Foo'],
false,
new ClassLikeWrapper(new Class_(new Identifier('Foo'))),
1
));

// when
$sut->enterNode(new FullyQualified('Hirokinoue\DependencyVisualizer\Tests\data\Baz'));

// then
$this->assertSame(2, DiagramUnit::countVisitedClasses());
}

/**
* @return Stmt[]
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/data/Visitor/Config/maxDepth/config.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);
return $config = [
'memoryLimit' => '',
'endOfAnalysis' => [],
'excludeFromAnalysis' => [],
'maxDepth' => 2,
];

0 comments on commit 43c2ff8

Please sign in to comment.