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:
1 | <?php namespace BB\Repo; |
||
6 | class UserRepository extends DBRepository |
||
7 | { |
||
8 | |||
9 | /** |
||
10 | * @var User |
||
11 | */ |
||
12 | protected $model; |
||
13 | /** |
||
14 | * @var AddressRepository |
||
15 | */ |
||
16 | private $addressRepository; |
||
17 | /** |
||
18 | * @var ProfileDataRepository |
||
19 | */ |
||
20 | private $profileDataRepository; |
||
21 | /** |
||
22 | * @var SubscriptionChargeRepository |
||
23 | */ |
||
24 | private $subscriptionChargeRepository; |
||
25 | |||
26 | public function __construct(User $model, AddressRepository $addressRepository, ProfileDataRepository $profileDataRepository, SubscriptionChargeRepository $subscriptionChargeRepository) |
||
34 | |||
35 | public function getActive() |
||
39 | |||
40 | public function getBillableActive() |
||
44 | |||
45 | public function getPaginated(array $params) |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Return a collection of members for public display |
||
64 | * @param bool $showPrivateMembers Some members don't want to listed on public pages, set to true to show everyone |
||
65 | * @return mixed |
||
66 | */ |
||
67 | public function getActivePublicList($showPrivateMembers = false) |
||
68 | { |
||
69 | if ($showPrivateMembers) { |
||
70 | return $this->model->with('profile', 'roles')->active()->where('status', '!=', 'leaving')->orderBy('given_name')->get(); |
||
71 | } else { |
||
72 | return $this->model->with('profile', 'roles')->active()->where('status', '!=', 'leaving')->where('profile_private', 0)->orderBy('given_name')->get(); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | public function getTrustedMissingPhotos() |
||
80 | |||
81 | /** |
||
82 | * Get a list of active members suitable for use in a dropdown |
||
83 | * @return array |
||
84 | */ |
||
85 | public function getAllAsDropdown() |
||
94 | |||
95 | /** |
||
96 | * @param array $memberData The new members details |
||
97 | * @param boolean $isAdminCreating Is the user making the change an admin |
||
98 | * @return User |
||
99 | */ |
||
100 | public function registerMember(array $memberData, $isAdminCreating) |
||
121 | |||
122 | /** |
||
123 | * The user has setup a payment method of some kind so they are now considered active |
||
124 | * This will kick off the automated member checking processes |
||
125 | * |
||
126 | * @param integer $userId |
||
127 | */ |
||
128 | public function ensureMembershipActive($userId) |
||
160 | |||
161 | /** |
||
162 | * @param integer $userId The ID of the user to be updated |
||
163 | * @param array $recordData The data to be updated |
||
164 | * @param boolean $isAdminUpdating Is the user making the change an admin |
||
165 | */ |
||
166 | public function updateMember($userId, array $recordData, $isAdminUpdating) |
||
181 | |||
182 | /** |
||
183 | * The member has left, disable their account and cancel any out stand sub charge records |
||
184 | * The payment day is also cleared so when they start again the payment is charge happens at restart time |
||
185 | * |
||
186 | * @param $userId |
||
187 | */ |
||
188 | public function memberLeft($userId) |
||
198 | |||
199 | public function recordGoCardlessMandateDetails($userId, $subscriptionId) |
||
207 | |||
208 | public function updateUserPaymentMethod($userId, $paymentMethod, $paymentDay = null) |
||
218 | |||
219 | public function recordGoCardlessSubscription($userId, $subscriptionId, $paymentDay = null) |
||
229 | |||
230 | /** |
||
231 | * Record the fact that the user has agreed to the member induction and the rules |
||
232 | * |
||
233 | * @param $userId |
||
234 | */ |
||
235 | public function recordInductionCompleted($userId) |
||
245 | |||
246 | public function getPendingInductionConfirmation() |
||
254 | |||
255 | } |
||
256 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: