Passed
Push — main ( 34571b...e79ce9 )
by Breno
01:59
created

FoneSemDDD   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A evaluate() 0 10 3
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Rules\Brazilian;
5
6
use Attribute;
7
use BrenoRoosevelt\Validation\AbstractValidation;
8
use Throwable;
9
10
#[Attribute(Attribute::TARGET_PROPERTY)]
11
class FoneSemDDD extends AbstractValidation
12
{
13
    const WITH_MASK = '/^\[0-9]{4,5}-[0-9]{4}$/';
14
    const NO_MASK = '/^\[0-9]{8,9}$/';
15
16
    public function __construct(private bool $mask = true, ?string $message = 'Telefone inválido')
17
    {
18
        parent::__construct($message);
19
    }
20
21
    public function evaluate($input, array $context = []): bool
22
    {
23
        try {
24
            $fone = (string) $input;
25
        } catch (Throwable) {
0 ignored issues
show
Unused Code introduced by
catch (\Throwable) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
26
            return false;
27
        }
28
29
        $pattern = $this->mask ? self::WITH_MASK : self::NO_MASK;
30
        return preg_match($pattern, $fone) === 1;
31
    }
32
}
33