Complex classes like data 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 data, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class data extends query_builder |
||
15 | { |
||
16 | /** @var \phpbb\auth\auth */ |
||
17 | protected $auth; |
||
18 | |||
19 | /** @var \phpbb\config\config */ |
||
20 | protected $config; |
||
21 | |||
22 | /** @var \phpbb\content_visibility */ |
||
23 | protected $content_visibility; |
||
24 | |||
25 | /** @var \phpbb\db\driver\driver_interface */ |
||
26 | protected $db; |
||
27 | |||
28 | /** @var \phpbb\user */ |
||
29 | protected $user; |
||
30 | |||
31 | /** |
||
32 | * Constructor |
||
33 | * |
||
34 | * @param \phpbb\auth\auth $auth Auth object |
||
35 | * @param \phpbb\config\config $config Config object |
||
36 | * @param \phpbb\content_visibility $content_visibility Content visibility |
||
37 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
38 | * @param \phpbb\user $user User object |
||
39 | * @param integer $cache_time Cache results for 3 hours by default |
||
40 | */ |
||
41 | 26 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\content_visibility $content_visibility, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, $cache_time = 10800) |
|
42 | { |
||
43 | 26 | parent::__construct($auth, $config, $content_visibility, $db, $user, $cache_time); |
|
44 | |||
45 | 26 | $this->auth = $auth; |
|
46 | 26 | $this->config = $config; |
|
47 | 26 | $this->content_visibility = $content_visibility; |
|
48 | 26 | $this->db = $db; |
|
49 | 26 | $this->user = $user; |
|
50 | 26 | } |
|
51 | |||
52 | /** |
||
53 | * Get topic data |
||
54 | * |
||
55 | * @param mixed|false $limit |
||
56 | * @param int $start |
||
57 | * @return array |
||
58 | */ |
||
59 | 12 | public function get_topic_data($limit = false, $start = 0) |
|
60 | { |
||
61 | 12 | $sql = $this->db->sql_build_query('SELECT', $this->store['sql_array']); |
|
62 | 12 | $result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time); |
|
63 | |||
64 | 12 | while ($row = $this->db->sql_fetchrow($result)) |
|
65 | { |
||
66 | 10 | $this->store['topic'][$row['topic_id']] = $row; |
|
67 | |||
68 | 10 | $this->store['tracking'][$row['forum_id']]['topic_list'][] = $row['topic_id']; |
|
69 | 10 | $this->store['tracking'][$row['forum_id']]['mark_time'] =& $row['forum_mark_time']; |
|
70 | 10 | $this->store['post_ids']['first'][] = $row['topic_first_post_id']; |
|
71 | 10 | $this->store['post_ids']['last'][] = $row['topic_last_post_id']; |
|
72 | 10 | } |
|
73 | 12 | $this->db->sql_freeresult($result); |
|
74 | |||
75 | 12 | return $this->store['topic']; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Get post data |
||
80 | * |
||
81 | * @param mixed|false $topic_first_or_last_post (first|last) |
||
82 | * @param array $post_ids |
||
83 | * @param bool|false $limit |
||
84 | * @param int $start |
||
85 | * @param array $sql_array |
||
86 | * @return array |
||
87 | */ |
||
88 | 10 | public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array()) |
|
89 | { |
||
90 | 10 | $sql = $this->db->sql_build_query('SELECT_DISTINCT', $this->_get_posts_sql_array($topic_first_or_last_post, $post_ids, $sql_array)); |
|
91 | 10 | $result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time); |
|
92 | |||
93 | 10 | $post_data = array(); |
|
94 | 10 | while ($row = $this->db->sql_fetchrow($result)) |
|
95 | { |
||
96 | 9 | $parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES; |
|
97 | 9 | $row['post_text'] = generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, true); |
|
98 | |||
99 | 9 | $post_data[$row['topic_id']][$row['post_id']] = $row; |
|
100 | 9 | $this->store['poster_ids'][] = $row['poster_id']; |
|
101 | 9 | $this->store['poster_ids'][] = $row['post_edit_user']; |
|
102 | 9 | $this->store['poster_ids'][] = $row['post_delete_user']; |
|
103 | 9 | $this->store['attachments'][] = $row['post_id']; |
|
104 | 9 | } |
|
105 | 10 | $this->db->sql_freeresult($result); |
|
106 | |||
107 | 10 | return $post_data; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get attachments... |
||
112 | * |
||
113 | * @param int $forum_id |
||
114 | * @param array $allowed_extensions |
||
115 | * @param bool $exclude_in_message |
||
116 | * @param string $order_by |
||
117 | * @return array |
||
118 | */ |
||
119 | 7 | public function get_attachments($forum_id = 0, $allowed_extensions = array(), $exclude_in_message = true, $order_by = 'filetime DESC, post_msg_id ASC') |
|
120 | { |
||
121 | 7 | $this->store['attachments'] = array_filter($this->store['attachments']); |
|
122 | |||
123 | 7 | $attachments = array(); |
|
124 | 7 | if ($this->_attachments_allowed($forum_id)) |
|
125 | 7 | { |
|
126 | 6 | $sql = $this->_get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by); |
|
127 | 6 | $result = $this->db->sql_query($sql); |
|
128 | |||
129 | 6 | while ($row = $this->db->sql_fetchrow($result)) |
|
130 | { |
||
131 | 6 | $attachments[$row['post_msg_id']][] = $row; |
|
132 | 6 | } |
|
133 | 6 | $this->db->sql_freeresult($result); |
|
134 | 6 | } |
|
135 | 7 | $this->store['attachments'] = array(); |
|
136 | |||
137 | 7 | return $attachments; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Get topic tracking info |
||
142 | * |
||
143 | * @param int $forum_id |
||
144 | * @return array |
||
145 | */ |
||
146 | 5 | public function get_topic_tracking_info($forum_id = 0) |
|
147 | { |
||
148 | 5 | if (!sizeof($this->store['tracking'])) |
|
149 | 5 | { |
|
150 | return array(); |
||
151 | } |
||
152 | |||
153 | 5 | $tracking_info = $this->_get_tracking_info(); |
|
154 | |||
155 | 5 | return ($forum_id) ? (isset($tracking_info[$forum_id]) ? $tracking_info[$forum_id] : array()) : $tracking_info; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * Returns an array of topic first post or last post ids |
||
160 | * |
||
161 | * @param string $first_or_last_post |
||
162 | * @return array |
||
163 | */ |
||
164 | 3 | public function get_topic_post_ids($first_or_last_post = 'first') |
|
168 | |||
169 | /** |
||
170 | * @param mixed $topic_first_or_last_post |
||
171 | * @param array $post_ids |
||
172 | * @param array $sql_array |
||
173 | * @return array |
||
174 | */ |
||
175 | 10 | private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array) |
|
192 | |||
193 | /** |
||
194 | * @param array $post_ids |
||
195 | * @param string $topic_first_or_last_post |
||
196 | * @return array |
||
197 | */ |
||
198 | 10 | private function _get_post_data_where(array $post_ids, $topic_first_or_last_post) |
|
215 | |||
216 | /** |
||
217 | * @param array $sql_where |
||
218 | * @param string $first_or_last_post |
||
|
|||
219 | */ |
||
220 | 3 | private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post) |
|
229 | |||
230 | /** |
||
231 | * @return array |
||
232 | */ |
||
233 | 5 | private function _get_tracking_info() |
|
247 | |||
248 | /** |
||
249 | * @param string $function |
||
250 | * @return array |
||
251 | */ |
||
252 | 5 | private function _build_tracking_info($function) |
|
262 | |||
263 | /** |
||
264 | * @return bool |
||
265 | */ |
||
266 | 5 | private function _can_track_by_lastread() |
|
270 | |||
271 | /** |
||
272 | * @return bool |
||
273 | */ |
||
274 | 1 | private function _can_track_anonymous() |
|
278 | |||
279 | /** |
||
280 | * @param int $forum_id |
||
281 | * @return bool |
||
282 | */ |
||
283 | 7 | private function _attachments_allowed($forum_id) |
|
287 | |||
288 | /** |
||
289 | * @param int $forum_id |
||
290 | * @return bool |
||
291 | */ |
||
292 | 6 | private function _user_can_download_attachments($forum_id) |
|
296 | |||
297 | /** |
||
298 | * @param array $allowed_extensions |
||
299 | * @param bool $exclude_in_message |
||
300 | * @param string $order_by |
||
301 | * @return string |
||
302 | */ |
||
303 | 6 | private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by) |
|
312 | } |
||
313 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.