Completed
Push — master ( 63d559...0e0340 )
by Mahmoud
03:20
created

AssignUserToRoleAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
3
namespace App\Containers\Authorization\Actions;
4
5
use App\Containers\Authorization\Tasks\AssignUserToRoleTask;
6
use App\Containers\Authorization\Tasks\GetRoleTask;
7
use App\Containers\User\Models\User;
8
use App\Containers\User\Tasks\FindUserByIdTask;
9
use App\Ship\Parents\Actions\Action;
10
11
/**
12
 * Class AssignUserToRoleAction.
13
 *
14
 * @author Mahmoud Zalt <[email protected]>
15
 */
16
class AssignUserToRoleAction extends Action
17
{
18
    /**
19
     * @param $user
20
     * @param $rolesIds
21
     *
22
     * @return  \App\Containers\User\Models\User
23
     */
24 View Code Duplication
    public function run($user, $rolesIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        if (!$user instanceof User) {
27
            $user = $this->call(FindUserByIdTask::class, [$user]);
28
        }
29
30
        if (!is_array($rolesIds)) {
31
            $rolesIds = [$rolesIds];
32
        }
33
34
        foreach ($rolesIds as $roleId) {
35
            $roles[] = $this->call(GetRoleTask::class, [$roleId]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$roles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $roles = 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...
36
        }
37
38
        return $this->call(AssignUserToRoleTask::class, [$user, $roles]);
0 ignored issues
show
Bug introduced by
The variable $roles 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...
39
    }
40
}
41