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 |
||
14 | final class UpdateAddressBookAddressHandler |
||
15 | { |
||
16 | /** |
||
17 | * @var RepositoryInterface |
||
18 | */ |
||
19 | private $addressRepository; |
||
20 | |||
21 | /** |
||
22 | * @var FactoryInterface |
||
23 | */ |
||
24 | private $addressFactory; |
||
25 | |||
26 | /** |
||
27 | * @var RepositoryInterface |
||
28 | */ |
||
29 | private $countryRepository; |
||
30 | |||
31 | /** |
||
32 | * @var RepositoryInterface |
||
33 | */ |
||
34 | private $provinceRepository; |
||
35 | |||
36 | /** |
||
37 | * @var RepositoryInterface |
||
38 | */ |
||
39 | private $shopUserRepository; |
||
40 | |||
41 | /** |
||
42 | * @param RepositoryInterface $addressRepository |
||
43 | * @param RepositoryInterface $countryRepository |
||
44 | * @param RepositoryInterface $provinceRepository |
||
45 | * @param RepositoryInterface $shopUserRepository |
||
46 | * @param FactoryInterface $addressFactory |
||
47 | */ |
||
48 | View Code Duplication | public function __construct( |
|
61 | |||
62 | public function handle(UpdateAddress $command): void |
||
92 | |||
93 | private function assertCountryExists(string $countryCode): void |
||
97 | |||
98 | private function assertProvinceExists($province): void |
||
102 | |||
103 | private function assertCurrentUserIsOwner(AddressInterface $address, ShopUserInterface $user) |
||
108 | |||
109 | private function assertAddressExists($address) |
||
113 | } |
||
114 |
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.