diff --git a/examples/MethodNames1.depfile.yml b/examples/MethodNames1.depfile.yml new file mode 100644 index 000000000..720b8fd63 --- /dev/null +++ b/examples/MethodNames1.depfile.yml @@ -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: [] \ No newline at end of file diff --git a/examples/MethodNames1/ClassA.php b/examples/MethodNames1/ClassA.php new file mode 100644 index 000000000..8368cfdad --- /dev/null +++ b/examples/MethodNames1/ClassA.php @@ -0,0 +1,8 @@ + + + + + diff --git a/src/ClassNameLayerResolver.php b/src/ClassNameLayerResolver.php index 03f65570a..e87069104 100644 --- a/src/ClassNameLayerResolver.php +++ b/src/ClassNameLayerResolver.php @@ -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 @@ -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) @@ -49,8 +56,10 @@ public function getLayersByClassName($className) $configurationCollector->getArgs(), $astClassReference, $this->astMap, - $this->collectorFactory - )) { + $this->collectorFactory, + $this->astParser + ) + ) { $layers[$configurationLayer->getName()] = true; } } diff --git a/src/Collector/BoolCollector.php b/src/Collector/BoolCollector.php index 86325751e..86ba72432 100644 --- a/src/Collector/BoolCollector.php +++ b/src/Collector/BoolCollector.php @@ -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; @@ -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'] = []; @@ -39,7 +41,8 @@ public function satisfy( $configurationForCollector->getArgs(), $abstractClassReference, $astMap, - $collectorFactory + $collectorFactory, + $astParser )) { return false; } @@ -52,7 +55,8 @@ public function satisfy( $configurationForCollector->getArgs(), $abstractClassReference, $astMap, - $collectorFactory + $collectorFactory, + $astParser )) { return false; } diff --git a/src/Collector/ClassNameCollector.php b/src/Collector/ClassNameCollector.php index 5baaa16f9..740ecd758 100644 --- a/src/Collector/ClassNameCollector.php +++ b/src/Collector/ClassNameCollector.php @@ -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'; @@ -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', diff --git a/src/Collector/CollectorInterface.php b/src/Collector/CollectorInterface.php index a3d74614d..39bd75ae1 100644 --- a/src/Collector/CollectorInterface.php +++ b/src/Collector/CollectorInterface.php @@ -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; @@ -14,6 +15,7 @@ public function satisfy( array $configuration, AstClassReferenceInterface $abstractClassReference, AstMap $astMap, - CollectorFactory $collectorFactory + CollectorFactory $collectorFactory, + AstParserInterface $astParser ); } diff --git a/src/Collector/InheritanceLevelCollector.php b/src/Collector/InheritanceLevelCollector.php index b2b0d16a2..0fed7a674 100644 --- a/src/Collector/InheritanceLevelCollector.php +++ b/src/Collector/InheritanceLevelCollector.php @@ -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; @@ -17,7 +18,8 @@ public function satisfy( array $configuration, AstClassReferenceInterface $abstractClassReference, AstMap $astMap, - CollectorFactory $collectorFactory + CollectorFactory $collectorFactory, + AstParserInterface $astParser ) { $classInherits = $astMap->getClassInherits($abstractClassReference->getClassName()); diff --git a/src/Collector/MethodCollector.php b/src/Collector/MethodCollector.php new file mode 100644 index 000000000..0640df215 --- /dev/null +++ b/src/Collector/MethodCollector.php @@ -0,0 +1,58 @@ +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; + } + +} diff --git a/src/Command/AnalyzeCommand.php b/src/Command/AnalyzeCommand.php index c1143c1b4..5486bebae 100644 --- a/src/Command/AnalyzeCommand.php +++ b/src/Command/AnalyzeCommand.php @@ -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); diff --git a/src/Tests/ClassNameLayerResolverTest.php b/src/Tests/ClassNameLayerResolverTest.php index fd303750c..f464ed573 100644 --- a/src/Tests/ClassNameLayerResolverTest.php +++ b/src/Tests/ClassNameLayerResolverTest.php @@ -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; @@ -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(); @@ -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( diff --git a/src/Tests/Collector/BoolCollectorTest.php b/src/Tests/Collector/BoolCollectorTest.php index 95fced6fb..e7c9f3532 100644 --- a/src/Tests/Collector/BoolCollectorTest.php +++ b/src/Tests/Collector/BoolCollectorTest.php @@ -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; @@ -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); @@ -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(); @@ -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); diff --git a/src/Tests/Collector/ClassNameCollectorTest.php b/src/Tests/Collector/ClassNameCollectorTest.php index ee0f696fb..d11fea0e3 100644 --- a/src/Tests/Collector/ClassNameCollectorTest.php +++ b/src/Tests/Collector/ClassNameCollectorTest.php @@ -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; @@ -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); @@ -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() ); } } diff --git a/src/Tests/Collector/InheritanceLevelCollectorTest.php b/src/Tests/Collector/InheritanceLevelCollectorTest.php index e910a8ad6..3394ac319 100644 --- a/src/Tests/Collector/InheritanceLevelCollectorTest.php +++ b/src/Tests/Collector/InheritanceLevelCollectorTest.php @@ -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; @@ -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() ) ; diff --git a/src/Tests/Collector/MethodCollectorTest.php b/src/Tests/Collector/MethodCollectorTest.php new file mode 100644 index 000000000..2e17862ea --- /dev/null +++ b/src/Tests/Collector/MethodCollectorTest.php @@ -0,0 +1,85 @@ + 'abc'], + [ + $this->getClassMethod('abc'), + $this->getClassMethod('abcdef'), + $this->getClassMethod('xyz') + ], + true + ]; + + yield [ + ['name' => 'abc'], + [ + $this->getClassMethod('abc'), + $this->getClassMethod('xyz') + ], + true + ]; + + yield [ + ['name' => 'abc'], + [ + $this->getClassMethod('xyz') + ], + false + ]; + } + + public function testType() + { + $this->assertEquals('method', (new MethodCollector())->getType()); + } + + private function getClassMethod($name) + { + $classMethod = new \StdClass(); + $classMethod->name = $name; + return $classMethod; + } + + /** + * @dataProvider dataProviderStatisfy + */ + public function testStatisfy($configuration, $methods, $expected) + { + $className = "foo"; + + $astClassReference = $this->prophesize(AstClassReferenceInterface::class); + $astClassReference->getClassName()->willReturn($className); + + $parser = $this->prophesize(NikicPhpParser::class); + $parser->getAstForClassname($className)->willReturn($ast = new \StdClass()); + + + $parser->findNodesOfType($ast, ClassMethod::class)->willReturn($methods); + + $stat = (new MethodCollector())->satisfy( + $configuration, + $astClassReference->reveal(), + $this->prophesize(AstMap::class)->reveal(), + $this->prophesize(CollectorFactory::class)->reveal(), + $parser->reveal() + ); + + $this->assertEquals($expected, $stat); + } + +}