UserRequest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B rules() 0 33 7
A authorize() 0 4 1
1
<?php
2
3
namespace Finder\Http\Requests\Admin;
4
5
use Population\Models\User;
6
use Illuminate\Foundation\Http\FormRequest;
7
8
class UserRequest extends FormRequest
9
{
10
11
    /**
12
     * Get the validation rules that apply to the request.
13
     *
14
     * @return array
15
     */
16
    public function rules()
17
    {
18
        if($this->segment(3)!="") {
19
            $user = User::find($this->segment(3));
20
        }
21
22
        switch($this->method())
23
        {
24
        case 'GET':
25
        case 'DELETE':
26
        {
27
              return [];
28
        }
29
        case 'POST':
30
        {
31
            return [
32
            'name' => 'required|min:3',
33
            'email' => 'required|email|unique:users,email',
34
            'password' => 'required',
35
            ];
36
        }
37
        case 'PUT':
38
        case 'PATCH':
39
        {
40
            return [
41
                    'name' => 'required|min:3',
42
                    'email' => 'required|email|unique:users,email,'.$user->id,
0 ignored issues
show
Bug introduced by
The variable $user does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
43
                ];
44
        }
45
        default:
46
            break;
47
        }
48
    }
49
50
    /**
51
     * Determine if the user is authorized to make this request.
52
     *
53
     * @return bool
54
     */
55
    public function authorize()
56
    {
57
        return true;
58
    }
59
60
}
61