Complex classes like CommentsController 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 CommentsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class CommentsController extends Controller |
||
| 10 | { |
||
| 11 | private function gEm() |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Manage comment process |
||
| 18 | * @param Request $request |
||
| 19 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 20 | * @internal param $bundle |
||
| 21 | * @internal param $ref |
||
| 22 | * @internal param $ref_id |
||
| 23 | */ |
||
| 24 | public function manageAction(Request $request) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param $params |
||
| 63 | * @param $request |
||
| 64 | * @param $comment_class |
||
| 65 | * @return Response |
||
| 66 | */ |
||
| 67 | private function ajaxInfo($params,$request,$comment_class) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Return a template |
||
| 99 | * @param $comment |
||
| 100 | * @param $request |
||
| 101 | * @param $max_depth |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | private function renderResponse($comment,$request,$max_depth) |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Init/save comment and user info |
||
| 124 | * @param $request |
||
| 125 | * @param $comment |
||
| 126 | * @param $params |
||
| 127 | * @param $session |
||
| 128 | * @return |
||
| 129 | */ |
||
| 130 | private function postInfo($params, $request, $comment, $session) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Save comment entity |
||
| 143 | * @param $comment |
||
| 144 | */ |
||
| 145 | private function save($comment) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Init comment info |
||
| 155 | * @param $params |
||
| 156 | * @param $comment |
||
| 157 | * @param $request |
||
| 158 | * @param $ref_id |
||
| 159 | * @return mixed |
||
| 160 | */ |
||
| 161 | private function commentInfo($params,$comment,$request,$ref_id) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Init user info |
||
| 175 | * @param $request |
||
| 176 | * @param $comment |
||
| 177 | * @return boo|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 178 | */ |
||
| 179 | private function userInfo($request, $comment) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Init message flash |
||
| 200 | * @param $session |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | private function messageFlash($session) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param $request |
||
| 217 | * @param $session |
||
| 218 | */ |
||
| 219 | private function error($request, $session) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get errors form for ajax |
||
| 230 | * @param $form |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | protected function getErrorsAsArray($form) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Error message for Ajax |
||
| 248 | * @param $form |
||
| 249 | * @return Response |
||
| 250 | */ |
||
| 251 | private function returnAjaxErrors($form) |
||
| 256 | } |
||
| 257 |