Completed
Pull Request — master (#17)
by Fèvre
05:35 queued 02:55
created

ForgotPasswordController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Xetaravel\Http\Controllers\Auth;
3
4
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
5
use Xetaravel\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
8
class ForgotPasswordController extends Controller
9
{
10
    /*
11
    |--------------------------------------------------------------------------
12
    | Password Reset Controller
13
    |--------------------------------------------------------------------------
14
    |
15
    | This controller is responsible for handling password reset emails and
16
    | includes a trait which assists in sending these notifications from
17
    | your application to your users. Feel free to explore this trait.
18
    |
19
    */
20
    use SendsPasswordResetEmails;
21
22
    /**
23
     * Create a new controller instance.
24
     *
25
     * @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...
26
     */
27
    public function __construct()
28
    {
29
        $this->middleware('guest');
30
    }
31
32
    /**
33
     * Display the form to request a password reset link.
34
     *
35
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
36
     */
37
    public function showLinkRequestForm()
38
    {
39
        return view('Auth.passwords.email');
40
    }
41
42
    /**
43
     * Validate the email for the given request.
44
     *
45
     * @param \Illuminate\Http\Request  $request
46
     * @return void
47
     */
48
    protected function validateEmail(Request $request)
49
    {
50
        $rule = [
0 ignored issues
show
Unused Code introduced by
$rule is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
            'email' => 'required|email'
52
        ];
53
54
        // Bipass the captcha for the unit testing.
55
        if (App::environment() !== 'testing') {
56
            $rules = array_merge($rules, ['g-recaptcha-response' => 'required|recaptcha']);
0 ignored issues
show
Bug introduced by
The variable $rules seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
57
        }
58
59
        $this->validate($request, $rules);
0 ignored issues
show
Bug introduced by
The variable $rules 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...
60
    }
61
62
    /**
63
     * Get the response for a successful password reset link.
64
     *
65
     * @param string $response
66
     *
67
     * @return \Illuminate\Http\RedirectResponse
68
     */
69
    protected function sendResetLinkResponse($response)
0 ignored issues
show
Unused Code introduced by
The parameter $response 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...
70
    {
71
        return redirect()
72
            ->route('page.index')
73
            ->with('success', 'We have e-mailed your password reset link!');
74
    }
75
}
76