GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UserDropdownBlock   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 7 1
A build() 0 28 1
1
<?php
2
3
namespace Drupal\df_tools_user\Plugin\Block;
4
5
use Drupal\user\Entity\User;
6
use Drupal\Core\Block\BlockBase;
7
use Drupal\Core\Cache\Cache;
8
use Drupal\Core\Form\FormBuilder;
9
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10
use Drupal\Core\Session\AccountProxyInterface;
11
use Drupal\Core\Url;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * Provides the "User Dropdown" block.
16
 *
17
 * @Block(
18
 *   id = "user_dropdown",
19
 *   admin_label = @Translation("User Dropdown"),
20
 *   category = @Translation("Menus")
21
 * )
22
 */
23
class UserDropdownBlock extends BlockBase implements ContainerFactoryPluginInterface {
24
25
  /**
26
   * The form builder.
27
   *
28
   * @var \Drupal\Core\Form\FormBuilder
29
   */
30
  protected $formBuilder;
31
32
  /**
33
   * The current user.
34
   *
35
   * @var \Drupal\Core\Session\AccountProxyInterface
36
   */
37
  protected $user;
38
39
  public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilder $form_builder, AccountProxyInterface $user) {
40
    parent::__construct($configuration, $plugin_id, $plugin_definition);
41
    $this->formBuilder = $form_builder;
42
    $this->user = $user;
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49
    return new static(
50
      $configuration,
51
      $plugin_id,
52
      $plugin_definition,
53
      $container->get('form_builder'),
0 ignored issues
show
Bug introduced by
It seems like $container->get('form_builder') can also be of type null; however, parameter $form_builder of Drupal\df_tools_user\Plu...ownBlock::__construct() does only seem to accept Drupal\Core\Form\FormBuilder, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
      /** @scrutinizer ignore-type */ $container->get('form_builder'),
Loading history...
54
      $container->get('current_user')
0 ignored issues
show
Bug introduced by
It seems like $container->get('current_user') can also be of type null; however, parameter $user of Drupal\df_tools_user\Plu...ownBlock::__construct() does only seem to accept Drupal\Core\Session\AccountProxyInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
      /** @scrutinizer ignore-type */ $container->get('current_user')
Loading history...
55
    );
56
  }
57
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public function build() {
62
    // Get the user login form.
63
    $form = $this->formBuilder->getForm('\Drupal\user\Form\UserLoginForm');
64
65
    // Remove default form descriptions.
66
    unset($form['name']['#description']);
67
    unset($form['name']['#attributes']['aria-describedby']);
68
    unset($form['pass']['#description']);
69
    unset($form['pass']['#attributes']['aria-describedby']);
70
71
    $user_page_url = Url::fromRoute('user.page')->toString();
72
    $user_logout_url = Url::fromRoute('user.logout')->toString();
73
74
    $current_user = \Drupal::currentUser();
75
76
    return [
77
      '#theme' => 'user_dropdown',
78
      '#form' => $form,
79
      '#user_page_url' => $user_page_url,
80
      '#user_logout_url' => $user_logout_url,
81
      '#attached' => [
82
        'library' => [
83
          'df_tools_user/user-dropdown',
84
        ],
85
      ],
86
      '#cache' => [
87
        'context' => ['user'],
88
        'tags' => User::load($current_user->id())->getCacheTags(),
89
      ],
90
    ];
91
  }
92
}
93