Completed
Push — master ( f066c0...72d72a )
by Yaro
01:50 queued 10s
created

DeleteHandlerTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 65%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 50
ccs 13
cts 20
cp 0.65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handleDelete() 0 34 5
init() 0 1 ?
bound() 0 1 ?
crud() 0 1 ?
1
<?php
2
3
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers;
4
5
use Illuminate\Http\Request;
6
use Spatie\Permission\Exceptions\UnauthorizedException;
7
use Yaro\Jarboe\Exceptions\PermissionDenied;
8
use Yaro\Jarboe\Table\CRUD;
9
10
trait DeleteHandlerTrait
11
{
12
    /**
13
     * Handle delete action.
14
     *
15
     * @param Request $request
16
     * @param $id
17
     * @return \Illuminate\Http\JsonResponse
18
     * @throws PermissionDenied
19
     * @throws UnauthorizedException
20
     */
21 1
    public function handleDelete(Request $request, $id)
22
    {
23 1
        $this->init();
24 1
        $this->bound();
25
26 1
        $model = $this->crud()->repo()->find($id);
27 1
        if (!$this->crud()->actions()->isAllowed('delete', $model)) {
28
            throw new PermissionDenied();
29
        }
30
31 1
        if (!$this->can('delete')) {
0 ignored issues
show
Bug introduced by
It seems like can() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32
            throw UnauthorizedException::forPermissions(['delete']);
33
        }
34
35 1
        $this->idEntity = $model->getKey();
0 ignored issues
show
Bug introduced by
The property idEntity does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
37 1
        if ($this->crud()->repo()->delete($id)) {
38 1
            $type = 'hidden';
39
            try {
40 1
                $this->crud()->repo()->find($id);
41
            } catch (\Exception $e) {
42
                $type = 'removed';
43
            }
44
45 1
            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...
46 1
                'type' => $type,
47 1
                'message' => __('jarboe::common.list.delete_success_message', ['id' => $id]),
48
            ]);
49
        }
50
51
        return response()->json([
52
            'message' => __('jarboe::common.list.delete_failed_message', ['id' => $id]),
53
        ], 422);
54
    }
55
56
    abstract protected function init();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
57
    abstract protected function bound();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
58
    abstract protected function crud(): CRUD;
59
}
60