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 | class OverviewController implements |
||
19 | ConfigureInterface, |
||
20 | InjectionAwareInterface |
||
21 | { |
||
22 | use ConfigureTrait, |
||
23 | InjectionAwareTrait; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * @var $data description |
||
28 | */ |
||
29 | //private $data; |
||
30 | |||
31 | public function overview() |
||
32 | { |
||
33 | $title = "Overview"; |
||
34 | $view = $this->di->get("view"); |
||
35 | $pageRender = $this->di->get("pageRender"); |
||
36 | |||
37 | $posts = $this->getPosts(); |
||
38 | |||
39 | $allPosts = []; |
||
40 | |||
41 | View Code Duplication | foreach ($posts as $p) { |
|
|
|||
42 | array_push($allPosts, [$p, $this->getTags([$p->postid])]); |
||
43 | } |
||
44 | |||
45 | $data = [ |
||
46 | "items" => $this->getUsers(), |
||
47 | "posts" => $allPosts, |
||
48 | "popularTags" => $this->getPopularTags() |
||
49 | ]; |
||
50 | |||
51 | $view->add("overview/overview", $data); |
||
52 | |||
53 | $pageRender->renderPage(["title" => $title]); |
||
54 | } |
||
55 | |||
56 | View Code Duplication | public function getUsers() |
|
57 | { |
||
58 | $overview = new Overview(); |
||
59 | |||
60 | $data = $overview->returnLimitUsers($this->di->get("db"), "points desc", 5); |
||
61 | |||
62 | return $data; |
||
63 | } |
||
64 | |||
65 | public function getPosts() |
||
73 | |||
74 | View Code Duplication | public function getTags($id) |
|
82 | |||
83 | public function getPopularTags() |
||
91 | |||
92 | View Code Duplication | public function getCatName($id) |
|
99 | } |
||
100 |
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.