Passed
Pull Request — master (#465)
by
unknown
04:30 queued 01:59
created

PropagateOptionsHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 13
dl 0
loc 41
rs 10
c 4
b 1
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B propagate() 0 24 9
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
            if ($parentRule instanceof SkipOnEmptyInterface && $childRule instanceof SkipOnEmptyInterface) {
39
                $childRule = $childRule->skipOnEmpty($parentRule->getSkipOnEmpty());
40
            }
41
42
            if ($parentRule instanceof SkipOnErrorInterface && $childRule instanceof SkipOnErrorInterface) {
43
                $childRule = $childRule->skipOnError($parentRule->shouldSkipOnError());
44
            }
45
46
            if ($parentRule instanceof WhenInterface && $childRule instanceof WhenInterface) {
47
                $childRule = $childRule->when($parentRule->getWhen());
48
            }
49
50
            if ($childRule instanceof PropagateOptionsInterface) {
51
                $childRule->propagateOptions();
52
            }
53
54
            $rules[] = $childRule;
55
        }
56
57
        return $rules;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rules returns the type array which is incompatible with the documented return type Yiisoft\Validator\Helper\list.
Loading history...
58
    }
59
}
60