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 |
||
21 | class Drupal8 extends AbstractCore { |
||
22 | |||
23 | /** |
||
24 | * Tracks original configuration values. |
||
25 | * |
||
26 | * This is necessary since configurations modified here are actually saved so |
||
27 | * that they persist values across bootstraps. |
||
28 | * |
||
29 | * @var array |
||
30 | * An array of data, keyed by configuration name. |
||
31 | */ |
||
32 | protected $originalConfiguration = []; |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public function bootstrap() { |
||
38 | // Validate, and prepare environment for Drupal bootstrap. |
||
39 | if (!defined('DRUPAL_ROOT')) { |
||
40 | define('DRUPAL_ROOT', $this->drupalRoot); |
||
41 | } |
||
42 | |||
43 | // Bootstrap Drupal. |
||
44 | chdir(DRUPAL_ROOT); |
||
45 | $autoloader = require DRUPAL_ROOT . '/autoload.php'; |
||
46 | require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc'; |
||
47 | $this->validateDrupalSite(); |
||
48 | |||
49 | $request = Request::createFromGlobals(); |
||
50 | $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod'); |
||
51 | $kernel->boot(); |
||
52 | $kernel->prepareLegacyRequest($request); |
||
53 | |||
54 | // Initialise an anonymous session. required for the bootstrap. |
||
55 | \Drupal::service('session_manager')->start(); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function clearCache() { |
||
62 | // Need to change into the Drupal root directory or the registry explodes. |
||
63 | drupal_flush_all_caches(); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function nodeCreate($node) { |
||
70 | // Throw an exception if the node type is missing or does not exist. |
||
71 | if (!isset($node->type) || !$node->type) { |
||
72 | throw new \Exception("Cannot create content because it is missing the required property 'type'."); |
||
73 | } |
||
74 | $bundles = \Drupal::entityManager()->getBundleInfo('node'); |
||
75 | if (!in_array($node->type, array_keys($bundles))) { |
||
76 | throw new \Exception("Cannot create content because provided content type '$node->type' does not exist."); |
||
77 | } |
||
78 | // If 'author' is set, remap it to 'uid'. |
||
79 | if (isset($node->author)) { |
||
80 | $user = user_load_by_name($node->author); |
||
81 | if ($user) { |
||
82 | $node->uid = $user->id(); |
||
83 | } |
||
84 | } |
||
85 | $this->expandEntityFields('node', $node); |
||
86 | $entity = Node::create((array) $node); |
||
87 | $entity->save(); |
||
88 | |||
89 | $node->nid = $entity->id(); |
||
90 | |||
91 | return $node; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | public function nodeDelete($node) { |
||
98 | $node = $node instanceof NodeInterface ? $node : Node::load($node->nid); |
||
|
|||
99 | if ($node instanceof NodeInterface) { |
||
100 | $node->delete(); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function runCron() { |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function userCreate(\stdClass $user) { |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function roleCreate(array $permissions) { |
||
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | public function roleDelete($role_name) { |
||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | public function processBatch() { |
||
183 | $this->validateDrupalSite(); |
||
184 | $batch =& batch_get(); |
||
185 | $batch['progressive'] = FALSE; |
||
186 | batch_process(); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Retrieve all permissions. |
||
191 | * |
||
192 | * @return array |
||
193 | * Array of all defined permissions. |
||
194 | */ |
||
195 | protected function getAllPermissions() { |
||
204 | |||
205 | /** |
||
206 | * Convert any permission labels to machine name. |
||
207 | * |
||
208 | * @param array &$permissions |
||
209 | * Array of permission names. |
||
210 | */ |
||
211 | protected function convertPermissions(array &$permissions) { |
||
212 | $all_permissions = $this->getAllPermissions(); |
||
213 | |||
214 | foreach ($all_permissions as $name => $definition) { |
||
215 | $key = array_search($definition['title'], $permissions); |
||
216 | if (FALSE !== $key) { |
||
217 | $permissions[$key] = $name; |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Check to make sure that the array of permissions are valid. |
||
224 | * |
||
225 | * @param array $permissions |
||
226 | * Permissions to check. |
||
227 | */ |
||
228 | protected function checkPermissions(array &$permissions) { |
||
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | public function userDelete(\stdClass $user) { |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function userAddRole(\stdClass $user, $role_name) { |
||
249 | // Allow both machine and human role names. |
||
250 | $roles = user_role_names(); |
||
264 | |||
265 | /** |
||
266 | * {@inheritdoc} |
||
267 | */ |
||
268 | public function validateDrupalSite() { |
||
319 | |||
320 | /** |
||
321 | * {@inheritdoc} |
||
322 | */ |
||
323 | public function termCreate(\stdClass $term) { |
||
341 | |||
342 | /** |
||
343 | * {@inheritdoc} |
||
344 | */ |
||
345 | public function termDelete(\stdClass $term) { |
||
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | */ |
||
355 | public function getModuleList() { |
||
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | public function getExtensionPathList() { |
||
372 | |||
373 | /** |
||
374 | * Expands specified base fields on the entity object. |
||
375 | * |
||
376 | * @param string $entity_type |
||
377 | * The entity type for which to return the field types. |
||
378 | * @param \stdClass $entity |
||
379 | * Entity object. |
||
380 | * @param array $base_fields |
||
381 | * Base fields to be expanded in addition to user defined fields. |
||
382 | */ |
||
383 | public function expandEntityBaseFields($entity_type, \stdClass $entity, array $base_fields) { |
||
386 | |||
387 | /** |
||
388 | * {@inheritdoc} |
||
389 | */ |
||
390 | public function getEntityFieldTypes($entity_type, array $base_fields = array()) { |
||
401 | |||
402 | /** |
||
403 | * {@inheritdoc} |
||
404 | */ |
||
405 | public function isField($entity_type, $field_name) { |
||
409 | |||
410 | /** |
||
411 | * {@inheritdoc} |
||
412 | */ |
||
413 | public function isBaseField($entity_type, $field_name) { |
||
417 | |||
418 | /** |
||
419 | * {@inheritdoc} |
||
420 | */ |
||
421 | public function languageCreate(\stdClass $language) { |
||
436 | |||
437 | /** |
||
438 | * {@inheritdoc} |
||
439 | */ |
||
440 | public function languageDelete(\stdClass $language) { |
||
444 | |||
445 | /** |
||
446 | * {@inheritdoc} |
||
447 | */ |
||
448 | public function clearStaticCaches() { |
||
452 | |||
453 | /** |
||
454 | * {@inheritdoc} |
||
455 | */ |
||
456 | public function configGet($name, $key = '') { |
||
459 | |||
460 | /** |
||
461 | * {@inheritdoc} |
||
462 | */ |
||
463 | public function configSet($name, $key, $value) { |
||
468 | |||
469 | /** |
||
470 | * {@inheritdoc} |
||
471 | */ |
||
472 | public function entityCreate($entity_type, $entity) { |
||
498 | |||
499 | /** |
||
500 | * {@inheritdoc} |
||
501 | */ |
||
502 | public function entityDelete($entity_type, $entity) { |
||
508 | |||
509 | /** |
||
510 | * {@inheritdoc} |
||
511 | */ |
||
512 | public function startCollectingMail() { |
||
525 | |||
526 | /** |
||
527 | * {@inheritdoc} |
||
528 | */ |
||
529 | public function stopCollectingMail() { |
||
535 | |||
536 | /** |
||
537 | * {@inheritdoc} |
||
538 | */ |
||
539 | public function getMail() { |
||
548 | |||
549 | /** |
||
550 | * {@inheritdoc} |
||
551 | */ |
||
552 | public function clearMail() { |
||
555 | |||
556 | /** |
||
557 | * {@inheritdoc} |
||
558 | */ |
||
559 | public function sendMail($body = '', $subject = '', $to = '', $langcode = '') { |
||
567 | |||
568 | /** |
||
569 | * If the Mail System module is enabled, collect that mail too. |
||
570 | * |
||
571 | * @see MailsystemManager::getPluginInstance() |
||
572 | */ |
||
573 | protected function startCollectingMailSystemMail() { |
||
586 | |||
587 | /** |
||
588 | * Find and replace all the mail system sender plugins with the test plugin. |
||
589 | * |
||
590 | * This method calls itself recursively. |
||
591 | */ |
||
592 | protected function findMailSystemSenders(array $data) { |
||
605 | |||
606 | /** |
||
607 | * If the Mail System module is enabled, stop collecting those mails. |
||
608 | */ |
||
609 | protected function stopCollectingMailSystemMail() { |
||
614 | |||
615 | } |
||
616 |
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.