| Total Complexity | 40 |
| Total Lines | 330 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like QueryData 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 QueryData, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | final class QueryData |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $params = []; |
||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $query; |
||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $mapClassName = ''; |
||
| 48 | /** |
||
| 49 | * @var DataModelBase |
||
| 50 | */ |
||
| 51 | protected $mapClass; |
||
| 52 | /** |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $useKeyPair = false; |
||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $select = ''; |
||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $from = ''; |
||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $where = ''; |
||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $groupBy = ''; |
||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $order = ''; |
||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $limit = ''; |
||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $queryCount = ''; |
||
| 84 | /** |
||
| 85 | * @var int |
||
| 86 | */ |
||
| 87 | protected $queryNumRows = 0; |
||
| 88 | /** |
||
| 89 | * @var int Código de estado tras realizar la consulta |
||
| 90 | */ |
||
| 91 | protected $queryStatus = 0; |
||
| 92 | /** |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | protected $onErrorMessage; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Añadir un parámetro a la consulta |
||
| 99 | * |
||
| 100 | * @param $value |
||
| 101 | * @param $name |
||
| 102 | */ |
||
| 103 | public function addParam($value, $name = null) |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function getParams() |
||
| 116 | { |
||
| 117 | return $this->params; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Establecer los parámetros de la consulta |
||
| 122 | * |
||
| 123 | * @param array $data |
||
| 124 | */ |
||
| 125 | public function setParams(array $data) |
||
| 126 | { |
||
| 127 | $this->params = $data; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getQuery() |
||
| 134 | { |
||
| 135 | if (empty($this->query)) { |
||
| 136 | return $this->select . ' ' . $this->from . ' ' . $this->where . ' ' . $this->groupBy . ' ' . $this->order . ' ' . $this->limit; |
||
| 137 | } |
||
| 138 | |||
| 139 | return $this->query; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param $query |
||
| 144 | */ |
||
| 145 | public function setQuery($query) |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function getMapClassName() |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $mapClassName |
||
| 160 | */ |
||
| 161 | public function setMapClassName($mapClassName) |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function isUseKeyPair() |
||
| 170 | { |
||
| 171 | return $this->useKeyPair; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param boolean $useKeyPair |
||
| 176 | */ |
||
| 177 | public function setUseKeyPair($useKeyPair) |
||
| 178 | { |
||
| 179 | $this->useKeyPair = (bool)$useKeyPair; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getSelect() |
||
| 186 | { |
||
| 187 | return $this->select; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $select |
||
| 192 | */ |
||
| 193 | public function setSelect($select) |
||
| 194 | { |
||
| 195 | $this->select = 'SELECT ' . $select; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getOrder() |
||
| 202 | { |
||
| 203 | return $this->order; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $order |
||
| 208 | */ |
||
| 209 | public function setOrder($order) |
||
| 210 | { |
||
| 211 | if (!empty($order)) { |
||
| 212 | $this->order = 'ORDER BY ' . $order; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function getLimit() |
||
| 220 | { |
||
| 221 | return $this->limit; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $limit |
||
| 226 | * @param array|null $params |
||
| 227 | */ |
||
| 228 | public function setLimit($limit, array $params = null) |
||
| 229 | { |
||
| 230 | if (!empty($limit)) { |
||
| 231 | $this->limit = 'LIMIT ' . $limit; |
||
| 232 | |||
| 233 | if ($params !== null) { |
||
| 234 | $this->addParams($params); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Añadir parámetros a la consulta |
||
| 241 | * |
||
| 242 | * @param array $params |
||
| 243 | */ |
||
| 244 | public function addParams(array $params) |
||
| 245 | { |
||
| 246 | $this->params = array_merge($this->params, $params); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function getQueryCount() |
||
| 253 | { |
||
| 254 | if (empty($this->queryCount)) { |
||
| 255 | return 'SELECT COUNT(*) ' . $this->from . ' ' . $this->where; |
||
| 256 | } |
||
| 257 | |||
| 258 | return $this->queryCount; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public function getFrom() |
||
| 265 | { |
||
| 266 | return $this->from; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $from |
||
| 271 | */ |
||
| 272 | public function setFrom($from) |
||
| 273 | { |
||
| 274 | if (!empty($from)) { |
||
| 275 | $this->from = 'FROM ' . $from; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function getWhere() |
||
| 283 | { |
||
| 284 | return $this->where; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param array|string $where |
||
| 289 | */ |
||
| 290 | public function setWhere($where) |
||
| 291 | { |
||
| 292 | if (!empty($where)) { |
||
| 293 | if (is_array($where)) { |
||
| 294 | $this->where = 'WHERE ' . implode(' AND ', $where); |
||
| 295 | } else { |
||
| 296 | $this->where = 'WHERE ' . $where; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return int |
||
| 303 | */ |
||
| 304 | public function getQueryNumRows() |
||
| 305 | { |
||
| 306 | return (int)$this->queryNumRows; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param int $queryNumRows |
||
| 311 | */ |
||
| 312 | public function setQueryNumRows($queryNumRows) |
||
| 313 | { |
||
| 314 | $this->queryNumRows = (int)$queryNumRows; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return int |
||
| 319 | */ |
||
| 320 | public function getQueryStatus() |
||
| 321 | { |
||
| 322 | return $this->queryStatus; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param int $queryStatus |
||
| 327 | */ |
||
| 328 | public function setQueryStatus($queryStatus) |
||
| 329 | { |
||
| 330 | $this->queryStatus = $queryStatus; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public function getOnErrorMessage() |
||
| 337 | { |
||
| 338 | return $this->onErrorMessage ?: __u('Error while querying'); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param string $onErrorMessage |
||
| 343 | */ |
||
| 344 | public function setOnErrorMessage($onErrorMessage) |
||
| 345 | { |
||
| 346 | $this->onErrorMessage = $onErrorMessage; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | public function getGroupBy() |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param string $groupBy |
||
| 359 | */ |
||
| 360 | public function setGroupBy($groupBy) |
||
| 361 | { |
||
| 362 | if (!empty($groupBy)) { |
||
| 363 | $this->groupBy = 'GROUP BY ' . $groupBy; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |