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 |
||
28 | class ThemeRoutesGenerator implements GeneratorInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var RouteServiceInterface |
||
32 | */ |
||
33 | protected $routeService; |
||
34 | |||
35 | /** |
||
36 | * @var RouteRepositoryInterface |
||
37 | */ |
||
38 | protected $routeRepository; |
||
39 | |||
40 | /** |
||
41 | * @var RouteProviderInterface |
||
42 | */ |
||
43 | protected $routeProvider; |
||
44 | |||
45 | /** |
||
46 | * @var RouteFactoryInterface |
||
47 | */ |
||
48 | protected $routeFactory; |
||
49 | |||
50 | /** |
||
51 | * @var FormFactoryInterface |
||
52 | */ |
||
53 | protected $formFactory; |
||
54 | |||
55 | /** |
||
56 | * @var FakeArticlesGeneratorInterface |
||
57 | */ |
||
58 | protected $fakeArticlesGenerator; |
||
59 | |||
60 | /** |
||
61 | * ThemeRoutesGenerator constructor. |
||
62 | * |
||
63 | * @param RouteServiceInterface $routeService |
||
64 | * @param RouteRepositoryInterface $routeRepository |
||
65 | * @param RouteProviderInterface $routeProvider |
||
66 | * @param RouteFactoryInterface $routeFactory |
||
67 | * @param FormFactoryInterface $formFactory |
||
68 | * @param FakeArticlesGeneratorInterface $fakeArticlesGenerator |
||
69 | */ |
||
70 | public function __construct(RouteServiceInterface $routeService, RouteRepositoryInterface $routeRepository, RouteProviderInterface $routeProvider, RouteFactoryInterface $routeFactory, FormFactoryInterface $formFactory, FakeArticlesGeneratorInterface $fakeArticlesGenerator) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function generate(array $routes): void |
||
98 | |||
99 | /** |
||
100 | * @param array $routeData |
||
101 | * |
||
102 | * @return RouteInterface |
||
103 | * |
||
104 | * @throws \Exception |
||
105 | */ |
||
106 | View Code Duplication | protected function createRoute(array $routeData): RouteInterface |
|
130 | |||
131 | /** |
||
132 | * @param RouteInterface $route |
||
133 | * @param array $routeData |
||
134 | */ |
||
135 | protected function processFakeArticles(RouteInterface $route, array $routeData) |
||
148 | } |
||
149 |
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.