Conditions | 15 |
Paths | 13 |
Total Lines | 151 |
Lines | 100 |
Ratio | 66.23 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
264 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { |
||
265 | parent::buildOptionsForm($form, $form_state); |
||
266 | |||
267 | switch ($form_state->get('section')) { |
||
268 | case 'graphql_query_name': |
||
269 | $form['#title'] .= $this->t('Query name'); |
||
270 | $form['graphql_query_name'] = [ |
||
271 | '#type' => 'textfield', |
||
272 | '#description' => $this->t('This will be the graphQL query name.'), |
||
273 | '#default_value' => $this->getGraphQLQueryName(), |
||
274 | ]; |
||
275 | break; |
||
276 | |||
277 | case 'entity_type': |
||
278 | $entity_info = \Drupal::entityManager()->getDefinitions(); |
||
279 | $entity_names = [NULL => $this->t('None')]; |
||
280 | foreach ($entity_info as $type => $info) { |
||
281 | // is this a content/front-facing entity? |
||
282 | if ($info instanceof \Drupal\Core\Entity\ContentEntityType) { |
||
|
|||
283 | $entity_names[$type] = $info->get('label'); |
||
284 | } |
||
285 | } |
||
286 | |||
287 | $form['#title'] .= $this->t('Entity type'); |
||
288 | $form['entity_type'] = [ |
||
289 | '#type' => 'radios', |
||
290 | '#required' => FALSE, |
||
291 | '#title' => $this->t('Attach this display to the following entity type'), |
||
292 | '#options' => $entity_names, |
||
293 | '#default_value' => $this->getOption('entity_type'), |
||
294 | ]; |
||
295 | break; |
||
296 | |||
297 | case 'bundles': |
||
298 | $options = []; |
||
299 | $entity_type = $this->getOption('entity_type'); |
||
300 | foreach (\Drupal::entityManager()->getBundleInfo($entity_type) as $bundle => $info) { |
||
301 | $options[$bundle] = $info['label']; |
||
302 | } |
||
303 | $form['#title'] .= $this->t('Bundles'); |
||
304 | $form['bundles'] = [ |
||
305 | '#type' => 'checkboxes', |
||
306 | '#title' => $this->t('Attach this display to the following bundles. If no bundles are selected, the display will be attached to all.'), |
||
307 | '#options' => $options, |
||
308 | '#default_value' => $this->getOption('bundles'), |
||
309 | ]; |
||
310 | break; |
||
311 | |||
312 | View Code Duplication | case 'arguments': |
|
313 | $form['#title'] .= $this->t('Arguments'); |
||
314 | $default = $this->getOption('argument_mode'); |
||
315 | $options = [ |
||
316 | 'None' => $this->t("No special handling"), |
||
317 | 'token' => $this->t("Use tokens from the entity the view is attached to"), |
||
318 | ]; |
||
319 | |||
320 | $form['argument_mode'] = [ |
||
321 | '#type' => 'radios', |
||
322 | '#title' => $this->t("How should this display populate the view's arguments?"), |
||
323 | '#options' => $options, |
||
324 | '#default_value' => $default, |
||
325 | ]; |
||
326 | |||
327 | $form['token'] = [ |
||
328 | '#type' => 'fieldset', |
||
329 | '#title' => $this->t('Token replacement'), |
||
330 | '#collapsible' => TRUE, |
||
331 | '#states' => [ |
||
332 | 'visible' => [ |
||
333 | ':input[name=argument_mode]' => ['value' => 'token'], |
||
334 | ], |
||
335 | ], |
||
336 | ]; |
||
337 | |||
338 | $form['token']['default_argument'] = [ |
||
339 | '#title' => $this->t('Arguments'), |
||
340 | '#type' => 'textfield', |
||
341 | '#default_value' => $this->getOption('default_argument'), |
||
342 | '#description' => $this->t('You may use token replacement to provide arguments based on the current entity. Separate arguments with "/".'), |
||
343 | ]; |
||
344 | |||
345 | // Add a token browser. |
||
346 | if (\Drupal::service('module_handler')->moduleExists('token') && $entity_type = $this->getOption('entity_type')) { |
||
347 | $token_types = [$entity_type => $entity_type]; |
||
348 | $token_mapper = \Drupal::service('token.entity_mapper'); |
||
349 | if (!empty($token_types)) { |
||
350 | $token_types = array_map(function ($type) use ($token_mapper) { |
||
351 | return $token_mapper->getTokenTypeForEntityType($type); |
||
352 | }, (array) $token_types); |
||
353 | } |
||
354 | $form['token']['browser'] = [ |
||
355 | '#theme' => 'token_tree_link', |
||
356 | '#token_types' => $token_types, |
||
357 | '#global_types' => TRUE, |
||
358 | '#show_nested' => FALSE, |
||
359 | ]; |
||
360 | } |
||
361 | break; |
||
362 | |||
363 | View Code Duplication | case 'limit': |
|
364 | $form['#title'] .= $this->t('Limit'); |
||
365 | $default = $this->getOption('limit_mode'); |
||
366 | $options = [ |
||
367 | 'None' => $this->t("No special handling"), |
||
368 | 'token' => $this->t("Use tokens from the entity the view is attached to"), |
||
369 | ]; |
||
370 | |||
371 | $form['limit_mode'] = [ |
||
372 | '#type' => 'radios', |
||
373 | '#title' => $this->t("How should this display populate the view's result limit?"), |
||
374 | '#options' => $options, |
||
375 | '#default_value' => $default, |
||
376 | ]; |
||
377 | |||
378 | $form['token'] = [ |
||
379 | '#type' => 'fieldset', |
||
380 | '#title' => $this->t('Token replacement'), |
||
381 | '#collapsible' => TRUE, |
||
382 | '#states' => [ |
||
383 | 'visible' => [ |
||
384 | ':input[name=limit_mode]' => ['value' => 'token'], |
||
385 | ], |
||
386 | ], |
||
387 | ]; |
||
388 | |||
389 | $form['token']['default_limit'] = [ |
||
390 | '#title' => $this->t('Limit'), |
||
391 | '#type' => 'textfield', |
||
392 | '#default_value' => $this->getOption('default_limit'), |
||
393 | '#description' => $this->t('You may use token replacement to provide the limit based on the current entity.'), |
||
394 | ]; |
||
395 | |||
396 | // Add a token browser. |
||
397 | if (\Drupal::service('module_handler')->moduleExists('token') && $entity_type = $this->getOption('entity_type')) { |
||
398 | $token_types = [$entity_type => $entity_type]; |
||
399 | $token_mapper = \Drupal::service('token.entity_mapper'); |
||
400 | if (!empty($token_types)) { |
||
401 | $token_types = array_map(function ($type) use ($token_mapper) { |
||
402 | return $token_mapper->getTokenTypeForEntityType($type); |
||
403 | }, (array) $token_types); |
||
404 | } |
||
405 | $form['token']['browser'] = [ |
||
406 | '#theme' => 'token_tree_link', |
||
407 | '#token_types' => $token_types, |
||
408 | '#global_types' => TRUE, |
||
409 | '#show_nested' => FALSE, |
||
410 | ]; |
||
411 | } |
||
412 | break; |
||
413 | } |
||
414 | } |
||
415 | |||
492 |
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.