Total Complexity | 43 |
Total Lines | 231 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like XoopsModelRead 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 XoopsModelRead, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class XoopsModelRead extends XoopsModelAbstract |
||
29 | { |
||
30 | /** |
||
31 | * get all objects matching a condition |
||
32 | * |
||
33 | * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match |
||
34 | * @param array $fields variables to fetch |
||
35 | * @param bool $asObject flag indicating as object, otherwise as array |
||
36 | * @param bool $id_as_key use the ID as key for the array |
||
37 | * @return array of objects/array {@link XoopsObject} |
||
38 | */ |
||
39 | public function &getAll(CriteriaElement $criteria = null, $fields = null, $asObject = true, $id_as_key = true) |
||
40 | { |
||
41 | if (is_array($fields) && count($fields) > 0) { |
||
42 | if (!in_array($this->handler->keyName, $fields)) { |
||
43 | $fields[] = $this->handler->keyName; |
||
44 | } |
||
45 | $select = '`' . implode('`, `', $fields) . '`'; |
||
46 | } else { |
||
47 | $select = '*'; |
||
48 | } |
||
49 | $limit = null; |
||
50 | $start = null; |
||
51 | $sql = "SELECT {$select} FROM `{$this->handler->table}`"; |
||
52 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
53 | $sql .= ' ' . $criteria->renderWhere(); |
||
|
|||
54 | if ($groupby = $criteria->getGroupby()) { |
||
55 | $sql .= $groupby; |
||
56 | } |
||
57 | if ($sort = $criteria->getSort()) { |
||
58 | $sql .= " ORDER BY {$sort} " . $criteria->getOrder(); |
||
59 | } |
||
60 | $limit = $criteria->getLimit(); |
||
61 | $start = $criteria->getStart(); |
||
62 | } |
||
63 | $result = $this->handler->db->query($sql, $limit, $start); |
||
64 | if (!$this->handler->db->isResultSet($result)) { |
||
65 | throw new \RuntimeException( |
||
66 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->handler->db->error(), E_USER_ERROR |
||
67 | ); |
||
68 | } |
||
69 | $ret = array(); |
||
70 | if (false !== $result) { |
||
71 | if ($asObject) { |
||
72 | while (false !== ($myrow = $this->handler->db->fetchArray($result))) { |
||
73 | $object = $this->handler->create(false); |
||
74 | $object->assignVars($myrow); |
||
75 | if ($id_as_key) { |
||
76 | $ret[$myrow[$this->handler->keyName]] = $object; |
||
77 | } else { |
||
78 | $ret[] = $object; |
||
79 | } |
||
80 | unset($object); |
||
81 | } |
||
82 | } else { |
||
83 | $object = $this->handler->create(false); |
||
84 | while (false !== ($myrow = $this->handler->db->fetchArray($result))) { |
||
85 | $object->assignVars($myrow); |
||
86 | if ($id_as_key) { |
||
87 | $ret[$myrow[$this->handler->keyName]] = $object->getValues(array_keys($myrow)); |
||
88 | } else { |
||
89 | $ret[] = $object->getValues(array_keys($myrow)); |
||
90 | } |
||
91 | } |
||
92 | unset($object); |
||
93 | } |
||
94 | } |
||
95 | return $ret; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * retrieve objects from the database |
||
100 | * |
||
101 | * For performance consideration, getAll() is recommended |
||
102 | * |
||
103 | * @param CriteriaElement $criteria {@link CriteriaElement} conditions to be met |
||
104 | * @param bool $id_as_key use the ID as key for the array |
||
105 | * @param bool $as_object return an array of objects? |
||
106 | * @return array |
||
107 | */ |
||
108 | public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true) |
||
109 | { |
||
110 | $objects =& $this->getAll($criteria, null, $as_object, $id_as_key); |
||
111 | |||
112 | return $objects; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Retrieve a list of objects data |
||
117 | * |
||
118 | * @param CriteriaElement $criteria {@link CriteriaElement} conditions to be met |
||
119 | * @param int $limit Max number of objects to fetch |
||
120 | * @param int $start Which record to start at |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getList(CriteriaElement $criteria = null, $limit = 0, $start = 0) |
||
124 | { |
||
125 | $ret = array(); |
||
126 | if ($criteria == null) { |
||
127 | $criteria = new CriteriaCompo(); |
||
128 | $criteria->setLimit($limit); |
||
129 | $criteria->setStart($start); |
||
130 | } |
||
131 | |||
132 | $sql = "SELECT `{$this->handler->keyName}`"; |
||
133 | if (!empty($this->handler->identifierName)) { |
||
134 | $sql .= ", `{$this->handler->identifierName}`"; |
||
135 | } |
||
136 | $sql .= " FROM `{$this->handler->table}`"; |
||
137 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
138 | $sql .= ' ' . $criteria->renderWhere(); |
||
139 | if ($sort = $criteria->getSort()) { |
||
140 | $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder(); |
||
141 | } |
||
142 | if ((0 == $limit) && (0 == $start)) { |
||
143 | $limit = $criteria->getLimit(); |
||
144 | $start = $criteria->getStart(); |
||
145 | } |
||
146 | } |
||
147 | $result = $this->handler->db->query($sql, $limit, $start); |
||
148 | if (!$this->handler->db->isResultSet($result)) { |
||
149 | return $ret; |
||
150 | } |
||
151 | |||
152 | $myts = \MyTextSanitizer::getInstance(); |
||
153 | while (false !== ($myrow = $this->handler->db->fetchArray($result))) { |
||
154 | // identifiers should be textboxes, so sanitize them like that |
||
155 | $ret[$myrow[$this->handler->keyName]] = empty($this->handler->identifierName) ? 1 : $myts->htmlSpecialChars($myrow[$this->handler->identifierName]); |
||
156 | } |
||
157 | |||
158 | return $ret; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * get IDs of objects matching a condition |
||
163 | * |
||
164 | * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match |
||
165 | * @return array of object IDs |
||
166 | */ |
||
167 | public function &getIds(CriteriaElement $criteria = null) |
||
168 | { |
||
169 | $ret = array(); |
||
170 | $sql = "SELECT `{$this->handler->keyName}` FROM `{$this->handler->table}`"; |
||
171 | $limit = $start = null; |
||
172 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
173 | $sql .= ' ' . $criteria->renderWhere(); |
||
174 | $limit = $criteria->getLimit(); |
||
175 | $start = $criteria->getStart(); |
||
176 | } |
||
177 | $result = $this->handler->db->query($sql, $limit, $start); |
||
178 | if (!$this->handler->db->isResultSet($result)) { |
||
179 | return $ret; |
||
180 | } |
||
181 | |||
182 | while (false !== ($myrow = $this->handler->db->fetchArray($result))) { |
||
183 | $ret[] = $myrow[$this->handler->keyName]; |
||
184 | } |
||
185 | |||
186 | return $ret; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * get a limited list of objects matching a condition |
||
191 | * |
||
192 | * {@link CriteriaCompo} |
||
193 | * |
||
194 | * @param int $limit Max number of objects to fetch |
||
195 | * @param int $start Which record to start at |
||
196 | * @param CriteriaElement $criteria {@link CriteriaElement} to match |
||
197 | * @param array $fields variables to fetch |
||
198 | * @param bool $asObject flag indicating as object, otherwise as array |
||
199 | * @return array of objects {@link XoopsObject} |
||
200 | */ |
||
201 | public function &getByLimit($limit = 0, $start = 0, CriteriaElement $criteria = null, $fields = null, $asObject = true) |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Convert a database resultset to a returnable array |
||
219 | * |
||
220 | * @param object $result database resultset |
||
221 | * @param bool $id_as_key - should NOT be used with joint keys |
||
222 | * @param bool $as_object |
||
223 | * @return array |
||
224 | */ |
||
225 | public function convertResultSet($result, $id_as_key = false, $as_object = true) |
||
261 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.