Skip to content

Commit

Permalink
Merge pull request #46 from sensiolabs-de/feature/methodname-collector
Browse files Browse the repository at this point in the history
method collector
  • Loading branch information
slde-flash committed Apr 28, 2016
2 parents 421cf6f + cad9ea8 commit b50b834
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 20 deletions.
12 changes: 12 additions & 0 deletions examples/MethodNames1.depfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
paths: ["./examples/MethodNames1/"]
exclude_files: []
layers:
- name: SomeA
collectors:
- type: method
name: SomeA
- name: SomeB
collectors:
- type: method
name: SomeB
ruleset: []
8 changes: 8 additions & 0 deletions examples/MethodNames1/ClassA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

class ClassA
{
public function getSomeA() {

}
}
10 changes: 10 additions & 0 deletions examples/MethodNames1/ClassB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

class ClassB
{
public function getSomeB() {

new ClassA();

}
}
4 changes: 4 additions & 0 deletions services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<tag name="collector"/>
</service>

<service id="collector_method" class="SensioLabs\Deptrac\Collector\MethodCollector">
<tag name="collector"/>
</service>

<service id="collector_bool" class="SensioLabs\Deptrac\Collector\BoolCollector">
<tag name="collector"/>
</service>
Expand Down
19 changes: 14 additions & 5 deletions src/ClassNameLayerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SensioLabs\Deptrac;

use SensioLabs\AstRunner\AstMap;
use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\AstRunner\AstParser\NikicPhpParser\AstClassReference;

class ClassNameLayerResolver implements ClassNameLayerResolverInterface
Expand All @@ -16,21 +17,27 @@ class ClassNameLayerResolver implements ClassNameLayerResolverInterface
/** @var CollectorFactory */
protected $collectorFactory;

/** @var AstParserInterface */
protected $astParser;


/**
* ClassNameLayerResolver constructor.
*
* @param Configuration $configuration
* @param AstMap $astMap
* @param Configuration $configuration
* @param AstMap $astMap
* @param CollectorFactory $collectorFactory
*/
public function __construct(
Configuration $configuration,
AstMap $astMap,
CollectorFactory $collectorFactory
CollectorFactory $collectorFactory,
AstParserInterface $astParser
) {
$this->configuration = $configuration;
$this->astMap = $astMap;
$this->collectorFactory = $collectorFactory;
$this->astParser = $astParser;
}

public function getLayersByClassName($className)
Expand All @@ -49,8 +56,10 @@ public function getLayersByClassName($className)
$configurationCollector->getArgs(),
$astClassReference,
$this->astMap,
$this->collectorFactory
)) {
$this->collectorFactory,
$this->astParser
)
) {
$layers[$configurationLayer->getName()] = true;
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/Collector/BoolCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SensioLabs\Deptrac\Collector;

use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\Deptrac\CollectorFactory;
use SensioLabs\Deptrac\Configuration\ConfigurationCollector;
use SensioLabs\AstRunner\AstMap;
Expand All @@ -18,7 +19,8 @@ public function satisfy(
array $configuration,
AstClassReferenceInterface $abstractClassReference,
AstMap $astMap,
CollectorFactory $collectorFactory
CollectorFactory $collectorFactory,
AstParserInterface $astParser
) {
if (!isset($configuration['must'])) {
$configuration['must'] = [];
Expand All @@ -39,7 +41,8 @@ public function satisfy(
$configurationForCollector->getArgs(),
$abstractClassReference,
$astMap,
$collectorFactory
$collectorFactory,
$astParser
)) {
return false;
}
Expand All @@ -52,7 +55,8 @@ public function satisfy(
$configurationForCollector->getArgs(),
$abstractClassReference,
$astMap,
$collectorFactory
$collectorFactory,
$astParser
)) {
return false;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Collector/ClassNameCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace SensioLabs\Deptrac\Collector;

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

class ClassNameCollector implements CollectorInterface
{


public function getType()
{
return 'className';
Expand All @@ -26,7 +29,8 @@ public function satisfy(
array $configuration,
AstClassReferenceInterface $abstractClassReference,
AstMap $astMap,
CollectorFactory $collectorFactory
CollectorFactory $collectorFactory,
AstParserInterface $astParser
) {
return preg_match(
'/'.$this->getRegexByConfiguration($configuration).'/i',
Expand Down
4 changes: 3 additions & 1 deletion src/Collector/CollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SensioLabs\Deptrac\Collector;

use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\Deptrac\CollectorFactory;
use SensioLabs\AstRunner\AstMap;
use SensioLabs\AstRunner\AstParser\AstClassReferenceInterface;
Expand All @@ -14,6 +15,7 @@ public function satisfy(
array $configuration,
AstClassReferenceInterface $abstractClassReference,
AstMap $astMap,
CollectorFactory $collectorFactory
CollectorFactory $collectorFactory,
AstParserInterface $astParser
);
}
4 changes: 3 additions & 1 deletion src/Collector/InheritanceLevelCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SensioLabs\Deptrac\Collector;

use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\Deptrac\CollectorFactory;
use SensioLabs\AstRunner\AstMap;
use SensioLabs\AstRunner\AstParser\AstClassReferenceInterface;
Expand All @@ -17,7 +18,8 @@ public function satisfy(
array $configuration,
AstClassReferenceInterface $abstractClassReference,
AstMap $astMap,
CollectorFactory $collectorFactory
CollectorFactory $collectorFactory,
AstParserInterface $astParser
) {
$classInherits = $astMap->getClassInherits($abstractClassReference->getClassName());

Expand Down
58 changes: 58 additions & 0 deletions src/Collector/MethodCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace SensioLabs\Deptrac\Collector;

use SensioLabs\AstRunner\AstMap;
use SensioLabs\AstRunner\AstParser\AstClassReferenceInterface;
use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\AstRunner\AstParser\NikicPhpParser\NikicPhpParser;
use SensioLabs\Deptrac\CollectorFactory;
use PhpParser\Node\Stmt\ClassMethod;

class MethodCollector
{
public function getType()
{
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,
AstMap $astMap,
CollectorFactory $collectorFactory,
AstParserInterface $astParser
) {

if (!$astParser instanceof NikicPhpParser) {
return false;
}

$ast = $astParser->getAstForClassname($classReference->getClassName());

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

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

return false;
}

}
2 changes: 1 addition & 1 deletion src/Command/AnalyzeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected function execute(
$this->printFlattenEnd($output);

$classNameLayerResolver = new ClassNameLayerResolverCacheDecorator(
new ClassNameLayerResolver($configuration, $astMap, $this->collectorFactory)
new ClassNameLayerResolver($configuration, $astMap, $this->collectorFactory, $parser)
);

$this->printCollectViolations($output);
Expand Down
7 changes: 5 additions & 2 deletions src/Tests/ClassNameLayerResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SensioLabs\Deptrac\Tests;

use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\Deptrac\ClassNameLayerResolver;
use SensioLabs\Deptrac\Collector\CollectorInterface;
use SensioLabs\Deptrac\CollectorFactory;
Expand Down Expand Up @@ -29,7 +30,8 @@ private function getCollector($return)
Argument::type('array'),
Argument::type(AstClassReferenceInterface::class),
Argument::type(AstMap::class),
Argument::type(CollectorFactory::class)
Argument::type(CollectorFactory::class),
Argument::type(AstParserInterface::class)
)->willReturn($return);

return $collector->reveal();
Expand Down Expand Up @@ -103,7 +105,8 @@ public function testGetLayersByClassName($collectA, $collectB1, $collectB2, arra
$resolver = new ClassNameLayerResolver(
$configuration->reveal(),
$astMap->reveal(),
$collectorFactory->reveal()
$collectorFactory->reveal(),
$this->prophesize(AstParserInterface::class)->reveal()
);

$this->assertEquals(
Expand Down
10 changes: 7 additions & 3 deletions src/Tests/Collector/BoolCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SensioLabs\Deptrac\Tests\Collector;

use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\Deptrac\Collector\BoolCollector;
use SensioLabs\Deptrac\Collector\CollectorInterface;
use SensioLabs\Deptrac\CollectorFactory;
Expand All @@ -20,7 +21,8 @@ public function testStatisfy()
[],
$this->prophesize(AstClassReferenceInterface::class)->reveal(),
$this->prophesize(AstMap::class)->reveal(),
$this->prophesize(CollectorFactory::class)->reveal()
$this->prophesize(CollectorFactory::class)->reveal(),
$this->prophesize(AstParserInterface::class)->reveal()
);

$this->assertEquals(true, $stat);
Expand All @@ -38,7 +40,8 @@ public function getCalculatorMock($returns)
['type' => $returns, 'foo' => 'bar'],
Argument::type(AstClassReferenceInterface::class),
Argument::type(AstMap::class),
Argument::type(CollectorFactory::class)
Argument::type(CollectorFactory::class),
Argument::type(AstParserInterface::class)
)->willReturn($returns);

return $collector->reveal();
Expand Down Expand Up @@ -173,7 +176,8 @@ public function testStatisfyBasicTest($configuration, $expected)
$configuration,
$this->prophesize(AstClassReferenceInterface::class)->reveal(),
$this->prophesize(AstMap::class)->reveal(),
$collectorFactory->reveal()
$collectorFactory->reveal(),
$this->prophesize(AstParserInterface::class)->reveal()
);

$this->assertEquals($expected, $stat);
Expand Down
7 changes: 5 additions & 2 deletions src/Tests/Collector/ClassNameCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SensioLabs\Deptrac\Tests\Collector;

use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\Deptrac\Collector\ClassNameCollector;
use SensioLabs\Deptrac\CollectorFactory;
use SensioLabs\AstRunner\AstMap;
Expand Down Expand Up @@ -32,7 +33,8 @@ public function testStatisfy($configuration, $className, $expected)
$configuration,
$astClassReference->reveal(),
$this->prophesize(AstMap::class)->reveal(),
$this->prophesize(CollectorFactory::class)->reveal()
$this->prophesize(CollectorFactory::class)->reveal(),
$this->prophesize(AstParserInterface::class)->reveal()
);

$this->assertEquals($expected, $stat);
Expand All @@ -47,7 +49,8 @@ public function testWrongRegexParam()
['Foo' => 'a'],
$this->prophesize(AstClassReferenceInterface::class)->reveal(),
$this->prophesize(AstMap::class)->reveal(),
$this->prophesize(CollectorFactory::class)->reveal()
$this->prophesize(CollectorFactory::class)->reveal(),
$this->prophesize(AstParserInterface::class)->reveal()
);
}
}
4 changes: 3 additions & 1 deletion src/Tests/Collector/InheritanceLevelCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SensioLabs\Deptrac\Tests\Collector;

use SensioLabs\AstRunner\AstParser\AstParserInterface;
use SensioLabs\Deptrac\Collector\InheritanceLevelCollector;
use SensioLabs\Deptrac\CollectorFactory;
use Prophecy\Argument;
Expand Down Expand Up @@ -46,7 +47,8 @@ public function testSatisfy($pathLevel, $levelConfig, $expected)
['level' => $levelConfig],
$this->prophesize(AstClassReferenceInterface::class)->reveal(),
$astMap->reveal(),
$this->prophesize(CollectorFactory::class)->reveal()
$this->prophesize(CollectorFactory::class)->reveal(),
$this->prophesize(AstParserInterface::class)->reveal()
)
;

Expand Down
Loading

0 comments on commit b50b834

Please sign in to comment.