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 |
||
9 | class Health extends Controller |
||
10 | { |
||
11 | /** |
||
12 | * @var Service |
||
13 | */ |
||
14 | private $healthService; |
||
15 | |||
16 | /** |
||
17 | * Health constructor. |
||
18 | * |
||
19 | * @param Service $healthService |
||
20 | */ |
||
21 | public function __construct(Service $healthService) |
||
25 | |||
26 | /** |
||
27 | * Check all resources. |
||
28 | * |
||
29 | * @return array |
||
30 | * @throws \Exception |
||
31 | */ |
||
32 | public function check() |
||
38 | |||
39 | /** |
||
40 | * Check and get one resource. |
||
41 | * |
||
42 | * @param $slug |
||
43 | * @return mixed |
||
44 | * @throws \Exception |
||
45 | */ |
||
46 | public function getResource($slug) |
||
52 | |||
53 | /** |
||
54 | * Get all resources. |
||
55 | * |
||
56 | * @return mixed |
||
57 | * @throws \Exception |
||
58 | */ |
||
59 | public function allResources() |
||
63 | |||
64 | /** |
||
65 | * @return mixed |
||
66 | * @throws \Exception |
||
67 | */ |
||
68 | public function string() |
||
77 | |||
78 | /** |
||
79 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
80 | * @throws \Exception |
||
81 | */ |
||
82 | public function panel() |
||
88 | |||
89 | View Code Duplication | public function assetAppJs() |
|
99 | |||
100 | View Code Duplication | public function assetAppCss() |
|
110 | |||
111 | public function config() |
||
115 | } |
||
116 |
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: