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 |
||
25 | View Code Duplication | class UserOfIdHandler |
|
1 ignored issue
–
show
|
|||
26 | { |
||
27 | /** |
||
28 | * The user data transformer. |
||
29 | * |
||
30 | * @var UserDataTransformer |
||
31 | */ |
||
32 | private $dataTransformer; |
||
33 | |||
34 | /** |
||
35 | * The user repository. |
||
36 | * |
||
37 | * @var UserRepository |
||
38 | */ |
||
39 | private $repository; |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * @param UserRepository $aRepository The user repository |
||
45 | * @param UserDataTransformer $aDataTransformer The user data transformer |
||
46 | */ |
||
47 | public function __construct(UserRepository $aRepository, UserDataTransformer $aDataTransformer) |
||
52 | |||
53 | /** |
||
54 | * Handles the given query. |
||
55 | * |
||
56 | * @param UserOfIdQuery $aQuery The query |
||
57 | * |
||
58 | * @throws UserDoesNotExistException when the user does not exist |
||
59 | * |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function __invoke(UserOfIdQuery $aQuery) |
||
73 | } |
||
74 |
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.