Completed
Push — master ( 02710e...15b052 )
by Freek
06:30
created

Delimited   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 91.53%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 144
ccs 54
cts 59
cp 0.9153
rs 10
c 0
b 0
f 0

10 Methods

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