Complex classes like permissions 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 permissions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class permissions |
||
| 13 | { |
||
| 14 | /** @var \phpbb\auth\auth */ |
||
| 15 | protected $auth; |
||
| 16 | |||
| 17 | /** @var \phpbb\config\db */ |
||
| 18 | protected $config; |
||
| 19 | |||
| 20 | /** @var \phpbb\user */ |
||
| 21 | protected $user; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructor |
||
| 25 | * |
||
| 26 | * @param \phpbb\auth\auth $auth Auth object |
||
| 27 | * @param \phpbb\config\db $config Config object |
||
| 28 | * @param \phpbb\user $user User object |
||
| 29 | */ |
||
| 30 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\db $config, \phpbb\user $user) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param array $post_data |
||
| 39 | * @return bool |
||
| 40 | */ |
||
| 41 | public function display_attachments_notice(array $post_data) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param array $post_data |
||
| 48 | * @return bool |
||
| 49 | */ |
||
| 50 | public function permanent_delete_allowed(array $post_data) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param int $poster_id |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public function user_is_poster($poster_id) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param int $forum_id |
||
| 67 | * @return bool |
||
| 68 | */ |
||
| 69 | public function can_report_post($forum_id) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param array $topic_data |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | public function topic_has_unapproved_posts(array $topic_data) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param array $topic_data |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | public function topic_is_reported(array $topic_data) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param array $topic_data |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | public function topic_is_locked(array $topic_data) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param array $post_data |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public function post_is_unapproved(array $post_data) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param array $post_data |
||
| 112 | * @param array $topic_data |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | protected function edit_allowed(array $post_data, array $topic_data) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param array $topic_data |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | protected function quote_allowed(array $topic_data) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param array $post_data |
||
| 137 | * @param array $topic_data |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | protected function post_is_quotable(array $post_data, array $topic_data) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param array $post_data |
||
| 147 | * @param array $topic_data |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | protected function delete_allowed(array $post_data, array $topic_data) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param array $post_data |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | protected function softdelete_allowed(array $post_data) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param array $post_data |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | protected function cannot_edit(array $post_data) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param array $post_data |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | protected function cannot_edit_time(array $post_data) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param array $post_data |
||
| 190 | * @param array $topic_data |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | protected function cannot_edit_locked(array $post_data, array $topic_data) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param array $post_data |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | protected function cannot_delete(array $post_data) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param array $post_data |
||
| 212 | * @param array $topic_data |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | protected function cannot_delete_lastpost(array $post_data, array $topic_data) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param array $post_data |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | protected function cannot_delete_time(array $post_data) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * we do not want to allow removal of the last post if a moderator locked it! |
||
| 231 | * @param array $post_data |
||
| 232 | * @param array $topic_data |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | protected function cannot_delete_locked(array $post_data, array $topic_data) |
||
| 239 | } |
||
| 240 |