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 |
||
18 | View Code Duplication | class PostPaginateAction |
|
|
|||
19 | { |
||
20 | /** |
||
21 | * @var ResponseFactory |
||
22 | */ |
||
23 | private $responseFactory; |
||
24 | |||
25 | /** |
||
26 | * @var PostManager |
||
27 | */ |
||
28 | private $postManager; |
||
29 | |||
30 | /** |
||
31 | * @var CacheManager |
||
32 | */ |
||
33 | private $cacheManager; |
||
34 | |||
35 | /** |
||
36 | * PostPaginateAction constructor. |
||
37 | * |
||
38 | * @param ResponseFactory $responseFactory |
||
39 | * @param PostManager $postManager |
||
40 | * @param CacheManager $cacheManager |
||
41 | */ |
||
42 | public function __construct(ResponseFactory $responseFactory, PostManager $postManager, CacheManager $cacheManager) |
||
48 | |||
49 | /** |
||
50 | * @apiVersion 1.0.0 |
||
51 | * @api {get} /v1/posts?page=:page&per_page=:per_page Paginate |
||
52 | * @apiName Paginate |
||
53 | * @apiGroup Posts |
||
54 | * @apiHeader {String} Accept application/json |
||
55 | * @apiParam {Integer{1..N}} [page=1] |
||
56 | * @apiParam {Integer{1..100}} [per_page=20] |
||
57 | * @apiSuccessExample {json} Success-Response: |
||
58 | * HTTP/1.1 200 OK |
||
59 | * { |
||
60 | * "total": 100, |
||
61 | * "per_page": 10, |
||
62 | * "current_page": 2, |
||
63 | * "last_page": 10, |
||
64 | * "next_page_url": "http://path/to/api/resource?page=3", |
||
65 | * "prev_page_url": "http://path/to/api/resource?page=1", |
||
66 | * "from": 10, |
||
67 | * "to": 20, |
||
68 | * "data": [ |
||
69 | * { |
||
70 | * "id": 1, |
||
71 | * "created_by_user_id": 1, |
||
72 | * "is_published": true, |
||
73 | * "description": "The post description.", |
||
74 | * "published_at": "2099-12-31T23:59:59+00:00", |
||
75 | * "created_at": "2099-12-31T23:59:59+00:00", |
||
76 | * "updated_at": "2099-12-31T23:59:59+00:00", |
||
77 | * "photo": { |
||
78 | * "id": 1, |
||
79 | * "created_by_user_id" 1, |
||
80 | * "avg_color": "#000000", |
||
81 | * "created_at": "2099-12-31T23:59:59+00:00", |
||
82 | * "exif": { |
||
83 | * "manufacturer": "Manufacturer Name", |
||
84 | * "model": "Model Number", |
||
85 | * "exposure_time": "1/160", |
||
86 | * "aperture": "f/11.0", |
||
87 | * "iso": 200, |
||
88 | * "taken_at": "2099-12-31T23:59:59+00:00", |
||
89 | * "software": "Software Name" |
||
90 | * }, |
||
91 | * "thumbnails": [ |
||
92 | * "medium": { |
||
93 | * "url": "http://path/to/photo/thumbnail/medium_file" |
||
94 | * "width": 500, |
||
95 | * "height": 500 |
||
96 | * }, |
||
97 | * "large": { |
||
98 | * "url": "http://path/to/photo/thumbnail/large_file" |
||
99 | * "width": 1000, |
||
100 | * "height": 1000 |
||
101 | * } |
||
102 | * ] |
||
103 | * }, |
||
104 | * "tags": [ |
||
105 | * { |
||
106 | * "value": "tag", |
||
107 | * } |
||
108 | * ] |
||
109 | * } |
||
110 | * ] |
||
111 | * } |
||
112 | */ |
||
113 | |||
114 | /** |
||
115 | * Paginate over posts. |
||
116 | * |
||
117 | * @param PaginatedRequest $request |
||
118 | * @return JsonResponse |
||
119 | */ |
||
120 | public function __invoke(PaginatedRequest $request): JsonResponse |
||
130 | } |
||
131 |
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.