ResetPasswordController::sendResetResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 6
loc 6
rs 9.4285
1
<?php
2
3
namespace App\Http\Controllers\Front\Auth;
4
5
use Auth;
6
use Password;
7
use Illuminate\Http\Request;
8
use App\Services\Auth\Front\User;
9
use App\Http\Controllers\Controller;
10
use Illuminate\Foundation\Auth\ResetsPasswords;
11
12 View Code Duplication
class ResetPasswordController extends Controller
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...
13
{
14
    protected $redirectTo = '/';
15
16
    use ResetsPasswords;
17
18
    /**
19
     * Create a new controller instance.
20
     */
21
    public function __construct()
22
    {
23
        $this->middleware('guest');
24
    }
25
26
    /**
27
     * Display the password reset view for the given token.
28
     *
29
     * If no token is present, display the link request form.
30
     *
31
     * @param \Illuminate\Http\Request $request
32
     * @param string|null              $token
33
     *
34
     * @return \Illuminate\Http\Response
35
     */
36
    public function showResetForm(Request $request, $token = null)
37
    {
38
        if (! $user = User::findByToken($token)) {
39
            flash()->error(trans('passwordReset.invalidToken'));
0 ignored issues
show
Bug introduced by
It seems like trans('passwordReset.invalidToken') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Laracasts\Flash\FlashNotifier::error() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
40
41
            return redirect()->to(login_url());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect()->to(login_url()); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by App\Http\Controllers\Fro...ntroller::showResetForm 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...
42
        }
43
44
        return view('front.auth.resetPassword')->with(
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
45
            ['token' => $token, 'email' => $request->email, 'user' => $user]
46
        );
47
    }
48
49
    /**
50
     * Get the response for a successful password reset.
51
     *
52
     * @param string $response
53
     *
54
     * @return \Illuminate\Http\Response
55
     */
56
    protected function sendResetResponse($response)
57
    {
58
        flash()->info(trans($response));
0 ignored issues
show
Bug introduced by
It seems like trans($response) targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Laracasts\Flash\FlashNotifier::info() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
59
60
        return redirect($this->redirectPath());
61
    }
62
63
    protected function guard()
64
    {
65
        return Auth::guard('front');
66
    }
67
68
    /**
69
     * Get the broker to be used during password reset.
70
     *
71
     * @return \Illuminate\Contracts\Auth\PasswordBroker
72
     */
73
    public function broker()
74
    {
75
        return Password::broker('front');
76
    }
77
}
78