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 |
||
30 | View Code Duplication | class SignUpUserHandler |
|
|
|||
31 | { |
||
32 | /** |
||
33 | * The user password encoder. |
||
34 | * |
||
35 | * @var UserPasswordEncoder |
||
36 | */ |
||
37 | private $encoder; |
||
38 | |||
39 | /** |
||
40 | * The user sign up factory. |
||
41 | * |
||
42 | * @var UserFactorySignUp |
||
43 | */ |
||
44 | private $factory; |
||
45 | |||
46 | /** |
||
47 | * The user repository. |
||
48 | * |
||
49 | * @var UserRepository |
||
50 | */ |
||
51 | private $repository; |
||
52 | |||
53 | /** |
||
54 | * Constructor. |
||
55 | * |
||
56 | * @param UserRepository $aRepository The user repository |
||
57 | * @param UserPasswordEncoder $anEncoder The password encoder |
||
58 | * @param UserFactorySignUp $aFactory The user sign up factory |
||
59 | */ |
||
60 | public function __construct( |
||
69 | |||
70 | /** |
||
71 | * Handles the given command. |
||
72 | * |
||
73 | * @param SignUpUserCommand $aCommand The command |
||
74 | * |
||
75 | * @throws UserAlreadyExistException when the user id is already exists |
||
76 | */ |
||
77 | public function __invoke(SignUpUserCommand $aCommand) |
||
102 | } |
||
103 |
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.