Completed
Push — master ( 769d17...6fd8a2 )
by Mahmoud
03:54
created

FindUserAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A run() 0 10 2
A byEverything() 0 12 4
1
<?php
2
3
namespace App\Containers\User\Actions;
4
5
use App\Containers\User\Exceptions\UserNotFoundException;
6
use App\Containers\User\Services\FindUserService;
7
use App\Port\Action\Abstracts\Action;
8
use Exception;
9
use Illuminate\Support\Facades\Auth;
10
11
/**
12
 * Class FindUserAction.
13
 *
14
 * @author Mahmoud Zalt <[email protected]>
15
 */
16
class FindUserAction extends Action
17
{
18
19
    /**
20
     * @var  \App\Containers\User\Services\FindUserService
21
     */
22
    private $findUserService;
23
24
    /**
25
     * FindUserByIdAction constructor.
26
     *
27
     * @param \App\Containers\User\Services\FindUserService $findUserService
28
     */
29
    public function __construct(
30
        FindUserService $findUserService
31
    ) {
32
        $this->findUserService = $findUserService;
33
    }
34
35
36
    /**
37
     * @param $id
38
     *
39
     * @return  mixed
40
     */
41
    public function run($id)
42
    {
43
        try {
44
            $user = $this->findUserService->byId($id);
45
        } catch (Exception $e) {
46
            throw new UserNotFoundException;
47
        }
48
49
        return $user;
50
    }
51
52
    /**
53
     * @param $userId
54
     * @param $visitorId
55
     * @param $token
56
     *
57
     * @return  mixed
58
     */
59
    public function byEverything($userId, $visitorId, $token)
60
    {
61
        if($userId){
62
            $user = $this->findUserService->byId($userId);
63
        }else if($token){
64
            $user = Auth::user();
65
        }else if($visitorId){
66
            $user = $this->findUserService->byVisitorId($visitorId);
67
        }
68
69
        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...
70
    }
71
72
73
74
}
75