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 |
||
27 | class SearchSuggestionSet { |
||
28 | /** |
||
29 | * @var SearchSuggestion[] |
||
30 | */ |
||
31 | private $suggestions = []; |
||
32 | |||
33 | /** |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private $pageMap = []; |
||
38 | |||
39 | /** |
||
40 | * Builds a new set of suggestions. |
||
41 | * |
||
42 | * NOTE: the array should be sorted by score (higher is better), |
||
43 | * in descending order. |
||
44 | * SearchSuggestionSet will not try to re-order this input array. |
||
45 | * Providing an unsorted input array is a mistake and will lead to |
||
46 | * unexpected behaviors. |
||
47 | * |
||
48 | * @param SearchSuggestion[] $suggestions (must be sorted by score) |
||
49 | */ |
||
50 | public function __construct( array $suggestions ) { |
||
59 | |||
60 | /** |
||
61 | * Get the list of suggestions. |
||
62 | * @return SearchSuggestion[] |
||
63 | */ |
||
64 | public function getSuggestions() { |
||
67 | |||
68 | /** |
||
69 | * Call array_map on the suggestions array |
||
70 | * @param callback $callback |
||
71 | * @return array |
||
72 | */ |
||
73 | public function map( $callback ) { |
||
76 | |||
77 | /** |
||
78 | * Add a new suggestion at the end. |
||
79 | * If the score of the new suggestion is greater than the worst one, |
||
80 | * the new suggestion score will be updated (worst - 1). |
||
81 | * |
||
82 | * @param SearchSuggestion $suggestion |
||
83 | */ |
||
84 | View Code Duplication | public function append( SearchSuggestion $suggestion ) { |
|
97 | |||
98 | /** |
||
99 | * Add suggestion set to the end of the current one. |
||
100 | * @param SearchSuggestionSet $set |
||
101 | */ |
||
102 | public function appendAll( SearchSuggestionSet $set ) { |
||
107 | |||
108 | /** |
||
109 | * Move the suggestion at index $key to the first position |
||
110 | */ |
||
111 | public function rescore( $key ) { |
||
116 | |||
117 | /** |
||
118 | * Add a new suggestion at the top. If the new suggestion score |
||
119 | * is lower than the best one its score will be updated (best + 1) |
||
120 | * @param SearchSuggestion $suggestion |
||
121 | */ |
||
122 | View Code Duplication | public function prepend( SearchSuggestion $suggestion ) { |
|
135 | |||
136 | /** |
||
137 | * @return float the best score in this suggestion set |
||
138 | */ |
||
139 | public function getBestScore() { |
||
145 | |||
146 | /** |
||
147 | * @return float the worst score in this set |
||
148 | */ |
||
149 | public function getWorstScore() { |
||
155 | |||
156 | /** |
||
157 | * @return int the number of suggestion in this set |
||
158 | */ |
||
159 | public function getSize() { |
||
162 | |||
163 | /** |
||
164 | * Remove any extra elements in the suggestions set |
||
165 | * @param int $limit the max size of this set. |
||
166 | */ |
||
167 | public function shrink( $limit ) { |
||
172 | |||
173 | /** |
||
174 | * Builds a new set of suggestion based on a title array. |
||
175 | * Useful when using a backend that supports only Titles. |
||
176 | * |
||
177 | * NOTE: Suggestion scores will be generated. |
||
178 | * |
||
179 | * @param Title[] $titles |
||
180 | * @return SearchSuggestionSet |
||
181 | */ |
||
182 | View Code Duplication | public static function fromTitles( array $titles ) { |
|
189 | |||
190 | /** |
||
191 | * Builds a new set of suggestion based on a string array. |
||
192 | * |
||
193 | * NOTE: Suggestion scores will be generated. |
||
194 | * |
||
195 | * @param string[] $titles |
||
196 | * @return SearchSuggestionSet |
||
197 | */ |
||
198 | View Code Duplication | public static function fromStrings( array $titles ) { |
|
205 | |||
206 | /** |
||
207 | * @return SearchSuggestionSet an empty suggestion set |
||
208 | */ |
||
209 | public static function emptySuggestionSet() { |
||
212 | } |
||
213 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: