Completed
Push — master ( 8f08c7...7bd638 )
by Mohamed
02:25
created

MediaLibraryController::delete()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Microboard\Http\Controllers;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response;
9
use Microboard\Http\Requests\Media\StoreFormRequest;
10
11
class MediaLibraryController extends Controller
12
{
13
    /**
14
     * Store a newly created resource in storage.
15
     *
16
     * @param StoreFormRequest $request
17
     * @param Filesystem $files
18
     * @return JsonResponse
19
     */
20
    public function upload(StoreFormRequest $request, Filesystem $files)
21
    {
22
        $path = storage_path('tmp');
23
24
        if (!$files->isDirectory($path)) {
25
            $files->makeDirectory($path, 0777, true);
26
        }
27
28
        $file = $request->file('file');
29
30
        $file->move($path, $name = uniqid() . '_' . trim($file->getClientOriginalName()));
31
32
        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...
33
            'name' => $name,
34
            'original_name' => $file->getClientOriginalName(),
35
        ]);
36
    }
37
38
    /**
39
     * Remove the specified resource from storage.
40
     *
41
     * @param Request $request
42
     * @param Filesystem $files
43
     * @return Response
44
     */
45
    public function delete(Request $request, Filesystem $files)
46
    {
47
        if ($request->has('name') && $files->exists($path = storage_path("tmp/{$request->input('name')}"))) {
48
            $files->delete($path);
49
        }
50
51
        return response('DONE');
52
    }
53
}
54