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

CreateHandlerTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 55
loc 55
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handleCreate() 19 19 3
A getCreateViewsAbove() 4 4 1
A getCreateViewsBelow() 4 4 1
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 CreateHandlerTrait
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
     * Show create form page.
14
     *
15
     * @param Request $request
16
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
17
     * @throws PermissionDenied
18
     * @throws UnauthorizedException
19
     */
20 1
    public function handleCreate(Request $request)
21
    {
22 1
        $this->init();
23 1
        $this->bound();
24
25 1
        if (!$this->crud()->actions()->isAllowed('create')) {
26
            throw new PermissionDenied();
27
        }
28
29 1
        if (!$this->can('create')) {
30
            throw UnauthorizedException::forPermissions(['create']);
31
        }
32
33 1
        return view($this->viewCrudCreate, [
0 ignored issues
show
Bug introduced by
The property viewCrudCreate 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...
34 1
            'crud' => $this->crud(),
35 1
            'viewsAbove' => $this->getCreateViewsAbove(),
36 1
            'viewsBelow' => $this->getCreateViewsBelow(),
37
        ]);
38
    }
39
40
    /**
41
     * Get array of view's objects, that should be rendered above content of `create` view.
42
     *
43
     * @return array
44
     */
45 2
    protected function getCreateViewsAbove(): array
46
    {
47 2
        return [];
48
    }
49
50
    /**
51
     * Get array of view's objects, that should be rendered below content of `create` view.
52
     *
53
     * @return array
54
     */
55 2
    protected function getCreateViewsBelow(): array
56
    {
57 2
        return [];
58
    }
59
60
    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...
61
    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...
62
    abstract protected function crud(): CRUD;
63
    abstract protected function can($action): bool;
64
}
65