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 |
||
16 | View Code Duplication | class UserGetByIdAction |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * @var ResponseFactory |
||
20 | */ |
||
21 | private $responseFactory; |
||
22 | |||
23 | /** |
||
24 | * @var UserManager |
||
25 | */ |
||
26 | private $userManager; |
||
27 | |||
28 | /** |
||
29 | * UserGetByIdAction constructor. |
||
30 | * |
||
31 | * @param ResponseFactory $responseFactory |
||
32 | * @param UserManager $userManager |
||
33 | */ |
||
34 | public function __construct(ResponseFactory $responseFactory, UserManager $userManager) |
||
39 | |||
40 | /** |
||
41 | * @apiVersion 1.0.0 |
||
42 | * @api {get} /v1/users/:user_id Get |
||
43 | * @apiName Get |
||
44 | * @apiGroup Users |
||
45 | * @apiHeader {String} Accept application/json |
||
46 | * @apiParam {Integer{1...N}='me'} :user_id Unique resource ID. |
||
47 | * @apiSuccessExample {json} Success-Response: |
||
48 | * HTTP/1.1 20O OK |
||
49 | * { |
||
50 | * "id": 1, |
||
51 | * "name": "username", |
||
52 | * "email": "[email protected]", |
||
53 | * "role": "Customer", |
||
54 | * "created_at": "2099-12-31T23:59:59+00:00", |
||
55 | * "updated_at": "2099-12-31T23:59:59+00:00" |
||
56 | * } |
||
57 | */ |
||
58 | |||
59 | /** |
||
60 | * Get a user. |
||
61 | * |
||
62 | * @param mixed $id |
||
63 | * @return JsonResponse |
||
64 | */ |
||
65 | public function __invoke($id): JsonResponse |
||
71 | } |
||
72 |
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.