Issues (15)

src/Rules/Parser.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Kontrolio\Rules;
4
5
final class Parser
6
{
7
    private $cache;
8
    private $rules;
9
10
    public function __construct(Repository $rules)
11
    {
12
        $this->rules = $rules;
13
    }
14
15
    /**
16
     * Converts validation string into an array of rule instances.
17
     *
18
     * @param string $string validation string like "equal_to:foo|not_equal_to:bar"
19
     *
20
     * @return RuleInterface[]
21
     */
22
    public function parse($string)
23
    {
24
        if (isset($this->cache[$string])) {
25
            return $this->cache[$string];
26
        }
27
28
        $this->cache[$string] = array_map(function ($rule) {
29
            return $this->rules->make(...$this->getNameAndArguments($rule));
0 ignored issues
show
The call to Kontrolio\Rules\Repository::make() has too few arguments starting with arguments. ( Ignorable by Annotation )

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

29
            return $this->rules->/** @scrutinizer ignore-call */ make(...$this->getNameAndArguments($rule));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
30
        }, explode('|', $string));
31
32
        return $this->cache[$string];
33
    }
34
35
    /**
36
     * Takes rule identifier and arguments from the string.
37
     *
38
     * @param string $rule
39
     *
40
     * @return array
41
     */
42
    private function getNameAndArguments($rule)
43
    {
44
        $delimpos = strpos($rule, ':');
45
46
        if ($delimpos === false) {
47
            return [$rule, []];
48
        }
49
50
        return [
51
            substr($rule, 0, $delimpos),
52
            (array) explode(',', substr($rule, $delimpos + 1))
53
        ];
54
    }
55
}
56