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 |
||
21 | View Code Duplication | class RecentCommand extends AbstractController implements CommandInterface |
|
|
|||
22 | { |
||
23 | /** |
||
24 | * JSON view for displaying the statistics. |
||
25 | * |
||
26 | * @var StatsJsonView |
||
27 | */ |
||
28 | private $view; |
||
29 | |||
30 | /** |
||
31 | * Constructor. |
||
32 | * |
||
33 | * @param StatsJsonView $view JSON view for displaying the statistics. |
||
34 | */ |
||
35 | public function __construct(StatsJsonView $view) |
||
39 | |||
40 | /** |
||
41 | * Execute the controller. |
||
42 | * |
||
43 | * @return boolean |
||
44 | */ |
||
45 | public function execute() |
||
64 | |||
65 | /** |
||
66 | * Get the command's description |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | public function getDescription() : string |
||
74 | |||
75 | /** |
||
76 | * Get the command's title |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | public function getTitle() : string |
||
84 | } |
||
85 |
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.