Completed
Push — master ( e1cce4...d2c19b )
by David
09:34 queued 11s
created

WelcomeController::roomSelect()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 9
nop 1
dl 0
loc 39
rs 8.9848
c 0
b 0
f 0
1
<?php
2
3
namespace Torralbodavid\DuckFunkCore\Http\Controllers\API\Welcome;
4
5
use Exception;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Controller;
8
use Torralbodavid\DuckFunkCore\Http\Request\Avatar\UserRequest;
9
use Torralbodavid\DuckFunkCore\Http\Request\Avatar\UserSaveRequest;
10
use Torralbodavid\DuckFunkCore\Models\Arcturus\Room;
11
12
class WelcomeController extends Controller
13
{
14
    private const ROOMINDEX = [1, 2, 3];
15
16
    public function check(UserRequest $request)
17
    {
18
        $request->validated();
19
20
        return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
21
            'code' => 'OK',
22
            'validationResult' => null,
23
            'suggestions' => [],
24
        ]);
25
    }
26
27
    public function select(UserRequest $request)
28
    {
29
        core()->user()->update([
30
            'username' => $request['name'],
31
        ]);
32
33
        core()->user()->settings->update([
34
            'welcome_flow_step' => 2,
35
        ]);
36
37
        return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
38
            'code' => 'OK',
39
            'validationResult' => null,
40
            'suggestions' => [],
41
        ]);
42
    }
43
44
    public function save(UserSaveRequest $request)
45
    {
46
        $request = $request->validated();
47
48
        core()->user()->update([
49
            'look' => $request['figure'],
50
            'gender' => $request['gender'],
51
        ]);
52
53
        return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54
            'uniqueId' => core()->user()->id,
55
            'name' => core()->user()->username,
56
            'figureString' => $request['figure'],
57
            'motto' => core()->user()->motto,
58
        ]);
59
    }
60
61
    public function roomSelect(Request $request)
62
    {
63
        if (! in_array($request->roomIndex, self::ROOMINDEX)) {
64
            throw new Exception('Debe escoger una sala válida');
65
        }
66
67
        if ($request->roomIndex == 1) {
68
            $roomTemplate = Room::find(3);
69
        }
70
71
        if ($request->roomIndex == 2) {
72
            $roomTemplate = Room::find(5);
73
        }
74
75
        if ($request->roomIndex == 3) {
76
            $roomTemplate = Room::find(6);
77
        }
78
79
        $room = new Room();
80
        $room->owner_id = core()->user()->id;
81
        $room->owner_name = core()->user()->username;
82
        $room->name = 'Territorio '.core()->user()->username;
83
        $room->description = '¡Una sala pre-decorada!';
84
        $room->model = 'model_h';
85
        $room->paper_floor = $roomTemplate->paper_floor;
0 ignored issues
show
Bug introduced by
The variable $roomTemplate 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...
86
        $room->paper_wall = $roomTemplate->paper_wall;
87
        $room->paper_landscape = $roomTemplate->paper_landscape;
88
        $room->saveOrFail();
89
90
        $roomTemplate->items->each(function ($itemTemplate) use ($room) {
91
            $item = $itemTemplate->replicate();
92
            $item->user_id = core()->user()->id;
93
            $item->room_id = $room->id;
94
            $item->saveOrFail();
95
        });
96
97
        core()->user()->settings->update(['welcome_flow_enabled' => false]);
98
        core()->user()->update(['home_room' => $room->id]);
99
    }
100
}
101