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 |
||
36 | class InstallationManager extends \Composer\Installer\InstallationManager |
||
37 | { |
||
38 | /** |
||
39 | * The package information. |
||
40 | * |
||
41 | * @var JsonArray |
||
42 | */ |
||
43 | private $packageInformation; |
||
44 | |||
45 | /** |
||
46 | * The dumper to use. |
||
47 | * |
||
48 | * @var ArrayDumper |
||
49 | */ |
||
50 | private $dumper; |
||
51 | |||
52 | /** |
||
53 | * The pool in use. |
||
54 | * |
||
55 | * @var Pool |
||
56 | */ |
||
57 | private $pool; |
||
58 | |||
59 | /** |
||
60 | * Create a new instance. |
||
61 | * |
||
62 | * @param JsonArray $packageInformation The log where package manipulations shall get logged to. |
||
63 | */ |
||
64 | public function __construct(JsonArray $packageInformation) |
||
69 | |||
70 | /** |
||
71 | * Set the pool. |
||
72 | * |
||
73 | * @param Pool $pool The new value. |
||
74 | * |
||
75 | * @return InstallationManager |
||
76 | */ |
||
77 | public function setPool($pool) |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | View Code Duplication | public function install(RepositoryInterface $repo, InstallOperation $operation) |
|
100 | |||
101 | /** |
||
102 | * {@inheritDoc} |
||
103 | */ |
||
104 | View Code Duplication | public function update(RepositoryInterface $repo, UpdateOperation $operation) |
|
117 | |||
118 | /** |
||
119 | * {@inheritDoc} |
||
120 | */ |
||
121 | public function uninstall(RepositoryInterface $repo, UninstallOperation $operation) |
||
132 | |||
133 | /** |
||
134 | * Convert reason to text. |
||
135 | * |
||
136 | * @param OperationInterface $operation The operation to obtain the reason from. |
||
137 | * |
||
138 | * @return string|null |
||
139 | */ |
||
140 | private function getReason(OperationInterface $operation) |
||
159 | } |
||
160 |
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.