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 |
||
| 37 | class RoutableSubscriber implements EventSubscriberInterface |
||
| 38 | { |
||
| 39 | const ROUTE_FIELD = 'routePath'; |
||
| 40 | const ROUTES_PROPERTY = 'suluRoutes'; |
||
| 41 | const TAG_NAME = 'sulu_article.article_route'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var ChainRouteGeneratorInterface |
||
| 45 | */ |
||
| 46 | private $chainRouteGenerator; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var RouteManagerInterface |
||
| 50 | 35 | */ |
|
| 51 | private $routeManager; |
||
| 52 | 35 | ||
| 53 | 35 | /** |
|
| 54 | 35 | * @var RouteRepositoryInterface |
|
| 55 | 35 | */ |
|
| 56 | private $routeRepository; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var EntityManagerInterface |
||
| 60 | 31 | */ |
|
| 61 | private $entityManager; |
||
| 62 | |||
| 63 | 31 | /** |
|
| 64 | 31 | * @var PropertyEncoder |
|
| 65 | 31 | */ |
|
| 66 | 31 | private $propertyEncoder; |
|
| 67 | 31 | ||
| 68 | /** |
||
| 69 | * @var StructureMetadataFactoryInterface |
||
| 70 | */ |
||
| 71 | private $metadataFactory; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var DocumentInspector |
||
| 75 | */ |
||
| 76 | 31 | private $documentInspector; |
|
| 77 | |||
| 78 | 31 | /** |
|
| 79 | 31 | * @param ChainRouteGeneratorInterface $chainRouteGenerator |
|
| 80 | 29 | * @param RouteManagerInterface $routeManager |
|
| 81 | * @param RouteRepositoryInterface $routeRepository |
||
| 82 | * @param EntityManagerInterface $entityManager |
||
| 83 | 31 | * @param PropertyEncoder $propertyEncoder |
|
| 84 | 31 | * @param StructureMetadataFactoryInterface $metadataFactory |
|
| 85 | 30 | * @param DocumentInspector $documentInspector |
|
| 86 | */ |
||
| 87 | public function __construct( |
||
| 104 | |||
| 105 | 32 | /** |
|
| 106 | 32 | * {@inheritdoc} |
|
| 107 | 32 | */ |
|
| 108 | 32 | View Code Duplication | public static function getSubscribedEvents() |
| 124 | |||
| 125 | 1 | /** |
|
| 126 | 1 | * Load route. |
|
| 127 | 1 | * |
|
| 128 | 1 | * @param AbstractMappingEvent $event |
|
| 129 | 1 | */ |
|
| 130 | public function handleHydrate(AbstractMappingEvent $event) |
||
| 146 | |||
| 147 | /** |
||
| 148 | 1 | * Generate route and save route-path. |
|
| 149 | 1 | * |
|
| 150 | 1 | * @param AbstractMappingEvent $event |
|
| 151 | */ |
||
| 152 | public function handlePersist(AbstractMappingEvent $event) |
||
| 171 | 30 | ||
| 172 | /** |
||
| 173 | * Handle publish event and generate route and the child-routes. |
||
| 174 | * |
||
| 175 | * @param PublishEvent $event |
||
| 176 | */ |
||
| 177 | public function handlePublish(PublishEvent $event) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Create or update for given document. |
||
| 205 | * |
||
| 206 | * @param RoutablePageBehavior $document |
||
| 207 | * @param string $locale |
||
| 208 | * |
||
| 209 | * @return RouteInterface |
||
| 210 | */ |
||
| 211 | View Code Duplication | private function createOrUpdatePageRoute(RoutablePageBehavior $document, $locale) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Create or update for given document. |
||
| 225 | * |
||
| 226 | * @param RoutableBehavior $document |
||
| 227 | * @param string $locale |
||
| 228 | * |
||
| 229 | * @return RouteInterface |
||
| 230 | */ |
||
| 231 | View Code Duplication | private function createOrUpdateRoute(RoutableBehavior $document, $locale) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Removes old-routes where the node does not exists anymore. |
||
| 245 | * |
||
| 246 | * @param SessionInterface $session |
||
| 247 | * @param array $oldRoutes |
||
| 248 | * @param string $locale |
||
| 249 | */ |
||
| 250 | private function removeOldChildRoutes(SessionInterface $session, array $oldRoutes, $locale) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Generates child routes. |
||
| 264 | * |
||
| 265 | * @param ChildrenBehavior $document |
||
| 266 | * @param string $locale |
||
| 267 | * |
||
| 268 | * @return string[] |
||
| 269 | */ |
||
| 270 | private function generateChildRoutes(ChildrenBehavior $document, $locale) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Removes route. |
||
| 295 | * |
||
| 296 | * @param RemoveEvent $event |
||
| 297 | */ |
||
| 298 | public function handleRemove(RemoveEvent $event) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Iterate over children and remove routes. |
||
| 321 | * |
||
| 322 | * @param ChildrenBehavior $document |
||
| 323 | */ |
||
| 324 | private function removeChildRoutes(ChildrenBehavior $document) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Removes route if exists. |
||
| 339 | * |
||
| 340 | * @param RoutablePageBehavior $document |
||
| 341 | */ |
||
| 342 | private function removeChildRoute(RoutablePageBehavior $document) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Add route-path to options. |
||
| 352 | * |
||
| 353 | * @param ConfigureOptionsEvent $event |
||
| 354 | */ |
||
| 355 | public function configureOptions(ConfigureOptionsEvent $event) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Returns encoded "routePath" property-name. |
||
| 363 | * |
||
| 364 | * @param string $structureType |
||
| 365 | * @param string $locale |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | private function getRoutePathPropertyName($structureType, $locale) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Returns encoded property-name. |
||
| 382 | * |
||
| 383 | * @param string $locale |
||
| 384 | * @param string $field |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | private function getPropertyName($locale, $field) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Returns true if given uuid exists. |
||
| 395 | * |
||
| 396 | * @param SessionInterface $session |
||
| 397 | * @param string $uuid |
||
| 398 | * |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | private function nodeExists(SessionInterface $session, $uuid) |
||
| 411 | } |
||
| 412 |
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.