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 |
||
| 17 | class Car |
||
| 18 | { |
||
| 19 | /** @var string */ |
||
| 20 | private $id; |
||
| 21 | |||
| 22 | /** @var string */ |
||
| 23 | private $responsiblePerson; |
||
| 24 | |||
| 25 | /** @var string */ |
||
| 26 | private $model; |
||
| 27 | |||
| 28 | /** @var string */ |
||
| 29 | private $brand; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | private $registrationNumber; |
||
| 33 | |||
| 34 | /** @var \DateTime */ |
||
| 35 | private $productionDate; |
||
| 36 | |||
| 37 | /** @var \DateTime */ |
||
| 38 | private $warrantyPeriod; |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | private $fuelType; |
||
|
|
|||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | private $engineSize; |
||
| 45 | |||
| 46 | /** @var string */ |
||
| 47 | private $gearBox; |
||
| 48 | |||
| 49 | /** @var string */ |
||
| 50 | private $city; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | private $department; |
||
| 54 | |||
| 55 | /** @var Insurance[] */ |
||
| 56 | private $insurances = []; |
||
| 57 | |||
| 58 | /** @var CarDocument[] */ |
||
| 59 | private $carDocuments = []; |
||
| 60 | |||
| 61 | /** @var VehicleInspection[] */ |
||
| 62 | private $vehicleInspection = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Car constructor. |
||
| 66 | * |
||
| 67 | * @param $id |
||
| 68 | * @param $model |
||
| 69 | * @param $brand |
||
| 70 | * @param $registrationNumber |
||
| 71 | * @param \DateTime $productionDate |
||
| 72 | * @param \DateTime $warrantyPeriod |
||
| 73 | * @param $city |
||
| 74 | * @param $department |
||
| 75 | */ |
||
| 76 | View Code Duplication | private function __construct( |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param $id |
||
| 100 | * @param $model |
||
| 101 | * @param $brand |
||
| 102 | * @param $registrationNumber |
||
| 103 | * @param \DateTime $productionDate |
||
| 104 | * @param \DateTime $warrantyPeriod |
||
| 105 | * @param $city |
||
| 106 | * @param $department |
||
| 107 | * |
||
| 108 | * @return \Madkom\RegistryApplication\Domain\CarManagement\Car |
||
| 109 | */ |
||
| 110 | public static function createCustom( |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param string $responsiblePerson |
||
| 136 | */ |
||
| 137 | public function changeResponsiblePersonTo($responsiblePerson) |
||
| 141 | |||
| 142 | public function addVehicleInspection(VehicleInspection $newVehicleInspection) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param \Madkom\RegistryApplication\Domain\CarManagement\VehicleInspection\VehicleInspection $vehicleInspection |
||
| 161 | * |
||
| 162 | * @throws \Madkom\RegistryApplication\Domain\CarManagement\CarExceptions\RemovingNonexistentElementException |
||
| 163 | */ |
||
| 164 | public function removeVehicleInspection(VehicleInspection $vehicleInspection) |
||
| 172 | |||
| 173 | public function getVehicleInspection() |
||
| 177 | |||
| 178 | public function addInsurance(Insurance $newInsurance) |
||
| 188 | |||
| 189 | public function removeInsurance($selectedInsuranceId) |
||
| 201 | |||
| 202 | public function addInsuranceDocument($insuranceId, InsuranceDocument $insuranceDocument) |
||
| 214 | |||
| 215 | public function getInsuranceDocuments($insuranceId) |
||
| 225 | |||
| 226 | public function addCarDocument(CarDocument $carDocument) |
||
| 230 | |||
| 231 | public function removeCarDocument($carDocumentId) |
||
| 243 | |||
| 244 | public function getCarDocument() |
||
| 248 | |||
| 249 | public function removeInsuranceDocument($insuranceId, $documentId) |
||
| 261 | |||
| 262 | public function getInsurances() |
||
| 266 | |||
| 267 | public function changeCity($city) |
||
| 271 | |||
| 272 | public function changeDepartment($department) |
||
| 276 | |||
| 277 | public function getId() |
||
| 281 | } |
||
| 282 |
This check marks private properties in classes that are never used. Those properties can be removed.