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 |
||
25 | class Drupal8 extends AbstractCore { |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function bootstrap() { |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function clearCache() { |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public function nodeCreate($node) { |
||
63 | // Default status to 1 if not set. |
||
64 | if (!isset($node->status)) { |
||
65 | $node->status = 1; |
||
66 | } |
||
67 | // If 'author' is set, remap it to 'uid'. |
||
68 | if (isset($node->author)) { |
||
69 | $user = user_load_by_name($node->author); |
||
70 | if ($user) { |
||
71 | $node->uid = $user->id(); |
||
72 | } |
||
73 | } |
||
74 | $this->expandEntityFields('node', $node); |
||
75 | $entity = entity_create('node', (array) $node); |
||
76 | $entity->save(); |
||
77 | |||
78 | $node->nid = $entity->id(); |
||
79 | |||
80 | return $node; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function nodeDelete($node) { |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function runCron() { |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function userCreate(\stdClass $user) { |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function roleCreate(array $permissions) { |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function roleDelete($role_name) { |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function processBatch() { |
||
177 | |||
178 | /** |
||
179 | * Retrieve all permissions. |
||
180 | * |
||
181 | * @return array |
||
182 | * Array of all defined permissions. |
||
183 | */ |
||
184 | protected function getAllPermissions() { |
||
193 | |||
194 | /** |
||
195 | * Convert any permission labels to machine name. |
||
196 | * |
||
197 | * @param array &$permissions |
||
198 | * Array of permission names. |
||
199 | */ |
||
200 | protected function convertPermissions(array &$permissions) { |
||
210 | |||
211 | /** |
||
212 | * Check to make sure that the array of permissions are valid. |
||
213 | * |
||
214 | * @param array $permissions |
||
215 | * Permissions to check. |
||
216 | */ |
||
217 | protected function checkPermissions(array &$permissions) { |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function userDelete(\stdClass $user) { |
||
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | public function userAddRole(\stdClass $user, $role_name) { |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | public function validateDrupalSite() { |
||
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | public function termCreate(\stdClass $term) { |
||
317 | |||
318 | /** |
||
319 | * {@inheritdoc} |
||
320 | */ |
||
321 | public function termDelete(\stdClass $term) { |
||
327 | |||
328 | /** |
||
329 | * {@inheritdoc} |
||
330 | */ |
||
331 | public function getModuleList() { |
||
334 | |||
335 | /** |
||
336 | * {@inheritdoc} |
||
337 | */ |
||
338 | public function installModules(array $modules, $install_dependencies = TRUE) { |
||
346 | |||
347 | /** |
||
348 | * {@inheritdoc} |
||
349 | */ |
||
350 | public function uninstallModules(array $modules) { |
||
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | public function getExtensionPathList() { |
||
372 | |||
373 | /** |
||
374 | * {@inheritdoc} |
||
375 | */ |
||
376 | public function getEntityFieldTypes($entity_type) { |
||
386 | |||
387 | /** |
||
388 | * {@inheritdoc} |
||
389 | */ |
||
390 | public function isField($entity_type, $field_name) { |
||
394 | |||
395 | /** |
||
396 | * {@inheritdoc} |
||
397 | */ |
||
398 | public function languageCreate(\stdClass $language) { |
||
413 | |||
414 | /** |
||
415 | * {@inheritdoc} |
||
416 | */ |
||
417 | public function languageDelete(\stdClass $language) { |
||
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | public function clearStaticCaches() { |
||
429 | |||
430 | } |
||
431 |
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.