RejectUnusedRulesPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 28
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B compile() 0 22 5
1
<?php
2
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Compiler;
3
4
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleCompilerInterface;
5
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleContextBuilder;
6
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintRule;
7
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection;
8
9
/**
10
 * @author Warnar Boekkooi <[email protected]>
11
 */
12
class RejectUnusedRulesPass implements FormRuleCompilerInterface
13
{
14
    /**
15
     * @param FormRuleContextBuilder $context
16
     */
17
    public function compile(FormRuleContextBuilder $context)
18
    {
19
        $it = new \RecursiveIteratorIterator(
20
            new \RecursiveArrayIterator($context->getGroups())
21
        );
22
        $groups = array_unique(array_filter(iterator_to_array($it, false)));
23
24
        /** @var RuleCollection $ruleCollection */
25
        foreach ($context->all() as $ruleCollection) {
26
            foreach ($ruleCollection as $name => $rule) {
27
                if (!$rule instanceof ConstraintRule) {
28
                    continue;
29
                }
30
31
                $rule->groups = array_intersect($rule->groups, $groups);
32
33
                if (empty($rule->groups)) {
34
                    $ruleCollection->remove($name);
35
                }
36
            }
37
        }
38
    }
39
}
40