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 | final class CourseSearchController |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $defaultLanguage; |
||
24 | |||
25 | /** |
||
26 | * @var RepositoryInterface |
||
27 | */ |
||
28 | private $repository; |
||
29 | |||
30 | /** |
||
31 | * @var RequestManagerInterface |
||
32 | */ |
||
33 | private $requestManager; |
||
34 | |||
35 | /** |
||
36 | * @var ResponseManagerInterface |
||
37 | */ |
||
38 | private $responseManager; |
||
39 | |||
40 | /** |
||
41 | * @var TranslatorInterface |
||
42 | */ |
||
43 | private $translator; |
||
44 | |||
45 | /** |
||
46 | * @var ValidatorInterface |
||
47 | */ |
||
48 | private $validator; |
||
49 | |||
50 | /** |
||
51 | * @param string $defaultLanguage |
||
52 | * @param RepositoryInterface $repository |
||
53 | * @param RequestManagerInterface $requestManager |
||
54 | * @param ResponseManagerInterface $responseManager |
||
55 | * @param TranslatorInterface $translator |
||
56 | * @param ValidatorInterface $validator |
||
57 | */ |
||
58 | View Code Duplication | public function __construct( |
|
73 | |||
74 | /** |
||
75 | * @param Request $request |
||
76 | * |
||
77 | * @return Response |
||
78 | */ |
||
79 | public function __invoke(Request $request): Response |
||
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.