| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * PHP version 7.1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This source file is subject to the license that is bundled with this package in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace PhUml\Code; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * It represents a class definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * It does not support class constants yet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class ClassDefinition extends Definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var Attribute[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $attributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var InterfaceDefinition[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $implements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 150 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         string $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         array $attributes = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         array $methods = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         array $implements = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         ClassDefinition $extends = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 150 |  |         parent::__construct($name, $methods, $extends); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 150 |  |         $this->attributes = $attributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 150 |  |         $this->implements = $implements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 150 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function hasConstructor(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 27 |  |         return \count(array_filter($this->methods, function (Method $function) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 24 |  |             return $function->isConstructor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 27 |  |         })) === 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** @return Variable[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 18 |  |     public function constructorParameters(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 18 |  |         if (!$this->hasConstructor()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 15 |  |         $constructors = array_filter($this->methods, function (Method $method) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 15 |  |             return $method->isConstructor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 15 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 15 |  |         return reset($constructors)->parameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 42 |  |     public function hasParent(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 42 |  |         return $this->extends !== null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function countAttributesByVisibility(Visibility $modifier): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 12 |  |         return \count(array_filter($this->attributes, function (Attribute $attribute) use ($modifier) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 12 |  |             return $attribute->hasVisibility($modifier); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 12 |  |         })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function countTypedAttributesByVisibility(Visibility $modifier): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 12 |  |         return \count(array_filter($this->attributes, function (Attribute $attribute) use ($modifier) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 12 |  |             return $attribute->isTyped() && $attribute->hasVisibility($modifier); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 12 |  |         })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** @return Attribute[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 42 |  |     public function attributes(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 42 |  |         return $this->attributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |     /** @return InterfaceDefinition[] */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 45 |  |     public function implements(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 45 |  |         return $this->implements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  |