Total Complexity | 65 |
Total Lines | 236 |
Duplicated Lines | 0 % |
Coverage | 46.06% |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
Complex classes like CRUDHelper 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 CRUDHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class CRUDHelper { |
||
22 | |||
23 | 7 | public static function getIdentifierFunction($model) { |
|
34 | 7 | }; |
|
35 | } |
||
36 | |||
37 | 1 | public static function search($model, $search, $fields, $initialCondition = '1=1') { |
|
38 | 1 | $words = preg_split ( "@(\s*?(\(|\)|\|\||\&\&)\s*?)@", $search ); |
|
39 | 1 | $params = [ ]; |
|
40 | 1 | $count = \count ( $fields ); |
|
41 | 1 | $db = DAO::getDb ( $model ); |
|
42 | 1 | $like = $db->getSpecificSQL ( 'tostring' ) . ' LIKE '; |
|
43 | 1 | SqlUtils::$quote = $db->quote; |
|
44 | 1 | if ($words !== false) { |
|
45 | 1 | $words = array_filter ( $words, 'strlen' ); |
|
46 | 1 | $condition = "(" . SqlUtils::getSearchWhere ( $like, $fields, '?', '', '' ) . ")"; |
|
47 | 1 | foreach ( $words as $word ) { |
|
48 | 1 | $word = \trim ( $word ); |
|
49 | 1 | $params = [ ...$params,...(\array_fill ( 0, $count, "%{$word}%" )) ]; |
|
50 | } |
||
51 | |||
52 | 1 | $condition = \str_replace ( '||', ' OR ', $condition ); |
|
53 | 1 | $condition = \str_replace ( '&&', ' AND ', $condition ); |
|
54 | 1 | $condition = '(' . $condition . ') AND ' . $initialCondition . ''; |
|
55 | } else { |
||
56 | $condition = $initialCondition; |
||
57 | } |
||
58 | 1 | return DAO::getAll ( $model, $condition, false, $params ); |
|
59 | } |
||
60 | |||
61 | 3 | public static function update($instance, $values, $setValues = true, $updateMany = true, $eventCallback = null) { |
|
62 | 3 | $className = \get_class ( $instance ); |
|
63 | 3 | $fieldsInRelationForUpdate = OrmUtils::getFieldsInRelationsForUpdate_ ( $className ); |
|
64 | 3 | $manyToOneRelations = $fieldsInRelationForUpdate ['manyToOne']; |
|
65 | 3 | $manyToManyRelations = $fieldsInRelationForUpdate ['manyToMany']; |
|
66 | 3 | $oneToManyRelations = $fieldsInRelationForUpdate ['oneToMany']; |
|
67 | |||
68 | 3 | $members = \array_keys ( $values ); |
|
69 | 3 | OrmUtils::setFieldToMemberNames ( $members, $fieldsInRelationForUpdate ['relations'] ); |
|
70 | 3 | $update = false; |
|
71 | |||
72 | 3 | $fieldTypes = OrmUtils::getFieldTypes ( $className ); |
|
73 | 3 | foreach ( $fieldTypes as $property => $type ) { |
|
74 | 3 | if (DbTypes::isBoolean($type)) { |
|
75 | if (isset ( $values [$property] )) { |
||
76 | $values [$property] = 1; |
||
77 | } else { |
||
78 | $values [$property] = 0; |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | 3 | if ($setValues) { |
|
83 | 3 | URequest::setValuesToObject ( $instance, $values ); |
|
84 | } |
||
85 | 3 | if ($manyToOneRelations) { |
|
86 | self::updateManyToOne ( $manyToOneRelations, $members, $className, $instance, $values ); |
||
87 | } |
||
88 | 3 | if (isset ( $instance )) { |
|
89 | 3 | if (isset ( $eventCallback )) { |
|
90 | 3 | $eventCallback ( $instance, $instance->_new ); |
|
91 | } |
||
92 | 3 | if ($instance->_new) { |
|
93 | 1 | $update = DAO::insert ( $instance ); |
|
94 | } else { |
||
95 | 2 | $update = DAO::update ( $instance ); |
|
96 | 2 | if (DbCache::$active) { |
|
97 | // TODO update dbCache |
||
98 | } |
||
99 | } |
||
100 | 3 | if ($updateMany && $update && $manyToManyRelations) { |
|
101 | self::updateManyToMany ( $manyToManyRelations, $members, $className, $instance, $values ); |
||
102 | } |
||
103 | 3 | if ($updateMany && $update && $oneToManyRelations) { |
|
104 | 3 | self::updateOneToMany ( $oneToManyRelations, $members, $className, $instance, $values ); |
|
105 | } |
||
106 | } |
||
107 | 3 | return $update; |
|
108 | } |
||
109 | |||
110 | private static function getInputValues($values,$index){ |
||
111 | $r=[]; |
||
112 | foreach ($values as $k=>$oValues){ |
||
113 | if($k!=='_status') { |
||
114 | $r[$k] = $oValues[$index]; |
||
115 | } |
||
116 | } |
||
117 | return $r; |
||
118 | } |
||
119 | |||
120 | private static function getOneToManyKeys($keys,$values,$index,$defaultId){ |
||
129 | } |
||
130 | |||
131 | 3 | protected static function updateOneToMany($oneToManyRelations,$members,$className,$instance,$values){ |
|
132 | 3 | $id=OrmUtils::getFirstKeyValue($instance); |
|
133 | 3 | foreach ($oneToManyRelations as $name){ |
|
134 | 3 | $member=$name.'Ids'; |
|
135 | 3 | $len=\strlen($member); |
|
136 | 3 | if(($values[$member]??'')==='updated'){ |
|
137 | foreach ($values as $k=>$v){ |
||
138 | if(\substr($k, 0, $len) === $member){ |
||
139 | $newK=\substr($k,$len+1); |
||
140 | if($newK!=null) { |
||
141 | $newValues[$newK] = $v; |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | $r=OrmUtils::getAnnotationInfoMember($className,'#oneToMany',$name); |
||
146 | $fkClass=$r['className']; |
||
147 | $keys=\array_keys(OrmUtils::getKeyFields($fkClass)); |
||
1 ignored issue
–
show
|
|||
148 | foreach ($newValues['_status'] as $index=>$status){ |
||
149 | $kv=self::getOneToManyKeys($keys,$newValues,$index,$id); |
||
150 | if($kv!==false) { |
||
151 | switch ($status) { |
||
152 | case 'deleted': |
||
153 | DAO::deleteById($fkClass,$kv); |
||
154 | break; |
||
155 | case 'updated': |
||
156 | $o = DAO::getById($fkClass, $kv); |
||
157 | if ($o) { |
||
158 | $oValues = self::getInputValues($newValues, $index); |
||
159 | URequest::setValuesToObject($o, $oValues); |
||
160 | DAO::update($o); |
||
161 | } |
||
162 | break; |
||
163 | case 'added': |
||
164 | $o=new $fkClass(); |
||
165 | $oValues = \array_merge($kv,self::getInputValues($newValues, $index)); |
||
166 | URequest::setValuesToObject($o, $oValues); |
||
167 | DAO::insert($o); |
||
168 | break; |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | 3 | } |
|
175 | |||
176 | protected static function updateManyToOne($manyToOneRelations, $members, $className, $instance, $values) { |
||
177 | foreach ( $manyToOneRelations as $member ) { |
||
178 | if (\array_search ( $member, $members ) !== false) { |
||
179 | $joinColumn = OrmUtils::getAnnotationInfoMember ( $className, '#joinColumn', $member ); |
||
180 | if ($joinColumn) { |
||
181 | $fkClass = $joinColumn ['className']; |
||
182 | $fkField = $joinColumn ['name']; |
||
183 | if (isset ( $values [$fkField] )) { |
||
184 | if ($values [$fkField] != null) { |
||
185 | $fkObject = DAO::getById ( $fkClass, $values ["$fkField"] ); |
||
186 | Reflexion::setMemberValue ( $instance, $member, $fkObject ); |
||
187 | } elseif ($joinColumn ['nullable'] ?? false) { |
||
188 | Reflexion::setMemberValue ( $instance, $member, null ); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | |||
196 | protected static function updateManyToMany($manyToManyRelations, $members, $className, $instance, $values) { |
||
197 | foreach ( $manyToManyRelations as $member ) { |
||
198 | if (\array_search ( $member, $members ) !== false) { |
||
199 | if (($annot = OrmUtils::getAnnotationInfoMember ( $className, '#manyToMany', $member )) !== false) { |
||
200 | $newField = $member . 'Ids'; |
||
201 | $fkClass = $annot ['targetEntity']; |
||
202 | $fkObjects = DAO::getAll ( $fkClass, self::getMultiWhere ( $values [$newField], $fkClass ) ); |
||
203 | if (Reflexion::setMemberValue ( $instance, $member, $fkObjects )) { |
||
204 | DAO::insertOrUpdateManyToMany ( $instance, $member ); |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | |||
211 | private static function getMultiWhere($ids, $class) { |
||
212 | $pk = OrmUtils::getFirstKey ( $class ); |
||
213 | $ids = explode ( ',', $ids ); |
||
214 | $idCount = \count ( $ids ); |
||
215 | if ($idCount < 1) |
||
216 | return ''; |
||
217 | $strs = [ ]; |
||
218 | for($i = 0; $i < $idCount; $i ++) { |
||
219 | $strs [] = $pk . "='" . $ids [$i] . "'"; |
||
220 | } |
||
221 | return \implode ( " OR ", $strs ); |
||
222 | } |
||
223 | |||
224 | 3 | public static function getFkIntance($instance, $model, $member, $included = false) { |
|
245 | } |
||
246 | |||
247 | 3 | public static function getFKIntances($instance, $model, $included = false) { |
|
257 | } |
||
258 | } |
||
259 | |||
260 |