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

GetUserAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 27
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 4
1
<?php
2
3
namespace App\Containers\User\Actions;
4
5
use App\Containers\Authentication\Tasks\GetAuthenticatedUserTask;
6
use App\Containers\User\Exceptions\UserNotFoundException;
7
use App\Containers\User\Tasks\FindUserByIdTask;
8
use App\Ship\Parents\Actions\Action;
9
10
/**
11
 * Class GetUserAction.
12
 *
13
 * @author Mahmoud Zalt <[email protected]>
14
 */
15
class GetUserAction extends Action
16
{
17
18
    /**
19
     * @param      $userId
20
     * @param null $token
21
     *
22
     * @return  mixed
23
     */
24
    public function run($userId, $token = null)
25
    {
26
        if ($userId) {
27
            $user = $this->call(FindUserByIdTask::class, [$userId])->withToken();
28
        } else {
29
            if ($token) {
30
                $user = $this->call(GetAuthenticatedUserTask::class, [])->withToken();
31
            }
32
        }
33
34
        if (!$user) {
0 ignored issues
show
Bug introduced by
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...
35
            throw new UserNotFoundException();
36
        }
37
38
        return $user;
39
    }
40
41
}
42