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::build()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 75
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 75
rs 9.1781
c 0
b 0
f 0
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Drupal\df_tools_user\Plugin\Block;
4
5
use Drupal\Core\Block\BlockBase;
6
use Drupal\Core\Cache\Cache;
7
use Drupal\Core\Form\FormBuilder;
8
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9
use Drupal\Core\Session\AccountProxyInterface;
10
use Drupal\Core\Url;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
 * Provides the "User Dropdown" block.
15
 *
16
 * @Block(
17
 *   id = "user_dropdown",
18
 *   admin_label = @Translation("User Dropdown"),
19
 *   category = @Translation("Menus")
20
 * )
21
 */
22
class UserDropdownBlock extends BlockBase implements ContainerFactoryPluginInterface {
23
24
  /**
25
   * The form builder.
26
   *
27
   * @var \Drupal\Core\Form\FormBuilder
28
   */
29
  protected $formBuilder;
30
31
  /**
32
   * The current user.
33
   *
34
   * @var \Drupal\Core\Session\AccountProxyInterface
35
   */
36
  protected $user;
37
38
  public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilder $form_builder, AccountProxyInterface $user) {
39
    parent::__construct($configuration, $plugin_id, $plugin_definition);
40
    $this->formBuilder = $form_builder;
41
    $this->user = $user;
42
  }
43
44
  /**
45
   * {@inheritdoc}
46
   */
47
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
48
    return new static(
49
      $configuration,
50
      $plugin_id,
51
      $plugin_definition,
52
      $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

52
      /** @scrutinizer ignore-type */ $container->get('form_builder'),
Loading history...
53
      $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

53
      /** @scrutinizer ignore-type */ $container->get('current_user')
Loading history...
54
    );
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function build() {
61
    $block = [];
62
    $links_cache_contexts = [];
63
64
    // Display different links to depending on whether the user is logged in.
65
    if ($this->user->isAnonymous()) {
66
      // Create a 'person' icon that toggles a login form.
67
      $markup = '';
68
      $markup .= '<a class="login-dropdown-button standard-icon meta-icon-size left" data-toggle="user-login-wrapper" aria-controls="user-login-wrapper" aria-expanded="false">';
69
      $markup .= '<i class="icon ion-ios-person"><span>Login</span></i>';
70
      $markup .= '</a>';
71
72
      $block['login'] = [
73
        '#markup'=> $markup,
74
      ];
75
76
      // Wrap the login form with some required foundation classes/attributes
77
      $login_wrapper = [
78
        '#type' => 'container',
79
        '#attributes' =>[
80
          'class' => ['dropdown-pane', 'display-none'],
81
          'id' => 'user-login-wrapper',
82
          'data-dropdown' => '',
83
          'aria-hidden' => 'true',
84
          'aria-autoclose' => 'true',
85
          'data-auto-focus' => 'true',
86
          'tabindex' => '-1'
87
        ],
88
      ];
89
90
      // Get the user login form.
91
      $form = $this->formBuilder->getForm('\Drupal\user\Form\UserLoginForm');
92
93
      // Remove default form descriptions.
94
      unset($form['name']['#description']);
95
      unset($form['name']['#attributes']['aria-describedby']);
96
      unset($form['pass']['#description']);
97
      unset($form['pass']['#attributes']['aria-describedby']);
98
99
      $login_wrapper['form'] = $form;
100
101
      $block['wrapper'] = $login_wrapper;
102
    }
103
    else {
104
      $username = $this->user->getDisplayName();
105
      $user_page_url = Url::fromRoute('user.page')->toString();
106
      $user_logout_url = Url::fromRoute('user.logout')->toString();
107
108
      // Create a 'person' icon that toggles user profile and log out links.
109
      $markup = '';
110
      $markup .= '<a class="login-dropdown-button standard-icon meta-icon-size left" data-toggle="user-logout-wrapper" aria-controls="user-logout-wrapper" aria-expanded="false">';
111
      $markup .= '<i class="icon ion-ios-person"></i>';
112
      $markup .= '<span> '. $username . '</span>';
113
      $markup .= '</a>';
114
115
      $markup .= '<div id="user-logout-wrapper" class="dropdown-pane f-dropdown" data-dropdown aria-hidden="true" aria-autoclose="false" data-auto-focus="false">';
116
      $markup .= '<div class="user-links">';
117
      $markup .= '<a class="" href="' . $user_page_url . '"><i class="icon ion-ios-person"></i> ' . $username . '</a>';
0 ignored issues
show
Bug introduced by
Are you sure $user_page_url of type Drupal\Core\GeneratedUrl|string can be used in concatenation? ( Ignorable by Annotation )

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

117
      $markup .= '<a class="" href="' . /** @scrutinizer ignore-type */ $user_page_url . '"><i class="icon ion-ios-person"></i> ' . $username . '</a>';
Loading history...
118
      $markup .= '<a class="logout-button" href="' . $user_logout_url . '"><i class="icon ion-log-out"></i> ' . t('Log Out') . '</a>';
0 ignored issues
show
Bug introduced by
Are you sure $user_logout_url of type Drupal\Core\GeneratedUrl|string can be used in concatenation? ( Ignorable by Annotation )

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

118
      $markup .= '<a class="logout-button" href="' . /** @scrutinizer ignore-type */ $user_logout_url . '"><i class="icon ion-log-out"></i> ' . t('Log Out') . '</a>';
Loading history...
119
      $markup .= '</div>';
120
      $markup .= '</div>';
121
122
      $block['login'] = [
123
        '#markup'=> $markup,
124
      ];
125
126
      // The "Edit user account" link is per-user.
127
      $links_cache_contexts[] = 'user';
128
    }
129
130
    // Cacheable per "authenticated or not", because the links to
131
    // display depend on that.
132
    $block['#cache']['contexts'] = Cache::mergeContexts(['user.roles:authenticated'], $links_cache_contexts);
133
134
    return $block;
135
  }
136
137
}
138