Passed
Pull Request — master (#564)
by Sergei
20:34 queued 17:40
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 all child rules at all nesting levels recursively.
21
     * The 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 for this particular parent rule which options'
29
     * values must be changed to be the same as in parent rule.
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::propagateToRule($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
    /**
44
     * Performs propagation of options' values for a single pair of one parent rule and one of its direct child rules.
45
     * If the child rule also supports such propagation, it delegates the further propagation to
46
     * {@see PropagateOptionsInterface::propagateOptions()} implementation in this child rule.
47
     *
48
     * @param RuleInterface $parentRule A parent rule which options' values need to be propagated.
49
     * @param RuleInterface $childRule One of the direct child rules for this particular parent rule which options'
50
     * values must be changed to be the same as in parent rule.
51
     *
52
     * @return RuleInterface The same child rule instance with changed options' values or unchanged if none of the
53
     * required interfaces were implemented.
54
     */
55
    public static function propagateToRule(RuleInterface $parentRule, RuleInterface $childRule): RuleInterface
56
    {
57
        if ($parentRule instanceof SkipOnEmptyInterface && $childRule instanceof SkipOnEmptyInterface) {
58
            $childRule = $childRule->skipOnEmpty($parentRule->getSkipOnEmpty());
59
        }
60
61
        if ($parentRule instanceof SkipOnErrorInterface && $childRule instanceof SkipOnErrorInterface) {
62
            $childRule = $childRule->skipOnError($parentRule->shouldSkipOnError());
63
        }
64
65
        if ($parentRule instanceof WhenInterface && $childRule instanceof WhenInterface) {
66
            $childRule = $childRule->when($parentRule->getWhen());
67
        }
68
69
        if ($childRule instanceof PropagateOptionsInterface) {
70
            $childRule->propagateOptions();
71
        }
72
73
        return $childRule;
74
    }
75
}
76