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 |
||
12 | class ActivityLogger |
||
13 | { |
||
14 | use Macroable; |
||
15 | |||
16 | /** @var \Illuminate\Auth\AuthManager */ |
||
17 | protected $auth; |
||
18 | |||
19 | protected $logName = ''; |
||
20 | |||
21 | /** @var \Illuminate\Database\Eloquent\Model */ |
||
22 | protected $performedOn; |
||
23 | |||
24 | /** @var \Illuminate\Database\Eloquent\Model */ |
||
25 | protected $causedBy; |
||
26 | |||
27 | /** @var \Illuminate\Support\Collection */ |
||
28 | protected $properties; |
||
29 | |||
30 | /** @var string */ |
||
31 | protected $authDriver; |
||
32 | |||
33 | /** @var \Spatie\Activitylog\ActivityLogStatus */ |
||
34 | protected $logStatus; |
||
35 | |||
36 | public function __construct(AuthManager $auth, Repository $config, ActivityLogStatus $logStatus) |
||
56 | |||
57 | public function setLogStatus(ActivityLogStatus $logStatus) |
||
63 | |||
64 | public function performedOn(Model $model) |
||
70 | |||
71 | public function on(Model $model) |
||
75 | |||
76 | /** |
||
77 | * @param \Illuminate\Database\Eloquent\Model|int|string $modelOrId |
||
78 | * |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function causedBy($modelOrId) |
||
89 | |||
90 | public function by($modelOrId) |
||
94 | |||
95 | /** |
||
96 | * @param array|\Illuminate\Support\Collection $properties |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function withProperties($properties) |
||
106 | |||
107 | /** |
||
108 | * @param string $key |
||
109 | * @param mixed $value |
||
110 | * |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function withProperty(string $key, $value) |
||
119 | |||
120 | public function useLog(string $logName) |
||
126 | |||
127 | public function inLog(string $logName) |
||
131 | |||
132 | public function enableLogging() |
||
138 | |||
139 | public function disableLogging() |
||
145 | |||
146 | /** |
||
147 | * @param string $description |
||
148 | * |
||
149 | * @return null|mixed |
||
150 | */ |
||
151 | public function log(string $description) |
||
177 | |||
178 | /** |
||
179 | * @param \Illuminate\Database\Eloquent\Model|int|string $modelOrId |
||
180 | * |
||
181 | * @throws \Spatie\Activitylog\Exceptions\CouldNotLogActivity |
||
182 | * |
||
183 | * @return \Illuminate\Database\Eloquent\Model |
||
184 | */ |
||
185 | protected function normalizeCauser($modelOrId): Model |
||
203 | |||
204 | protected function replacePlaceholders(string $description, Activity $activity): string |
||
228 | } |
||
229 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.