RoleRequest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 37
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 4 4 1
A rules() 19 19 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mamikon\RoleManager\Requests;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use Illuminate\Support\Facades\Request;
7
use Mamikon\RoleManager\Models\Roles;
8
9
/**
10
 * Role Request Validation
11
 *
12
 * @category Laravel_Package
13
 * @package  Mamikon\RoleManager
14
 * @author   Mamikon Arakelyan <[email protected]>
15
 * @license  https://github.com/mamikon/role-manager/blob/master/LICENSE.md MIT
16
 * @link     https://github.com/mamikon/role-manager
17
 */
18 View Code Duplication
class RoleRequest extends FormRequest
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    /**
21
     * Determine if the user is authorized to make this request.
22
     *
23
     * @return bool
24
     */
25
    public function authorize()
26
    {
27
        return true;
28
    }
29
30
    /**
31
     * Get the validation rules that apply to the request.
32
     *
33
     * @return array
34
     */
35
    public function rules()
36
    {
37
        $return = [
38
            'description' => 'required|max:255',
39
            'name' => 'required|max:255|unique:' .
40
                config('roleManager.rolesTable') . ',name',
41
            'permission.*' => 'exists:' .
42
                config('roleManager.permissionsTable') . ',id'
43
44
        ];
45
46
        if (Request::isMethod('put')) {
47
            $role = Roles::where('id', Request::route('role'))->firstOrFail();
48
            if ($role->name == Request::input('name')) {
49
                $return['name'] = 'required|max:255';
50
            }
51
        }
52
        return $return;
53
    }
54
}
55