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 | View Code Duplication | class DispatchEvent extends Event |
|
|
|||
30 | { |
||
31 | const TYPE = 'dispatch'; |
||
32 | |||
33 | /** |
||
34 | * The route. |
||
35 | * |
||
36 | * @var Route |
||
37 | */ |
||
38 | private $route; |
||
39 | |||
40 | protected function get_route() |
||
44 | |||
45 | /** |
||
46 | * The HTTP request. |
||
47 | * |
||
48 | * @var Request |
||
49 | */ |
||
50 | private $request; |
||
51 | |||
52 | protected function get_request() |
||
56 | |||
57 | /** |
||
58 | * Reference to the HTTP response. |
||
59 | * |
||
60 | * @var Response |
||
61 | */ |
||
62 | private $response; |
||
63 | |||
64 | protected function get_response() |
||
68 | |||
69 | protected function set_response(Response $response = null) |
||
73 | |||
74 | /** |
||
75 | * The event is constructed with the type `dispatch`. |
||
76 | * |
||
77 | * @param RouteDispatcher $target |
||
78 | * @param Route $route |
||
79 | * @param Request $request |
||
80 | * @param Response|null $response |
||
81 | */ |
||
82 | public function __construct(RouteDispatcher $target, Route $route, Request $request, Response &$response = null) |
||
90 | } |
||
91 |
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.