Issues (88)

src/Rules/Distinct.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 BlitzPHP\Utilities\Support\Invader;
15
use Rakit\Validation\Rule;
16
17
class Distinct extends AbstractRule
18
{
19
    protected array $parameters = [];
20
21
    /**
22
     * {@inheritDoc}
23
     */
24
    public function fillParameters(array $params): Rule
25
    {
26 2
        $this->parameters = $params;
27
28 2
        return $this;
29
    }
30
31
    public function ignoreCase(bool $bool = true): static
32
    {
33 2
        return $this->_setParams('ignore_case', $bool);
34
    }
35
36
    public function strict(bool $bool = true): static
37
    {
38 2
        return $this->_setParams('strict', $bool);
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function check($value): bool
45
    {
46
        if (! is_array($value)) {
47
            if (null === $attribute = $this->attribute->getPrimaryAttribute()) {
0 ignored issues
show
The method getPrimaryAttribute() does not exist on null. ( Ignorable by Annotation )

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

47
            if (null === $attribute = $this->attribute->/** @scrutinizer ignore-call */ getPrimaryAttribute()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
                return false;
49
            }
50
51
            $value = array_map(
52
                fn ($attribute) => $attribute->getValue(),
53
                Invader::make($this->validation)->parseArrayAttribute($attribute)
0 ignored issues
show
It seems like $this->validation can also be of type null; however, parameter $obj of BlitzPHP\Utilities\Support\Invader::make() does only seem to accept object, 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

53
                Invader::make(/** @scrutinizer ignore-type */ $this->validation)->parseArrayAttribute($attribute)
Loading history...
54 2
            );
55
        }
56
57
        if (in_array('ignore_case', $this->parameters, true)) {
58
            $value = array_map(function ($v) {
59
                if (is_string($v)) {
60 2
                    $v = strtolower($v);
61
                }
62
63 2
                return $v;
64 2
            }, $value);
65
        }
66
67 2
        return $value === array_unique($value, SORT_REGULAR);
68
    }
69
70
    private function _setParams(string $param, bool $bool = true): static
71
    {
72
        if ($bool) {
73 2
            $this->parameters[] = $param;
74 2
            $this->parameters   = array_unique($this->parameters);
75
        } else {
76
            $this->parameters = array_filter($this->parameters, fn ($value) => $value !== $param);
77
        }
78
79 2
        return $this;
80
    }
81
}
82