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 |
||
24 | class EntityBrowserFormController extends HtmlFormController implements ContainerInjectionInterface { |
||
25 | |||
26 | /** |
||
27 | * Current route match service. |
||
28 | * |
||
29 | * @var \Drupal\Core\Routing\RouteMatchInterface |
||
30 | */ |
||
31 | protected $currentRouteMatch; |
||
32 | |||
33 | /** |
||
34 | * The browser storage. |
||
35 | * |
||
36 | * @var \Drupal\Core\Entity\EntityStorageInterface |
||
37 | */ |
||
38 | protected $browserStorage; |
||
39 | |||
40 | /** |
||
41 | * Current request. |
||
42 | * |
||
43 | * @var \Symfony\Component\HttpFoundation\Request |
||
44 | */ |
||
45 | protected $request; |
||
46 | |||
47 | /** |
||
48 | * Constructs Entity browser form controller. |
||
49 | * |
||
50 | * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver |
||
51 | * The controller resolver. |
||
52 | * @param \Drupal\Core\Form\FormBuilderInterface $form_builder |
||
53 | * The form builder. |
||
54 | * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver |
||
55 | * The class resolver. |
||
56 | * @param RouteMatchInterface $route_match |
||
57 | * Current route match service. |
||
58 | * @param EntityManagerInterface $entity_manager |
||
59 | * Entity manager service. |
||
60 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
61 | * Current request. |
||
62 | */ |
||
63 | public function __construct(ControllerResolverInterface $controller_resolver, FormBuilderInterface $form_builder, ClassResolverInterface $class_resolver, RouteMatchInterface $route_match, EntityManagerInterface $entity_manager, Request $request) { |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | View Code Duplication | public static function create(ContainerInterface $container) { |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | protected function getFormObject(RouteMatchInterface $route_match, $form_arg) { |
||
91 | |||
92 | /** |
||
93 | * Standalone entity browser title callback. |
||
94 | */ |
||
95 | public function title() { |
||
99 | |||
100 | /** |
||
101 | * Loads entity browser object for this page. |
||
102 | * |
||
103 | * @return \Drupal\entity_browser\EntityBrowserInterface |
||
104 | * Loads the entity browser object |
||
105 | */ |
||
106 | protected function loadBrowser() { |
||
112 | |||
113 | } |
||
114 |
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.