Completed
Push — master ( 957e25...61e39d )
by Marcel
02:22
created

src/Rules/Voucher.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace BeyondCode\Vouchers\Rules;
4
5
use Vouchers;
6
use Illuminate\Contracts\Validation\Rule;
7
use BeyondCode\Vouchers\Exceptions\VoucherExpired;
8
use BeyondCode\Vouchers\Exceptions\VoucherIsInvalid;
9
use BeyondCode\Vouchers\Exceptions\VoucherAlreadyRedeemed;
10
11
class Voucher implements Rule
12
{
13
    protected $isInvalid = false;
14
    protected $isExpired = false;
15
    protected $wasRedeemd = false;
16
17
    /**
18
     * Determine if the validation rule passes.
19
     *
20
     * @param  string $attribute
21
     * @param  mixed $value
22
     * @return bool
23
     */
24
    public function passes($attribute, $value)
25
    {
26
        try {
27
            $voucher = Vouchers::check($value);
28
29
            // Check if the voucher was already redeemed
30
            if (auth()->check() && $voucher->users()->wherePivot('user_id', auth()->id())->exists()) {
0 ignored issues
show
The method check does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\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...
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\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...
31
                throw VoucherAlreadyRedeemed::create($voucher);
32
            }
33
        } catch (VoucherIsInvalid $exception) {
34
            $this->isInvalid = true;
35
            return false;
36
        } catch (VoucherExpired $exception) {
37
            $this->isExpired = true;
38
            return false;
39
        } catch (VoucherAlreadyRedeemed $exception) {
40
            $this->wasRedeemd = true;
41
            return false;
42
        }
43
44
        return true;
45
    }
46
47
    /**
48
     * Get the validation error message.
49
     *
50
     * @return string
51
     */
52
    public function message()
53
    {
54
        if ($this->wasRedeemd) {
55
            return trans('vouchers::validation.code_redeemed');
56
        }
57
        if ($this->isExpired) {
58
            return trans('vouchers::validation.code_expired');
59
        }
60
        return trans('vouchers::validation.code_invalid');
61
    }
62
}