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 RescueEvent extends Event |
|
|
|||
30 | { |
||
31 | const TYPE = 'rescue'; |
||
32 | |||
33 | /** |
||
34 | * Reference to the exception to throw if the rescue fails. |
||
35 | * |
||
36 | * @var \Exception |
||
37 | */ |
||
38 | private $exception; |
||
39 | |||
40 | /** |
||
41 | * @return \Exception |
||
42 | */ |
||
43 | protected function get_exception() |
||
47 | |||
48 | /** |
||
49 | * @param \Exception $exception |
||
50 | * |
||
51 | * @return \Exception |
||
52 | */ |
||
53 | protected function set_exception(\Exception $exception) |
||
57 | |||
58 | /** |
||
59 | * The request. |
||
60 | * |
||
61 | * @var Request |
||
62 | */ |
||
63 | private $request; |
||
64 | |||
65 | /** |
||
66 | * @return Request |
||
67 | */ |
||
68 | protected function get_request() |
||
72 | |||
73 | /** |
||
74 | * Reference to the response that rescue the route. |
||
75 | * |
||
76 | * @var Response |
||
77 | */ |
||
78 | private $response; |
||
79 | |||
80 | /** |
||
81 | * @return Response|null |
||
82 | */ |
||
83 | protected function get_response() |
||
87 | |||
88 | /** |
||
89 | * @param Response|null $response |
||
90 | */ |
||
91 | protected function set_response(Response $response = null) |
||
95 | |||
96 | /** |
||
97 | * The event is constructed with the type {@link self::TYPE}. |
||
98 | * |
||
99 | * @param Route $target |
||
100 | * @param \Exception $exception Reference to the exception thrown while dispatching the route. |
||
101 | * @param Request $request |
||
102 | * @param Response|null $response |
||
103 | */ |
||
104 | public function __construct(Route $target, \Exception &$exception, Request $request, Response &$response = null) |
||
112 | } |
||
113 |
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.