Skip to content

Commit

Permalink
Merge pull request #147 from sensiolabs-de/feature-directory-collector
Browse files Browse the repository at this point in the history
Feature directory collector
  • Loading branch information
timglabisch authored Feb 28, 2018
2 parents 0f1b761 + ec7b180 commit 60f0e5c
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ this rule was violated.
1. [Different layers and different views](#different-layers-and-different-views)
1. [Collectors](#collectors)
1. [`className` Collector](#classname-collector)
1. [`directory` Collector](#directory-collector)
1. [`bool` Collector](#bool-collector)
1. [More collectors](#more-collectors)
1. [Formatters](#formatters)
Expand Down Expand Up @@ -424,6 +425,22 @@ layers:
Every classname that matches the regular expression `.*Controller.*` becomes a part of the *controller* layer.


### `directory` Collector

The `directory` collector allows collecting classes by matching their file path they are declared in to a regular expression.
Any matching class will be added to the assigned layer.

```yaml
layers:
- name: Controller
collectors:
- type: directory
regex: src/Controller/.*
```

Every file path that matches the regular expression `src/Controller/.*` becomes a part of the *controller* layer.


### `bool` collector

The `bool` collector allows combining other collectors with or without negation.
Expand Down
20 changes: 20 additions & 0 deletions examples/DirectoryLayer.depfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
paths: ["./examples/Layer1/", "./examples/Layer2/"]
exclude_files: []
layers:
- name: Controller
collectors:
- type: className
regex: .*Controller.*
- name: Layer1
collectors:
- type: directory
regex: ./examples/Layer1/.*
- name: Layer2
collectors:
- type: directory
regex: ./examples/Layer2/.*
ruleset:
Controller:
- Layer1
Layer1:
- Layer2
28 changes: 28 additions & 0 deletions examples/Layer1/AnotherClassLikeAController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace examples\Layer1;

use examples\Layer2\SomeOtherClass;

class AnotherClassLikeAController
{
/**
* @var SomeClass
*/
private $someClass;

/**
* @var SomeOtherClass
*/
private $someOtherClass;

/**
* @param SomeClass $someClass
* @param SomeOtherClass $someOtherClass
*/
public function __construct(SomeClass $someClass, SomeOtherClass $someOtherClass)
{
$this->someClass = $someClass;
$this->someOtherClass = $someOtherClass;
}
}
21 changes: 21 additions & 0 deletions examples/Layer1/SomeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace examples\Layer1;

use examples\Layer2\SomeOtherClass;

class SomeClass
{
/**
* @var SomeOtherClass
*/
private $someOtherClass;

/**
* @param SomeOtherClass $someOtherClass
*/
public function __construct(SomeOtherClass $someOtherClass)
{
$this->someOtherClass = $someOtherClass;
}
}
21 changes: 21 additions & 0 deletions examples/Layer1/SomeClass2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace examples\Layer1;

use examples\Layer2\SomeOtherClass2;

class SomeClass2
{
/**
* @var SomeOtherClass2
*/
private $someOtherClass2;

/**
* @param SomeOtherClass2 $someOtherClass2
*/
public function __construct(SomeOtherClass2 $someOtherClass2)
{
$this->someOtherClass2 = $someOtherClass2;
}
}
21 changes: 21 additions & 0 deletions examples/Layer2/SomeOtherClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace examples\Layer2;

use examples\Layer1\SomeClass;

class SomeOtherClass
{
/**
* @var SomeClass
*/
private $someClass;

/**
* @param SomeClass $someClass
*/
public function __construct(SomeClass $someClass)
{
$this->someClass = $someClass;
}
}
21 changes: 21 additions & 0 deletions examples/Layer2/SomeOtherClass2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace examples\Layer2;

use examples\Layer1\SomeClass2;

class SomeOtherClass2
{
/**
* @var SomeClass2
*/
private $someClass2;

/**
* @param SomeClass2 $someClass2
*/
public function __construct(SomeClass2 $someClass2)
{
$this->someClass2 = $someClass2;
}
}
4 changes: 4 additions & 0 deletions services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<tag name="collector" />
</service>

<service id="collector_directory" class="SensioLabs\Deptrac\Collector\DirectoryCollector">
<tag name="collector"></tag>
</service>

<service id="command_analyze" class="SensioLabs\Deptrac\Command\AnalyzeCommand">
<argument type="service" id="dispatcher"/>
<argument type="service" id="astrunner"/>
Expand Down
43 changes: 43 additions & 0 deletions src/Collector/DirectoryCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace SensioLabs\Deptrac\Collector;

use SensioLabs\AstRunner\AstMap;
use SensioLabs\AstRunner\AstParser\AstClassReferenceInterface;
use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\AstRunner\AstParser\NikicPhpParser\AstFileReference;
use SensioLabs\Deptrac\CollectorFactory;

class DirectoryCollector implements CollectorInterface
{
public function getType(): string
{
return 'directory';
}

private function getRegexByConfiguration(array $configuration)
{
if (!isset($configuration['regex'])) {
throw new \LogicException('DirectoryCollector needs the regex configuration.');
}

return $configuration['regex'];
}

public function satisfy(
array $configuration,
AstClassReferenceInterface $abstractClassReference,
AstMap $astMap,
CollectorFactory $collectorFactory,
AstParserInterface $astParser
): bool
{
$fileReference = $abstractClassReference->getFileReference();
assert($fileReference instanceof AstFileReference);
return preg_match(
'#'.$this->getRegexByConfiguration($configuration).'#i',
$fileReference->getFilepath(),
$collectorFactory
);
}
}
47 changes: 47 additions & 0 deletions src/Tests/Collector/DirectoryCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace SensioLabs\Deptrac\Tests\Collector;

use SensioLabs\AstRunner\AstMap;
use SensioLabs\AstRunner\AstParser\AstClassReferenceInterface;
use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\AstRunner\AstParser\NikicPhpParser\AstFileReference;
use SensioLabs\Deptrac\Collector\DirectoryCollector;
use SensioLabs\Deptrac\CollectorFactory;

class DirectoryCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testType()
{
$this->assertEquals('directory', (new DirectoryCollector())->getType());
}

public function dataProviderStatisfy()
{
yield [['regex' => 'foo/layer1/.*'], 'foo/layer1/bar.php', true];
yield [['regex' => 'foo/layer1/.*'], 'foo/layer1/dir/bar.php', true];
yield [['regex' => 'foo/layer1/.*'], 'foo/layer2/bar.php', false];
}

/**
* @dataProvider dataProviderStatisfy
*/
public function testSatisfy($configuration, $filePath, $expected)
{
$fileReference = $this->prophesize(AstFileReference::class);
$fileReference->getFilepath()->willReturn($filePath);

$astClassReference = $this->prophesize(AstClassReferenceInterface::class);
$astClassReference->getFileReference()->willReturn($fileReference->reveal());

$stat = (new DirectoryCollector())->satisfy(
$configuration,
$astClassReference->reveal(),
$this->prophesize(AstMap::class)->reveal(),
$this->prophesize(CollectorFactory::class)->reveal(),
$this->prophesize(AstParserInterface::class)->reveal()
);

$this->assertEquals($expected, $stat);
}
}

0 comments on commit 60f0e5c

Please sign in to comment.