Complex classes like AccountController 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 AccountController, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace BB\Http\Controllers; |
||
| 11 | class AccountController extends Controller |
||
| 12 | { |
||
| 13 | |||
| 14 | protected $layout = 'layouts.main'; |
||
| 15 | |||
| 16 | protected $userForm; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var \BB\Helpers\UserImage |
||
| 20 | */ |
||
| 21 | private $userImage; |
||
| 22 | /** |
||
| 23 | * @var \BB\Validators\UserDetails |
||
| 24 | */ |
||
| 25 | private $userDetailsForm; |
||
| 26 | /** |
||
| 27 | * @var \BB\Repo\ProfileDataRepository |
||
| 28 | */ |
||
| 29 | private $profileRepo; |
||
| 30 | /** |
||
| 31 | * @var \BB\Repo\InductionRepository |
||
| 32 | */ |
||
| 33 | private $inductionRepository; |
||
| 34 | /** |
||
| 35 | * @var \BB\Repo\EquipmentRepository |
||
| 36 | */ |
||
| 37 | private $equipmentRepository; |
||
| 38 | /** |
||
| 39 | * @var \BB\Repo\UserRepository |
||
| 40 | */ |
||
| 41 | private $userRepository; |
||
| 42 | /** |
||
| 43 | * @var \BB\Validators\ProfileValidator |
||
| 44 | */ |
||
| 45 | private $profileValidator; |
||
| 46 | /** |
||
| 47 | * @var \BB\Repo\AddressRepository |
||
| 48 | */ |
||
| 49 | private $addressRepository; |
||
| 50 | /** |
||
| 51 | * @var \BB\Repo\SubscriptionChargeRepository |
||
| 52 | */ |
||
| 53 | private $subscriptionChargeRepository; |
||
| 54 | |||
| 55 | |||
| 56 | function __construct( |
||
|
|
|||
| 57 | \BB\Validators\UserValidator $userForm, |
||
| 58 | \BB\Validators\UpdateSubscription $updateSubscriptionAdminForm, |
||
| 59 | \BB\Helpers\GoCardlessHelper $goCardless, |
||
| 60 | \BB\Helpers\UserImage $userImage, |
||
| 61 | \BB\Validators\UserDetails $userDetailsForm, |
||
| 62 | \BB\Repo\ProfileDataRepository $profileRepo, |
||
| 63 | \BB\Repo\InductionRepository $inductionRepository, |
||
| 64 | \BB\Repo\EquipmentRepository $equipmentRepository, |
||
| 65 | \BB\Repo\UserRepository $userRepository, |
||
| 66 | \BB\Validators\ProfileValidator $profileValidator, |
||
| 67 | \BB\Repo\AddressRepository $addressRepository, |
||
| 68 | \BB\Repo\SubscriptionChargeRepository $subscriptionChargeRepository, |
||
| 69 | \BB\Services\Credit $bbCredit) |
||
| 70 | { |
||
| 71 | $this->userForm = $userForm; |
||
| 72 | $this->updateSubscriptionAdminForm = $updateSubscriptionAdminForm; |
||
| 73 | $this->goCardless = $goCardless; |
||
| 74 | $this->userImage = $userImage; |
||
| 75 | $this->userDetailsForm = $userDetailsForm; |
||
| 76 | $this->profileRepo = $profileRepo; |
||
| 77 | $this->inductionRepository = $inductionRepository; |
||
| 78 | $this->equipmentRepository = $equipmentRepository; |
||
| 79 | $this->userRepository = $userRepository; |
||
| 80 | $this->profileValidator = $profileValidator; |
||
| 81 | $this->addressRepository = $addressRepository; |
||
| 82 | $this->subscriptionChargeRepository = $subscriptionChargeRepository; |
||
| 83 | $this->bbCredit = $bbCredit; |
||
| 84 | |||
| 85 | //This tones down some validation rules for admins |
||
| 86 | $this->userForm->setAdminOverride( ! \Auth::guest() && \Auth::user()->hasRole('admin')); |
||
| 87 | |||
| 88 | $this->middleware('role:member', array('except' => ['create', 'store'])); |
||
| 89 | $this->middleware('role:admin', array('only' => ['index'])); |
||
| 90 | //$this->middleware('guest', array('only' => ['create', 'store'])); |
||
| 91 | |||
| 92 | $paymentMethods = [ |
||
| 93 | 'gocardless' => 'GoCardless', |
||
| 94 | 'paypal' => 'PayPal', |
||
| 95 | 'bank-transfer' => 'Manual Bank Transfer', |
||
| 96 | 'other' => 'Other' |
||
| 97 | ]; |
||
| 98 | \View::share('paymentMethods', $paymentMethods); |
||
| 99 | \View::share('paymentDays', array_combine(range(1, 31), range(1, 31))); |
||
| 100 | |||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Display a listing of the resource. |
||
| 105 | * |
||
| 106 | * @return Response |
||
| 107 | */ |
||
| 108 | public function index() |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Show the form for creating a new resource. |
||
| 120 | * |
||
| 121 | * @return Response |
||
| 122 | */ |
||
| 123 | public function create() |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Store a newly created resource in storage. |
||
| 132 | * |
||
| 133 | * @return Illuminate\Http\RedirectResponse |
||
| 134 | */ |
||
| 135 | public function store() |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * Display the specified resource. |
||
| 184 | * |
||
| 185 | * @param int $id |
||
| 186 | * @return Response |
||
| 187 | */ |
||
| 188 | public function show($id) |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Show the form for editing the specified resource. |
||
| 225 | * |
||
| 226 | * @param int $id |
||
| 227 | * @return Response |
||
| 228 | */ |
||
| 229 | public function edit($id) |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * Update the specified resource in storage. |
||
| 242 | * |
||
| 243 | * @param int $id |
||
| 244 | * @return \Illuminate\Http\RedirectResponse |
||
| 245 | */ |
||
| 246 | public function update($id) |
||
| 258 | |||
| 259 | |||
| 260 | |||
| 261 | public function adminUpdate($id) |
||
| 327 | |||
| 328 | |||
| 329 | public function alterSubscription($id) |
||
| 351 | |||
| 352 | public function confirmEmail($id, $hash) |
||
| 363 | |||
| 364 | |||
| 365 | |||
| 366 | public function destroy($id) |
||
| 384 | |||
| 385 | |||
| 386 | public function rejoin($id) |
||
| 393 | |||
| 394 | public function updateSubscriptionAmount($id) |
||
| 409 | } |
||
| 410 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.