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 |
||
19 | class PostsController extends Controller |
||
20 | { |
||
21 | /** |
||
22 | * @ApiDoc( |
||
23 | * resource=true, |
||
24 | * description="Returns a collection of Posts", |
||
25 | * statusCodes={ |
||
26 | * 200="Returned when all parameters were correct", |
||
27 | * 404="Returned when the entities with given limit and offset are not found", |
||
28 | * }, |
||
29 | * output = "array<AppBundle\Model\PostsResponse>" |
||
30 | * ) |
||
31 | * |
||
32 | * @QueryParam(name="limit", requirements="\d+", default="10", description="Count entries at one page") |
||
33 | * @QueryParam(name="page", requirements="\d+", default="1", description="Number of page to be shown") |
||
34 | * @QueryParam( |
||
35 | * name="locale", |
||
36 | * requirements="^[a-zA-Z]+", |
||
37 | * default="uk", |
||
38 | * description="Selects language of data you want to receive" |
||
39 | * ) |
||
40 | * @QueryParam(name="tag", description="You can receive posts by Tag slug, without Tag you will receive all posts") |
||
41 | * |
||
42 | * @RestView |
||
43 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
44 | */ |
||
45 | 3 | public function cgetAction(ParamFetcher $paramFetcher) |
|
46 | { |
||
47 | 3 | $em = $this->getDoctrine()->getManager(); |
|
48 | |||
49 | 3 | $posts = $em->getRepository('AppBundle:Post') |
|
50 | 3 | ->findAllOrByTag( |
|
51 | 3 | $paramFetcher->get('limit'), |
|
52 | 3 | ($paramFetcher->get('page')-1) * $paramFetcher->get('limit'), |
|
53 | 3 | $paramFetcher->get('tag') |
|
54 | ) |
||
55 | ; |
||
56 | |||
57 | 3 | $postsTranslated = []; |
|
58 | |||
59 | 3 | View Code Duplication | foreach ($posts as $post) { |
|
|||
60 | 3 | $post->setLocale($paramFetcher->get('locale')); |
|
61 | 3 | $em->refresh($post); |
|
62 | |||
63 | 3 | if ($post->getTranslations()) { |
|
64 | 3 | $post->unsetTranslations(); |
|
65 | } |
||
66 | |||
67 | 3 | $tags = $post->getTags(); |
|
68 | |||
69 | 3 | foreach ($tags as $tag) { |
|
70 | 3 | $tag->setLocale($paramFetcher->get('locale')); |
|
71 | 3 | $em->refresh($tag); |
|
72 | |||
73 | 3 | if ($tag->getTranslations()) { |
|
74 | 3 | $tag->unsetTranslations(); |
|
75 | } |
||
76 | } |
||
77 | |||
78 | 3 | $postsTranslated[] = $post; |
|
79 | } |
||
80 | |||
81 | 3 | $posts = $postsTranslated; |
|
82 | |||
83 | 3 | $postsResponse = new PostsResponse(); |
|
84 | 3 | $postsResponse->setPosts($posts); |
|
85 | 3 | $postsResponse->setTotalCount( |
|
86 | 3 | $this->getDoctrine()->getManager()->getRepository('AppBundle:Post')->getCount($paramFetcher->get('tag')) |
|
87 | ); |
||
88 | 3 | $postsResponse->setPageCount(ceil($postsResponse->getTotalCount() / $paramFetcher->get('limit'))); |
|
89 | 3 | $postsResponse->setPage($paramFetcher->get('page')); |
|
90 | |||
91 | 3 | $self = $this->generateUrl( |
|
92 | 3 | 'get_posts', |
|
93 | [ |
||
94 | 3 | 'locale' => $paramFetcher->get('locale'), |
|
95 | 3 | 'limit' => $paramFetcher->get('limit'), |
|
96 | 3 | 'page' => $paramFetcher->get('page'), |
|
97 | 3 | 'tag' => $paramFetcher->get('tag'), |
|
98 | ], |
||
99 | 3 | UrlGeneratorInterface::ABSOLUTE_URL |
|
100 | ); |
||
101 | |||
102 | 3 | $first = $this->generateUrl( |
|
103 | 3 | 'get_posts', |
|
104 | [ |
||
105 | 3 | 'locale' => $paramFetcher->get('locale'), |
|
106 | 3 | 'limit' => $paramFetcher->get('limit'), |
|
107 | 3 | 'tag' => $paramFetcher->get('tag'), |
|
108 | ], |
||
109 | 3 | UrlGeneratorInterface::ABSOLUTE_URL |
|
110 | ); |
||
111 | |||
112 | 3 | $nextPage = $paramFetcher->get('page') < $postsResponse->getPageCount() ? |
|
113 | 3 | $this->generateUrl( |
|
114 | 3 | 'get_posts', |
|
115 | [ |
||
116 | 3 | 'locale' => $paramFetcher->get('locale'), |
|
117 | 3 | 'limit' => $paramFetcher->get('limit'), |
|
118 | 3 | 'page' => $paramFetcher->get('page')+1, |
|
119 | 3 | 'tag' => $paramFetcher->get('tag'), |
|
120 | ], |
||
121 | 3 | UrlGeneratorInterface::ABSOLUTE_URL |
|
122 | ) : |
||
123 | 3 | 'false'; |
|
124 | |||
125 | 3 | $previsiousPage = $paramFetcher->get('page') > 1 ? |
|
126 | $this->generateUrl( |
||
127 | 'get_posts', |
||
128 | [ |
||
129 | 'locale' => $paramFetcher->get('locale'), |
||
130 | 'limit' => $paramFetcher->get('limit'), |
||
131 | 'page' => $paramFetcher->get('page')-1, |
||
132 | 'tag' => $paramFetcher->get('tag'), |
||
133 | ], |
||
134 | UrlGeneratorInterface::ABSOLUTE_URL |
||
135 | ) : |
||
136 | 3 | 'false'; |
|
137 | |||
138 | 3 | $last = $this->generateUrl( |
|
139 | 3 | 'get_posts', |
|
140 | [ |
||
141 | 3 | 'locale' => $paramFetcher->get('locale'), |
|
142 | 3 | 'limit' => $paramFetcher->get('limit'), |
|
143 | 3 | 'page' => $postsResponse->getPageCount(), |
|
144 | 3 | 'tag' => $paramFetcher->get('tag'), |
|
145 | ], |
||
146 | 3 | UrlGeneratorInterface::ABSOLUTE_URL |
|
147 | ); |
||
148 | |||
149 | 3 | $links = new PaginationLinks(); |
|
150 | |||
151 | 3 | $postsResponse->setLinks($links->setSelf(new Link($self))); |
|
152 | 3 | $postsResponse->setLinks($links->setFirst(new Link($first))); |
|
153 | 3 | $postsResponse->setLinks($links->setNext(new Link($nextPage))); |
|
154 | 3 | $postsResponse->setLinks($links->setPrev(new Link($previsiousPage))); |
|
155 | 3 | $postsResponse->setLinks($links->setLast(new Link($last))); |
|
156 | |||
157 | 3 | return $postsResponse; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * @ApiDoc( |
||
162 | * resource=true, |
||
163 | * description="Returns an Post by unique property {slug}", |
||
164 | * statusCodes={ |
||
165 | * 200="Returned when Post by {slug} was found", |
||
166 | * 404="Returned when Post by {slug} was not found", |
||
167 | * }, |
||
168 | * output = "AppBundle\Entity\Post" |
||
169 | * ) |
||
170 | * |
||
171 | * @QueryParam( |
||
172 | * name="locale", |
||
173 | * requirements="^[a-zA-Z]+", |
||
174 | * default="uk", |
||
175 | * description="Selects language of data you want to receive" |
||
176 | * ) |
||
177 | * |
||
178 | * @RestView |
||
179 | */ |
||
180 | 1 | public function getAction(ParamFetcher $paramFetcher, $slug) |
|
211 | } |
||
212 |
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.