SensioLabs\Deptrac\ClassNameLayerResolver::getLayersByClassName PHP Method

getLayersByClassName() public method

public getLayersByClassName ( $className )
    public function getLayersByClassName($className)
    {
        $layers = [];
        foreach ($this->configuration->getLayers() as $configurationLayer) {
            foreach ($configurationLayer->getCollectors() as $configurationCollector) {
                $collector = $this->collectorFactory->getCollector($configurationCollector->getType());
                if (!($astClassReference = $this->astMap->getClassReferenceByClassName($className))) {
                    $astClassReference = new AstClassReference($className);
                }
                if ($collector->satisfy($configurationCollector->getArgs(), $astClassReference, $this->astMap, $this->collectorFactory, $this->astParser)) {
                    $layers[$configurationLayer->getName()] = true;
                }
            }
        }
        return array_keys($layers);
    }

Usage Example

 /**
  * @param $collectA
  * @param $collectB1
  * @param $collectB2
  * @param array $expectedLayers
  * @dataProvider provideGetLayersByClassName
  */
 public function testGetLayersByClassName($collectA, $collectB1, $collectB2, array $expectedLayers)
 {
     $configuration = $this->prophesize(Configuration::class);
     $configuration->getLayers()->willReturn([ConfigurationLayer::fromArray(['name' => 'LayerA', 'collectors' => [['type' => 'CollectorA']]]), ConfigurationLayer::fromArray(['name' => 'LayerB', 'collectors' => [['type' => 'CollectorB1'], ['type' => 'CollectorB2']]])]);
     $astMap = $this->prophesize(AstMap::class);
     $collectorFactory = $this->prophesize(CollectorFactory::class);
     $collectorFactory->getCollector('CollectorA')->willReturn($this->getCollector($collectA, ['type' => 'CollectorA', 'foo' => 'bar']));
     $collectorFactory->getCollector('CollectorB1')->willReturn($this->getCollector($collectB1, ['type' => 'CollectorB', 'foo' => 'bar']));
     $collectorFactory->getCollector('CollectorB2')->willReturn($this->getCollector($collectB2, ['type' => 'CollectorB', 'foo' => 'bar']));
     $resolver = new ClassNameLayerResolver($configuration->reveal(), $astMap->reveal(), $collectorFactory->reveal(), $this->prophesize(AstParserInterface::class)->reveal());
     $this->assertEquals($expectedLayers, $resolver->getLayersByClassName('classA'));
 }