Completed
Push — master ( 648aa9...3341b8 )
by Song
03:51
created

UserController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 7.48 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
dl 8
loc 107
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 8 8 1
A edit() 0 8 1
A create() 0 8 1
B grid() 0 26 2
A form() 0 21 3

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 Encore\Admin\Controllers;
4
5
use Encore\Admin\Auth\Database\Administrator;
6
use Encore\Admin\Auth\Database\Role;
7
use Encore\Admin\Facades\Admin;
8
use Encore\Admin\Form;
9
use Encore\Admin\Grid;
10
use Encore\Admin\Layout\Content;
11
use Illuminate\Routing\Controller;
12
13
class UserController extends Controller
14
{
15
    use AdminController;
16
17
    /**
18
     * Index interface.
19
     *
20
     * @return Content
21
     */
22 View Code Duplication
    public function index()
0 ignored issues
show
Duplication introduced by
This method 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...
23
    {
24
        return Admin::content(function (Content $content) {
25
            $content->header(trans('admin::lang.administrator'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.administrator') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
26
            $content->description(trans('admin::lang.list'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.list') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
27
            $content->body($this->grid()->render());
28
        });
29
    }
30
31
    /**
32
     * Edit interface.
33
     *
34
     * @param $id
35
     *
36
     * @return Content
37
     */
38
    public function edit($id)
39
    {
40
        return Admin::content(function (Content $content) use ($id) {
41
            $content->header(trans('admin::lang.administrator'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.administrator') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
42
            $content->description(trans('admin::lang.edit'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.edit') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
43
            $content->body($this->form()->edit($id));
44
        });
45
    }
46
47
    /**
48
     * Create interface.
49
     *
50
     * @return Content
51
     */
52
    public function create()
53
    {
54
        return Admin::content(function (Content $content) {
55
            $content->header(trans('admin::lang.administrator'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.administrator') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
56
            $content->description(trans('admin::lang.create'));
0 ignored issues
show
Bug introduced by
It seems like trans('admin::lang.create') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
57
            $content->body($this->form());
58
        });
59
    }
60
61
    /**
62
     * Make a grid builder.
63
     *
64
     * @return Grid
65
     */
66
    protected function grid()
67
    {
68
        return Admin::grid(Administrator::class, function (Grid $grid) {
69
            $grid->id('ID')->sortable();
0 ignored issues
show
Documentation Bug introduced by
The method id does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
70
            $grid->username(trans('admin::lang.username'));
0 ignored issues
show
Documentation Bug introduced by
The method username does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
71
            $grid->name(trans('admin::lang.name'));
0 ignored issues
show
Documentation Bug introduced by
The method name does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
72
73
            $grid->roles(trans('admin::lang.roles'))->value(function ($roles) {
0 ignored issues
show
Documentation Bug introduced by
The method roles does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
74
75
                $roles = array_map(function ($role) {
76
                    return "<span class='label label-success'>{$role['name']}</span>";
77
                }, $roles);
78
79
                return join('&nbsp;', $roles);
80
            });
81
82
            $grid->created_at(trans('admin::lang.created_at'));
0 ignored issues
show
Documentation Bug introduced by
The method created_at does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
83
            $grid->updated_at(trans('admin::lang.updated_at'));
0 ignored issues
show
Documentation Bug introduced by
The method updated_at does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
84
85
            $grid->rows(function($row){
86
                if($row->id == 1) {
87
                    $row->actions('edit');
88
                }
89
            });
90
        });
91
    }
92
93
    /**
94
     * Make a form builder.
95
     *
96
     * @return Form
97
     */
98
    public function form()
99
    {
100
        return Admin::form(Administrator::class, function (Form $form) {
101
            $form->display('id', 'ID');
102
103
            $form->text('username', trans('admin::lang.username'))->rules('required');
104
            $form->text('name', trans('admin::lang.name'))->rules('required');
105
            $form->password('password', trans('admin::lang.password'))->rules('required');
106
107
            $form->multipleSelect('roles', trans('admin::lang.roles'))->options(Role::all()->pluck('name', 'id'));
108
109
            $form->display('created_at', trans('admin::lang.created_at'));
110
            $form->display('updated_at', trans('admin::lang.updated_at'));
111
112
            $form->saving(function (Form $form) {
113
                if ($form->password && $form->model()->password != $form->password) {
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<Encore\Admin\Form>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
114
                    $form->password = bcrypt($form->password);
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<Encore\Admin\Form>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property password does not exist on object<Encore\Admin\Form>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
115
                }
116
            });
117
        });
118
    }
119
}
120