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 |
||
17 | class NotifierBuilder implements BuilderInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $projectKey; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $projectId; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $host; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $httpClient; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $rootDirectory; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $ignoredExceptions; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $environment; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $appVersion; |
||
58 | |||
59 | /** |
||
60 | * NotifierBuilder constructor. |
||
61 | */ |
||
62 | 20 | public function __construct() |
|
66 | |||
67 | 8 | public function withProjectKey(string $projectKey): NotifierBuilder |
|
77 | |||
78 | 8 | public function withProjectId(string $projectId): NotifierBuilder |
|
88 | |||
89 | 7 | View Code Duplication | public function withHost(string $host): NotifierBuilder |
99 | |||
100 | 6 | public function withHttpClient(string $httpClient): NotifierBuilder |
|
110 | |||
111 | 7 | public function withRootDirectory(string $rootDirectory): NotifierBuilder |
|
121 | |||
122 | 5 | public function withIgnoredExceptions(array $ignoredExceptions = []): NotifierBuilder |
|
128 | |||
129 | 7 | View Code Duplication | public function withEnvironment(string $environment): NotifierBuilder |
139 | |||
140 | 7 | public function withAppVersion(string $appVersion): NotifierBuilder |
|
150 | |||
151 | /** |
||
152 | * @inheritDoc |
||
153 | */ |
||
154 | 5 | public function build(): Notifier |
|
171 | |||
172 | /** |
||
173 | * @inheritDoc |
||
174 | */ |
||
175 | 20 | public function clear() |
|
186 | |||
187 | /** |
||
188 | * Add filtered exceptions to the notifier. |
||
189 | * |
||
190 | * @param Notifier $notifierInstance |
||
191 | */ |
||
192 | 5 | protected function addFilteredExceptions(Notifier $notifierInstance) |
|
207 | } |
||
208 |
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.