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 Drupal8 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 Drupal8, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Drupal8 extends AbstractCore { |
||
20 | |||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | public function bootstrap() { |
||
25 | // Validate, and prepare environment for Drupal bootstrap. |
||
26 | if (!defined('DRUPAL_ROOT')) { |
||
27 | define('DRUPAL_ROOT', $this->drupalRoot); |
||
28 | } |
||
29 | |||
30 | // Bootstrap Drupal. |
||
31 | chdir(DRUPAL_ROOT); |
||
32 | $autoloader = require DRUPAL_ROOT . '/autoload.php'; |
||
33 | require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc'; |
||
34 | $this->validateDrupalSite(); |
||
35 | |||
36 | $request = Request::createFromGlobals(); |
||
37 | $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod'); |
||
38 | $kernel->boot(); |
||
39 | $kernel->prepareLegacyRequest($request); |
||
40 | |||
41 | // Initialise an anonymous session. required for the bootstrap. |
||
42 | \Drupal::service('session_manager')->start(); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | public function clearCache() { |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function nodeCreate($node) { |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function nodeDelete($node) { |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function runCron() { |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function userCreate(\stdClass $user) { |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function roleCreate(array $permissions) { |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function roleDelete($role_name) { |
||
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | public function processBatch() { |
||
175 | |||
176 | /** |
||
177 | * Retrieve all permissions. |
||
178 | * |
||
179 | * @return array |
||
180 | * Array of all defined permissions. |
||
181 | */ |
||
182 | protected function getAllPermissions() { |
||
191 | |||
192 | /** |
||
193 | * Convert any permission labels to machine name. |
||
194 | * |
||
195 | * @param array &$permissions |
||
196 | * Array of permission names. |
||
197 | */ |
||
198 | protected function convertPermissions(array &$permissions) { |
||
208 | |||
209 | /** |
||
210 | * Check to make sure that the array of permissions are valid. |
||
211 | * |
||
212 | * @param array $permissions |
||
213 | * Permissions to check. |
||
214 | */ |
||
215 | protected function checkPermissions(array &$permissions) { |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | public function userDelete(\stdClass $user) { |
||
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | */ |
||
235 | public function userAddRole(\stdClass $user, $role_name) { |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function validateDrupalSite() { |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function termCreate(\stdClass $term) { |
||
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | public function termDelete(\stdClass $term) { |
||
338 | |||
339 | /** |
||
340 | * {@inheritdoc} |
||
341 | */ |
||
342 | public function getModuleList() { |
||
345 | |||
346 | /** |
||
347 | * {@inheritdoc} |
||
348 | */ |
||
349 | public function getExtensionPathList() { |
||
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | public function getEntityFieldTypes($entity_type) { |
||
373 | |||
374 | /** |
||
375 | * {@inheritdoc} |
||
376 | */ |
||
377 | public function isField($entity_type, $field_name) { |
||
381 | |||
382 | /** |
||
383 | * {@inheritdoc} |
||
384 | */ |
||
385 | public function languageCreate(\stdClass $language) { |
||
400 | |||
401 | /** |
||
402 | * {@inheritdoc} |
||
403 | */ |
||
404 | public function languageDelete(\stdClass $language) { |
||
408 | |||
409 | /** |
||
410 | * {@inheritdoc} |
||
411 | */ |
||
412 | public function clearStaticCaches() { |
||
416 | |||
417 | /** |
||
418 | * {@inheritdoc} |
||
419 | */ |
||
420 | public function configGet($name, $key = '') { |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | public function configSet($name, $key, $value) { |
||
432 | |||
433 | /** |
||
434 | * {@inheritdoc} |
||
435 | */ |
||
436 | public function entityCreate($entity_type, $entity) { |
||
462 | |||
463 | /** |
||
464 | * {@inheritdoc} |
||
465 | */ |
||
466 | public function entityDelete($entity_type, $entity) { |
||
472 | |||
473 | /** |
||
474 | * {@inheritdoc} |
||
475 | */ |
||
476 | public function startCollectingMail() { |
||
481 | |||
482 | /** |
||
483 | * {@inheritdoc} |
||
484 | */ |
||
485 | public function stopCollectingMail() { |
||
490 | |||
491 | /** |
||
492 | * {@inheritdoc} |
||
493 | */ |
||
494 | public function getMail() { |
||
503 | |||
504 | /** |
||
505 | * {@inheritdoc} |
||
506 | */ |
||
507 | public function clearMail() { |
||
510 | |||
511 | /** |
||
512 | * {@inheritdoc} |
||
513 | */ |
||
514 | public function sendMail($body = '', $subject = '', $to = '', $langcode = '') { |
||
522 | |||
523 | } |
||
524 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.