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 |
||
13 | class AlertEndpointController extends AbstractEndpointController |
||
14 | { |
||
15 | /** |
||
16 | * Construct |
||
17 | * |
||
18 | * @param Ps2alerts\Api\Repository\AlertRepository $repository |
||
19 | * @param Ps2alerts\Api\Transformer\AlertTransformer $transformer |
||
20 | * @param League\Fractal\Manager $fractal |
||
21 | */ |
||
22 | public function __construct( |
||
31 | |||
32 | /** |
||
33 | * Returns a single alert's information |
||
34 | * |
||
35 | * @see AbstractEndpointController::respondWithItem |
||
36 | * |
||
37 | * @param Symfony\Component\HttpFoundation\Request $request |
||
38 | * @param Symfony\Component\HttpFoundation\Response $response |
||
39 | * @param array $args |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | View Code Duplication | public function getSingle(Request $request, Response $response, array $args) |
|
|
|||
44 | { |
||
45 | $alert = $this->repository->readSingleById($args['id']); |
||
46 | |||
47 | if (empty($alert)) { |
||
48 | return $this->errorEmpty($response); |
||
49 | } |
||
50 | |||
51 | return $this->respond('item', $alert, $this->transformer, $request, $response); |
||
52 | } |
||
53 | |||
54 | View Code Duplication | public function getActives(Request $request, Response $response) |
|
55 | { |
||
56 | $actives = $this->repository->readAllByFields(['InProgress', 1]); |
||
57 | |||
58 | if (empty($actives)) { |
||
59 | return $this->errorEmpty($response); |
||
60 | } |
||
61 | |||
62 | return $this->respond('collection', $actives, $this->transformer, $request, $response); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Returns the victories of each faction and the totals |
||
67 | * |
||
68 | * @param Symfony\Component\HttpFoundation\Request $request |
||
69 | * @param Symfony\Component\HttpFoundation\Response $response |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | public function getVictories(Request $request, Response $response) |
||
89 | |||
90 | /** |
||
91 | * Returns the dominations of each faction and the totals |
||
92 | * |
||
93 | * @param Symfony\Component\HttpFoundation\Request $request |
||
94 | * @param Symfony\Component\HttpFoundation\Response $response |
||
95 | * |
||
96 | * @return array |
||
97 | */ |
||
98 | public function getDominations(Request $request, Response $response) |
||
113 | } |
||
114 |
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.