Skip to content

Commit

Permalink
#139 - use your own regex pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
smoench committed May 15, 2018
1 parent 3b645f6 commit ad61b47
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 40 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,11 @@ layers:
collectors:
- type: className
regex: .*Controller.*
predefined_delimiter: true
```

Every classname that matches the regular expression `.*Controller.*` becomes a part of the *controller* layer.
Every class name that matches the regular expression `.*Controller.*` becomes a part of the *controller* layer.
Set `predefined_delimiter` to `false` to use your own delimiters and/or modifiers.


### `directory` Collector
Expand All @@ -436,9 +438,11 @@ layers:
collectors:
- type: directory
regex: src/Controller/.*
predefined_delimiter: true
```

Every file path that matches the regular expression `src/Controller/.*` becomes a part of the *controller* layer.
Set `predefined_delimiter` to `false` to use your own delimiters and/or modifiers.


### `bool` collector
Expand Down
29 changes: 16 additions & 13 deletions src/Collector/ClassNameCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@ public function getType(): string
return 'className';
}

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

return $configuration['regex'];
}

public function satisfy(
array $configuration,
AstClassReferenceInterface $abstractClassReference,
AstMap $astMap,
CollectorFactory $collectorFactory,
AstParserInterface $astParser
): bool {
return 1 === preg_match(
'/'.$this->getRegexByConfiguration($configuration).'/i',
$abstractClassReference->getClassName()
);
return 1 === preg_match($this->getPattern($configuration), $abstractClassReference->getClassName());
}

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

$pattern = $configuration['regex'];

if ($configuration['predefined_delimiter'] ?? true) {
$pattern = '/'.$pattern.'/i';
}

return $pattern;
}
}
15 changes: 9 additions & 6 deletions src/Collector/DirectoryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ public function satisfy(
$fileReference = $abstractClassReference->getFileReference();
assert($fileReference instanceof AstFileReference);

return 1 === preg_match(
'#'.$this->getRegexByConfiguration($configuration).'#i',
$fileReference->getFilepath()
);
return 1 === preg_match($this->getPattern($configuration), $fileReference->getFilepath());
}

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

return $configuration['regex'];
$pattern = $configuration['regex'];

if ($configuration['predefined_delimiter'] ?? true) {
$pattern = '#'.$pattern.'#i';
}

return $pattern;
}
}
30 changes: 17 additions & 13 deletions src/Collector/MethodCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ public function getType(): string
return 'method';
}

private function getMethodNameRegexByConfiguration(array $configuration)
{
if (!isset($configuration['name'])) {
throw new \LogicException('MethodCollector needs the name configuration.');
}

return $configuration['name'];
}

public function satisfy(
array $configuration,
AstClassReferenceInterface $classReference,
Expand All @@ -40,16 +31,29 @@ public function satisfy(

/** @var ClassMethod[] $classMethods */
$classMethods = $astParser->findNodesOfType((array) $ast, ClassMethod::class);
$pattern = $this->getPattern($configuration);

foreach ($classMethods as $classMethod) {
if (1 === preg_match(
'/'.$this->getMethodNameRegexByConfiguration($configuration).'/i',
$classMethod->name
)) {
if (1 === preg_match($pattern, $classMethod->name)) {
return true;
}
}

return false;
}

private function getPattern(array $configuration)
{
if (!isset($configuration['name'])) {
throw new \LogicException('MethodCollector needs the name configuration.');
}

$pattern = $configuration['name'];

if ($configuration['predefined_delimiter'] ?? true) {
$pattern = '/'.$pattern.'/i';
}

return $pattern;
}
}
8 changes: 5 additions & 3 deletions tests/Collector/ClassNameCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

class ClassNameCollectorTest extends TestCase
{
public function dataProviderStatisfy()
public function dataProviderSatisfy()
{
yield [['regex' => 'a'], 'foo\bar', true];
yield [['regex' => 'a'], 'foo\bbr', false];
yield [['regex' => '/^Foo\\\\Bar$/i', 'predefined_delimiter' => false], 'Foo\\Bar', true];
yield [['regex' => '/^Foo\\\\Bar$/i', 'predefined_delimiter' => false], 'Foo\\Baz', false];
}

public function testType()
Expand All @@ -23,9 +25,9 @@ public function testType()
}

/**
* @dataProvider dataProviderStatisfy
* @dataProvider dataProviderSatisfy
*/
public function testStatisfy($configuration, $className, $expected)
public function testSatisfy(array $configuration, string $className, bool $expected)
{
$astClassReference = $this->prophesize(AstClassReferenceInterface::class);
$astClassReference->getClassName()->willReturn($className);
Expand Down
6 changes: 4 additions & 2 deletions tests/Collector/DirectoryCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ public function testType()
$this->assertEquals('directory', (new DirectoryCollector())->getType());
}

public function dataProviderStatisfy()
public function dataProviderSatisfy()
{
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];
yield [['regex' => '#^foo/bar.php$#','predefined_delimiter' => false], 'foo/bar.php', true];
yield [['regex' => '#^foo/bar.php$#','predefined_delimiter' => false], 'foo/baz.php', false];
}

/**
* @dataProvider dataProviderStatisfy
* @dataProvider dataProviderSatisfy
*/
public function testSatisfy(array $configuration, string $filePath, bool $expected)
{
Expand Down
16 changes: 14 additions & 2 deletions tests/Collector/MethodCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class MethodCollectorTest extends TestCase
{
public function dataProviderStatisfy()
public function dataProviderSatisfy()
{
yield [
['name' => 'abc'],
Expand Down Expand Up @@ -41,6 +41,18 @@ public function dataProviderStatisfy()
],
false,
];

yield [
['name' => '/^fooBar$/', 'predefined_delimiter' => false],
[$this->getClassMethod('fooBar')],
true,
];

yield [
['name' => '/^fooBar$/', 'predefined_delimiter' => false],
[$this->getClassMethod('fooBaz')],
false,
];
}

public function testType()
Expand All @@ -57,7 +69,7 @@ private function getClassMethod($name)
}

/**
* @dataProvider dataProviderStatisfy
* @dataProvider dataProviderSatisfy
*/
public function testStatisfy($configuration, $methods, $expected)
{
Expand Down

0 comments on commit ad61b47

Please sign in to comment.