| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This source file is subject to the license that is bundled with this package in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace PhUml\Graphviz\Builders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PhUml\Code\ClassDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PhUml\Code\Codebase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use PhUml\Graphviz\Edge; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use PhUml\Graphviz\HasDotRepresentation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PhUml\Graphviz\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * It produces the collection of nodes and edges related to a class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * - It creates an edge with the class it extends, if any, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * - It creates edges from the interfaces it implements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * - It creates a node with the class itself | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * - It, optionally, discovers associations between classes/interfaces, by inspecting both: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *   - The class properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *   - The class constructor's parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | final class ClassGraphBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var HasDotRepresentation[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private array $dotElements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private readonly EdgesBuilder $associationsBuilder; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 29 |  |     public function __construct(EdgesBuilder $associationsBuilder) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 29 |  |         $this->dotElements = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 29 |  |         $this->associationsBuilder = $associationsBuilder; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * The order in which the nodes and edges are created is as follows | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * 1. The edges discovered via properties inspection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * 2. The edges discovered via the constructor parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * 3. The node representing the class itself | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * 4. The parent class, if any | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * 5. The interfaces it implements, if any | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return HasDotRepresentation[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 22 |  |     public function extractFrom(ClassDefinition $class, Codebase $codebase): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 22 |  |         $this->dotElements = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 22 |  |         $this->addAssociations($this->associationsBuilder->fromProperties($class, $codebase)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 22 |  |         $this->addAssociations($this->associationsBuilder->fromConstructor($class, $codebase)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 22 |  |         $this->dotElements[] = new Node($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 22 |  |         if ($class->hasParent()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 11 |  |             $this->dotElements[] = Edge::inheritance($codebase->get($class->parent()), $class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 22 |  |         foreach ($class->interfaces() as $interface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 10 |  |             $this->dotElements[] = Edge::implementation($codebase->get($interface), $class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 22 |  |         foreach ($class->traits() as $trait) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 8 |  |             $this->dotElements[] = Edge::use($codebase->get($trait), $class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 22 |  |         return $this->dotElements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** @param Edge[] $edges */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 22 |  |     private function addAssociations(array $edges): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 22 |  |         $this->dotElements = array_merge($this->dotElements, $edges); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |