ProfileController::getPublicData()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\User as UserFacade;
6
use App\Models\Photo;
7
use App\Models\User;
8
use App\Models\UserBadge;
9
use App\Models\UserPreferences;
10
use App\Models\UserProfile;
11
use Illuminate\Http\JsonResponse;
12
use Illuminate\Http\Request;
13
use Laravel\Lumen\Routing\Controller as BaseController;
14
15
/**
16
 * Class ProfileController.
17
 */
18
class ProfileController extends BaseController
19
{
20
    /**
21
     * Get Public User Data.
22
     *
23
     * @param Request $request
24
     *
25
     * @return JsonResponse
26
     */
27
    public function getPublicData(Request $request): JsonResponse
28
    {
29
        $userData = User::where('username', $request->input('name'))->first();
30
31
        if ($userData == null || $userData->isBanned) {
32
            return response()->json(null, 404);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\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...
33
        }
34
35
        $userPreferences = UserPreferences::find($userData->uniqueId);
36
37
        $userData->selectedBadges = UserBadge::where('user_id', $userData->uniqueId)->where('slot_id', '>', 0)->orderBy('slot_id', 'ASC')->get() ?? [];
38
        $userData->profileVisible = $userPreferences == null ? true : $userPreferences->profileVisible == '1';
39
40
        return response()->json($userData);
41
    }
42
43
    /**
44
     * Get Public User Profile.
45
     *
46
     * @param int $userId
47
     *
48
     * @return JsonResponse
49
     */
50
    public function getPublicProfile($userId): JsonResponse
51
    {
52
        $userData = User::find($userId);
53
54
        if ($userData == null || $userData->isBanned) {
55
            return response()->json(null, 404);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\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...
56
        }
57
58
        $userPreferences = UserPreferences::find($userData->uniqueId);
59
60
        if ($userPreferences != null && $userPreferences->profileVisible == '0') {
61
            return response()->json(null, 404);
62
        }
63
64
        return response()->json(new UserProfile($userData));
0 ignored issues
show
Documentation introduced by
new \App\Models\UserProfile($userData) is of type object<App\Models\UserProfile>, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
    }
66
67
    /**
68
     * Get Private User Profile.
69
     *
70
     * @return JsonResponse
71
     */
72
    public function getProfile(): JsonResponse
73
    {
74
        return response()->json(new UserProfile(UserFacade::getUser()));
0 ignored issues
show
Documentation introduced by
new \App\Models\UserProf...acades\User::getUser()) is of type object<App\Models\UserProfile>, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\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...
75
    }
76
77
    /**
78
     * Get User Stories.
79
     *
80
     * @TODO: Implement Habbo Stories
81
     *
82
     * @return JsonResponse
83
     */
84
    public function getStories(): JsonResponse
85
    {
86
        return response()->json([]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\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...
87
    }
88
89
    /**
90
     * Get User Photos.
91
     *
92
     * @param int $userId
93
     *
94
     * @return JsonResponse
95
     */
96
    public function getPhotos(int $userId): JsonResponse
97
    {
98
        if (Photo::where('creator_id', $userId)->count() == 0) {
99
            return response()->json([]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\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...
100
        }
101
102
        $userPhotos = Photo::where('creator_id', $userId)->get();
103
104
        return response()->json($userPhotos);
105
    }
106
}
107