Completed
Push — master ( f92151...0a9cfa )
by Freek
10:51
created

Delimited::validationMessageWord()   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 Delimited implements Rule
10
{
11
    /** @var string|array|\Illuminate\Contracts\Validation\Rule */
12
    protected $rule;
13
14
    /** @var int|null */
15
    protected $minimum;
16
17
    /** @var int|null */
18
    protected $maximum;
19
20
    /** @var bool */
21
    protected $allowDuplicates = false;
22
23
    protected $message = '';
24
25
    protected $separatedBy = ',';
26
27
    /** @var bool */
28
    protected $trimItems = true;
29
30
    /** @var string */
31
    protected $validationMessageWord = 'item';
32
33
    public function __construct($rule)
34
    {
35
        $this->rule = $rule;
36
    }
37
38
    public function min(int $minimum)
39
    {
40
        $this->minimum = $minimum;
41
42
        return $this;
43
    }
44
45
    public function max(int $maximum)
46
    {
47
        $this->maximum = $maximum;
48
49
        return $this;
50
    }
51
52
    public function allowDuplicates(bool $allowed = true)
53
    {
54
        $this->allowDuplicates = $allowed;
55
56
        return $this;
57
    }
58
59
    public function separatedBy(string $separator)
60
    {
61
        $this->separatedBy = $separator;
62
63
        return $this;
64
    }
65
66
    public function doNotTrimItems()
67
    {
68
        $this->trimItems = false;
69
70
        return true;
71
    }
72
73
    public function validationMessageWord(string $word)
74
    {
75
        $this->validationMessageWord = $word;
76
77
        return $this;
78
    }
79
80
    public function passes($attribute, $value)
81
    {
82
        $items = collect(explode($this->separatedBy, $value));
83
84
        if (!is_null($this->minimum)) {
85
            if ($items->count() < $this->minimum) {
86
                $this->message = __('validation.delimited.min', [
0 ignored issues
show
Documentation Bug introduced by
It seems like __('validation.delimited...ord, $items->count()))) 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...
87
                    'minimum' => $this->minimum,
88
                    'actual' => $items->count(),
89
                    'item' => Str::plural($this->validationMessageWord, $items->count())
90
                ]);
91
92
                return false;
93
            }
94
        }
95
96
        if (!is_null($this->maximum)) {
97
            if ($items->count() > $this->maximum) {
98
                $this->message = __('validation.delimited.max', [
0 ignored issues
show
Documentation Bug introduced by
It seems like __('validation.delimited...ord, $items->count()))) 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...
99
                    'maximum' => $this->maximum,
100
                    'actual' => $items->count(),
101
                    'item' => Str::plural($this->validationMessageWord, $items->count())
102
                ]);
103
104
                return false;
105
            }
106
        }
107
108
        if ($this->trimItems) {
109
            $items = $items->map(function ($item) {
110
                return trim($item);
111
            });
112
        }
113
114
        foreach ($items as $item) {
115
            [$isValid, $validationMessage] = $this->validate($attribute, $item);
0 ignored issues
show
Bug introduced by
The variable $isValid 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
The variable $validationMessage 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...
116
117
            if (! $isValid) {
118
                $this->message = $validationMessage;
119
120
                return false;
121
            }
122
        }
123
124
        if (!$this->allowDuplicates) {
125
            if ($items->unique()->count() !== $items->count()) {
126
                $this->message = __('validation.delimited.unique');
0 ignored issues
show
Documentation Bug introduced by
It seems like __('validation.delimited.unique') 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...
127
                return false;
128
            }
129
        }
130
131
        return true;
132
    }
133
134
    public function message()
135
    {
136
        return $this->message;
137
    }
138
139
    protected function validate($attribute, $item): array
140
    {
141
        $validator = Validator::make([$attribute => $item], [$attribute => $this->rule]);
142
143
        return [
144
            $validator->passes(),
145
            $validator->getMessageBag()->first($attribute),
146
        ];
147
    }
148
}
149
150