Completed
Push — master ( 6fd8a2...c6c9a2 )
by Mahmoud
04:04
created

FindUserByAnythingAction::run()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 10
nc 4
nop 3
1
<?php
2
3
namespace App\Containers\User\Actions;
4
5
use App\Containers\User\Services\FindUserService;
6
use App\Port\Action\Abstracts\Action;
7
use Illuminate\Support\Facades\Auth;
8
9
/**
10
 * Class FindUserByAnythingAction.
11
 *
12
 * @author Mahmoud Zalt <[email protected]>
13
 */
14
class FindUserByAnythingAction extends Action
15
{
16
17
    /**
18
     * @var  \App\Containers\User\Services\FindUserService
19
     */
20
    private $findUserService;
21
22
    /**
23
     * FindUserByAnythingAction constructor.
24
     *
25
     * @param \App\Containers\User\Services\FindUserService $findUserService
26
     */
27
    public function __construct(
28
        FindUserService $findUserService
29
    ) {
30
        $this->findUserService = $findUserService;
31
    }
32
33
    /**
34
     * @param $userId
35
     * @param $visitorId
36
     * @param $token
37
     *
38
     * @return  mixed
39
     */
40
    public function run($userId, $visitorId, $token)
41
    {
42
        if ($userId) {
43
            $user = $this->findUserService->byId($userId);
44
        } else {
45
            if ($token) {
46
                $user = Auth::user();
47
            } else {
48
                if ($visitorId) {
49
                    $user = $this->findUserService->byVisitorId($visitorId);
50
                }
51
            }
52
        }
53
54
        return $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...
55
    }
56
57
}
58