Complex classes like Comments 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Comments, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Comments { |
||
24 | use |
||
25 | CRUD_helpers, |
||
26 | Singleton; |
||
27 | |||
28 | /** |
||
29 | * @var Cache |
||
30 | */ |
||
31 | protected $cache; |
||
32 | /** |
||
33 | * @var int Avatar size in px, can be redefined |
||
34 | */ |
||
35 | public $avatar_size = 36; |
||
36 | |||
37 | protected $data_model = [ |
||
38 | 'id' => 'int:1', |
||
39 | 'parent' => 'int:0', |
||
40 | 'module' => 'text', |
||
41 | 'item' => 'int:1', |
||
42 | 'user' => 'int:1', |
||
43 | 'date' => 'int:1', |
||
44 | 'text' => 'html', |
||
45 | 'lang' => 'text' |
||
46 | ]; |
||
47 | |||
48 | protected $table = '[prefix]comments'; |
||
49 | |||
50 | protected function construct () { |
||
53 | /** |
||
54 | * Returns database index |
||
55 | * |
||
56 | * @return int |
||
57 | */ |
||
58 | protected function cdb () { |
||
61 | /** |
||
62 | * Get comment data |
||
63 | * |
||
64 | * @param int|int[] $id |
||
65 | * |
||
66 | * @return array|false |
||
1 ignored issue
–
show
|
|||
67 | */ |
||
68 | function get ($id) { |
||
71 | /** |
||
72 | * Add new comment |
||
73 | * |
||
74 | * @param int $item Item id |
||
75 | * @param string $module Module name |
||
76 | * @param string $text Comment text |
||
77 | * @param int $parent Parent comment id |
||
78 | * |
||
79 | * @return false|int |
||
1 ignored issue
–
show
|
|||
80 | */ |
||
81 | function add ($item, $module, $text, $parent = 0) { |
||
100 | /** |
||
101 | * Set comment text |
||
102 | * |
||
103 | * @param int $id |
||
104 | * @param string $text |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | function set ($id, $text) { |
||
124 | /** |
||
125 | * Delete comment |
||
126 | * |
||
127 | * @param int $id |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | function del ($id) { |
||
150 | /** |
||
151 | * Delete all comments of specified item |
||
152 | * |
||
153 | * @param int $item Item id |
||
154 | * @param string $module Module name |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | function del_all ($item, $module) { |
||
173 | /** |
||
174 | * Count of comments for specified item |
||
175 | * |
||
176 | * @param int $item Item id |
||
177 | * @param string $module Module name |
||
178 | * |
||
179 | * @return int |
||
180 | */ |
||
181 | function count ($item, $module) { |
||
198 | /** |
||
199 | * Get comments tree in html format for specified item (look at ::block() method before usage) |
||
200 | * |
||
201 | * @param int $item Item id |
||
202 | * @param string $module Module name |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | function tree ($item, $module) { |
||
209 | /** |
||
210 | * Get comments structure of specified item |
||
211 | * |
||
212 | * @param int $item |
||
213 | * @param string $module |
||
214 | * @param int $parent |
||
215 | * |
||
216 | * @return false|array |
||
217 | */ |
||
218 | function tree_data ($item, $module, $parent = 0) { |
||
256 | /** |
||
257 | * Get comments tree in html format for given data structure (usually uses ::tree_data() method) |
||
258 | * |
||
259 | * @param array[] $comments |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | function tree_html ($comments) { |
||
320 | /** |
||
321 | * Get comments block with comments tree and comments sending form |
||
322 | * |
||
323 | * @param int $item Item id |
||
324 | * @param string $module Module name |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | function block ($item, $module) { |
||
367 | } |
||
368 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..