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:
Complex classes like Plugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Plugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
30 | { |
||
31 | const PERMISSION_CODE = 0755; |
||
32 | const REPEAT = 5; |
||
33 | CONST DIRECTORIES = [ |
||
34 | 'application', |
||
35 | 'data', |
||
36 | 'public', |
||
37 | 'tests' |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * @var Installer |
||
42 | */ |
||
43 | protected $installer; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $environment; |
||
49 | |||
50 | /** |
||
51 | * @var Filesystem |
||
52 | */ |
||
53 | protected $filesystem; |
||
54 | |||
55 | /** |
||
56 | * Create instance, define constants |
||
57 | */ |
||
58 | public function __construct() |
||
63 | |||
64 | /** |
||
65 | * Called after the plugin is loaded |
||
66 | * |
||
67 | * It setup composer installer |
||
68 | * |
||
69 | * {@inheritDoc} |
||
70 | */ |
||
71 | public function activate(Composer $composer, IOInterface $io) |
||
76 | |||
77 | /** |
||
78 | * Registered events after the plugin is loaded |
||
79 | * |
||
80 | * {@inheritDoc} |
||
81 | */ |
||
82 | public static function getSubscribedEvents(): array |
||
95 | |||
96 | /** |
||
97 | * Hook which is called after install package |
||
98 | * |
||
99 | * It copies bluz module |
||
100 | * |
||
101 | * @throws \InvalidArgumentException |
||
102 | */ |
||
103 | View Code Duplication | public function copyFiles(PackageEvent $event) |
|
114 | |||
115 | /** |
||
116 | * Hook which is called before update package |
||
117 | * |
||
118 | * It checks bluz module |
||
119 | */ |
||
120 | View Code Duplication | public function removeFiles(PackageEvent $event) |
|
131 | |||
132 | /** |
||
133 | * Get Filesystem |
||
134 | * |
||
135 | * @return Filesystem |
||
136 | */ |
||
137 | protected function getFilesystem() |
||
144 | |||
145 | /** |
||
146 | * getExtra |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | protected function getExtraFiles() : array |
||
159 | |||
160 | /** |
||
161 | * Copy Module files |
||
162 | * |
||
163 | * @return void |
||
164 | * @throws \InvalidArgumentException |
||
165 | */ |
||
166 | protected function copyModule() |
||
182 | |||
183 | /** |
||
184 | * copyExtras |
||
185 | * |
||
186 | * @param array $files |
||
187 | * |
||
188 | * @return void |
||
189 | * @throws \InvalidArgumentException |
||
190 | */ |
||
191 | protected function copyExtras($files) |
||
200 | |||
201 | /** |
||
202 | * It recursively copies the files and directories |
||
203 | * |
||
204 | * @param $source |
||
205 | * @param $target |
||
206 | * |
||
207 | * @return void |
||
208 | * @throws \InvalidArgumentException |
||
209 | */ |
||
210 | protected function copy($source, $target) |
||
273 | |||
274 | /** |
||
275 | * It recursively removes the files and empty directories |
||
276 | * @return void |
||
277 | */ |
||
278 | protected function removeModule() |
||
291 | |||
292 | /** |
||
293 | * removeExtras |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | protected function removeExtras($files) |
||
303 | |||
304 | /** |
||
305 | * It recursively removes the files and directories |
||
306 | * @param $directory |
||
307 | * @return void |
||
308 | */ |
||
309 | protected function remove($directory) |
||
368 | } |
||
369 |
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.