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 |
||
| 19 | View Code Duplication | class PlaceAutocompleteResponse |
|
|
|
|||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string|null |
||
| 23 | */ |
||
| 24 | private $status; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var PlaceAutocompleteRequestInterface|null |
||
| 28 | */ |
||
| 29 | private $request; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var PlaceAutocompletePrediction[] |
||
| 33 | */ |
||
| 34 | private $predictions = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return bool |
||
| 38 | */ |
||
| 39 | public function hasStatus() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return string|null |
||
| 46 | */ |
||
| 47 | public function getStatus() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string|null $status |
||
| 54 | */ |
||
| 55 | public function setStatus($status) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | public function hasRequest() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return PlaceAutocompleteRequestInterface|null |
||
| 70 | */ |
||
| 71 | public function getRequest() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param PlaceAutocompleteRequestInterface|null $request |
||
| 78 | */ |
||
| 79 | public function setRequest(PlaceAutocompleteRequestInterface $request = null) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | public function hasPredictions() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return PlaceAutocompletePrediction[] |
||
| 94 | */ |
||
| 95 | public function getPredictions() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param PlaceAutocompletePrediction[] $predictions |
||
| 102 | */ |
||
| 103 | public function setPredictions(array $predictions) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param PlaceAutocompletePrediction[] $predictions |
||
| 111 | */ |
||
| 112 | public function addPredictions(array $predictions) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param PlaceAutocompletePrediction $prediction |
||
| 121 | * |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function hasPrediction(PlaceAutocompletePrediction $prediction) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param PlaceAutocompletePrediction $prediction |
||
| 131 | */ |
||
| 132 | public function addPrediction(PlaceAutocompletePrediction $prediction) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param PlaceAutocompletePrediction $prediction |
||
| 141 | */ |
||
| 142 | public function removePrediction(PlaceAutocompletePrediction $prediction) |
||
| 147 | } |
||
| 148 |
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.