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

New collector for glob patterns #899

Merged
merged 1 commit into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use Qossmic\Deptrac\Layer\Collector\DirectoryCollector;
use Qossmic\Deptrac\Layer\Collector\ExtendsCollector;
use Qossmic\Deptrac\Layer\Collector\FunctionNameCollector;
use Qossmic\Deptrac\Layer\Collector\GlobCollector;
use Qossmic\Deptrac\Layer\Collector\ImplementsCollector;
use Qossmic\Deptrac\Layer\Collector\InheritanceLevelCollector;
use Qossmic\Deptrac\Layer\Collector\InheritsCollector;
Expand Down Expand Up @@ -220,6 +221,12 @@
$services
->set(FunctionNameCollector::class)
->tag('collector', ['type' => CollectorTypes::TYPE_FUNCTION_NAME]);
$services
->set(GlobCollector::class)
->args([
'$basePath' => param('projectDirectory'),
])
->tag('collector', ['type' => CollectorTypes::TYPE_GLOB]);
$services
->set(ImplementsCollector::class)
->tag('collector', ['type' => CollectorTypes::TYPE_IMPLEMENTS]);
Expand Down
13 changes: 13 additions & 0 deletions docs/collectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ parameters:
value: .*array_.*
```

## `glob` Collector

The `glob` collector finds all files matching the provided glob pattern.

```yaml
deptrac:
layers:
- name: Repositories
collectors:
- type: glob
value: src/Modules/**/Repository
```

## `implements` Collector

The `implements` collector allows collecting classes implementing a specified
Expand Down
15 changes: 15 additions & 0 deletions docs/examples/GlobCollector.depfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
deptrac:
paths: ["./Layer1/", "./Layer2/"]
exclude_files: []
layers:
- name: Controller
collectors:
- type: className
value: .*Controller.*
- name: Class
collectors:
- type: glob
value: Layer*/*Class*.php
ruleset:
Controller:
- Class
1 change: 1 addition & 0 deletions src/Layer/Collector/CollectorTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class CollectorTypes
public const TYPE_DIRECTORY = 'directory';
public const TYPE_EXTENDS = 'extends';
public const TYPE_FUNCTION_NAME = 'functionName';
public const TYPE_GLOB = 'glob';
public const TYPE_IMPLEMENTS = 'implements';
public const TYPE_INHERITANCE = 'inheritanceLevel';
public const TYPE_INHERITS = 'inherits';
Expand Down
46 changes: 46 additions & 0 deletions src/Layer/Collector/GlobCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Qossmic\Deptrac\Layer\Collector;

use LogicException;
use Qossmic\Deptrac\Ast\AstMap\AstMap;
use Qossmic\Deptrac\Ast\AstMap\TokenReferenceInterface;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\Glob;

final class GlobCollector extends RegexCollector
{
private string $basePath;

public function __construct(string $basePath)
{
$this->basePath = Path::normalize($basePath);
}

public function satisfy(array $config, TokenReferenceInterface $reference, AstMap $astMap): bool
{
$fileReference = $reference->getFileReference();

if (null === $fileReference) {
return false;
}

$filePath = $fileReference->getFilepath();
$validatedPattern = $this->getValidatedPattern($config);
$normalizedPath = Path::normalize($filePath);
$relativeFilePath = Path::makeRelative($normalizedPath, $this->basePath);

return 1 === preg_match($validatedPattern, $relativeFilePath);
}

protected function getPattern(array $config): string
{
if (!isset($config['value']) || !is_string($config['value'])) {
throw new LogicException('GlobCollector needs the glob pattern configuration.');
}

return Glob::toRegex($config['value']);
}
}
49 changes: 49 additions & 0 deletions tests/Layer/Collector/GlobCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tests\Qossmic\Deptrac\Layer\Collector;

use PHPUnit\Framework\TestCase;
use Qossmic\Deptrac\Ast\AstMap\AstMap;
use Qossmic\Deptrac\Ast\AstMap\File\FileReferenceBuilder;
use Qossmic\Deptrac\Layer\Collector\GlobCollector;

final class GlobCollectorTest extends TestCase
{
private GlobCollector $collector;

protected function setUp(): void
{
parent::setUp();

$this->collector = new GlobCollector(__DIR__);
}

public function dataProviderSatisfy(): iterable
{
yield [['value' => 'foo/layer1/*'], 'foo/layer1/bar.php', true];
yield [['value' => 'foo/*/*.php'], 'foo/layer1/bar.php', true];
yield [['value' => 'foo/**/*'], 'foo/layer1/dir/bar.php', true];
yield [['value' => 'foo/layer1/*'], 'foo/layer2/bar.php', false];
yield [['value' => 'foo/layer2/*'], 'foo\\layer2\\bar.php', true];
}

/**
* @dataProvider dataProviderSatisfy
*/
public function testSatisfy(array $configuration, string $filePath, bool $expected): void
{
$fileReferenceBuilder = FileReferenceBuilder::create($filePath);
$fileReferenceBuilder->newClassLike('Test');
$fileReference = $fileReferenceBuilder->build();

$actual = $this->collector->satisfy(
$configuration,
$fileReference->getClassLikeReferences()[0],
new AstMap([])
);

self::assertSame($expected, $actual);
}
}