Completed
Push — master ( e089fb...e812a5 )
by Mahmoud
03:46
created

FindUserAction::run()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 20
rs 8.8571
cc 5
eloc 12
nc 8
nop 3
1
<?php
2
3
namespace App\Containers\User\Actions;
4
5
use App\Containers\User\Exceptions\UserNotFoundException;
6
use App\Containers\User\Tasks\FindUserByIdTask;
7
use App\Containers\User\Tasks\FindUserByVisitorIdTask;
8
use App\Containers\User\Tasks\GetAuthenticatedUserTask;
9
use App\Port\Action\Abstracts\Action;
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\Tasks\FindUserByVisitorIdTask
21
     */
22
    private $findUserByVisitorIdTask;
23
24
    /**
25
     * @var  \App\Containers\User\Tasks\GetAuthenticatedUserTask
26
     */
27
    private $getAuthenticatedUserTask;
28
29
    /**
30
     * FindUserByAnythingAction constructor.
31
     *
32
     * @param \App\Containers\User\Tasks\FindUserByVisitorIdTask  $findUserByVisitorIdTask
33
     * @param \App\Containers\User\Tasks\FindUserByIdTask         $findUserByIdTask
34
     * @param \App\Containers\User\Tasks\GetAuthenticatedUserTask $getAuthenticatedUserTask
35
     */
36
    public function __construct(
37
        FindUserByVisitorIdTask $findUserByVisitorIdTask,
38
        FindUserByIdTask $findUserByIdTask,
39
        GetAuthenticatedUserTask $getAuthenticatedUserTask
40
    ) {
41
        $this->findUserByVisitorIdTask = $findUserByVisitorIdTask;
42
        $this->findUserByIdTask = $findUserByIdTask;
0 ignored issues
show
Bug introduced by
The property findUserByIdTask does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
        $this->getAuthenticatedUserTask = $getAuthenticatedUserTask;
44
    }
45
46
    /**
47
     * @param $userId
48
     * @param $visitorId
49
     * @param $token
50
     *
51
     * @return  mixed
52
     */
53
    public function run($userId, $visitorId, $token)
54
    {
55
        if ($userId) {
56
            $user = $this->findUserByIdTask->run($userId);
57
        } else {
58
            if ($token) {
59
                $user = $this->getAuthenticatedUserTask->run();
60
            } else {
61
                if ($visitorId) {
62
                    $user = $this->findUserByVisitorIdTask->run($visitorId);
63
                }
64
            }
65
        }
66
67
        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...
68
            throw new UserNotFoundException();
69
        }
70
71
        return $user;
72
    }
73
74
}
75