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 |
||
18 | class Drupal8 extends AbstractCore { |
||
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | public function bootstrap() { |
||
24 | // Validate, and prepare environment for Drupal bootstrap. |
||
25 | if (!defined('DRUPAL_ROOT')) { |
||
26 | define('DRUPAL_ROOT', $this->drupalRoot); |
||
27 | } |
||
28 | |||
29 | // Bootstrap Drupal. |
||
30 | chdir(DRUPAL_ROOT); |
||
31 | $autoloader = require DRUPAL_ROOT . '/autoload.php'; |
||
32 | require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc'; |
||
33 | $this->validateDrupalSite(); |
||
34 | |||
35 | $request = Request::createFromGlobals(); |
||
36 | $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod'); |
||
37 | $kernel->boot(); |
||
38 | $kernel->prepareLegacyRequest($request); |
||
39 | |||
40 | // Initialise an anonymous session. required for the bootstrap. |
||
41 | \Drupal::service('session_manager')->start(); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public function clearCache() { |
||
48 | // Need to change into the Drupal root directory or the registry explodes. |
||
49 | drupal_flush_all_caches(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function nodeCreate($node) { |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function nodeDelete($node) { |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | public function nodeDeleteMultiple(array $nids) { |
||
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() { |
||
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) { |
||
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 userDeleteMultiple(array $uids) { |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function userAddRole(\stdClass $user, $role_name) { |
||
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | public function validateDrupalSite() { |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function termCreate(\stdClass $term) { |
||
339 | |||
340 | /** |
||
341 | * {@inheritdoc} |
||
342 | */ |
||
343 | public function termDelete(\stdClass $term) { |
||
349 | |||
350 | /** |
||
351 | * {@inheritdoc} |
||
352 | */ |
||
353 | public function getModuleList() { |
||
356 | |||
357 | /** |
||
358 | * {@inheritdoc} |
||
359 | */ |
||
360 | public function getExtensionPathList() { |
||
370 | |||
371 | /** |
||
372 | * {@inheritdoc} |
||
373 | */ |
||
374 | public function getEntityFieldTypes($entity_type) { |
||
384 | |||
385 | /** |
||
386 | * {@inheritdoc} |
||
387 | */ |
||
388 | public function isField($entity_type, $field_name) { |
||
392 | |||
393 | /** |
||
394 | * {@inheritdoc} |
||
395 | */ |
||
396 | public function languageCreate(\stdClass $language) { |
||
411 | |||
412 | /** |
||
413 | * {@inheritdoc} |
||
414 | */ |
||
415 | public function languageDelete(\stdClass $language) { |
||
419 | |||
420 | /** |
||
421 | * {@inheritdoc} |
||
422 | */ |
||
423 | public function clearStaticCaches() { |
||
427 | |||
428 | /** |
||
429 | * {@inheritdoc} |
||
430 | */ |
||
431 | public function configGet($name, $key = '') { |
||
434 | |||
435 | /** |
||
436 | * {@inheritdoc} |
||
437 | */ |
||
438 | public function configSet($name, $key, $value) { |
||
443 | |||
444 | } |
||
445 |
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.