| Total Complexity | 48 |
| Total Lines | 177 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like OrmUtils 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 OrmUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class OrmUtils { |
||
| 19 | |||
| 20 | use OrmUtilsFieldsTrait,OrmUtilsRelationsTrait; |
||
| 21 | |||
| 22 | private static $modelsMetadatas; |
||
| 23 | |||
| 24 | public static function getModelMetadata($className) { |
||
| 25 | if (!isset(self::$modelsMetadatas[$className])) { |
||
| 26 | self::$modelsMetadatas[$className]=CacheManager::getOrmModelCache($className); |
||
| 27 | } |
||
| 28 | return self::$modelsMetadatas[$className]; |
||
| 29 | } |
||
| 30 | |||
| 31 | public static function isSerializable($class, $member) { |
||
| 32 | return !self::_is($class, $member, "#notSerializable"); |
||
| 33 | } |
||
| 34 | |||
| 35 | public static function isNullable($class, $member) { |
||
| 36 | return self::_is($class, $member, "#nullable"); |
||
| 37 | } |
||
| 38 | |||
| 39 | protected static function _is($class,$member,$like){ |
||
| 45 | } |
||
| 46 | |||
| 47 | public static function getFieldName($class, $member) { |
||
| 48 | $ret=self::getAnnotationInfo($class, "#fieldNames"); |
||
| 53 | } |
||
| 54 | |||
| 55 | public static function getTableName($class) { |
||
| 58 | } |
||
| 59 | |||
| 60 | public static function getKeyFieldsAndValues($instance) { |
||
| 61 | $class=get_class($instance); |
||
| 62 | $kf=self::getAnnotationInfo($class, "#primaryKeys"); |
||
| 63 | return self::getFieldsAndValues_($instance, $kf); |
||
| 64 | } |
||
| 65 | |||
| 66 | public static function getFieldsAndValues_($instance,$members) { |
||
| 67 | $ret=[]; |
||
| 68 | foreach ($members as $member){ |
||
| 69 | $v=Reflexion::getMemberValue($instance, $member); |
||
| 70 | $ret[$member]=$v; |
||
| 71 | } |
||
| 72 | return $ret; |
||
| 73 | } |
||
| 74 | |||
| 75 | public static function getKeyPropsAndValues_($instance,$props) { |
||
| 82 | } |
||
| 83 | |||
| 84 | public static function getMembers($className) { |
||
| 85 | $fieldNames=self::getAnnotationInfo($className, "#fieldNames"); |
||
| 86 | if ($fieldNames !== false) |
||
| 87 | return \array_keys($fieldNames); |
||
| 88 | return [ ]; |
||
| 89 | } |
||
| 90 | |||
| 91 | public static function getMembersAndValues($instance, $members=NULL) { |
||
| 92 | $ret=array (); |
||
| 93 | $className=get_class($instance); |
||
| 94 | if (is_null($members)) |
||
| 95 | $members=self::getMembers($className); |
||
| 96 | foreach ( $members as $member ) { |
||
| 97 | if (self::isSerializable($className, $member)) { |
||
| 98 | $v=Reflexion::getMemberValue($instance, $member); |
||
| 99 | if (self::isNotNullOrNullAccepted($v, $className, $member)) { |
||
| 100 | $name=self::getFieldName($className, $member); |
||
| 101 | $ret[$name]=$v; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | return $ret; |
||
| 106 | } |
||
| 107 | |||
| 108 | public static function isNotNullOrNullAccepted($v, $className, $member) { |
||
| 111 | } |
||
| 112 | |||
| 113 | public static function getFirstKeyValue($instance) { |
||
| 114 | $prop=OrmUtils::getFirstPropKey(get_class($instance)); |
||
| 115 | return Reflexion::getPropValue($instance, $prop); |
||
| 116 | } |
||
| 117 | |||
| 118 | public static function getFirstKeyValue_($instance,$members) { |
||
| 119 | $fkv=self::getFieldsAndValues_($instance,$members); |
||
| 120 | return \current($fkv); |
||
| 121 | } |
||
| 122 | |||
| 123 | public static function getKeyValues($instance) { |
||
| 124 | $fkv=self::getKeyFieldsAndValues($instance); |
||
| 125 | return implode("_",$fkv); |
||
| 126 | } |
||
| 127 | |||
| 128 | public static function getPropKeyValues($instance,$props) { |
||
| 129 | $values=[]; |
||
| 130 | foreach ($props as $prop){ |
||
| 131 | $values[]=Reflexion::getPropValue($instance, $prop); |
||
| 132 | } |
||
| 133 | return implode("_",$values); |
||
| 134 | } |
||
| 135 | |||
| 136 | public static function getMembersWithAnnotation($class, $annotation) { |
||
| 137 | if (isset(self::getModelMetadata($class)[$annotation])) |
||
| 138 | return self::getModelMetadata($class)[$annotation]; |
||
| 139 | return [ ]; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * |
||
| 144 | * @param object $instance |
||
| 145 | * @param string $memberKey |
||
| 146 | * @param array $array |
||
| 147 | * @return boolean |
||
| 148 | */ |
||
| 149 | public static function exists($instance, $memberKey, $array) { |
||
| 150 | $accessor="get" . ucfirst($memberKey); |
||
| 151 | if (method_exists($instance, $accessor)) { |
||
| 152 | foreach ( $array as $value ) { |
||
| 153 | if ($value->$accessor() == $instance->$accessor()) |
||
| 154 | return true; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | |||
| 160 | public static function getAnnotationInfo($class, $keyAnnotation) { |
||
| 161 | $metas=self::getModelMetadata($class); |
||
| 162 | if (isset($metas[$keyAnnotation])) |
||
| 163 | return $metas[$keyAnnotation]; |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | public static function getAnnotationInfoMember($class, $keyAnnotation, $member) { |
||
| 168 | $info=self::getAnnotationInfo($class, $keyAnnotation); |
||
| 169 | if ($info !== false) { |
||
| 170 | if(UArray::isAssociative($info)){ |
||
| 171 | if (isset($info[$member])) { |
||
| 172 | return $info[$member]; |
||
| 173 | } |
||
| 174 | }else{ |
||
| 175 | if(\array_search($member, $info)!==false){ |
||
| 176 | return $member; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | return false; |
||
| 181 | } |
||
| 182 | |||
| 183 | public static function setFieldToMemberNames(&$fields,$relFields){ |
||
| 184 | foreach ($fields as $index=>$field){ |
||
| 185 | if(isset($relFields[$field])){ |
||
| 186 | $fields[$index]=$relFields[$field]; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | public static function objectAsJSON($instance){ |
||
| 195 | } |
||
| 196 | } |
||
| 197 |