Completed
Push — master ( 5c4b87...207d41 )
by Yaro
06:22
created

RestoreHandlerTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 46
loc 46
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B handleRestore() 29 29 6
init() 1 1 ?
bound() 1 1 ?
crud() 1 1 ?
can() 1 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
trait RestoreHandlerTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
{
12
    /**
13
     * Restore record by its id.
14
     *
15
     * @param $id
16
     * @param Request $request
17
     * @return \Illuminate\Http\JsonResponse
18
     * @throws PermissionDenied
19
     * @throws UnauthorizedException
20
     */
21 6
    public function handleRestore(Request $request, $id)
22
    {
23 6
        $this->init();
24 6
        $this->bound();
25
26 6
        if (!$this->crud()->isSoftDeleteEnabled()) {
27 1
            throw new PermissionDenied();
28
        }
29
30 5
        if (!$this->can('restore')) {
31 2
            throw UnauthorizedException::forPermissions(['restore']);
32
        }
33
34 3
        $model = $this->crud()->repo()->find($id);
35 3
        if (!$this->crud()->actions()->isAllowed('restore', $model)) {
36 1
            throw new PermissionDenied();
37
        }
38
39 2
        $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...
40
41 2
        if (!$this->crud()->repo()->restore($id) || $model->trashed()) {
42 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...
43 1
                'message' => __('jarboe::common.list.restore_failed_message', ['id' => $id]),
44 1
            ], 422);
45
        }
46 1
        return response()->json([
47 1
            'message' => __('jarboe::common.list.restore_success_message', ['id' => $id]),
48
        ]);
49
    }
50
51
    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...
52
    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...
53
    abstract protected function crud(): CRUD;
54
    abstract protected function can($action): bool;
55
}
56