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 |
||
9 | class CollectionSpec extends ObjectBehavior { |
||
10 | |||
11 | function let() |
||
70 | |||
71 | function it_is_initializable() |
||
75 | |||
76 | public function it_gets_results_where_key_is_equal_to_given_value() |
||
80 | |||
81 | public function it_gets_results_where_key_is_equal_to_given_value_and_a_zero() |
||
85 | |||
86 | public function it_gets_results_that_are_in_a_given_array() |
||
90 | |||
91 | public function it_gets_results_between_two_given_values() |
||
95 | |||
96 | public function it_gets_results_when_key_is_null() |
||
100 | |||
101 | public function it_gets_results_where_key_is_similar_to_given_value() |
||
105 | |||
106 | public function it_gets_results_where_key_is_not_equal_to_given_value() |
||
110 | |||
111 | public function it_gets_results_that_are_not_in_a_given_array() |
||
115 | |||
116 | public function it_gets_results_that_are_not_between_two_given_values() |
||
120 | |||
121 | public function it_gets_results_when_the_key_is_not_null() |
||
125 | |||
126 | public function it_gets_results_where_key_is_not_similar_to_given_value() |
||
130 | |||
131 | View Code Duplication | public function it_gets_the_first_result_in_a_given_array() |
|
138 | |||
139 | View Code Duplication | public function it_gets_the_last_result_in_a_given_array() |
|
146 | |||
147 | public function it_taps_through_the_given_key_and_checks_the_given_value() |
||
154 | |||
155 | public function it_taps_through_the_given_key_and_checks_in_array() |
||
159 | |||
160 | public function it_taps_through_the_given_key_and_checks_between_two_given_values() |
||
164 | |||
165 | public function it_taps_through_the_given_key_and_checks_for_null() |
||
169 | |||
170 | public function it_taps_through_the_given_key_and_checks_for_similar() |
||
174 | |||
175 | public function it_taps_through_the_given_key_and_checks_not_the_given_value() |
||
179 | |||
180 | public function it_taps_through_the_given_key_and_checks_not_in_array() |
||
184 | |||
185 | public function it_taps_through_the_given_key_and_checks_not_between_two_given_values() |
||
189 | |||
190 | public function it_taps_through_the_given_key_and_checks_for_not_null() |
||
194 | |||
195 | public function it_taps_through_the_given_key_and_checks_for_not_similar() |
||
199 | |||
200 | View Code Duplication | public function it_taps_through_the_object_to_find_the_first_with_a_given_value() |
|
207 | |||
208 | View Code Duplication | public function it_taps_through_the_object_to_find_the_last_with_a_given_value() |
|
215 | |||
216 | public function it_taps_through_a_collection_and_retrieves_a_collection() |
||
224 | } |
||
225 |
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.