Passed
Pull Request — master (#564)
by Sergei
02:39
created

PropagateOptionsHelper::propagateRule()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 9
c 0
b 0
f 0
nc 16
nop 2
dl 0
loc 19
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Helper;
6
7
use Yiisoft\Validator\PropagateOptionsInterface;
8
use Yiisoft\Validator\RuleInterface;
9
use Yiisoft\Validator\SkipOnEmptyInterface;
10
use Yiisoft\Validator\SkipOnErrorInterface;
11
use Yiisoft\Validator\WhenInterface;
12
13
/**
14
 * A helper class used to propagate options' values from a single parent rule to its child rules at all nesting levels
15
 * recursively.
16
 */
17
final class PropagateOptionsHelper
18
{
19
    /**
20
     * Propagates options' values from a single parent rule to its child rules at all nesting levels recursively. The
21
     * following options' values are propagated:
22
     *
23
     * - `$skipOnEmpty` (both rules must implement {@see SkipOnEmptyInterface}).
24
     * - `$skipOnError` (both rules must implement {@see SkipOnErrorInterface}).
25
     * - `$when` (both rules must implement {@see WhenInterface}).
26
     *
27
     * @param RuleInterface $parentRule A parent rule which options' values need to be propagated.
28
     * @param iterable<RuleInterface> $childRules Direct child rules (located at the first nesting level) which options'
29
     * values must be changed to be the same as in parent.
30
     *
31
     * @return list<RuleInterface> A list of child rules of the same nesting level with changed options' values or
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\Helper\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
     * unchanged if none of the required interfaces were implemented. The order is preserved.
33
     */
34
    public static function propagate(RuleInterface $parentRule, iterable $childRules): array
35
    {
36
        $rules = [];
37
        foreach ($childRules as $childRule) {
38
            $rules[] = self::propagateRule($parentRule, $childRule);
39
        }
40
        return $rules;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rules returns the type Yiisoft\Validator\RuleInterface[]|array which is incompatible with the documented return type Yiisoft\Validator\Helper\list.
Loading history...
41
    }
42
43
    public static function propagateRule(RuleInterface $parentRule, RuleInterface $childRule): RuleInterface
44
    {
45
        if ($parentRule instanceof SkipOnEmptyInterface && $childRule instanceof SkipOnEmptyInterface) {
46
            $childRule = $childRule->skipOnEmpty($parentRule->getSkipOnEmpty());
47
        }
48
49
        if ($parentRule instanceof SkipOnErrorInterface && $childRule instanceof SkipOnErrorInterface) {
50
            $childRule = $childRule->skipOnError($parentRule->shouldSkipOnError());
51
        }
52
53
        if ($parentRule instanceof WhenInterface && $childRule instanceof WhenInterface) {
54
            $childRule = $childRule->when($parentRule->getWhen());
55
        }
56
57
        if ($childRule instanceof PropagateOptionsInterface) {
58
            $childRule->propagateOptions();
59
        }
60
61
        return $childRule;
62
    }
63
}
64