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 | final class AcceptNegotiator implements NegotiatorInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private $supportedMimeTypes; |
||
18 | |||
19 | /** |
||
20 | * @param array $supportedMimeTypes |
||
21 | */ |
||
22 | 8 | public function __construct(array $supportedMimeTypes) |
|
26 | |||
27 | /** |
||
28 | * @param Request $request |
||
29 | * |
||
30 | * @return NegotiatedValue|null |
||
31 | */ |
||
32 | 8 | View Code Duplication | public function negotiate(Request $request) |
46 | |||
47 | /** |
||
48 | * @param string $header |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | 6 | private function aggregatedValues(string $header): array |
|
77 | |||
78 | /** |
||
79 | * @param array $aggregatedValues |
||
80 | * |
||
81 | * @return NegotiatedValue|null |
||
82 | */ |
||
83 | 6 | private function compareAgainstSupportedTypes(array $aggregatedValues) |
|
107 | } |
||
108 |
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.