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 |
||
| 22 | class RouteService implements RouteServiceInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var EventDispatcherInterface |
||
| 26 | */ |
||
| 27 | private $eventDispatcher; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * RouteService constructor. |
||
| 31 | * |
||
| 32 | * @param EventDispatcherInterface $eventDispatcher |
||
| 33 | */ |
||
| 34 | 21 | public function __construct(EventDispatcherInterface $eventDispatcher) |
|
| 35 | { |
||
| 36 | 21 | $this->eventDispatcher = $eventDispatcher; |
|
| 37 | 21 | } |
|
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | 14 | View Code Duplication | public function createRoute(RouteInterface $route) |
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | View Code Duplication | public function updateRoute(RouteInterface $route) |
|
| 57 | { |
||
| 58 | $this->dispatchRouteEvent(RouteEvents::PRE_UPDATE, $route); |
||
| 59 | |||
| 60 | $route = $this->fillRoute($route); |
||
| 61 | |||
| 62 | $this->dispatchRouteEvent(RouteEvents::POST_UPDATE, $route); |
||
| 63 | |||
| 64 | return $route; |
||
| 65 | } |
||
| 66 | |||
| 67 | 14 | private function dispatchRouteEvent($eventName, RouteInterface $route) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param RouteInterface $route |
||
| 74 | * |
||
| 75 | * @return RouteInterface |
||
| 76 | */ |
||
| 77 | 21 | public function fillRoute(RouteInterface $route) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param RouteInterface $route |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 21 | protected function generatePath(RouteInterface $route) |
|
| 113 | } |
||
| 114 |