Completed
Pull Request — master (#107)
by Glenn
05:30
created

StaffController::destroy_permission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace app\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Http\Requests;
7
use App\User;
8
use App\Countries;
9
use App\Teams;
10
use App\Role;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Input;
13
use Illuminate\Support\Facades\Mail;
14
use Illuminate\Support\Facades\Redirect;
15
use Intervention\Image\Facades\Image;
16
use App\Permission;
17
use Bouncer;
18
use App\Http\Controllers\Controller;
19
20
class StaffController extends Controller
21
{
22
    public function __construct()
23
    {
24
        $this->middleware('auth');
25
    }
26
27
    /**
28
     * Display all users.
29
     *
30
     * @return mixed
31
     */
32
    public function index()
33
    {
34
        $data['users'] = User::orderBy('fname', 'asc')->paginate(10);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
35
36
        return view('staff/users', $data);
37
    }
38
39
    /**
40
     * Show the form for creating a new employee.
41
     *
42
     * @return mixed
43
     */
44
    public function create()
45
    {
46
        $data['countries'] = Countries::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
47
48
        return view('staff/create_user', $data);
49
    }
50
51
    /**
52
     * Store a newly created employee in storage.
53
     *
54
     * @param Requests\StaffValidator|Request $request
55
     *
56
     * @return mixed
57
     */
58
    public function store(Requests\StaffValidator $request)
59
    {
60
        $user = new User();
61
        $user->fname = $request->get('fname');
0 ignored issues
show
Documentation introduced by
The property fname 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...
62
        $user->name = $request->get('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...
63
        $user->address = $request->get('address');
0 ignored issues
show
Documentation introduced by
The property address 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...
64
        $user->postal_code = $request->get('postal_code');
0 ignored issues
show
Documentation introduced by
The property postal_code 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...
65
        $user->city = $request->get('city');
0 ignored issues
show
Documentation introduced by
The property city 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...
66
        $user->email = $request->get('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...
67
        $user->password = bcrypt($request->get('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...
68
        $user->save();
69
70
        $mailbox = env('MAIL_USERNAME');
71
        $mail_password = $request->get('password');
72
        session()->flash('message', 'New employee has been added to the application');
73
74
        $injectionData = ['user' => $user, 'password' => $mail_password];
75
76
        Mail::send('emails.new_user', $injectionData, function ($m) use ($user, $mailbox) {
77
            $m->from($mailbox);
78
            $m->to($user->email)->subject('Your user credentials!');
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<App\User>. 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...
79
        });
80
81
        return redirect('staff');
82
    }
83
84
    /**
85
     * Update a user.
86
     *
87
     * @param Int,    $id
0 ignored issues
show
Documentation introduced by
The doc-type Int, could not be parsed: Expected "|" or "end of type", but got "," at position 3. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
88
     * @param Request $request
89
     *
90
     * @return mixed
91
     */
92
    public function updateUser($id, Request $request)
93
    {
94
        $user = User::find($id);
95
        $user->fname = $request->get('fname');
96
        $user->name = $request->get('name');
97
        $user->address = $request->get('address');
98
        $user->postal_code = $request->get('postal_code');
99
        $user->city = $request->get('city');
100
        $user->email = $request->get('email');
101
        $user->assignRole($request->get('user_type'));
102
        $user->update();
103
104
        $user = User::find($id);
105
        Bouncer::assign($request->get('user_typ'))->to($user);
106
107
        session()->flash('message', 'User details have been updated');
108
109
        return \Redirect::back();
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115
    public function policies()
116
    {
117
        $data['roles'] = Role::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
118
119
        return view('staff/roles', $data);
120
    }
121
122
    /**
123
     * @return mixed
124
     */
125
    public function addpolicies()
126
    {
127
        $data['permissions'] = Permission::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
128
129
        return view('staff/create_role', $data);
130
    }
131
132
    /**
133
     * @param Request $request
134
     *
135
     * @return string
136
     */
137
    public function addRole(Request $request)
138
    {
139
        $role = Role::create(['name' => $request->get('role_name')]);
140
        foreach ($request->get('permissions') as $permission) {
141
            $role->givePermissionTo($permission);
0 ignored issues
show
Documentation Bug introduced by
The method givePermissionTo does not exist on object<App\Role>? 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...
142
        }
143
        if ($assign_role) {
0 ignored issues
show
Bug introduced by
The variable $assign_role does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
144
            session()->flash('message', 'New user role has been created');
145
146
            return redirect('staff/policies');
147
        } else {
148
            return 'Mislukt';
149
        }
150
    }
151
152
    /**
153
     * @param $id
154
     *
155
     * @return mixed
156
     */
157
    public function editpolicies($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
158
    {
159
        $data['permissions'] = Permission::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
160
161
        return view('staff/edit_role', $data);
162
    }
163
164
    /**
165
     * Remove the user role.
166
     *
167
     * @param int, $id
0 ignored issues
show
Documentation introduced by
The doc-type int, could not be parsed: Expected "|" or "end of type", but got "," at position 3. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
168
     *
169
     * @return redirect
170
     */
171
    public function destroyRole($id)
172
    {
173
        $role = Role::find($id);
174
        $role->delete();
175
        session()->flash('message', 'User role has been removed from the database');
176
177
        return redirect('staff/policies');
178
    }
179
180
    /**
181
     * Show all permission.
182
     */
183
    public function permissions()
184
    {
185
        $data['permissions'] = Permission::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
186
187
        return view('staff/permissions', $data);
188
    }
189
190
    /**
191
     * Show the form to create a new permission.
192
     */
193
    public function createPermission()
194
    {
195
        return view('staff/create_permission');
196
    }
197
198
    /**
199
     * Show the form to edit a permission.
200
     */
201
    public function EditPermission(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...
202
    {
203
        $data['permission'] = Permission::find($id);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
204
        return view('staff/edit_permission', $data);
205
    }
206
207
    /**
208
     * Save the new permission.
209
     *
210
     * @param Request $request
211
     *
212
     * @return redirect
213
     */
214
    public function savePermission(Request $request)
215
    {
216
        Permission::create(['name' => $request->get('permission_name')]);
217
        session()->flash('message', 'The new permission has been added to the database');
218
219
        return redirect('staff/permissions');
220
    }
221
222
    public function destroyPermission($id)
223
    {
224
        $permission = Permission::find($id);
225
        $permission->delete();
226
227
        session()->flash('message', 'Permission has been removed from the database');
228
229
        return redirect('staff/permissions');
230
    }
231
232
    /**
233
     * Display the specified resource.
234
     *
235
     * @param int $id
236
     *
237
     * @return \Illuminate\Http\Response
238
     */
239
    public function show($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
240
    {
241
        //
242
    }
243
244
    /**
245
     * Show the form for editing the specified resource.
246
     *
247
     * @param $id
248
     *
249
     * @return
250
     */
251
    public function edit($id)
252
    {
253
        $data['user']  = User::findOrFail($id);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
254
        $data['teams'] = Teams::all();
255
        $data['countries'] = Countries::all();
256
        $data['roles'] = Role::all();
257
258
        return view('staff/edit_user', $data);
259
    }
260
261
    /**
262
     * @return mixed
263
     */
264
    public function profile()
265
    {
266
        $data['countries'] = Countries::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
267
268
        return view('staff/profile', $data);
269
    }
270
271
    /**
272
     * change a password.
273
     */
274
    public function chPass()
275
    {
276
    }
277
278
    /**
279
     * Update the specified resource in storage.
280
     *
281
     * @param Requests\accountManagementValidator|Request $request
282
     * @return \Illuminate\Http\Response
283
     */
284
    public function update(Requests\accountManagementValidator $request)
285
    {
286
        $user = User::findOrFail(auth()->user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
287
        $user->fname = $request->get('email');
288
        $user->name = $request->get('name');
289
        $user->email = $request->get('email');
290
        $user->address = $request->get('address');
291
        $user->email = $request->get('email');
292
293
        if (Input::file()) {
294
            $image = Input::file('avatar');
295
            $filename = time().'.'.$image->getClientOriginalExtension();
296
            $path = public_path('profilepics/'.$filename);
297
298
            Image::make($image->getRealPath())->resize(200, 200)->save($path);
299
            $user->image = $filename;
300
        }
301
302
        $user->save();
303
304
        return redirect()->back();
305
    }
306
307
    /**
308
     * Remove the specified employee from the database.
309
     *
310
     * @param int $id
311
     *
312
     * @return \Illuminate\Http\Response
313
     */
314
    public function destroy($id)
315
    {
316
        if (!Auth::user()->is('Administrator')) {
317
            return Redirect::back();
318
        }
319
320
        User::Destroy($id);
321
        session()->flash('message', 'User has been removed from the database');
322
323
        return redirect('staff');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect('staff'); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by app\Http\Controllers\StaffController::destroy of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
324
    }
325
}
326