RuleCollection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 45
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 6 1
A add() 0 4 1
A addCollection() 0 6 2
A assertRuleInstance() 0 9 2
1
<?php
2
namespace Boekkooi\Bundle\JqueryValidationBundle\Form;
3
4
use Boekkooi\Bundle\JqueryValidationBundle\Exception\InvalidArgumentException;
5
use Boekkooi\Bundle\JqueryValidationBundle\Exception\LogicException;
6
use Doctrine\Common\Collections\ArrayCollection;
7
8
/**
9
 * @author Warnar Boekkooi <[email protected]>
10
 */
11
class RuleCollection extends ArrayCollection
12
{
13
    /**
14
     * @param int|string $key
15
     * @param Rule $value
16
     */
17 7
    public function set($key, $value)
18
    {
19 7
        $this->assertRuleInstance($value);
20
21 2
        parent::set($key, $value);
22 2
    }
23
24
    /**
25
     * {@inheritdoc}
26
     * @throw LogicException
27
     */
28 1
    public function add($value)
29
    {
30 1
        throw new LogicException('RuleCollection must be used as a dictionary');
31
    }
32
33
    /**
34
     * Adds a rule collection at the end of the current set by appending all
35
     * rule of the added collection.
36
     *
37
     * @param RuleCollection $collection A RuleCollection instance
38
     */
39 1
    public function addCollection(RuleCollection $collection)
40
    {
41 1
        foreach ($collection as $name => $rule) {
42 1
            $this->set($name, $rule);
43 1
        }
44 1
    }
45
46 7
    private function assertRuleInstance($value)
47
    {
48 7
        if (!$value instanceof Rule) {
49 5
            throw new InvalidArgumentException(sprintf(
50 5
                'Expected a "%s" instance',
51
                Rule::class
52 5
            ));
53
        }
54 2
    }
55
}
56