| Total Complexity | 43 |
| Total Lines | 231 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR); |
||
| 66 | } |
||
| 67 | $ret = array(); |
||
| 68 | if (false !== $result) { |
||
| 69 | if ($asObject) { |
||
| 70 | while (false !== ($myrow = $this->handler->db->fetchArray($result))) { |
||
| 71 | $object = $this->handler->create(false); |
||
| 72 | $object->assignVars($myrow); |
||
| 73 | if ($id_as_key) { |
||
| 74 | $ret[$myrow[$this->handler->keyName]] = $object; |
||
| 75 | } else { |
||
| 76 | $ret[] = $object; |
||
| 77 | } |
||
| 78 | unset($object); |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | $object = $this->handler->create(false); |
||
| 82 | while (false !== ($myrow = $this->handler->db->fetchArray($result))) { |
||
| 83 | $object->assignVars($myrow); |
||
| 84 | if ($id_as_key) { |
||
| 85 | $ret[$myrow[$this->handler->keyName]] = $object->getValues(array_keys($myrow)); |
||
| 86 | } else { |
||
| 87 | $ret[] = $object->getValues(array_keys($myrow)); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | unset($object); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | return $ret; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * retrieve objects from the database |
||
| 98 | * |
||
| 99 | * For performance consideration, getAll() is recommended |
||
| 100 | * |
||
| 101 | * @param CriteriaElement $criteria {@link CriteriaElement} conditions to be met |
||
| 102 | * @param bool $id_as_key use the ID as key for the array |
||
| 103 | * @param bool $as_object return an array of objects? |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true) |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Retrieve a list of objects data |
||
| 115 | * |
||
| 116 | * @param CriteriaElement $criteria {@link CriteriaElement} conditions to be met |
||
| 117 | * @param int $limit Max number of objects to fetch |
||
| 118 | * @param int $start Which record to start at |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public function getList(CriteriaElement $criteria = null, $limit = 0, $start = 0) |
||
| 122 | { |
||
| 123 | $ret = array(); |
||
| 124 | if ($criteria == null) { |
||
| 125 | $criteria = new CriteriaCompo(); |
||
| 126 | $criteria->setLimit($limit); |
||
| 127 | $criteria->setStart($start); |
||
| 128 | } |
||
| 129 | |||
| 130 | $sql = "SELECT `{$this->handler->keyName}`"; |
||
| 131 | if (!empty($this->handler->identifierName)) { |
||
| 132 | $sql .= ", `{$this->handler->identifierName}`"; |
||
| 133 | } |
||
| 134 | $sql .= " FROM `{$this->handler->table}`"; |
||
| 135 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 136 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 137 | if ($sort = $criteria->getSort()) { |
||
| 138 | $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder(); |
||
| 139 | } |
||
| 140 | if ((0 == $limit) && (0 == $start)) { |
||
| 141 | $limit = $criteria->getLimit(); |
||
| 142 | $start = $criteria->getStart(); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | $result = $this->handler->db->query($sql, $limit, $start); |
||
| 146 | if (!$this->handler->db->isResultSet($result)) { |
||
| 147 | // \trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR); |
||
| 148 | return $ret; |
||
| 149 | } |
||
| 150 | |||
| 151 | $myts = \MyTextSanitizer::getInstance(); |
||
| 152 | while (false !== ($myrow = $this->handler->db->fetchArray($result))) { |
||
| 153 | // identifiers should be textboxes, so sanitize them like that |
||
| 154 | $ret[$myrow[$this->handler->keyName]] = empty($this->handler->identifierName) ? 1 : $myts->htmlSpecialChars($myrow[$this->handler->identifierName]); |
||
| 155 | } |
||
| 156 | |||
| 157 | return $ret; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * get IDs of objects matching a condition |
||
| 162 | * |
||
| 163 | * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match |
||
| 164 | * @return array of object IDs |
||
| 165 | */ |
||
| 166 | public function &getIds(CriteriaElement $criteria = null) |
||
| 167 | { |
||
| 168 | $ret = array(); |
||
| 169 | $sql = "SELECT `{$this->handler->keyName}` FROM `{$this->handler->table}`"; |
||
| 170 | $limit = $start = null; |
||
| 171 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 172 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 173 | $limit = $criteria->getLimit(); |
||
| 174 | $start = $criteria->getStart(); |
||
| 175 | } |
||
| 176 | $result = $this->handler->db->query($sql, $limit, $start); |
||
| 177 | if (!$this->handler->db->isResultSet($result)) { |
||
| 178 | // \trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR); |
||
| 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.