| Total Complexity | 47 |
| Total Lines | 209 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ClassToYuml often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ClassToYuml, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class ClassToYuml { |
||
| 12 | protected $class; |
||
| 13 | protected $displayProperties=true; |
||
| 14 | protected $displayMethods=false; |
||
| 15 | protected $displayMethodsParams=false; |
||
| 16 | protected $displayPropertiesTypes=false; |
||
| 17 | protected $displayAssociations; |
||
| 18 | protected $displayAssociationClassProperties=false; |
||
| 19 | protected $displayForeignKeys=true; |
||
| 20 | protected $properties; |
||
| 21 | protected $oneToManys=[]; |
||
| 22 | protected $manyToOne=[]; |
||
| 23 | protected $parseResult; |
||
| 24 | protected $note; |
||
| 25 | |||
| 26 | public function __construct($class,$displayProperties=true,$displayAssociations=true,$displayMethods=false,$displayMethodsParams=false,$displayPropertiesTypes=false,$displayAssociationClassProperties=false){ |
||
| 27 | $this->class=$class; |
||
| 28 | $this->displayProperties=$displayProperties; |
||
| 29 | $this->displayAssociations=$displayAssociations; |
||
| 30 | $this->displayMethods=$displayMethods; |
||
| 31 | $this->displayMethodsParams=$displayMethodsParams; |
||
| 32 | $this->displayPropertiesTypes=$displayPropertiesTypes; |
||
| 33 | $this->displayAssociationClassProperties=$displayAssociationClassProperties; |
||
| 34 | } |
||
| 35 | |||
| 36 | public function init($hasManyToOne,$hasOneToMany){ |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | public function parse(){ |
||
| 46 | $reflect=new \ReflectionClass($this->class); |
||
| 47 | $yumlAnnot=OrmUtils::getAnnotationInfo($this->class, "#yuml"); |
||
| 48 | $color=""; |
||
| 49 | if ($yumlAnnot!==false){ |
||
| 50 | if(isset($yumlAnnot["color"])){ |
||
| 51 | $color="{bg:".$yumlAnnot["color"]."}"; |
||
| 52 | } |
||
| 53 | if(isset($yumlAnnot["note"])){ |
||
| 54 | $this->note=$yumlAnnot["note"]; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | $parts=[$reflect->getShortName()]; |
||
| 58 | |||
| 59 | if($this->displayProperties){ |
||
| 60 | $prikeys=OrmUtils::getKeyFields($this->class); |
||
| 61 | $types=OrmUtils::getFieldTypes($this->class); |
||
| 62 | $propertiesArray=[]; |
||
| 63 | $properties=$reflect->getProperties(); |
||
| 64 | foreach ($properties as $property){ |
||
| 65 | $this->parseProperty($propertiesArray, $property, $prikeys, $types); |
||
| 66 | } |
||
| 67 | $parts[]=\implode(Yuml::$memberSeparator, $propertiesArray); |
||
| 68 | } |
||
| 69 | |||
| 70 | if($this->displayMethods){ |
||
| 71 | $methodsArray=[]; |
||
| 72 | $methods=$reflect->getMethods(); |
||
| 73 | foreach ($methods as $method){ |
||
| 74 | $this->parseMethod($methodsArray, $method); |
||
| 75 | } |
||
| 76 | $parts[]=\implode(Yuml::$memberSeparator, $methodsArray); |
||
| 77 | } |
||
| 78 | |||
| 79 | $result=\implode(Yuml::$classSeparator, $parts).$color; |
||
| 80 | $result=Yuml::setClassContent($result); |
||
| 81 | if(isset($this->note)){ |
||
| 82 | $result.=$this->_getNote(); |
||
| 83 | } |
||
| 84 | $this->parseResult=$result; |
||
| 85 | return $result; |
||
| 86 | } |
||
| 87 | |||
| 88 | protected function parseProperty(&$propertiesArray,$property,$prikeys,$types){ |
||
| 89 | $propertyName=$property->getName(); |
||
| 90 | $type="";$isPri=""; |
||
| 91 | if($this->displayPropertiesTypes){ |
||
| 92 | if(\array_key_exists($propertyName, $types)){ |
||
| 93 | $type=Yuml::$parameterTypeSeparator.$types[$propertyName]; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | if(\array_search($propertyName, $prikeys)!==false){ |
||
| 97 | $isPri=Yuml::$primary; |
||
| 98 | } |
||
| 99 | $propertiesArray[]=Yuml::setPropertyVariables([$this->getAccess($property),$isPri,$propertyName,$type]); |
||
| 100 | } |
||
| 101 | |||
| 102 | protected function parseMethod(&$methodsArray,$method){ |
||
| 103 | $parameters=""; |
||
| 104 | if($this->displayMethodsParams){ |
||
| 105 | $parameters=$this->getMethodParameters($method); |
||
| 106 | } |
||
| 107 | $methodName=$method->getName(); |
||
| 108 | $type=""; |
||
| 109 | if($method->hasReturnType()){ |
||
| 110 | $type=Yuml::$parameterTypeSeparator.$method->getReturnType(); |
||
| 111 | } |
||
| 112 | $methodsArray[]=Yuml::setMethodVariables([$this->getAccess($method),$methodName,$parameters,$type]); |
||
| 113 | } |
||
| 114 | |||
| 115 | protected function getShortClassName($class){ |
||
| 116 | $reflect=new \ReflectionClass($class); |
||
| 117 | return $reflect->getShortName(); |
||
| 118 | } |
||
| 119 | |||
| 120 | protected function loadOneToManys(){ |
||
| 121 | $oneToManys=OrmUtils::getAnnotationInfo($this->class, "#oneToMany"); |
||
| 122 | if($oneToManys){ |
||
| 123 | foreach ($oneToManys as $member=>$array){ |
||
| 124 | $this->oneToManys[$member]=$array["className"]; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | protected function loadManyToOne(){ |
||
| 130 | $manyToOne=OrmUtils::getAnnotationInfo($this->class, "#manyToOne"); |
||
| 131 | if($manyToOne){ |
||
| 132 | foreach ($manyToOne as $member){ |
||
| 133 | $joinColumn=OrmUtils::getAnnotationInfoMember($this->class, "#joinColumn", $member); |
||
| 134 | if($joinColumn){ |
||
| 135 | if(isset($joinColumn["className"])){ |
||
| 136 | $this->manyToOne[$member]=$joinColumn["className"]; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | protected function _getYumlManyToOne(){ |
||
| 144 | return $this->_getYumlRelationsType($this->manyToOne,"0..*-1"); |
||
| 145 | } |
||
| 146 | |||
| 147 | protected function _getYumlOneToMany(){ |
||
| 148 | return $this->_getYumlRelationsType($this->oneToManys,"1-0..*"); |
||
| 149 | } |
||
| 150 | |||
| 151 | protected function _getYumlRelationsType($relations,$branche){ |
||
| 152 | $myClass=$this->getShortClassName($this->class); |
||
| 153 | $yumlRelations=[]; |
||
| 154 | foreach ($relations as $model){ |
||
| 155 | $yumlRelations[]=Yuml::setClassContent($myClass).$branche.new ClassToYuml($model,$this->displayAssociationClassProperties,false); |
||
| 156 | } |
||
| 157 | return $yumlRelations; |
||
| 158 | } |
||
| 159 | |||
| 160 | protected function _getNote(){ |
||
| 161 | return "-[note:".$this->note."]"; |
||
| 162 | } |
||
| 163 | |||
| 164 | protected function getMethodParameters(\ReflectionMethod $method){ |
||
| 165 | $paramsValues=[]; |
||
| 166 | $parameters=$method->getParameters(); |
||
| 167 | foreach ($parameters as $parameter){ |
||
| 168 | $v=$parameter->getName(); |
||
| 169 | if($parameter->hasType()){ |
||
| 170 | $v.=Yuml::$parameterTypeSeparator.$parameter->getType(); |
||
| 171 | } |
||
| 172 | $paramsValues[]=$v; |
||
| 173 | } |
||
| 174 | return \implode(Yuml::$parameterSeparator, $paramsValues); |
||
| 175 | } |
||
| 176 | |||
| 177 | protected function getAccess($property){ |
||
| 178 | $result=Yuml::$private; |
||
| 179 | if($property->isPublic()){ |
||
| 180 | $result=Yuml::$public; |
||
| 181 | }elseif($property->isProtected()){ |
||
| 182 | $result=Yuml::$protected; |
||
| 183 | } |
||
| 184 | return $result; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function manyToOneTostring(){ |
||
| 188 | $this->loadManyToOne(); |
||
| 189 | return \implode(Yuml::$groupeSeparator, $this->_getYumlManyToOne()); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function oneToManyTostring(){ |
||
| 193 | $this->loadOneToManys(); |
||
| 194 | return \implode(Yuml::$groupeSeparator, $this->_getYumlOneToMany()); |
||
| 195 | } |
||
| 196 | |||
| 197 | public function __toString(){ |
||
| 204 | } |
||
| 205 | |||
| 206 | |||
| 207 | public function setDisplayProperties($displayProperties) { |
||
| 208 | $this->displayProperties=$displayProperties; |
||
| 209 | return $this; |
||
| 210 | } |
||
| 211 | |||
| 212 | public function setDisplayMethods($displayMethods) { |
||
| 215 | } |
||
| 216 | |||
| 217 | public function setDisplayAssociations($displayAssociations) { |
||
| 218 | $this->displayAssociations=$displayAssociations; |
||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | } |
||
| 222 |