Completed
Push — master ( 5c8721...fc356f )
by Antonio Carlos
02:14
created

Response::sessionForget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace PragmaRX\Google2FALaravel\Support;
4
5
use Illuminate\Http\JsonResponse as IlluminateJsonResponse;
6
use Illuminate\Http\Response as IlluminateHtmlResponse;
7
use PragmaRX\Google2FALaravel\Events\OneTimePasswordRequested;
8
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
9
10
trait Response
11
{
12
    /**
13
     * Make a JSON response.
14
     *
15
     * @param $statusCode
16
     *
17
     * @return JsonResponse
18
     */
19
    protected function makeJsonResponse($statusCode)
20
    {
21
        return new IlluminateJsonResponse(
22
            $this->getErrorBagForStatusCode($statusCode),
0 ignored issues
show
Bug introduced by
It seems like getErrorBagForStatusCode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
23
            $statusCode
24
        );
25
    }
26
27
    /**
28
     * Make the status code, to respond accordingly.
29
     *
30
     * @return int
31
     */
32
    protected function makeStatusCode()
33
    {
34
        return
35
            $this->inputHasOneTimePassword() && !$this->checkOTP()
0 ignored issues
show
Bug introduced by
It seems like inputHasOneTimePassword() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like checkOTP() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
                ? SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY
37
                : SymfonyResponse::HTTP_OK;
38
    }
39
40
    /**
41
     * Make a web response.
42
     *
43
     * @param $statusCode
44
     *
45
     * @return \Illuminate\Http\Response
46
     */
47
    protected function makeHtmlResponse($statusCode)
48
    {
49
        if ($statusCode !== SymfonyResponse::HTTP_OK) {
50
            $this->getView()->withErrors($this->getErrorBagForStatusCode($statusCode));
0 ignored issues
show
Bug introduced by
It seems like getErrorBagForStatusCode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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...
51
        }
52
53
        return new IlluminateHtmlResponse($this->getView(), $statusCode);
54
    }
55
56
    /**
57
     * Create a response to request the OTP.
58
     *
59
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
60
     */
61
    public function makeRequestOneTimePasswordResponse()
62
    {
63
        event(new OneTimePasswordRequested($this->getUser()));
0 ignored issues
show
Bug introduced by
It seems like getUser() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64
65
        return
66
            $this->getRequest()->expectsJson()
0 ignored issues
show
Bug introduced by
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
67
                ? $this->makeJsonResponse($this->makeStatusCode())
68
                : $this->makeHtmlResponse($this->makeStatusCode());
69
    }
70
71
    /**
72
     * Get the OTP view.
73
     *
74
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
75
     */
76
    private function getView()
77
    {
78
        return view($this->config('view'));
0 ignored issues
show
Bug introduced by
It seems like config() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
79
    }
80
}
81