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 |
||
26 | class NodeHelper |
||
27 | { |
||
28 | /** @var EntityManagerInterface */ |
||
29 | private $em; |
||
30 | |||
31 | /** @var NodeAdminPublisher */ |
||
32 | private $nodeAdminPublisher; |
||
33 | |||
34 | /** @var TokenStorageInterface */ |
||
35 | private $tokenStorage; |
||
36 | |||
37 | /** @var CloneHelper */ |
||
38 | private $cloneHelper; |
||
39 | |||
40 | /** @var EventDispatcherInterface */ |
||
41 | private $eventDispatcher; |
||
42 | |||
43 | /** |
||
44 | * NodeHelper constructor. |
||
45 | * |
||
46 | * @param EntityManagerInterface $em |
||
47 | * @param NodeAdminPublisher $nodeAdminPublisher |
||
48 | * @param TokenStorageInterface $tokenStorage |
||
49 | * @param CloneHelper $cloneHelper |
||
50 | * @param EventDispatcherInterface $eventDispatcher |
||
51 | 12 | */ |
|
52 | View Code Duplication | public function __construct( |
|
|
|||
53 | EntityManagerInterface $em, |
||
54 | NodeAdminPublisher $nodeAdminPublisher, |
||
55 | TokenStorageInterface $tokenStorage, |
||
56 | CloneHelper $cloneHelper, |
||
57 | EventDispatcherInterface $eventDispatcher |
||
58 | 12 | ) { |
|
59 | 12 | $this->em = $em; |
|
60 | 12 | $this->nodeAdminPublisher = $nodeAdminPublisher; |
|
61 | 12 | $this->tokenStorage = $tokenStorage; |
|
62 | 12 | $this->cloneHelper = $cloneHelper; |
|
63 | 12 | $this->eventDispatcher = $eventDispatcher; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param HasNodeInterface $page The page |
||
68 | * @param NodeTranslation $nodeTranslation The node translation |
||
69 | * @param NodeVersion $nodeVersion The node version |
||
70 | * |
||
71 | * @return NodeVersion |
||
72 | 1 | */ |
|
73 | public function createDraftVersion( |
||
74 | HasNodeInterface $page, |
||
75 | NodeTranslation $nodeTranslation, |
||
76 | NodeVersion $nodeVersion |
||
77 | 1 | ) { |
|
78 | 1 | $user = $this->getAdminUser(); |
|
79 | $publicPage = $this->cloneHelper->deepCloneAndSave($page); |
||
80 | |||
81 | 1 | /* @var NodeVersion $publicNodeVersion */ |
|
82 | 1 | $publicNodeVersion = $this->em->getRepository(NodeVersion::class) |
|
83 | 1 | ->createNodeVersionFor( |
|
84 | $publicPage, |
||
85 | $nodeTranslation, |
||
86 | 1 | $user, |
|
87 | 1 | $nodeVersion->getOrigin(), |
|
88 | 1 | NodeVersion::PUBLIC_VERSION, |
|
89 | $nodeVersion->getCreated() |
||
90 | ); |
||
91 | 1 | ||
92 | 1 | $nodeTranslation->setPublicNodeVersion($publicNodeVersion); |
|
93 | 1 | $nodeVersion->setType(NodeVersion::DRAFT_VERSION); |
|
94 | 1 | $nodeVersion->setOrigin($publicNodeVersion); |
|
95 | $nodeVersion->setCreated(new \DateTime()); |
||
96 | 1 | ||
97 | 1 | $this->em->persist($nodeTranslation); |
|
98 | 1 | $this->em->persist($nodeVersion); |
|
99 | $this->em->flush(); |
||
100 | 1 | ||
101 | 1 | $this->dispatch( |
|
102 | 1 | new NodeEvent( |
|
103 | 1 | $nodeTranslation->getNode(), |
|
104 | $nodeTranslation, |
||
105 | $nodeVersion, |
||
106 | $page |
||
107 | ), |
||
108 | Events::CREATE_DRAFT_VERSION |
||
109 | ); |
||
110 | 1 | ||
111 | return $nodeVersion; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param NodeVersion $nodeVersion |
||
116 | * @param NodeTranslation $nodeTranslation |
||
117 | * @param int $nodeVersionTimeout |
||
118 | * @param bool $nodeVersionIsLocked |
||
119 | 2 | */ |
|
120 | public function prepareNodeVersion(NodeVersion $nodeVersion, NodeTranslation $nodeTranslation, $nodeVersionTimeout, $nodeVersionIsLocked) |
||
121 | 2 | { |
|
122 | 2 | $user = $this->getAdminUser(); |
|
123 | 2 | $thresholdDate = date('Y-m-d H:i:s', time() - $nodeVersionTimeout); |
|
124 | $updatedDate = date('Y-m-d H:i:s', strtotime($nodeVersion->getUpdated()->format('Y-m-d H:i:s'))); |
||
125 | 2 | ||
126 | 2 | View Code Duplication | if ($thresholdDate >= $updatedDate || $nodeVersionIsLocked) { |
127 | 2 | $page = $nodeVersion->getRef($this->em); |
|
128 | 1 | if ($nodeVersion === $nodeTranslation->getPublicNodeVersion()) { |
|
129 | 1 | $this->nodeAdminPublisher |
|
130 | 1 | ->createPublicVersion( |
|
131 | $page, |
||
132 | $nodeTranslation, |
||
133 | $nodeVersion, |
||
134 | $user |
||
135 | ); |
||
136 | 1 | } else { |
|
137 | 1 | $this->createDraftVersion( |
|
138 | $page, |
||
139 | $nodeTranslation, |
||
140 | $nodeVersion |
||
141 | ); |
||
142 | } |
||
143 | 2 | } |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param Node $node |
||
148 | * @param NodeTranslation $nodeTranslation |
||
149 | * @param NodeVersion $nodeVersion |
||
150 | * @param HasNodeInterface $page |
||
151 | * @param bool $isStructureNode |
||
152 | * @param TabPane $tabPane |
||
153 | * |
||
154 | * @return NodeTranslation |
||
155 | 1 | */ |
|
156 | public function updatePage( |
||
157 | Node $node, |
||
158 | NodeTranslation $nodeTranslation, |
||
159 | NodeVersion $nodeVersion, |
||
160 | HasNodeInterface $page, |
||
161 | $isStructureNode, |
||
162 | TabPane $tabPane = null |
||
163 | 1 | ) { |
|
164 | 1 | $this->dispatch( |
|
165 | 1 | new NodeEvent($node, $nodeTranslation, $nodeVersion, $page), |
|
166 | Events::PRE_PERSIST |
||
167 | ); |
||
168 | 1 | ||
169 | 1 | $nodeTranslation->setTitle($page->getTitle()); |
|
170 | if ($isStructureNode) { |
||
171 | $nodeTranslation->setSlug(''); |
||
172 | } |
||
173 | 1 | ||
174 | 1 | $nodeVersion->setUpdated(new \DateTime()); |
|
175 | 1 | if ($nodeVersion->getType() == NodeVersion::PUBLIC_VERSION) { |
|
176 | $nodeTranslation->setUpdated($nodeVersion->getUpdated()); |
||
177 | 1 | } |
|
178 | 1 | $this->em->persist($nodeTranslation); |
|
179 | 1 | $this->em->persist($nodeVersion); |
|
180 | 1 | $this->em->persist($node); |
|
181 | if (null !== $tabPane) { |
||
182 | $tabPane->persist($this->em); |
||
183 | 1 | } |
|
184 | $this->em->flush(); |
||
185 | 1 | ||
186 | 1 | $this->dispatch( |
|
187 | 1 | new NodeEvent($node, $nodeTranslation, $nodeVersion, $page), |
|
188 | Events::POST_PERSIST |
||
189 | ); |
||
190 | 1 | ||
191 | return $nodeTranslation; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param string $refEntityType |
||
196 | * @param string $pageTitle |
||
197 | * @param string $locale |
||
198 | * @param Node|null $parentNode |
||
199 | * |
||
200 | * @return NodeTranslation |
||
201 | 1 | */ |
|
202 | public function createPage( |
||
245 | |||
246 | /** |
||
247 | * @param Node $node |
||
248 | * @param string $locale |
||
249 | * |
||
250 | * @return NodeTranslation |
||
251 | */ |
||
252 | public function deletePage(Node $node, $locale) |
||
276 | |||
277 | /** |
||
278 | * @param Node $node |
||
279 | * @param string $locale |
||
280 | * |
||
281 | * @return HasNodeInterface |
||
282 | */ |
||
283 | public function getPageWithNodeInterface(Node $node, $locale) |
||
290 | |||
291 | /** |
||
292 | * @param Node $node |
||
293 | * @param string $sourceLocale |
||
294 | * @param string $locale |
||
295 | * |
||
296 | * @return NodeTranslation |
||
297 | */ |
||
298 | View Code Duplication | public function copyPageFromOtherLanguage(Node $node, $sourceLocale, $locale) |
|
327 | |||
328 | /** |
||
329 | * @param Node $node |
||
330 | * @param string $locale |
||
331 | * @param string $title |
||
332 | * |
||
333 | * @return NodeTranslation|null |
||
334 | */ |
||
335 | public function duplicatePage(Node $node, $locale, $title = 'New page') |
||
364 | |||
365 | /** |
||
366 | * @param Node $node |
||
367 | * @param int $sourceNodeTranslationId |
||
368 | * @param string $locale |
||
369 | * |
||
370 | * @return NodeTranslation |
||
371 | */ |
||
372 | View Code Duplication | public function createPageDraftFromOtherLanguage(Node $node, $sourceNodeTranslationId, $locale) |
|
401 | |||
402 | /** |
||
403 | * @param Node $node |
||
404 | * @param string $locale |
||
405 | * |
||
406 | * @return NodeTranslation |
||
407 | */ |
||
408 | public function createEmptyPage(Node $node, $locale) |
||
426 | |||
427 | /** |
||
428 | * @param string $entityType |
||
429 | * @param string $title |
||
430 | * |
||
431 | * @return HasNodeInterface |
||
432 | */ |
||
433 | protected function createNewPage($entityType, $title = 'No title') |
||
444 | |||
445 | /** |
||
446 | * @param Node $node |
||
447 | * @param string $locale |
||
448 | */ |
||
449 | View Code Duplication | protected function deleteNodeChildren(Node $node, $locale) |
|
450 | 1 | { |
|
451 | $children = $node->getChildren(); |
||
452 | 1 | ||
453 | /* @var Node $childNode */ |
||
454 | foreach ($children as $childNode) { |
||
455 | 1 | $childNodeTranslation = $childNode->getNodeTranslation($locale, true); |
|
456 | 1 | $childNodeVersion = $childNodeTranslation->getPublicNodeVersion(); |
|
457 | 1 | $childNodePage = $childNodeVersion->getRef($this->em); |
|
458 | 1 | ||
459 | $this->dispatch( |
||
460 | 1 | new NodeEvent( |
|
461 | 1 | $childNode, |
|
462 | 1 | $childNodeTranslation, |
|
463 | 1 | $childNodeVersion, |
|
464 | $childNodePage |
||
465 | ), |
||
466 | Events::PRE_DELETE |
||
467 | ); |
||
468 | |||
469 | $childNode->setDeleted(true); |
||
470 | 1 | $this->em->persist($childNode); |
|
471 | 1 | ||
472 | $this->deleteNodeChildren($childNode, $locale); |
||
473 | 1 | ||
474 | $this->dispatch( |
||
475 | 1 | new NodeEvent( |
|
476 | 1 | $childNode, |
|
477 | 1 | $childNodeTranslation, |
|
478 | 1 | $childNodeVersion, |
|
479 | $childNodePage |
||
480 | ), |
||
481 | Events::POST_DELETE |
||
482 | ); |
||
483 | } |
||
484 | } |
||
485 | 1 | ||
486 | /** |
||
487 | * @return mixed|null |
||
488 | */ |
||
489 | protected function getUser() |
||
501 | |||
502 | /** |
||
503 | * @return mixed |
||
504 | */ |
||
505 | protected function getAdminUser() |
||
514 | |||
515 | /** |
||
516 | * @param object $event |
||
517 | * @param string $eventName |
||
518 | * |
||
519 | * @return object |
||
520 | */ |
||
521 | View Code Duplication | private function dispatch($event, string $eventName) |
|
531 | } |
||
532 |
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.