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 |
||
30 | class FlowOperations extends Controller { |
||
31 | |||
32 | /** @var Manager */ |
||
33 | protected $manager; |
||
34 | |||
35 | /** |
||
36 | * @param IRequest $request |
||
37 | * @param Manager $manager |
||
38 | */ |
||
39 | public function __construct(IRequest $request, Manager $manager) { |
||
43 | |||
44 | /** |
||
45 | * @NoCSRFRequired |
||
46 | * |
||
47 | * @param string $class |
||
48 | * @return JSONResponse |
||
49 | */ |
||
50 | public function getOperations($class) { |
||
59 | |||
60 | /** |
||
61 | * @param string $class |
||
62 | * @param string $name |
||
63 | * @param array[] $checks |
||
64 | * @param string $operation |
||
65 | * @return JSONResponse The added element |
||
66 | */ |
||
67 | View Code Duplication | public function addOperation($class, $name, $checks, $operation) { |
|
76 | |||
77 | /** |
||
78 | * @param int $id |
||
79 | * @param string $name |
||
80 | * @param array[] $checks |
||
81 | * @param string $operation |
||
82 | * @return JSONResponse The updated element |
||
83 | */ |
||
84 | View Code Duplication | public function updateOperation($id, $name, $checks, $operation) { |
|
93 | |||
94 | /** |
||
95 | * @param int $id |
||
96 | * @return JSONResponse |
||
97 | */ |
||
98 | public function deleteOperation($id) { |
||
102 | |||
103 | /** |
||
104 | * @param array $operation |
||
105 | * @return array |
||
106 | */ |
||
107 | protected function prepareOperation(array $operation) { |
||
122 | } |
||
123 |
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.