| Total Complexity | 56 |
| Total Lines | 210 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Reflexion 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 Reflexion, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Reflexion { |
||
| 14 | |||
| 15 | public static function getProperties($instance) { |
||
| 16 | if (\is_string($instance)) { |
||
| 17 | $instance=new $instance(); |
||
| 18 | } |
||
| 19 | $reflect=new \ReflectionClass($instance); |
||
| 20 | $props=$reflect->getProperties(); |
||
| 21 | return $props; |
||
| 22 | } |
||
| 23 | |||
| 24 | public static function getMethods($instance, $filter=null) { |
||
| 25 | $reflect=new \ReflectionClass($instance); |
||
| 26 | $methods=$reflect->getMethods($filter); |
||
| 27 | return $methods; |
||
| 28 | } |
||
| 29 | |||
| 30 | public static function getKeyFields($instance) { |
||
| 31 | return Reflexion::getMembersNameWithAnnotation(get_class($instance), "@id"); |
||
| 32 | } |
||
| 33 | |||
| 34 | public static function getMemberValue($instance, $member) { |
||
| 38 | } |
||
| 39 | |||
| 40 | public static function setMemberValue($instance, $member,$value) { |
||
| 48 | } |
||
| 49 | |||
| 50 | public static function getProperty($instance, $member) { |
||
| 56 | } |
||
| 57 | |||
| 58 | public static function getPropertyType($class,$property){ |
||
| 59 | return self::getMetadata($class, $property, "@var", "type"); |
||
| 60 | } |
||
| 61 | |||
| 62 | public static function getMetadata($class,$property, $type, $name){ |
||
| 63 | $a = Annotations::ofProperty($class, $property, $type); |
||
| 64 | if (!count($a)){ |
||
| 65 | return false; |
||
| 66 | } |
||
| 67 | return trim($a[0]->$name,";"); |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | public static function getPropertiesAndValues($instance, $props=NULL) { |
||
| 72 | $ret=array (); |
||
| 73 | $className=get_class($instance); |
||
| 74 | if (is_null($props)) |
||
| 75 | $props=self::getProperties($instance); |
||
| 76 | foreach ( $props as $prop ) { |
||
| 77 | $prop->setAccessible(true); |
||
| 78 | $v=$prop->getValue($instance); |
||
| 79 | if (OrmUtils::isSerializable($className, $prop->getName())) { |
||
| 80 | if (OrmUtils::isNotNullOrNullAccepted($v, $className, $prop->getName())) { |
||
| 81 | $name=OrmUtils::getFieldName($className, $prop->getName()); |
||
| 82 | $ret[$name]=$v; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | return $ret; |
||
| 87 | } |
||
| 88 | |||
| 89 | public static function getAnnotationClass($class, $annotation) { |
||
| 90 | $annot=Annotations::ofClass($class, $annotation); |
||
| 91 | return $annot; |
||
| 92 | } |
||
| 93 | |||
| 94 | public static function getAnnotationMember($class, $member, $annotation) { |
||
| 95 | $annot=Annotations::ofProperty($class, $member, $annotation); |
||
| 96 | if (\sizeof($annot) > 0) |
||
| 97 | return $annot[0]; |
||
| 98 | return false; |
||
| 99 | } |
||
| 100 | |||
| 101 | public static function getAnnotationsMember($class, $member, $annotation) { |
||
| 102 | return Annotations::ofProperty($class, $member, $annotation); |
||
| 103 | } |
||
| 104 | |||
| 105 | public static function getAnnotationsMethod($class, $method, $annotation) { |
||
| 106 | if(is_array($annotation)){ |
||
| 107 | $result=[]; |
||
| 108 | foreach($annotation as $annot){ |
||
| 109 | $annots=Annotations::ofMethod($class, $method, $annot); |
||
| 110 | if(sizeof($annots)>0){ |
||
| 111 | $result=array_merge($result,$annots); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | return $result; |
||
| 115 | } |
||
| 116 | $annots=Annotations::ofMethod($class, $method, $annotation); |
||
| 117 | if (\sizeof($annots) > 0) |
||
| 118 | return $annots; |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | public static function getMembersAnnotationWithAnnotation($class, $annotation) { |
||
| 131 | } |
||
| 132 | |||
| 133 | public static function getMembersWithAnnotation($class, $annotation) { |
||
| 134 | $props=self::getProperties($class); |
||
| 135 | $ret=array (); |
||
| 136 | foreach ( $props as $prop ) { |
||
| 137 | $annot=self::getAnnotationMember($class, $prop->getName(), $annotation); |
||
| 138 | if ($annot !== false) |
||
| 139 | $ret[]=$prop; |
||
| 140 | } |
||
| 141 | return $ret; |
||
| 142 | } |
||
| 143 | |||
| 144 | public static function getMembersNameWithAnnotation($class, $annotation) { |
||
| 145 | $props=self::getProperties($class); |
||
| 146 | $ret=array (); |
||
| 147 | foreach ( $props as $prop ) { |
||
| 148 | $annot=self::getAnnotationMember($class, $prop->getName(), $annotation); |
||
| 149 | if ($annot !== false) |
||
| 150 | $ret[]=$prop->getName(); |
||
| 151 | } |
||
| 152 | return $ret; |
||
| 153 | } |
||
| 154 | |||
| 155 | public static function isNullable($class, $member) { |
||
| 156 | $ret=self::getAnnotationMember($class, $member, "@column"); |
||
| 157 | if (!$ret) |
||
| 158 | return false; |
||
| 159 | else |
||
| 160 | return $ret->nullable; |
||
|
|
|||
| 161 | } |
||
| 162 | |||
| 163 | public static function getDbType($class, $member) { |
||
| 164 | $ret=self::getAnnotationMember($class, $member, "@column"); |
||
| 165 | if (!$ret) |
||
| 166 | return false; |
||
| 167 | else |
||
| 168 | return $ret->dbType; |
||
| 169 | } |
||
| 170 | |||
| 171 | public static function isSerializable($class, $member) { |
||
| 172 | if (self::getAnnotationMember($class, $member, "@transient") !== false || self::getAnnotationMember($class, $member, "@manyToOne") !== false || self::getAnnotationMember($class, $member, "@manyToMany") !== false || self::getAnnotationMember($class, $member, "@oneToMany") !== false) |
||
| 173 | return false; |
||
| 174 | else |
||
| 175 | return true; |
||
| 176 | } |
||
| 177 | |||
| 178 | public static function getFieldName($class, $member) { |
||
| 179 | $ret=self::getAnnotationMember($class, $member, "@column"); |
||
| 180 | if ($ret === false || !isset($ret->name)) |
||
| 181 | $ret=$member; |
||
| 182 | else |
||
| 183 | $ret=$ret->name; |
||
| 184 | return $ret; |
||
| 185 | } |
||
| 186 | |||
| 187 | public static function getTableName($class) { |
||
| 188 | $ret=Reflexion::getAnnotationClass($class, "@table"); |
||
| 189 | if (\sizeof($ret) === 0) { |
||
| 190 | $posSlash=strrpos($class, '\\'); |
||
| 191 | if ($posSlash !== false) |
||
| 192 | $class=substr($class, $posSlash + 1); |
||
| 193 | $ret=$class; |
||
| 194 | } else { |
||
| 195 | $ret=$ret[0]->name; |
||
| 196 | } |
||
| 197 | return $ret; |
||
| 198 | } |
||
| 199 | |||
| 200 | public static function getMethodParameters(\ReflectionMethod $method) { |
||
| 201 | $result=array (); |
||
| 202 | foreach ( $method->getParameters() as $param ) { |
||
| 203 | $result[]=$param->name; |
||
| 204 | } |
||
| 205 | return $result; |
||
| 206 | } |
||
| 207 | |||
| 208 | public static function getJoinTables($class){ |
||
| 215 | } |
||
| 216 | |||
| 217 | public static function getAllJoinTables($models){ |
||
| 218 | $result=[]; |
||
| 219 | foreach ($models as $model){ |
||
| 220 | $result=array_merge($result,self::getJoinTables($model)); |
||
| 221 | } |
||
| 222 | return $result; |
||
| 223 | } |
||
| 224 | } |
||
| 225 |