Passed
Push — master ( 129074...847d40 )
by Tomas
11:01
created

UserManagerAbstract::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Eziat\PermissionBundle\Model;
6
7
/**
8
 * @author Tomas Jakl <[email protected]>
9
 */
10
abstract class UserManagerAbstract implements UserManagerInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function hasPermissions(UserPermissionInterface $user, array $permissions) : bool
16
    {
17
        $userPermissions = $this->getPermissions($user);
18
19
        foreach ($permissions as $permission) {
20
            if (!in_array($permission, $userPermissions)) {
21
                return false;
22
            }
23
        }
24
25
        return true;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function hasPermission(UserPermissionInterface $user, $permission) : bool
32
    {
33
        return $this->hasPermission($user, [$permission]);
34
    }
35
36
    protected function getUserPermissions(UserPermissionInterface $user) : array
37
    {
38
        foreach ($user->getPermissions() as $permission) {
39
            $permissions[] = $permission->getName();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$permissions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $permissions = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
40
        }
41
42
        return $permissions;
0 ignored issues
show
Bug introduced by
The variable $permissions does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
43
    }
44
}