Completed
Push — master ( b3f4fd...a98a34 )
by Freek
11:07
created

CommaSeparatedEmails::max()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\ValidationRules\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Support\Facades\Validator;
7
use Illuminate\Support\Str;
8
9
class CommaSeparatedEmails implements Rule
10
{
11
    /** @var int|null */
12
    protected $minimum;
13
14
    /** @var int|null */
15
    protected $maximum;
16
17
    protected $message = '';
18
19
    public function passes($attribute, $value)
20
    {
21
        [$validEmails, $invalidEmails] = collect(explode(',', $value))
0 ignored issues
show
Bug introduced by freek
The variable $validEmails does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by freek
The variable $invalidEmails does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
22
            ->map(function (string $rawEmail) {
23
                return trim($rawEmail);
24
            })
25
            ->partition(function (string $email) {
26
                return $this->isValidEmail($email);
27
            });
28
29
        if ($invalidEmails->count() === 1) {
30
            $this->message = __('validation.email', ['attribute' => $invalidEmails->first()]);
0 ignored issues
show
Documentation Bug introduced by freek
It seems like __('validation.email', a...nvalidEmails->first())) can also be of type array. However, the property $message is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
31
32
            return false;
33
        }
34
35
        if ($invalidEmails->count() > 1) {
36
            $this->message = __('validation.emails', ['attribute' => $invalidEmails->implode(',')]);
0 ignored issues
show
Documentation Bug introduced by freek
It seems like __('validation.emails', ...dEmails->implode(','))) can also be of type array. However, the property $message is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
37
38
            return false;
39
        }
40
41
        if ($validEmails->unique()->count() !== $validEmails->count()) {
42
            $this->message = __('validation.unique_emails');
0 ignored issues
show
Documentation Bug introduced by freek
It seems like __('validation.unique_emails') can also be of type array. However, the property $message is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
43
            return false;
44
        }
45
46
47
48
        if (! is_null($this->minimum)) {
49
            if ($validEmails->count() < $this->minimum) {
50
                $this->message = __('validation.minimum_emails', [
0 ignored issues
show
Documentation Bug introduced by freek
It seems like __('validation.minimum_e...ess', $this->minimum))) can also be of type array. However, the property $message is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
51
                    'actualCount' => $invalidEmails->implode(','),
52
                    'expectedMinimum' => $this->minimum,
53
                    'emailword' => Str::plural('e-mail address', $this->minimum)
54
                ]);
55
56
                return false;
57
            }
58
        }
59
60
        if (! is_null($this->maximum)) {
61
            if ($validEmails->count() > $this->maximum) {
62
                $this->message = __('validation.maximum_emails', [
0 ignored issues
show
Documentation Bug introduced by freek
It seems like __('validation.maximum_e...ess', $this->maximum))) can also be of type array. However, the property $message is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
63
                    'actualCount' => $invalidEmails->implode(','),
64
                    'expectedMaximum' => $this->maximum,
65
                    'emailword' => Str::plural('e-mail address', $this->maximum)
66
                ]);
67
68
                return false;
69
            }
70
        }
71
72
        return true;
73
    }
74
75
    public function message()
76
    {
77
        return $this->message;
78
    }
79
80
    public function min(int $minimum)
81
    {
82
        $this->minimum = $minimum;
83
84
        return $this;
85
    }
86
87
    public function max(int $maximum)
88
    {
89
        $this->maximum = $maximum;
90
91
        return $this;
92
    }
93
94
    protected function isValidEmail(string $email): bool
95
    {
96
        return Validator::make(['email' => $email], ['email' => 'email'])->passes();
97
    }
98
}
99
100