private function getDirectInherits($class, AstMap $astMap) { return array_map(function (AstMap\AstInherit $v) { return $v->__toString(); }, array_filter($astMap->getClassInherits($class), function (AstMap\AstInheritInterface $v) { return $v instanceof AstMap\AstInherit; })); }
public function satisfy(array $configuration, AstClassReferenceInterface $abstractClassReference, AstMap $astMap, CollectorFactory $collectorFactory, AstParserInterface $astParser) { $classInherits = $astMap->getClassInherits($abstractClassReference->getClassName()); foreach ($classInherits as $classInherit) { if (count($classInherit->getPath()) >= $configuration['level']) { return true; } } return false; }
public function applyDependencies(AstParserInterface $astParser, AstMap $astMap, DependencyResult $dependencyResult) { foreach ($astMap->getAstClassReferences() as $classReference) { foreach ($astMap->getClassInherits($classReference->getClassName()) as $inherit) { if ($inherit instanceof FlattenAstInherit) { continue; } $dependencyResult->addDependency(new Dependency($classReference->getClassName(), $inherit->getLine(), $inherit->getClassName())); } } }
public function flattenDependencies(AstMap $astMap, DependencyResult $dependencyResult) { foreach ($astMap->getAstClassReferences() as $classReference) { foreach ($astMap->getClassInherits($classReference->getClassName()) as $inherit) { if (!$inherit instanceof FlattenAstInherit) { continue; } foreach ($dependencyResult->getDependenciesByClass($inherit->getClassName()) as $dep) { $dependencyResult->addInheritDependency(new InheritDependency($classReference->getClassName(), $dep->getClassB(), $dep, $inherit)); } } } }
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); }
/** * @param AstParserInterface $astParser * @param EventDispatcherInterface $dispatcher * @param array $files * @return AstMap */ public function createAstMapByFiles(AstParserInterface $astParser, EventDispatcherInterface $dispatcher, array $files) { $dispatcher->dispatch(PreCreateAstMapEvent::class, new PreCreateAstMapEvent(count($files))); $astMap = new AstMap($astParser); foreach ($files as $file) { try { foreach ($astParser->parse($file) as $astReference) { if ($astReference instanceof AstClassReferenceInterface) { $astMap->addAstClassReference($astReference); } elseif ($astReference instanceof AstFileReferenceInterface) { $astMap->addAstFileReferences($astReference); } else { throw new \LogicException('unknown AST Type.'); } } $dispatcher->dispatch(AstFileAnalyzedEvent::class, new AstFileAnalyzedEvent($file)); } catch (\PhpParser\Error $e) { $dispatcher->dispatch(AstFileSyntaxErrorEvent::class, new AstFileSyntaxErrorEvent($file, $e->getMessage())); } } $dispatcher->dispatch(PostCreateAstMapEvent::class, new PostCreateAstMapEvent($astMap)); return $astMap; }
/** * @param AstMap $astMap * @param DependencyResult $dependencyResult * @param ClassNameLayerResolverInterface $classNameLayerResolver * * @return array */ private function calculateLayerDependencies(AstMap $astMap, DependencyResult $dependencyResult, ClassNameLayerResolverInterface $classNameLayerResolver) { $layersDependOnLayers = []; // all classes foreach ($astMap->getAstClassReferences() as $classReferences) { foreach ($classNameLayerResolver->getLayersByClassName($classReferences->getClassName()) as $classReferenceLayer) { $layersDependOnLayers[$classReferenceLayer] = []; } } // dependencies foreach ($dependencyResult->getDependenciesAndInheritDependencies() as $dependency) { $layersA = $classNameLayerResolver->getLayersByClassName($dependency->getClassA()); $layersB = $classNameLayerResolver->getLayersByClassName($dependency->getClassB()); if (empty($layersB)) { continue; } foreach ($layersA as $layerA) { if (!isset($layersDependOnLayers[$layerA])) { $layersDependOnLayers[$layerA] = []; } foreach ($layersB as $layerB) { if ($layerA == $layerB) { continue; } if (!isset($layersDependOnLayers[$layerA][$layerB])) { $layersDependOnLayers[$layerA][$layerB] = 1; continue; } $layersDependOnLayers[$layerA][$layerB] = $layersDependOnLayers[$layerA][$layerB] + 1; } } } return $layersDependOnLayers; }
public function applyDependencies(AstParserInterface $astParser, AstMap $astMap, DependencyResult $dependencyResult) { /* @var $astParser NikicPhpParser */ assert($astParser instanceof NikicPhpParser === true); foreach ($astMap->getAstFileReferences() as $fileReference) { $uses = $this->getUseStatements($astParser, $fileReference); foreach ($fileReference->getAstClassReferences() as $astClassReference) { /** @var $uses EmittedDependency[] */ $uses = array_merge($uses, $this->getInstanceOfStatements($astParser, $astClassReference), $this->getParamStatements($astParser, $astClassReference), $this->getNewStatements($astParser, $astClassReference), $this->getStaticPropertiesAccess($astParser, $astClassReference), $this->getStaticMethodCalls($astParser, $astClassReference)); foreach ($uses as $emittedDependency) { $dependencyResult->addDependency(new Dependency($astClassReference->getClassName(), $emittedDependency->getLine(), $emittedDependency->getClass())); } } } }