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\Prefix |
||
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 |
||
67 | */ |
||
68 | function get ($id) { |
||
71 | /** |
||
72 | * @param string $module |
||
73 | * @param int $item |
||
74 | * |
||
75 | * @return int[] |
||
1 ignored issue
–
show
|
|||
76 | */ |
||
77 | function get_for_module_item ($module, $item) { |
||
84 | /** |
||
85 | * Add new comment |
||
86 | * |
||
87 | * @param int $item Item id |
||
88 | * @param string $module Module name |
||
89 | * @param string $text Comment text |
||
90 | * @param int $parent Parent comment id |
||
91 | * |
||
92 | * @return false|int |
||
93 | */ |
||
94 | function add ($item, $module, $text, $parent = 0) { |
||
113 | /** |
||
114 | * Set comment text |
||
115 | * |
||
116 | * @param int $id |
||
117 | * @param string $text |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | function set ($id, $text) { |
||
137 | /** |
||
138 | * Delete comment |
||
139 | * |
||
140 | * @param int $id |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | function del ($id) { |
||
163 | /** |
||
164 | * Delete all comments of specified item |
||
165 | * |
||
166 | * @param int $item Item id |
||
167 | * @param string $module Module name |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | function del_all ($item, $module) { |
||
186 | /** |
||
187 | * Count of comments for specified item |
||
188 | * |
||
189 | * @param int $item Item id |
||
190 | * @param string $module Module name |
||
191 | * |
||
192 | * @return int |
||
193 | */ |
||
194 | function count ($item, $module) { |
||
211 | /** |
||
212 | * Get comments tree in html format for specified item (look at ::block() method before usage) |
||
213 | * |
||
214 | * @param int $item Item id |
||
215 | * @param string $module Module name |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | function tree ($item, $module) { |
||
222 | /** |
||
223 | * Get comments structure of specified item |
||
224 | * |
||
225 | * @param int $item |
||
226 | * @param string $module |
||
227 | * @param int $parent |
||
228 | * |
||
229 | * @return false|array |
||
230 | */ |
||
231 | function tree_data ($item, $module, $parent = 0) { |
||
269 | /** |
||
270 | * Get comments tree in html format for given data structure (usually uses ::tree_data() method) |
||
271 | * |
||
272 | * @param array[] $comments |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | function tree_html ($comments) { |
||
333 | /** |
||
334 | * Get comments block with comments tree and comments sending form |
||
335 | * |
||
336 | * @param int $item Item id |
||
337 | * @param string $module Module name |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | function block ($item, $module) { |
||
380 | } |
||
381 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.