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 |
||
10 | class DeleteController extends AbstractActionController |
||
11 | { |
||
12 | /** |
||
13 | * @var int |
||
14 | */ |
||
15 | private $id; |
||
16 | |||
17 | /** |
||
18 | * @var DeleterInterface |
||
19 | */ |
||
20 | private $deleter; |
||
21 | |||
22 | /** |
||
23 | * @var DeleteViewModelInterface |
||
24 | */ |
||
25 | private $viewModel; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $redirectTo; |
||
31 | |||
32 | /** |
||
33 | * @param $id |
||
34 | * @param DeleterInterface $deleter |
||
35 | * @param DeleteViewModelInterface $viewModel |
||
36 | * @param null $redirectTo |
||
37 | */ |
||
38 | public function __construct( |
||
50 | |||
51 | /** |
||
52 | * Execute the request |
||
53 | * |
||
54 | * @param MvcEvent $e |
||
55 | * @return DeleteViewModelInterface|\Zend\Http\Response |
||
56 | */ |
||
57 | public function onDispatch(MvcEvent $e) |
||
79 | } |
||
80 |
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.