-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from sensiolabs-de/feature-directory-collector
Feature directory collector
- Loading branch information
Showing
10 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |