Completed
Push — 316-authentication-manager ( e36a80 )
by Jonathan
01:33
created

DrupalUserManager::currentUserHasRole()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 3
nop 1
1
<?php
2
3
namespace Drupal\DrupalExtension\Manager;
4
5
/**
6
 * Default implementation of the Drupal user manager service.
7
 */
8
class DrupalUserManager implements DrupalUserManagerInterface {
9
10
  /**
11
   * The user object representing the currently logged in user.
12
   *
13
   * @var \stdClass|FALSE
14
   */
15
  protected $user = FALSE;
16
17
  /**
18
   * An array of user objects representing users created during the test.
19
   *
20
   * @var \stdClass[]
21
   */
22
  protected $users = [];
23
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function getCurrentUser() {
28
    return $this->user;
29
  }
30
31
  /**
32
   * {@inheritdoc}
33
   */
34
  public function setCurrentUser($user) {
35
    $this->user = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user can also be of type boolean. However, the property $user is declared as type object<stdClass>|false. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
36
  }
37
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public function addUser($user) {
42
    $this->users[$user->name] = $user;
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function removeUser($userName) {
49
    unset($this->users[$userName]);
50
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public function getUser($userName) {
56
    if (!isset($this->users[$userName])) {
57
      throw new \Exception(sprintf('No user with %s name is registered with the driver.', $userName));
58
    }
59
    return $this->users[$userName];
60
  }
61
62
  /**
63
   * {@inheritdoc}
64
   */
65
  public function getUsers() {
66
    return $this->users;
67
  }
68
69
  /**
70
   * {@inheritdoc}
71
   */
72
  public function clearUsers() {
73
    $this->user = FALSE;
74
    $this->users = [];
75
  }
76
77
  /**
78
   * {@inheritdoc}
79
   */
80
  public function hasUsers() {
81
    return !empty($this->users);
82
  }
83
84
  /**
85
   * {@inheritdoc}
86
   */
87
  public function currentUserIsAnonymous() {
88
    return empty($this->user);
89
  }
90
91
  /**
92
   * {@inheritdoc}
93
   */
94
  public function currentUserHasRole($role) {
95
    return !$this->currentUserIsAnonymous() && !empty($this->user->role) && $this->user->role == $role;
96
  }
97
98
}
99