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 |
||
29 | class QuerySubscriber implements EventSubscriberInterface |
||
30 | { |
||
31 | /** |
||
32 | * @var SessionInterface |
||
33 | */ |
||
34 | private $session; |
||
35 | |||
36 | /** |
||
37 | * @var EventDispatcherInterface |
||
38 | */ |
||
39 | private $eventDispatcher; |
||
40 | |||
41 | /** |
||
42 | * @param SessionInterface $session |
||
43 | * @param EventDispatcherInterface $eventDispatcher |
||
44 | */ |
||
45 | public function __construct(SessionInterface $session, EventDispatcherInterface $eventDispatcher) |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public static function getSubscribedEvents() |
||
62 | |||
63 | /** |
||
64 | * Create a new Sulu Query object. |
||
65 | * |
||
66 | * @param QueryCreateEvent $event |
||
67 | */ |
||
68 | public function handleCreate(QueryCreateEvent $event) |
||
93 | |||
94 | /** |
||
95 | * TODO: We should reuse the PHPCR-ODM query builder here, see: |
||
96 | * https://github.com/doctrine/phpcr-odm/issues/627. |
||
97 | * |
||
98 | * @param QueryCreateBuilderEvent $event |
||
99 | * |
||
100 | * @throws \Exception |
||
101 | */ |
||
102 | public function handleCreateBuilder(QueryCreateBuilderEvent $event) |
||
106 | |||
107 | /** |
||
108 | * Handle query execution. |
||
109 | * |
||
110 | * @param QueryExecuteEvent $event |
||
111 | */ |
||
112 | public function handleQueryExecute(QueryExecuteEvent $event) |
||
122 | |||
123 | /** |
||
124 | * @return QueryManagerInterface |
||
125 | */ |
||
126 | private function getQueryManager() |
||
130 | } |
||
131 |
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.