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 |
||
18 | abstract class BaseController |
||
19 | { |
||
20 | /** |
||
21 | * @var \Illuminate\Validation\Factory; |
||
22 | */ |
||
23 | public $validation; |
||
24 | |||
25 | /** |
||
26 | * @var \App\Common\Renderer |
||
27 | */ |
||
28 | public $renderer; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | public $settings; |
||
34 | |||
35 | /** |
||
36 | * @var \Swift_Mailer |
||
37 | */ |
||
38 | public $mailer; |
||
39 | |||
40 | /** |
||
41 | * @var \App\Common\MailRenderer |
||
42 | */ |
||
43 | public $mailRenderer; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $encodeEntitiesExtended = [ |
||
49 | 'App\Model\Log' => 'App\Schema\LogSchema', |
||
50 | 'App\Model\Right' => 'App\Schema\RightSchema', |
||
51 | 'App\Model\Role' => 'App\Schema\RoleSchema', |
||
52 | 'App\Model\User' => 'App\Schema\UserSchemaExtended', |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private $encodeEntities = [ |
||
59 | 'App\Model\Log' => 'App\Schema\LogSchema', |
||
60 | 'App\Model\Right' => 'App\Schema\RightSchema', |
||
61 | 'App\Model\Role' => 'App\Schema\RoleSchema', |
||
62 | 'App\Model\User' => 'App\Schema\UserSchema', |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * BaseController constructor. |
||
67 | * |
||
68 | * @param $container |
||
69 | */ |
||
70 | public function __construct($container) |
||
80 | |||
81 | /** |
||
82 | * @param array|null|object $params |
||
83 | * @param string $entity |
||
84 | * @param IRequest $request |
||
85 | * |
||
86 | * @return bool |
||
87 | * @throws JsonException |
||
88 | */ |
||
89 | public function validationRequest($params, $entity, $request) |
||
104 | |||
105 | /** |
||
106 | * @param Request $request |
||
107 | * @param mixed $entities |
||
108 | * @param integer|null $pageNumber |
||
109 | * @param integer|null $pageSize |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function encode(Request $request, $entities, $pageNumber = null, $pageSize = null) |
||
162 | |||
163 | /** |
||
164 | * Register model observers |
||
165 | */ |
||
166 | private function registerModelObservers(){ |
||
187 | } |
||
188 |
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.