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 |
||
| 28 | class SummaryPage extends SymfonyPage implements SummaryPageInterface |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var TableAccessorInterface |
||
| 32 | */ |
||
| 33 | private $tableAccessor; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Session $session |
||
| 37 | * @param array $parameters |
||
| 38 | * @param RouterInterface $router |
||
| 39 | * @param TableAccessorInterface $tableAccessor |
||
| 40 | */ |
||
| 41 | public function __construct( |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | public function getRouteName() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | View Code Duplication | public function hasItemWithProductAndQuantity($productName, $quantity) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritdoc} |
||
| 79 | */ |
||
| 80 | public function hasShippingAddress(AddressInterface $address) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | public function hasBillingAddress(AddressInterface $address) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | View Code Duplication | public function hasShippingMethod(ShippingMethodInterface $shippingMethod) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function hasPaymentMethod(PaymentMethodInterface $paymentMethod) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | public function hasProductDiscountedUnitPriceBy(ProductInterface $product, $amount) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function hasOrderTotal($total) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function addNotes($notes) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | public function hasPromotionTotal($promotionTotal) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | public function hasPromotion($promotionName) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | public function hasTaxTotal($taxTotal) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | public function hasShippingTotal($price) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritdoc} |
||
| 194 | */ |
||
| 195 | protected function getDefinedElements() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param ProductInterface $product |
||
| 215 | * |
||
| 216 | * @return NodeElement |
||
| 217 | */ |
||
| 218 | private function getProductRowElement(ProductInterface $product) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $displayedAddress |
||
| 225 | * @param AddressInterface $address |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | private function isAddressValid($displayedAddress, AddressInterface $address) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $address |
||
| 246 | * @param string $addressPart |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | private function hasAddressPart($address, $addressPart, $optional = false) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $countryCode |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | private function getCountryName($countryCode) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $price |
||
| 271 | * |
||
| 272 | * @return int |
||
| 273 | */ |
||
| 274 | private function getPriceFromString($price) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $total |
||
| 281 | * |
||
| 282 | * @return int |
||
| 283 | */ |
||
| 284 | private function getTotalFromString($total) |
||
| 290 | } |
||
| 291 |
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.