PhotosController::show()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\PhotoLike;
8
use App\Models\PhotoReport;
9
use Illuminate\Http\JsonResponse;
10
use Illuminate\Http\Request;
11
use Illuminate\Http\Response;
12
use Laravel\Lumen\Routing\Controller as BaseController;
13
14
/**
15
 * Class PhotosController.
16
 */
17
class PhotosController extends BaseController
18
{
19
    /**
20
     * Render a set of Public HabboWEB Photos.
21
     *
22
     * @TODO: Exclude Approved Reported Photos from the List
23
     *
24
     * @return JsonResponse
25
     */
26
    public function show(): JsonResponse
27
    {
28
        return response()->json(Photo::all(), 200, [], JSON_UNESCAPED_SLASHES);
0 ignored issues
show
Bug introduced by
It seems like \App\Models\Photo::all() targeting Illuminate\Database\Eloquent\Model::all() can also be of type object<Illuminate\Database\Eloquent\Collection>; however, Laravel\Lumen\Http\ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

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...
29
    }
30
31
    /**
32
     * Register a Report of a Photo
33
     * Observation.: We will not create a limit of max reports.
34
     * Since it's a retro we don't really care about reports.
35
     *
36
     * @MODERATION: Reporting Status (0 = Not Reviewed, 1 = Report Approved, 2 = Report Not Approved
37
     *
38
     * @param Request $request
39
     * @param int     $photoId
40
     *
41
     * @return Response
42
     */
43
    public function report(Request $request, int $photoId): Response
44
    {
45
        (new PhotoReport())->store($photoId, $request->json()->get('reason'), UserFacade::getUser()->uniqueId);
46
47
        return response(null);
48
    }
49
50
    /**
51
     * Like a Photo.
52
     *
53
     * @param int $photoId
54
     *
55
     * @return Response
56
     */
57
    public function likePhoto(int $photoId): Response
58
    {
59
        if (PhotoLike::where('username', UserFacade::getUser()->name)->where('photo_id', $photoId)->count() > 0) {
60
            return response(null);
61
        }
62
63
        (new PhotoLike())->store($photoId, UserFacade::getUser()->name);
64
65
        return response(null);
66
    }
67
68
    /**
69
     * Unlike a Photo.
70
     *
71
     * @param int $photoId
72
     *
73
     * @return Response
74
     */
75
    public function unlikePhoto(int $photoId): Response
76
    {
77
        if (PhotoLike::where('username', UserFacade::getUser()->name)->where('photo_id', $photoId)->count() == 0) {
78
            return response(null);
79
        }
80
81
        PhotoLike::where('username', UserFacade::getUser()->name)->where('photo_id', $photoId)->delete();
82
83
        return response(null);
84
    }
85
86
    /**
87
     * Delete a Photo.
88
     *
89
     * @param int $photoId
90
     *
91
     * @return Response
92
     */
93
    public function delete(int $photoId): Response
94
    {
95
        $photo = Photo::find($photoId);
96
97
        if ($photo == null || $photo->creator_id != UserFacade::getUser()->uniqueId) {
98
            return response(null, 401);
99
        }
100
101
        $photo->delete();
102
103
        return response(null);
104
    }
105
}
106