Completed
Push — master ( ac6145...bdf07d )
by Şəhriyar
14:49
created

PasswordController::postEmail()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 8
Bugs 1 Features 7
Metric Value
c 8
b 1
f 7
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4286
cc 3
eloc 5
nc 2
nop 2
crap 3
1
<?php namespace App\Http\Controllers\Users;
2
3
use App\Contracts\Registrar;
4
use App\Exceptions\Users\TokenNotValidException;
5
use App\Http\Controllers\Controller;
6
use Illuminate\Contracts\Auth\PasswordBroker;
7
use Illuminate\Http\Request;
8
9
class PasswordController extends Controller
10
{
11
    /**
12
     * Create a new password controller instance.
13
     */
14 16
    public function __construct()
15
    {
16 16
        $this->middleware('guest');
17 16
    }
18
19
    /**
20
     * Display the form to request a password reset link.
21
     *
22
     * @param \Illuminate\Http\Request $request
23
     *
24
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View|\Illuminate\Contracts\View\Factory
25
     */
26 5
    public function getEmail(Request $request)
27
    {
28 5
        if ($request->ajax() || $request->wantsJson()) {
29
            return response()->json([]);
30
        }
31
32 5
        return view('password/email');
33
    }
34
35
    /**
36
     * Send a password reset link to the given email's owner, via email.
37
     *
38
     * @param \Illuminate\Http\Request $request
39
     * @param \App\Contracts\Registrar $registrar
40
     *
41
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
42
     */
43 4
    public function postEmail(Request $request, Registrar $registrar)
44
    {
45 4
        $registrar->sendResetPasswordLinkViaEmail();
46
47 2
        if ($request->ajax() || $request->wantsJson()) {
48 1
            return response()->json(['message' => trans('passwords.sent')]);
49
        }
50
51 1
        return redirect()->back()->with('message', trans('passwords.sent'));
52
    }
53
54
    /**
55
     * Display the password reset view for the given token.
56
     *
57
     * @param \Illuminate\Http\Request $request
58
     * @param string                   $token
59
     *
60
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View|\Illuminate\Contracts\View\Factory
61
     */
62 7
    public function getReset(Request $request, $token = null)
63
    {
64 7
        if (is_null($token)) {
65 1
            if ($request->ajax() || $request->wantsJson()) {
66
                throw new TokenNotValidException();
67
            }
68
69 1
            return view('password/reset')->withErrors(['token' => trans(PasswordBroker::INVALID_TOKEN)]);
0 ignored issues
show
Bug introduced by
The method withErrors 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...
70
        }
71
72 6
        if ($request->ajax() || $request->wantsJson()) {
73
            return response()->json(['token' => $token]);
74
        }
75
76 6
        return view('password/reset')->with('token', $token);
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...
77
    }
78
79
    /**
80
     * Reset the password through password-reset-token and email provided.
81
     *
82
     * @param \Illuminate\Http\Request $request
83
     * @param \App\Contracts\Registrar $registrar
84
     *
85
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
86
     */
87 8
    public function postReset(Request $request, Registrar $registrar)
88
    {
89 8
        $registrar->resetPassword();
90
91 2
        if ($request->ajax() || $request->wantsJson()) {
92 1
            return response()->json(['message' => trans('passwords.reset')]);
93
        }
94
95 1
        return redirect($this->redirectPath())->with('message', trans('passwords.reset'));
96
    }
97
98
    /**
99
     * Get the post-register/-login redirect path.
100
     *
101
     * @return string
102
     */
103 1
    private function redirectPath()
104
    {
105 1
        if (isset($this->redirectPath)) {
106
            return $this->redirectPath;
0 ignored issues
show
Bug introduced by
The property redirectPath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
107
        }
108
109 1
        return isset($this->redirectTo) ? $this->redirectTo : '/login';
0 ignored issues
show
Bug introduced by
The property redirectTo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
110
    }
111
}
112