Total Complexity | 48 |
Total Lines | 244 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like MiscHandler 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 MiscHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class MiscHandler extends \XoopsPersistableObjectHandler |
||
34 | { |
||
35 | /** |
||
36 | * @var \XoopsMySQLDatabase |
||
37 | */ |
||
38 | public $db; |
||
39 | public $dbTable; |
||
40 | public $objClass = Misc::class; |
||
41 | public $objKey = 'misc_id'; |
||
42 | public $helper; |
||
43 | |||
44 | public function __construct(?\XoopsMySQLDatabase $db = null, ?Helper $helper = null) |
||
45 | { |
||
46 | if (null === $helper) { |
||
47 | $helper = Helper::getInstance(); |
||
48 | } |
||
49 | $this->helper = $helper; |
||
50 | |||
51 | if (null === $db) { |
||
52 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
53 | } |
||
54 | $this->db = $db; |
||
55 | $table = $db->prefix($helper->getDirname() . '_misc'); |
||
56 | $this->dbTable = $table; |
||
57 | |||
58 | parent::__construct($db, $table, Misc::class, 'misc_id', 'misc_title'); |
||
59 | } |
||
60 | |||
61 | public function getInstance(?\XoopsMySQLDatabase $db = null): MiscHandler |
||
62 | { |
||
63 | static $instance; |
||
64 | if (null === $instance) { |
||
65 | $instance = new static($db); |
||
66 | } |
||
67 | |||
68 | return $instance; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param bool $isNew |
||
73 | * @return \XoopsObject |
||
74 | */ |
||
75 | public function create($isNew = true): ?\XoopsObject |
||
76 | { |
||
77 | $obj = parent::create($isNew); |
||
78 | // if ($isNew) { |
||
79 | // $obj->setDefaultPermissions(); |
||
80 | // } |
||
81 | $obj->helper = $this->helper; |
||
82 | |||
83 | return $obj; |
||
84 | } |
||
85 | |||
86 | // public function get($id = null, $fields = '*') |
||
87 | |||
88 | /** |
||
89 | * @param null|int $id |
||
90 | * @param null|array $fields |
||
91 | */ |
||
92 | public function get($id = null, $fields = null): ?\XoopsObject |
||
93 | { |
||
94 | $criteria = new \Criteria($this->objKey, (string)$id); |
||
95 | $objs = $this->getObjects2($criteria); |
||
96 | if (\is_array($objs) && !empty($objs)) { |
||
97 | return 1 !== \count($objs) ? null : $objs[0]; |
||
98 | } |
||
99 | |||
100 | return null; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * count objects matching a condition |
||
105 | * @param null|\Criteria|\CriteriaCompo $criteria |
||
106 | */ |
||
107 | public function getCount($criteria = null): ?int |
||
108 | { |
||
109 | $ret = null; |
||
110 | $sql = 'SELECT COUNT(*) FROM ' . $this->dbTable; |
||
111 | if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
||
112 | $sql .= ' ' . $criteria->renderWhere(); |
||
113 | } |
||
114 | $result = $this->db->query($sql); |
||
115 | if ($result instanceof \mysqli_result) { |
||
116 | [$ret] = $this->db->fetchRow($result); |
||
117 | } |
||
118 | return $ret; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param null|\Criteria|\CriteriaCompo $criteria |
||
123 | */ |
||
124 | public function getObjects2($criteria = null, string $fields = '*', string $key = ''): ?array |
||
125 | { |
||
126 | $ret = null; |
||
127 | $start = 0; |
||
128 | $limit = $start; |
||
129 | // $fields = '*'; |
||
130 | $sql = 'SELECT ' . $fields . ' FROM ' . $this->dbTable; |
||
131 | if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
||
132 | $sql .= ' ' . $criteria->renderWhere(); |
||
133 | if ('' !== $criteria->getSort()) { |
||
134 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
135 | } |
||
136 | $limit = $criteria->getLimit(); |
||
137 | $start = $criteria->getStart(); |
||
138 | } |
||
139 | if (!\preg_match('/ORDER BY/', $sql)) { |
||
140 | $sql .= ' ORDER BY ' . $this->objKey . ' ASC'; |
||
141 | } |
||
142 | $result = $this->db->query($sql, $limit, $start); |
||
143 | if ($result instanceof \mysqli_result) { |
||
144 | $ret = []; |
||
145 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
146 | $obj = new $this->objClass(); |
||
147 | $obj->assignVars($myrow); |
||
148 | switch ($key) { |
||
149 | default: |
||
150 | $ret[] = $obj; |
||
151 | break; |
||
152 | case 'title': |
||
153 | $ret[$myrow['misc_title']] = $obj; |
||
154 | break; |
||
155 | case 'id': |
||
156 | $ret[$myrow[$this->objKey]] = $obj; |
||
157 | break; |
||
158 | } |
||
159 | unset($obj); |
||
160 | } |
||
161 | } |
||
162 | return $ret; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param bool $force flag to force the query execution despite security settings |
||
167 | * @return array|bool|int|mixed|null |
||
168 | */ |
||
169 | public function insert(\XoopsObject $object, $force = false) |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param bool $force flag to force the query execution despite security settings |
||
222 | * @param null|\Criteria|\CriteriaCompo $criteria |
||
223 | */ |
||
224 | public function modifyObjects(?\Criteria $criteria = null, array $fields = [], bool $force = false): ?string |
||
250 | } |
||
251 | |||
252 | //TODO this should be deleted? |
||
253 | /** |
||
254 | * delete an object from the database |
||
255 | * |
||
256 | * @param \XoopsObject $object reference to the object to delete |
||
257 | * @param bool $force |
||
258 | * @return bool FALSE if failed. |
||
259 | */ |
||
260 | public function delete(\XoopsObject $object, $force = false): bool |
||
277 | } |
||
278 | } |
||
279 |