TrixAttachmentController::destroy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Te7aHoudini\LaravelTrix\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Response;
7
use Illuminate\Routing\Controller;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\Facades\Validator;
10
use Te7aHoudini\LaravelTrix\Models\TrixAttachment;
11
12
class TrixAttachmentController extends Controller
13
{
14
    public function store(Request $request)
15
    {
16
        $validator = Validator::make($request->all(), [
17
            'file' => 'required|file',
18
            'modelClass' => 'required',
19
            'field' => 'required',
20
        ]);
21
22
        if ($validator->fails()) {
23
            return response()->json(['errors'=>$validator->errors()], Response::HTTP_UNPROCESSABLE_ENTITY);
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...
24
        }
25
26
        $attachment = $request->file->store('/', $request->disk ?? config('laravel-trix.storage_disk'));
27
28
        $url = Storage::disk($request->disk ?? config('laravel-trix.storage_disk'))->url($attachment);
29
30
        TrixAttachment::create([
31
            'field' => $request->field,
32
            'attachable_type' => $request->modelClass,
33
            'attachment' => $attachment,
34
            'disk' => $request->disk ?? config('laravel-trix.storage_disk'),
35
        ]);
36
37
        return response()->json(['url' => $url], Response::HTTP_CREATED);
38
    }
39
40
    public function destroy($url)
41
    {
42
        $attachment = TrixAttachment::where('attachment', basename($url))->first();
43
44
        return response()->json(optional($attachment)->purge());
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...
45
    }
46
}
47