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.

RoleHelper   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0
Metric Value
wmc 19
lcom 2
cbo 0
dl 0
loc 144
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A getRoleHierarchy() 0 11 3
A getAvailableRoles() 0 4 1
A getAvailableRoleKeys() 0 4 1
B hasRole() 0 12 5
B getUsersHighestRole() 0 17 5
A getUsersHighestRoleAsName() 0 12 2
1
<?php
2
3
/*
4
 * This file is part of the CCDNForum ForumBundle
5
 *
6
 * (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
7
 *
8
 * Available on github <http://www.github.com/codeconsortium/>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace CCDNForum\ForumBundle\Component\Helper;
15
16
use Symfony\Component\Security\Core\User\UserInterface;
17
use Symfony\Component\Security\Core\SecurityContextInterface;
18
19
/**
20
 *
21
 * @category CCDNForum
22
 * @package  ForumBundle
23
 *
24
 * @author   Reece Fowell <[email protected]>
25
 * @license  http://opensource.org/licenses/MIT MIT
26
 * @version  Release: 2.0
27
 * @link     https://github.com/codeconsortium/CCDNForumForumBundle
28
 *
29
 */
30
class RoleHelper
31
{
32
    /**
33
     *
34
     * @access protected
35
     * @var \Symfony\Component\Security\Core\SecurityContextInterface $securityContext
36
     */
37
    protected $securityContext;
38
39
    /**
40
     *
41
     * @access protected
42
     * @var array $availableRoles
43
     */
44
    protected $availableRoles;
45
46
    /**
47
     *
48
     * @access protected
49
     * @var array $availableRoleKeys
50
     */
51
    protected $availableRoleKeys;
52
53
    /**
54
     *
55
     * @access public
56
     * @param \Symfony\Component\Security\Core\SecurityContextInterface $securityContext
57
     * @param array                                                     $availableRoles
58
     */
59
    public function __construct(SecurityContextInterface $securityContext, $availableRoles)
60
    {
61
        $this->securityContext = $securityContext;
62
63
        // default role is array is empty.
64
        if (empty($availableRoles)) {
65
            $availableRoles[] = 'ROLE_USER';
66
        }
67
68
        $this->availableRoles = $availableRoles;
69
70
        // Remove the associate arrays.
71
        $this->availableRoleKeys = array_keys($availableRoles);
72
    }
73
74
    /**
75
     *
76
     * @access public
77
     * @return Array
78
     */
79
    public function getRoleHierarchy()
80
    {
81
        $roles = array();
82
83
        foreach ($this->availableRoles as $roleName => $roleSubs) {
84
            $subs = '<ul><li>' . implode('</li><li>', $roleSubs) . '</li></ul>';
85
            $roles[$roleName] = '<strong>' . $roleName . '</strong>' . ($subs != '<ul><li>' . $roleName . '</li></ul>' ? "\n" . $subs:'');
86
        }
87
88
        return $roles;
89
    }
90
91
    /**
92
     *
93
     * @access public
94
     * @return array $availableRoles
95
     */
96
    public function getAvailableRoles()
97
    {
98
        return $this->availableRoles;
99
    }
100
101
    /**
102
     *
103
     * @access public
104
     * @return array $availableRoles
105
     */
106
    public function getAvailableRoleKeys()
107
    {
108
        return $this->availableRoleKeys;
109
    }
110
111
    /**
112
     *
113
     * @access public
114
     * @param  \Symfony\Component\Security\Core\User\UserInterface $user
115
     * @param  string                                              $role
116
     * @return bool
117
     */
118
    public function hasRole(UserInterface $user, $role)
119
    {
120
        foreach ($this->availableRoles as $aRoleKey => $aRole) {
121
            if ($user->hasRole($aRoleKey)) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Security\Core\User\UserInterface as the method hasRole() does only exist in the following implementations of said interface: CCDNForum\ForumBundle\Te...ctional\src\Entity\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
122
                if (in_array($role, $aRole) || $role == $aRoleKey) {
123
                    return true;
124
                }
125
            }
126
        }
127
128
        return false;
129
    }
130
131
    /**
132
     *
133
     * @access public
134
     * @param  array $userRoles
0 ignored issues
show
Documentation introduced by
There is no parameter named $userRoles. Did you maybe mean $usersRoles?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
135
     * @return int   $highestUsersRoleKey
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
136
     */
137
    public function getUsersHighestRole($usersRoles)
138
    {
139
        $usersHighestRoleKey = 0;
140
141
        // Compare (A)vailable roles against (U)sers roles.
142
        foreach ($this->availableRoleKeys as $aRoleKey => $aRole) {
143
            foreach ($usersRoles as $uRole) {
144
                if ($uRole == $aRole && $aRoleKey > $usersHighestRoleKey) {
145
                    $usersHighestRoleKey = $aRoleKey;
146
147
                    break; // break because once uRole == aRole we know we cannot match anything else.
148
                }
149
            }
150
        }
151
152
        return $usersHighestRoleKey;
153
    }
154
155
    /**
156
     *
157
     * @access public
158
     * @param  array  $userRoles
0 ignored issues
show
Documentation introduced by
There is no parameter named $userRoles. Did you maybe mean $usersRoles?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
159
     * @return string $role
160
     */
161
    public function getUsersHighestRoleAsName($usersRoles)
162
    {
163
        $usersHighestRoleKey = $this->getUsersHighestRole($usersRoles);
164
165
        $roles = $this->availableRoleKeys;
166
167
        if (array_key_exists($usersHighestRoleKey, $roles)) {
168
            return $roles[$usersHighestRoleKey];
169
        } else {
170
            return 'ROLE_USER';
171
        }
172
    }
173
}
174