Issues (88)

src/Rules/Phone.php (2 issues)

1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Rules;
13
14
use BadMethodCallException;
15
use BlitzPHP\Utilities\Helpers;
16
use Dimtrovich\Validation\Utils\CountryPhoneCallback;
17
18
class Phone extends AbstractRule
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $fillableParams = ['code'];
24
25
    /**
26
     * Check if a given value is valid phone number
27
     *
28
     * @credit <a href="https://github.com/milwad-dev/laravel-validate">milwad/laravel-validate - Milwad\LaravelValidate\Rules\ValidPhoneNumber</a>
29
     *
30
     * @param mixed $value
31
     */
32
    public function check($value): bool
33
    {
34
        if (! empty($code = $this->parameter('code'))) {
35
            try {
36
                $passes = (new CountryPhoneCallback($value, $code))->callPhoneValidator();
37
            } catch (BadMethodCallException) {
38
                $code   = $this->getAttribute()->getValue($code);
39
                $passes = (new CountryPhoneCallback($value, $code))->callPhoneValidator();
0 ignored issues
show
It seems like $code can also be of type null; however, parameter $code of Dimtrovich\Validation\Ut...Callback::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
                $passes = (new CountryPhoneCallback($value, /** @scrutinizer ignore-type */ $code))->callPhoneValidator();
Loading history...
40
            }
41
42
            return Helpers::collect($passes)->some(fn ($passe) => $passe);
43
        }
44
45
        return preg_match('/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/', $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('/^[\+...?[0-9]{4,6}$/', $value) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
46
    }
47
}
48