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

Feature/directory collector #108

Closed
Closed
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
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
40 changes: 40 additions & 0 deletions src/Collector/DirectoryCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace SensioLabs\Deptrac\Collector;

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

class DirectoryCollector implements CollectorInterface
{
public function getType()
{
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
)
{
return preg_match(
'#'.$this->getRegexByConfiguration($configuration).'#i',
$abstractClassReference->getFileReference()->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);
}
}