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 |
||
15 | class TagPosts extends PostListAbstract |
||
16 | { |
||
17 | const NAME = 'postsWithTag'; |
||
18 | |||
19 | /** |
||
20 | * @var Tag |
||
21 | */ |
||
22 | public $tag; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected $includeSeriesPosts = false; |
||
28 | |||
29 | /** |
||
30 | * Component Registration |
||
31 | * @return array |
||
32 | */ |
||
33 | public function componentDetails(): array |
||
40 | |||
41 | /** |
||
42 | * Component Properties |
||
43 | * @return array |
||
44 | */ |
||
45 | View Code Duplication | public function defineProperties(): array |
|
|
|||
46 | { |
||
47 | return [ |
||
48 | 'tag' => [ |
||
49 | 'title' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.tag_title', |
||
50 | 'description' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.tag_description', |
||
51 | 'default' => '{{ :tag }}', |
||
52 | 'type' => 'string' |
||
53 | ], |
||
54 | 'includeSeriesPosts' => [ |
||
55 | 'title' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.include_series_posts_title', |
||
56 | 'description' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.include_series_posts_description', |
||
57 | 'default' => false, |
||
58 | 'type' => 'checkbox', |
||
59 | 'showExternalParam' => false |
||
60 | ] |
||
61 | ] + parent::defineProperties(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @inheritDoc |
||
66 | */ |
||
67 | protected function prepareVars() |
||
73 | |||
74 | /** |
||
75 | * @inheritDoc |
||
76 | */ |
||
77 | protected function prepareContextItem() |
||
84 | |||
85 | /** |
||
86 | * @return mixed |
||
87 | */ |
||
88 | protected function getPostsQuery() |
||
106 | } |
||
107 |
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.