CountryCode   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 40
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A nullable() 0 6 1
A passes() 0 12 5
A message() 0 6 1
1
<?php
2
3
namespace Spatie\ValidationRules\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Support\Arr;
7
use League\ISO3166\ISO3166;
8
9
class CountryCode implements Rule
10
{
11
    /** @var bool */
12
    protected $required;
13
14
    /** @var string */
15
    protected $attribute;
16 24
17
    public function __construct(bool $required = true)
18 24
    {
19 24
        $this->required = $required;
20
    }
21 6
22
    public function nullable(): self
23 6
    {
24
        $this->required = false;
25 6
26
        return $this;
27
    }
28 24
29
    public function passes($attribute, $value): bool
30 24
    {
31
        $this->attribute = $attribute;
32 24
33 6
        if (! $this->required && ($value === '0' || $value === 0 || $value === null)) {
34
            return true;
35
        }
36 24
37
        $countries = Arr::pluck((new ISO3166())->all(), ISO3166::KEY_ALPHA2);
0 ignored issues
show
Documentation introduced by
(new \League\ISO3166\ISO3166())->all() is of type array<integer,array>, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38 24
39
        return in_array($value, $countries, true);
40
    }
41 12
42
    public function message(): string
43 12
    {
44 12
        return __('validationRules::messages.country_code', [
45
            'attribute' => $this->attribute,
46
        ]);
47
    }
48
}
49