RevokeUserFromRoleAction::run()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 9.552
cc 4
nc 8
nop 1
1
<?php
2
3
namespace App\Containers\Authorization\Actions;
4
5
use Apiato\Core\Foundation\Facades\Apiato;
6
use App\Containers\User\Models\User;
7
use App\Ship\Parents\Actions\Action;
8
use App\Ship\Transporters\DataTransporter;
9
use Illuminate\Database\Eloquent\Collection;
10
11
/**
12
 * Class RevokeUserFromRoleAction.
13
 *
14
 * @author Mahmoud Zalt <[email protected]>
15
 */
16
class RevokeUserFromRoleAction extends Action
17
{
18
19
    /**
20
     * @param \App\Ship\Transporters\DataTransporter $data
21
     *
22
     * @return  \App\Containers\User\Models\User
23
     */
24
    public function run(DataTransporter $data): User
25
    {
26
        // if user ID is passed then convert it to instance of User (could be user Id Or Model)
27
        if (!$data->user_id instanceof User) {
28
            $user = Apiato::call('[email protected]', [$data->user_id]);
29
        }
30
31
        // convert to array in case single ID was passed (could be Single Or Multiple Role Ids)
32
        $rolesIds = (array)$data->roles_ids;
33
34
        $roles = new Collection();
35
36
        foreach ($rolesIds as $roleId) {
37
            $role = Apiato::call('[email protected]', [$roleId]);
38
            $roles->add($role);
39
        }
40
41
        foreach ($roles->pluck('name')->toArray() as $roleName) {
42
            $user->removeRole($roleName);
0 ignored issues
show
Bug introduced by Mahmoud Zalt
The variable $user 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
45
        return $user;
46
    }
47
48
}
49