Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like idea_controller 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 idea_controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class idea_controller extends base |
||
| 19 | { |
||
| 20 | /** @var array of idea data */ |
||
| 21 | protected $data; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Controller for /idea/{idea_id} |
||
| 25 | * |
||
| 26 | * @param $idea_id int The ID of the requested idea, maybe? |
||
| 27 | * @throws http_exception |
||
| 28 | * @return JsonResponse|RedirectResponse |
||
| 29 | */ |
||
| 30 | public function idea($idea_id) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Delete action (deletes an idea via confirm dialog) |
||
| 68 | * |
||
| 69 | * @throws http_exception |
||
| 70 | * @return void |
||
| 71 | * @access public |
||
| 72 | */ |
||
| 73 | public function delete() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Duplicate action (sets an idea's duplicate link) |
||
| 113 | * |
||
| 114 | * @return bool True if set, false if not |
||
| 115 | * @access public |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function duplicate() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Remove vote action (remove a user's vote from an idea) |
||
| 130 | * |
||
| 131 | * @return mixed Array of vote data, an error message, or false if failed |
||
| 132 | * @access public |
||
| 133 | */ |
||
| 134 | public function removevote() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * RFC action (sets an idea's rfc link) |
||
| 155 | * |
||
| 156 | * @return bool True if set, false if not |
||
| 157 | * @access public |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function rfc() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Status action (sets an idea's status) |
||
| 172 | * |
||
| 173 | * @return bool True if set, false if not |
||
| 174 | * @access public |
||
| 175 | */ |
||
| 176 | public function status() |
||
| 177 | { |
||
| 178 | $status = $this->request->variable('status', 0); |
||
| 179 | |||
| 180 | if ($status && $this->is_mod() && check_link_hash($this->get_hash(), "status_{$this->data['idea_id']}")) |
||
| 181 | { |
||
| 182 | $this->ideas->set_status($this->data['idea_id'], $status); |
||
| 183 | return true; |
||
| 184 | } |
||
| 185 | |||
| 186 | return false; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Ticket action (sets an idea's ticket link) |
||
| 191 | * |
||
| 192 | * @return bool True if set, false if not |
||
| 193 | * @access public |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function ticket() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Implemented action (sets an idea's implemented phpBB version) |
||
| 208 | * |
||
| 209 | * @return bool True if set, false if not |
||
| 210 | * @access public |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function implemented() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Vote action (sets an idea's vote) |
||
| 225 | * |
||
| 226 | * @return mixed Array of vote data, an error message, or false if failed |
||
| 227 | * @access public |
||
| 228 | */ |
||
| 229 | public function vote() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get a hash query parameter |
||
| 252 | * |
||
| 253 | * @return string The hash |
||
| 254 | * @access protected |
||
| 255 | */ |
||
| 256 | protected function get_hash() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Does the user have moderator privileges? |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | * @access protected |
||
| 266 | */ |
||
| 267 | protected function is_mod() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Is the user the author of the idea? |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | * @access protected |
||
| 277 | */ |
||
| 278 | protected function is_own() |
||
| 282 | } |
||
| 283 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: