Completed
Push — master ( 071fd8...3c3f7e )
by Mahmoud
03:40
created

GetUserAction::run()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 6
nop 2
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\Port\Action\Abstracts\Action;
9
10
/**
11
 * Class GetUserAction.
12
 *
13
 * @author Mahmoud Zalt <[email protected]>
14
 */
15
class GetUserAction extends Action
16
{
17
18
    /**
19
     * @var  \App\Containers\User\Tasks\GetAuthenticatedUserTask
20
     */
21
    private $getAuthenticatedUserTask;
22
23
    /**
24
     * FindUserByAnythingAction constructor.
25
     *
26
     * @param \App\Containers\User\Tasks\FindUserByIdTask         $findUserByIdTask
27
     * @param \App\Containers\User\Tasks\GetAuthenticatedUserTask $getAuthenticatedUserTask
28
     */
29
    public function __construct(
30
        FindUserByIdTask $findUserByIdTask,
31
        GetAuthenticatedUserTask $getAuthenticatedUserTask
32
    ) {
33
        $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...
34
        $this->getAuthenticatedUserTask = $getAuthenticatedUserTask;
0 ignored issues
show
Documentation Bug introduced by
It seems like $getAuthenticatedUserTask of type object<App\Containers\Au...tAuthenticatedUserTask> is incompatible with the declared type object<App\Containers\Us...tAuthenticatedUserTask> of property $getAuthenticatedUserTask.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
    }
36
37
    /**
38
     * @param      $userId
39
     * @param null $token
40
     *
41
     * @return  mixed
42
     */
43
    public function run($userId, $token = null)
44
    {
45
        if ($userId) {
46
            $user = $this->findUserByIdTask->run($userId)->withToken();
47
        } else {
48
            if ($token) {
49
                $user = $this->getAuthenticatedUserTask->run()->withToken();
50
            }
51
        }
52
53
        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...
54
            throw new UserNotFoundException();
55
        }
56
57
        return $user;
58
    }
59
60
}
61