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:
Complex classes like LeagueStatisticsEloquent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LeagueStatisticsEloquent, and based on these observations, apply Extract Interface, too.
1 | <?php namespace GolfLeague\Statistics\League; |
||
12 | class LeagueStatisticsEloquent implements LeagueStatistics |
||
13 | { |
||
14 | public function topFiveLowestScores() |
||
64 | public function topFiveNetScoresByYear($year) |
||
65 | { |
||
66 | $date1 = $year . '-01-01'; |
||
67 | $date2 = $year . '-12-31'; |
||
68 | |||
69 | return Netwinner::with('player','match','match.course') |
||
70 | ->where('created_at', '>=', $date1) |
||
71 | ->where('created_at', '<=', $date2) |
||
72 | ->orderBy('score')->take(5)->get(); |
||
73 | } |
||
74 | public function mostSkinsByYear($year) |
||
119 | View Code Duplication | public function totalBirdies($year) |
|
120 | { |
||
121 | $date1 = $year . '-01-01'; |
||
122 | $date2 = $year . '-12-31'; |
||
123 | $holescores = Holescore::with('round.player','hole') |
||
124 | ->where('created_at', '>=', $date1) |
||
125 | ->where('created_at', '<=', $date2) |
||
126 | ->get(); |
||
127 | $allBirdies = array(); |
||
128 | foreach($holescores as $key => $holescore) { |
||
129 | if($holescore['round']['match_id'] != null){ |
||
130 | if( ($holescore['hole']['par'] - $holescore['score']) === 1) { |
||
131 | $allBirdies[]= $holescore['round']['player']['name']; |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | $i =0; |
||
136 | $newArray = array_count_values($allBirdies); |
||
137 | $birds = array(); |
||
138 | foreach ($newArray as $key => $value) { |
||
139 | $birds[$i]['name'] = $key; |
||
140 | $birds[$i]['birds'] =$value; |
||
141 | $i++; |
||
142 | } |
||
143 | return $birds; |
||
144 | } |
||
145 | View Code Duplication | public function totalPars($year) |
|
146 | { |
||
147 | $year = $year . '-01-01'; |
||
148 | $holescores = Holescore::with('round.player','hole') |
||
149 | ->where('created_at', '>', $year) |
||
150 | ->get(); |
||
151 | $allPars = array(); |
||
152 | foreach($holescores as $key => $holescore) { |
||
153 | if($holescore['round']['match_id'] != null){ |
||
154 | if( ($holescore['hole']['par'] - $holescore['score']) === 0) { |
||
155 | $allPars[]= $holescore['round']['player']['name']; |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | $i =0; |
||
160 | $newArray = array_count_values($allPars); |
||
161 | $pars = array(); |
||
162 | foreach ($newArray as $key => $value) { |
||
163 | $pars[$i]['name'] = $key; |
||
164 | $pars[$i]['pars'] =$value; |
||
165 | $i++; |
||
166 | } |
||
167 | return $pars; |
||
168 | } |
||
169 | View Code Duplication | public function totalBogeys($year) |
|
170 | { |
||
171 | $year = $year . '-01-01'; |
||
172 | $holescores = Holescore::with('round.player','hole') |
||
173 | ->where('created_at', '>', $year) |
||
174 | ->get(); |
||
175 | $allBogeys = array(); |
||
176 | foreach($holescores as $key => $holescore) { |
||
177 | if($holescore['round']['match_id'] != null){ |
||
178 | if( ($holescore['score'] - $holescore['hole']['par']) === 1) { |
||
179 | $allBogeys[]= $holescore['round']['player']['name']; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | $i = 0; |
||
184 | $newArray = array_count_values($allBogeys); |
||
185 | $bogeys = array(); |
||
186 | foreach ($newArray as $key => $value) { |
||
187 | $bogeys[$i]['name'] = $key; |
||
188 | $bogeys[$i]['bogeys'] =$value; |
||
189 | $i++; |
||
190 | } |
||
191 | return $bogeys; |
||
192 | } |
||
193 | View Code Duplication | public function totalDoubles($year) |
|
194 | { |
||
195 | $year = $year . '-01-01'; |
||
196 | $holescores = Holescore::with('round.player','hole') |
||
197 | ->where('created_at', '>', $year) |
||
198 | ->get(); |
||
199 | $allDoubles = array(); |
||
200 | foreach($holescores as $key => $holescore) { |
||
201 | if($holescore['round']['match_id'] != null){ |
||
202 | if( ($holescore['score'] - $holescore['hole']['par']) === 2) { |
||
203 | $allDoubles[]= $holescore['round']['player']['name']; |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | $i = 0; |
||
208 | $newArray = array_count_values($allDoubles); |
||
209 | $doubles = array(); |
||
210 | foreach ($newArray as $key => $value) { |
||
211 | $doubles[$i]['name'] = $key; |
||
212 | $doubles[$i]['doubles'] =$value; |
||
213 | $i++; |
||
214 | } |
||
215 | return $doubles; |
||
216 | } |
||
217 | View Code Duplication | public function totalOthers($year) |
|
218 | { |
||
219 | $year = $year . '-01-01'; |
||
220 | $holescores = Holescore::with('round.player','hole') |
||
221 | ->where('created_at', '>', $year) |
||
222 | ->get(); |
||
223 | $allOthers = array(); |
||
224 | foreach($holescores as $key => $holescore) { |
||
225 | if($holescore['round']['match_id'] != null){ |
||
226 | if( ($holescore['score'] - $holescore['hole']['par']) > 2) { |
||
227 | $allOthers[]= $holescore['round']['player']['name']; |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | $i = 0; |
||
232 | $newArray = array_count_values($allOthers); |
||
233 | $others = array(); |
||
234 | foreach ($newArray as $key => $value) { |
||
235 | $others[$i]['name'] = $key; |
||
236 | $others[$i]['others'] =$value; |
||
237 | $i++; |
||
238 | } |
||
239 | return $others; |
||
240 | } |
||
241 | |||
242 | public function netScoresByPlayer($playerId) |
||
333 | |||
334 | View Code Duplication | public function netScoresLeague() |
|
387 | |||
388 | View Code Duplication | public function netCumulative() |
|
504 | |||
505 | |||
506 | } |
||
507 |
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.