Completed
Pull Request — master (#8)
by Brandon
04:08
created

UserController::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Http\Request;
7
use App\Http\Controllers\Controller;
8
use App\DataTables\UsersDataTable;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Validation\Rule;
11
use App\User;
12
13
class UserController extends Controller
14
{
15
    /**
16
     * Create a new controller instance.
17
     *
18
     * @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...
19
     */
20
    public function __construct()
21
    {
22
        $this->middleware('auth');
23
        // TODO: Setup logging
24
        // $this->middleware('log')->only('index');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
25
    }
26
27
    /**
28
     * Display index page and process dataTable ajax request.
29
     *
30
     * @param \App\DataTables\UsersDataTable $dataTable
31
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
32
     */
33
    public function index(UsersDataTable $dataTable)
34
    {
35
        return $dataTable->render('user.index');
36
    }
37
38
    /**
39
     * Show create user page.
40
     *
41
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
42
     */
43
    public function create()
44
    {
45
        return view('user.create');
46
    }
47
48
    /**
49
     * Store a new user.
50
     *
51
     * @param  Request  $request
52
     * @return Response
53
     */
54
    public function store(Request $request)
55
    {
56
        $validator = Validator::make($request->all(), [
57
            'name' => 'required|string|max:255',
58
            'email' => 'required|string|email|max:255|unique:users',
59
            'password' => 'required|string|min:6|confirmed',
60
            'role' => 'required|integer|max:3',
61
            'phone' => 'numeric|phone|nullable',
62
        ]);
63
64
        if ($validator->fails()) {
65
            return redirect('user/create')->withErrors($validator)->withInput();
66
        }
67
68
        $user = new User;
69
70
        $user->name = $request->input('name');
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\User>. 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...
71
        $user->email = $request->input('email');
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<App\User>. 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...
72
        $user->password = bcrypt($request->input('password'));
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<App\User>. 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...
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, bcrypt() 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...
73
        $user->role = $request->input('role');
0 ignored issues
show
Documentation introduced by
The property role does not exist on object<App\User>. 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...
74
        $user->phone = $request->input('phone');
0 ignored issues
show
Documentation introduced by
The property phone does not exist on object<App\User>. 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...
75
76
        $user->save();
77
78
        return redirect('user');
79
    }
80
81
    /**
82
     * Show the given user.
83
     *
84
     * @param  Request  $request
85
     * @param  string  $id
86
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
87
     */
88 View Code Duplication
    public function show(Request $request, $id)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
89
    {
90
        $user = User::findOrFail($id);
91
        $user->password = "";
92
93
        return view('user.show', ['user' => $user]);
94
    }
95
96
    /**
97
     * Edit the given user.
98
     *
99
     * @param  Request  $request
100
     * @param  string  $id
101
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
102
     */
103 View Code Duplication
    public function edit(Request $request, $id)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
104
    {
105
        $user = User::findOrFail($id);
106
        $user->password = "";
107
108
        return view('user.edit', ['user' => $user]);
109
    }
110
111
    /**
112
     * Update the given user.
113
     *
114
     * @param  Request  $request
115
     * @param  string  $id
116
     * @return Response
117
     */
118
    public function update(Request $request, $id)
119
    {
120
        // TODO: Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need
121
        // to add a hidden  _method field to spoof these HTTP verbs. The
122
        // method_field helper can create this field for you:
123
        // {{ method_field('PUT') }}
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
124
125
        $user = User::findOrFail($id);
126
127
        $validator = Validator::make($request->all(), [
128
            'name' => 'required|string|max:255',
129
            'email' => 'required|string|email|max:255|unique:users,email,'.$user->id,
130
            'role' => 'required|integer|max:3',
131
            'phone' => 'numeric|phone|nullable',
132
        ]);
133
134
        if ($validator->fails()) {
135
            return redirect('user/'.$id.'/edit')->withErrors($validator)->withInput();
136
        }
137
138
        $user->name = $request->input('name');
139
        $user->email = $request->input('email');
140
        $user->role = $request->input('role');
141
        $user->phone = $request->input('phone');
142
143
        $user->save();
144
145
        return redirect('user');
146
    }
147
148
    /**
149
     * Deletes a user.
150
     *
151
     * @param  Request  $request
152
     * @param  string  $id
153
     * @return Response
154
     */
155
    public function destroy(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
    {
157
        $user = User::findOrFail($id);
158
159
        if ($user->trashed()) {
160
            // if the user was already deleted then permananetly delete it
161
            User::destroy($id);
162
        } else {
163
            // soft delete the user the first time
164
            $user->delete();
165
        }
166
167
        return redirect('user');
168
    }
169
    
170
    /**
171
     * Confirms deletion a user.
172
     *
173
     * @param  Request  $request
174
     * @param  string  $id
175
     * @return Response
176
     */
177 View Code Duplication
    public function remove(Request $request, $id)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
178
    {
179
        $user = User::findOrFail($id);
180
        $user->password = "";
181
        
182
        return view('user.remove', ['user' => $user]);
183
    }
184
}
185