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 |
||
33 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
34 | { |
||
35 | const PERMISSION_CODE = 0755; |
||
36 | const REPEAT = 5; |
||
37 | const DIRECTORIES = [ |
||
38 | 'application', |
||
39 | 'data', |
||
40 | 'public', |
||
41 | 'tests' |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * @var Installer |
||
46 | */ |
||
47 | protected $installer; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $vendorPath; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $packagePath; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $environment; |
||
63 | |||
64 | /** |
||
65 | * @var Filesystem |
||
66 | */ |
||
67 | protected $filesystem; |
||
68 | |||
69 | /** |
||
70 | * Create instance, define constants |
||
71 | */ |
||
72 | public function __construct() |
||
77 | |||
78 | /** |
||
79 | * Called after the plugin is loaded |
||
80 | * |
||
81 | * It setup composer installer |
||
82 | * |
||
83 | * {@inheritDoc} |
||
84 | */ |
||
85 | public function activate(Composer $composer, IOInterface $io) |
||
91 | |||
92 | /** |
||
93 | * Registered events after the plugin is loaded |
||
94 | * |
||
95 | * {@inheritDoc} |
||
96 | */ |
||
97 | public static function getSubscribedEvents(): array |
||
111 | |||
112 | /** |
||
113 | * extractPackage |
||
114 | * |
||
115 | * @param PackageEvent $event |
||
116 | * |
||
117 | * @return PackageInterface |
||
118 | */ |
||
119 | protected function extractPackage(PackageEvent $event) |
||
126 | |||
127 | /** |
||
128 | * Copy extra files from compose.json of project |
||
129 | * |
||
130 | * @param Event $event |
||
131 | * |
||
132 | * @return void |
||
133 | * @throws \InvalidArgumentException |
||
134 | */ |
||
135 | public function copyProjectExtraFiles(Event $event) |
||
146 | |||
147 | /** |
||
148 | * Hook which is called after install package |
||
149 | * It copies bluz module |
||
150 | * |
||
151 | * @param PackageEvent $event |
||
152 | * |
||
153 | * @throws \InvalidArgumentException |
||
154 | */ |
||
155 | View Code Duplication | public function copyModuleFiles(PackageEvent $event) |
|
166 | |||
167 | /** |
||
168 | * Hook which is called before update package |
||
169 | * It checks bluz module |
||
170 | * |
||
171 | * @param PackageEvent $event |
||
172 | */ |
||
173 | View Code Duplication | public function removeModuleFiles(PackageEvent $event) |
|
184 | |||
185 | /** |
||
186 | * Get Filesystem |
||
187 | * |
||
188 | * @return Filesystem |
||
189 | */ |
||
190 | protected function getFilesystem() |
||
197 | |||
198 | /** |
||
199 | * Copy Module files |
||
200 | * |
||
201 | * @return void |
||
202 | * @throws \InvalidArgumentException |
||
203 | */ |
||
204 | protected function copyModule() |
||
221 | |||
222 | /** |
||
223 | * copyExtras |
||
224 | * |
||
225 | * @param array $files |
||
226 | * |
||
227 | * @return void |
||
228 | * @throws \InvalidArgumentException |
||
229 | */ |
||
230 | protected function copyExtras($files) |
||
239 | |||
240 | /** |
||
241 | * It recursively copies the files and directories |
||
242 | * |
||
243 | * @param string $source |
||
244 | * @param string $target |
||
245 | * |
||
246 | * @return void |
||
247 | * @throws \InvalidArgumentException |
||
248 | */ |
||
249 | protected function copy($source, $target) |
||
321 | |||
322 | /** |
||
323 | * It recursively removes the files and empty directories |
||
324 | * @return void |
||
325 | */ |
||
326 | protected function removeModule() |
||
340 | |||
341 | /** |
||
342 | * removeExtras |
||
343 | * |
||
344 | * @param array $files |
||
345 | * |
||
346 | * @return void |
||
347 | */ |
||
348 | protected function removeExtras($files) |
||
357 | |||
358 | /** |
||
359 | * It recursively removes the files and directories |
||
360 | * @param $directory |
||
361 | * @return void |
||
362 | */ |
||
363 | protected function remove($directory) |
||
425 | } |
||
426 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: