Completed
Pull Request — master (#107)
by Glenn
11:49 queued 05:48
created

StaffController   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 310
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Importance

Changes 6
Bugs 3 Features 4
Metric Value
wmc 25
c 6
b 3
f 4
lcom 0
cbo 12
dl 0
loc 310
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 6 1
A create() 0 6 1
B store() 0 25 1
A updateUser() 0 19 1
A policies() 0 6 1
A addpolicies() 0 6 1
A addRole() 0 14 3
A editpolicies() 0 6 1
A destroyRole() 0 8 1
A permissions() 0 6 1
A createPermission() 0 4 1
A EditPermission() 0 5 1
A savePermission() 0 7 1
A destroyPermission() 0 9 1
A show() 0 4 1
A edit() 0 10 1
A profile() 0 6 1
A chPass() 0 3 1
A update() 0 22 2
A destroy() 0 11 2
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\Departments;
10
use App\Teams;
11
use App\Role;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Input;
14
use Illuminate\Support\Facades\Mail;
15
use Illuminate\Support\Facades\Redirect;
16
use Intervention\Image\Facades\Image;
17
use App\Permission;
18
use Bouncer;
19
use App\Http\Controllers\Controller;
20
21
class StaffController extends Controller
22
{
23
    /**
24
     * StaffController constructor.
25
     */ 
26
    public function __construct()
27
    {
28
        $this->middleware('auth');
29
    }
30
31
    /**
32
     * Display all users.
33
     *
34
     * @return mixed
35
     */
36
    public function index()
37
    {
38
        $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...
39
40
        return view('staff/users', $data);
41
    }
42
43
    /**
44
     * Show the form for creating a new employee.
45
     *
46
     * @return mixed
47
     */
48
    public function create()
49
    {
50
        $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...
51
52
        return view('staff/create_user', $data);
53
    }
54
55
    /**
56
     * Store a newly created employee in storage.
57
     *
58
     * @param Requests\StaffValidator|Request $request
59
     *
60
     * @return mixed
61
     */
62
    public function store(Requests\StaffValidator $request)
63
    {
64
        $user = new User();
65
        $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...
66
        $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...
67
        $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...
68
        $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...
69
        $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...
70
        $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...
71
        $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...
72
        $user->save();
73
74
        $mailbox = env('MAIL_USERNAME');
75
        $mail_password = $request->get('password');
76
        session()->flash('message', 'New employee has been added to the application');
77
78
        $injectionData = ['user' => $user, 'password' => $mail_password];
79
80
        Mail::send('emails.new_user', $injectionData, function ($m) use ($user, $mailbox) {
81
            $m->from($mailbox);
82
            $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...
83
        });
84
85
        return redirect('staff');
86
    }
87
88
    /**
89
     * Update a user.
90
     *
91
     * @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...
92
     * @param Request $request
93
     *
94
     * @return mixed
95
     */
96
    public function updateUser($id, Request $request)
97
    {
98
        $user = User::find($id);
99
        $user->fname = $request->get('fname');
100
        $user->name = $request->get('name');
101
        $user->address = $request->get('address');
102
        $user->postal_code = $request->get('postal_code');
103
        $user->city = $request->get('city');
104
        $user->email = $request->get('email');
105
        $user->assignRole($request->get('user_type'));
106
        $user->update();
107
108
        $user = User::find($id);
109
        Bouncer::assign($request->get('user_typ'))->to($user);
110
111
        session()->flash('message', 'User details have been updated');
112
113
        return \Redirect::back();
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119
    public function policies()
120
    {
121
        $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...
122
123
        return view('staff/roles', $data);
124
    }
125
126
    /**
127
     * @return mixed
128
     */
129
    public function addpolicies()
130
    {
131
        $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...
132
133
        return view('staff/create_role', $data);
134
    }
135
136
    /**
137
     * @param Request $request
138
     *
139
     * @return string
140
     */
141
    public function addRole(Request $request)
142
    {
143
        $role = Role::create(['name' => $request->get('role_name')]);
144
        foreach ($request->get('permissions') as $permission) {
145
            $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...
146
        }
147
        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...
148
            session()->flash('message', 'New user role has been created');
149
150
            return redirect('staff/policies');
151
        } else {
152
            return 'Mislukt';
153
        }
154
    }
155
156
    /**
157
     * @param $id
158
     *
159
     * @return mixed
160
     */
161
    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...
162
    {
163
        $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...
164
165
        return view('staff/edit_role', $data);
166
    }
167
168
    /**
169
     * Remove the user role.
170
     *
171
     * @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...
172
     *
173
     * @return redirect
174
     */
175
    public function destroyRole($id)
176
    {
177
        $role = Role::find($id);
178
        $role->delete();
179
        session()->flash('message', 'User role has been removed from the database');
180
181
        return redirect('staff/policies');
182
    }
183
184
    /**
185
     * Show all permission.
186
     */
187
    public function permissions()
188
    {
189
        $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...
190
191
        return view('staff/permissions', $data);
192
    }
193
194
    /**
195
     * Show the form to create a new permission.
196
     */
197
    public function createPermission()
198
    {
199
        return view('staff/create_permission');
200
    }
201
202
    /**
203
     * Show the form to edit a permission.
204
     */
205
    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...
206
    {
207
        $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...
208
        return view('staff/edit_permission', $data);
209
    }
210
211
    /**
212
     * Save the new permission.
213
     *
214
     * @param Request $request
215
     *
216
     * @return redirect
217
     */
218
    public function savePermission(Request $request)
219
    {
220
        Permission::create(['name' => $request->get('permission_name')]);
221
        session()->flash('message', 'The new permission has been added to the database');
222
223
        return redirect('staff/permissions');
224
    }
225
226
    public function destroyPermission($id)
227
    {
228
        $permission = Permission::find($id);
229
        $permission->delete();
230
231
        session()->flash('message', 'Permission has been removed from the database');
232
233
        return redirect('staff/permissions');
234
    }
235
236
    /**
237
     * Display the specified resource.
238
     *
239
     * @param int $id
240
     *
241
     * @return \Illuminate\Http\Response
242
     */
243
    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...
244
    {
245
        //
246
    }
247
248
    /**
249
     * Show the form for editing the specified resource.
250
     *
251
     * @param $id
252
     *
253
     * @return
254
     */
255
    public function edit($id)
256
    {
257
        $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...
258
        $data['departments'] = Departments::all();
259
        $data['teams'] = Teams::all();
260
        $data['countries'] = Countries::all();
261
        $data['roles'] = Role::all();
262
263
        return view('staff/edit_user', $data);
264
    }
265
266
    /**
267
     * @return mixed
268
     */
269
    public function profile()
270
    {
271
        $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...
272
273
        return view('staff/profile', $data);
274
    }
275
276
    /**
277
     * change a password.
278
     */
279
    public function chPass()
280
    {
281
    }
282
283
    /**
284
     * Update the specified resource in storage.
285
     *
286
     * @param Requests\accountManagementValidator|Request $request
287
     * @return \Illuminate\Http\Response
288
     */
289
    public function update(Requests\accountManagementValidator $request)
290
    {
291
        $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...
292
        $user->fname = $request->get('email');
293
        $user->name = $request->get('name');
294
        $user->email = $request->get('email');
295
        $user->address = $request->get('address');
296
        $user->email = $request->get('email');
297
298
        if (Input::file()) {
299
            $image = Input::file('avatar');
300
            $filename = time().'.'.$image->getClientOriginalExtension();
301
            $path = public_path('profilepics/'.$filename);
302
303
            Image::make($image->getRealPath())->resize(200, 200)->save($path);
304
            $user->image = $filename;
305
        }
306
307
        $user->save();
308
309
        return redirect()->back();
310
    }
311
312
    /**
313
     * Remove the specified employee from the database.
314
     *
315
     * @param int $id
316
     *
317
     * @return \Illuminate\Http\Response
318
     */
319
    public function destroy($id)
320
    {
321
        if (!Auth::user()->is('Administrator')) {
322
            return Redirect::back();
323
        }
324
325
        User::Destroy($id);
326
        session()->flash('message', 'User has been removed from the database');
327
328
        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...
329
    }
330
}
331