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 |
||
| 24 | class PlaceAutocompleteService extends AbstractPlaceParsableService |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | public function setHttps($https) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param PlaceAutocompleteRequestInterface $request |
||
| 40 | * |
||
| 41 | * @return PlaceAutocompleteResponse |
||
| 42 | */ |
||
| 43 | View Code Duplication | public function process(PlaceAutocompleteRequestInterface $request) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param mixed[] $data |
||
| 65 | * |
||
| 66 | * @return PlaceAutocompleteResponse |
||
| 67 | */ |
||
| 68 | private function buildResponse(array $data) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param mixed[] $data |
||
| 79 | * |
||
| 80 | * @return PlaceAutocompletePrediction[] |
||
| 81 | */ |
||
| 82 | private function buildPredictions(array $data) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param mixed[] $data |
||
| 95 | * |
||
| 96 | * @return PlaceAutocompletePrediction |
||
| 97 | */ |
||
| 98 | private function buildPrediction(array $data) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param mixed[] $data |
||
| 112 | * |
||
| 113 | * @return PlaceAutocompleteTerm[] |
||
| 114 | */ |
||
| 115 | private function buildTerms(array $data) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param mixed[] $data |
||
| 128 | * |
||
| 129 | * @return PlaceAutocompleteTerm |
||
| 130 | */ |
||
| 131 | private function buildTerm(array $data) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param mixed[] $data |
||
| 142 | * |
||
| 143 | * @return PlaceAutocompleteMatch[] |
||
| 144 | */ |
||
| 145 | private function buildMatches(array $data) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param mixed[] $data |
||
| 158 | * |
||
| 159 | * @return PlaceAutocompleteMatch |
||
| 160 | */ |
||
| 161 | private function buildMatch(array $data) |
||
| 169 | } |
||
| 170 |
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.