Passed
Push — 1.x ( ced68d...fcb885 )
by Milwad
01:14 queued 14s
created

ValidLandlineNumber   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 24
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A passes() 0 9 2
A __construct() 0 1 1
A message() 0 3 1
1
<?php
2
3
namespace Milwad\LaravelValidate\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Milwad\LaravelValidate\Utils\CountryLandlineCallback;
7
8
class ValidLandlineNumber implements Rule
9
{
10
    public function __construct(protected ?string $code = null) {}
11
12
    /**
13
     * Check phone number is valid.
14
     */
15
    public function passes($attribute, $value): bool
16
    {
17
        if (is_string($this->code)) {
18
            $passes = CountryLandlineCallback::callLandlineValidator($this->code, $value);
19
20
            return collect($passes)->some(fn ($passe) => $passe);
21
        }
22
23
        return preg_match('/^(?:\+|00)(?:[1-9]\d{0,2})(?:[ .\-]?\(?\d+\)?)+$/', $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('/^(?:...\(?\d+\)?)+$/', $value) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
24
    }
25
26
    /**
27
     * Get the validation error message.
28
     */
29
    public function message(): string
30
    {
31
        return __('validate.landline-number');
32
    }
33
}
34