GroupController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 39.13 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 6
dl 45
loc 115
rs 10
c 0
b 0
f 0

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 App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Group;
7
use App\Account;
8
9
class GroupController extends Controller
10
{
11
    use SearchTrait;
12
13
    /**
14
     * Create a new controller instance.
15
     *
16
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
17
     */
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
    }
22
23 View Code Duplication
    public function showGroups(Request $request)
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...
24
    {
25
        $groups = Group::orderBy('name', 'asc');
26
27
        $results = $this->search($groups, $request->input('type'), $request->input('search'));
28
29
        if (false === is_null($results)) {
30
            $request->session()->flash(
31
                'results',
32
                trans_choice(
33
                    'groups.message.search',
34
                    $results,
35
                    ['number' => $results]
36
                )
37
            );
38
            $request->session()->flash('search', $request->input('search'));
39
            $request->session()->flash('type', $request->input('type'));
40
        }
41
42
        return view('group/groups', ['groups' => $groups->paginate(20)]);
43
    }
44
45
    public function addGroup(Request $request)
46
    {
47
        $this->validate($request, [
48
            'name' => 'required|max:100|unique:groups,name',
49
        ]);
50
51
        if (false === empty($request->id)) {
52
            return $this->updateGroup($request, $request->id);
53
        } else {
54
            $group = new Group;
55
            $group->name = $request->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Group>. 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...
56
            $group->created_by = $request->user()->id;
0 ignored issues
show
Documentation introduced by
The property created_by does not exist on object<App\Group>. 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...
57
            $group->save();
58
            return redirect()->back()->with(
59
                'status',
60
                trans('groups.message.add', ['group' => $group->name])
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Group>. 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...
61
            );
62
        }
63
    }
64
65
    public function updateGroup(Request $request, $id)
66
    {
67
        $this->validate($request, [
68
            'name' => 'required|max:100|unique:groups,name',
69
        ]);
70
71
        $group = Group::findOrFail($id);
72
        $name = $group->name;
73
        $group->name = $request->name;
74
        $group->update();
75
        return redirect()->back()->with(
76
            'status',
77
            trans(
78
                'groups.message.update',
79
                [
80
                    'old'   => $name,
81
                    'group' => $group->name,
82
                ]
83
            )
84
        );
85
    }
86
87
    public function removeGroup($id)
88
    {
89
        $group = Group::findOrFail($id);
90
        $name = $group->name;
91
        $group->delete();
92
        return redirect()->back()->with(
93
            'status',
94
            trans('groups.message.delete', ['group' => $name])
95
        );
96
    }
97
98 View Code Duplication
    public function purgeAccounts($id)
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...
99
    {
100
        $group = Group::findOrFail($id);
101
        $accounts = Group::Find($id)->accounts()->where('status', Account::ACCOUNT_DISABLE);
102
        if ($accounts->exists()) {
103
            $accounts->delete();
104
        }
105
        return redirect()->back()->with(
106
            'status',
107
            trans('groups.message.drop', ['group' => $group->name])
108
        );
109
    }
110
111 View Code Duplication
    public function disableAccounts($id)
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...
112
    {
113
        $group = Group::findOrFail($id);
114
        $accounts = Group::Find($id)->accounts()->where('status', Account::ACCOUNT_ENABLE)->get();
115
        foreach ($accounts as $account) {
116
            $account->disable();
117
        }
118
        return redirect()->back()->with(
119
            'status',
120
            trans('groups.message.disable', ['group' => $group->name])
121
        );
122
    }
123
}
124