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 topicsolved_test extends \phpbb_database_test_case |
||
20 | { |
||
21 | /** @var \tierra\topicsolved\topicsolved */ |
||
22 | protected $topicsolved; |
||
23 | |||
24 | /** @var \phpbb\user|\PHPUnit_Framework_MockObject_MockObject */ |
||
25 | protected $user; |
||
26 | |||
27 | /** @var \phpbb\auth\auth|\PHPUnit_Framework_MockObject_MockObject */ |
||
28 | protected $auth; |
||
29 | |||
30 | /** @var \phpbb\event\dispatcher|\PHPUnit_Framework_MockObject_MockObject */ |
||
31 | protected $dispatcher; |
||
32 | |||
33 | /** |
||
34 | * Configure the test environment. |
||
35 | * |
||
36 | * @return void |
||
37 | */ |
||
38 | public function setUp() |
||
39 | { |
||
40 | global $phpbb_root_path, $phpEx; |
||
41 | |||
42 | parent::setUp(); |
||
43 | |||
44 | $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); |
||
45 | $this->auth = $this->getMock('\phpbb\auth\auth'); |
||
46 | $this->dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher') |
||
47 | ->disableOriginalConstructor()->getMock(); |
||
48 | |||
49 | $this->topicsolved = new topicsolved( |
||
50 | $this->new_dbal(), $this->user, $this->auth, $this->dispatcher, $phpbb_root_path, $phpEx |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Load required fixtures. |
||
56 | * |
||
57 | * @return mixed |
||
58 | */ |
||
59 | public function getDataSet() |
||
63 | |||
64 | /** |
||
65 | * Ensure regular user can only lock own posts. |
||
66 | */ |
||
67 | public function test_user_can_only_lock_own_post() |
||
75 | |||
76 | /** |
||
77 | * Ensure regular user can lock their own posts. |
||
78 | */ |
||
79 | public function test_user_can_lock_own_post() |
||
89 | |||
90 | /** |
||
91 | * Ensure moderators can lock posts. |
||
92 | */ |
||
93 | public function test_moderator_can_lock_post() |
||
100 | |||
101 | /** |
||
102 | * Ensure tierra.topicsolved.mark_solved_after event works. |
||
103 | */ |
||
104 | View Code Duplication | public function test_mark_solved_event() |
|
|
|||
105 | { |
||
106 | $topic_data = array( |
||
107 | 'forum_lock_solved' => '1', |
||
108 | 'forum_id' => 2, |
||
109 | 'topic_id' => 1 |
||
110 | ); |
||
111 | |||
112 | $this->auth->expects($this->once())->method('acl_get') |
||
113 | ->with('m_lock', 2)->willReturn(true); |
||
114 | $this->dispatcher->expects($this->once())->method('trigger_event') |
||
115 | ->with( |
||
116 | 'tierra.topicsolved.mark_solved_after', |
||
117 | array( |
||
118 | 'topic_data' => $topic_data, |
||
119 | 'column_data' => array( |
||
120 | 'topic_solved' => 5, |
||
121 | 'topic_status' => ITEM_LOCKED |
||
122 | ) |
||
123 | ) |
||
124 | )->willReturn(array()); |
||
125 | |||
126 | $this->topicsolved->mark_solved($topic_data, 5); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Ensure tierra.topicsolved.mark_unsolved_after event works. |
||
131 | */ |
||
132 | View Code Duplication | public function test_mark_unsolved_event() |
|
133 | { |
||
134 | $topic_data = array( |
||
135 | 'forum_lock_solved' => '1', |
||
136 | 'forum_id' => 2, |
||
137 | 'topic_id' => 1 |
||
138 | ); |
||
139 | |||
140 | $this->auth->expects($this->once())->method('acl_get') |
||
141 | ->with('m_lock', 2)->willReturn(true); |
||
142 | $this->dispatcher->expects($this->once())->method('trigger_event') |
||
143 | ->with( |
||
144 | 'tierra.topicsolved.mark_unsolved_after', |
||
145 | array( |
||
146 | 'topic_data' => $topic_data, |
||
147 | 'column_data' => array( |
||
148 | 'topic_solved' => 0, |
||
149 | 'topic_status' => ITEM_UNLOCKED |
||
150 | ) |
||
151 | ) |
||
152 | )->willReturn(array()); |
||
153 | |||
154 | $this->topicsolved->mark_unsolved($topic_data); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Data set for test_image |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | public function image_test_data() |
||
179 | |||
180 | /** |
||
181 | * Ensure proper image markup is being generated. |
||
182 | * |
||
183 | * @param string $type One of "head", "list", or "post". |
||
184 | * @param string $alt Language code for title and alternative text. |
||
185 | * @param string $url Optional link to solved post. |
||
186 | * @param string $expected Result expected from image() call. |
||
187 | * |
||
188 | * @dataProvider image_test_data |
||
189 | */ |
||
190 | public function test_image($type, $alt, $url, $expected) |
||
199 | |||
200 | /** |
||
201 | * Data set for test_icon |
||
202 | * |
||
203 | * @return array |
||
204 | */ |
||
205 | public function icon_test_data() |
||
220 | |||
221 | /** |
||
222 | * Ensure proper icon markup is being generated. |
||
223 | * |
||
224 | * @param string $color Color to use for the icon. |
||
225 | * @param string $alt Language code for title and alternative text. |
||
226 | * @param string $url Optional link to solved post. |
||
227 | * @param string $expected Result expected from image() call. |
||
228 | * |
||
229 | * @dataProvider icon_test_data |
||
230 | */ |
||
231 | public function test_icon($color, $alt, $url, $expected) |
||
249 | } |
||
250 |
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.