Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
17.65% covered (danger)
17.65%
3 / 17
7.41% covered (danger)
7.41%
2 / 27
100.00% covered (success)
100.00%
2 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RuleParser
100.00% covered (success)
100.00%
21 / 21
17.65% covered (danger)
17.65%
3 / 17
7.41% covered (danger)
7.41%
2 / 27
100.00% covered (success)
100.00%
2 / 2
58.81
100.00% covered (success)
100.00%
1 / 1
 parse
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 unquoteWrappedArgument
100.00% covered (success)
100.00%
9 / 9
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 25
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace Celemas\Sire;
6
7use Override;
8use ValueError;
9
10/** @api */
11final class RuleParser implements Contract\RuleParser
12{
13    #[Override]
14    /** @return array{name: string, args: list<string>} */
15    public function parse(string $ruleDefinition): array
16    {
17        $ruleArray = DslSplitter::split($ruleDefinition, ':', true);
18        $ruleName = $ruleArray[0] ?? '';
19
20        if ($ruleName === '') {
21            throw new ValueError('Invalid rule definition: missing rule name');
22        }
23
24        $ruleArgs = array_map(
25            $this->unquoteWrappedArgument(...),
26            array_slice($ruleArray, 1),
27        );
28
29        return [
30            'name' => $ruleName,
31            'args' => $ruleArgs,
32        ];
33    }
34
35    private function unquoteWrappedArgument(string $arg): string
36    {
37        if (strlen($arg) < 2) {
38            return $arg;
39        }
40
41        $firstChar = $arg[0];
42        $lastChar = $arg[strlen($arg) - 1];
43
44        if (
45            ($firstChar === '"' || $firstChar === "'")
46            && $firstChar === $lastChar
47            && substr_count($arg, $firstChar) === 2
48        ) {
49            return substr($arg, 1, -1);
50        }
51
52        return $arg;
53    }
54}

Branches

Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once. Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement always has an else as part of its logical flow even if you didn't write one.

RuleParser->parse
15    public function parse(string $ruleDefinition): array
16    {
17        $ruleArray = DslSplitter::split($ruleDefinition, ':', true);
18        $ruleName = $ruleArray[0] ?? '';
19
20        if ($ruleName === '') {
21            throw new ValueError('Invalid rule definition: missing rule name');
24        $ruleArgs = array_map(
25            $this->unquoteWrappedArgument(...),
26            array_slice($ruleArray, 1),
27        );
28
29        return [
30            'name' => $ruleName,
31            'args' => $ruleArgs,
32        ];
33    }
RuleParser->unquoteWrappedArgument
35    private function unquoteWrappedArgument(string $arg): string
36    {
37        if (strlen($arg) < 2) {
38            return $arg;
41        $firstChar = $arg[0];
42        $lastChar = $arg[strlen($arg) - 1];
43
44        if (
45            ($firstChar === '"' || $firstChar === "'")
45            ($firstChar === '"' || $firstChar === "'")
45            ($firstChar === '"' || $firstChar === "'")
46            && $firstChar === $lastChar
46            && $firstChar === $lastChar
47            && substr_count($arg, $firstChar) === 2
47            && substr_count($arg, $firstChar) === 2
49            return substr($arg, 1, -1);
49            return substr($arg, 1, -1);
49            return substr($arg, 1, -1);
49            return substr($arg, 1, -1);
52        return $arg;
53    }
unquoteWrappedArgument
35    private function unquoteWrappedArgument(string $arg): string
36    {
37        if (strlen($arg) < 2) {
38            return $arg;
41        $firstChar = $arg[0];
42        $lastChar = $arg[strlen($arg) - 1];
43
44        if (
45            ($firstChar === '"' || $firstChar === "'")
45            ($firstChar === '"' || $firstChar === "'")
45            ($firstChar === '"' || $firstChar === "'")
46            && $firstChar === $lastChar
46            && $firstChar === $lastChar
47            && substr_count($arg, $firstChar) === 2
47            && substr_count($arg, $firstChar) === 2
49            return substr($arg, 1, -1);
49            return substr($arg, 1, -1);
49            return substr($arg, 1, -1);
49            return substr($arg, 1, -1);
52        return $arg;
53    }