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 |
||
12 | trait SortTrait |
||
13 | { |
||
14 | /** |
||
15 | * Sorts the elements in the entire Collection<T> using the specified comparer. |
||
16 | * @param ComparerInterface $comparer The ComparerInterface implementation to use when comparing elements, or null |
||
17 | * to use the default comparer Comparer<T>.Default. |
||
18 | * @return $this |
||
19 | */ |
||
20 | 1 | View Code Duplication | public function sort(ComparerInterface $comparer = null) |
29 | |||
30 | /** |
||
31 | * Sorts the keys in the entire Collection<T> using the specified comparer. |
||
32 | * @param ComparerInterface $comparer The ComparerInterface implementation to use when comparing elements, or |
||
33 | * null to use the default comparer Comparer<T>.Default. |
||
34 | * @return $this |
||
35 | */ |
||
36 | 1 | View Code Duplication | public function sortByKey(ComparerInterface $comparer = null) |
45 | } |
||
46 |
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.