DefinitionCollection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 57
loc 57
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 2
A add() 7 7 1
A getIterator() 4 4 1
A validateFactory() 6 6 2
A validateDefinition() 6 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Isolate\Framework\UnitOfWork\Entity;
4
5
use Isolate\Framework\UnitOfWork\Entity\Definition\Factory;
6
use Isolate\UnitOfWork\Entity\Definition;
7
8 View Code Duplication
class DefinitionCollection implements \IteratorAggregate 
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
9
{
10
    private $definitions;
11
12
    /**
13
     * @param array $elements
14
     */
15
    public function __construct(array $elements = array())
16
    {
17
        $this->definitions = [];
18
        
19
        foreach ($elements as $definitionFactory) {
20
            $this->validateFactory($definitionFactory);
21
            $definition = $definitionFactory->createDefinition();
22
            $this->validateDefinition($definition);
23
            
24
            $this->definitions[] = $definition;
25
        }
26
    }
27
28
    /**
29
     * @param Factory $definitionFactory
30
     * @return bool
31
     */
32
    public function add(Factory $definitionFactory)
33
    {
34
        $definition = $definitionFactory->createDefinition();
35
        $this->validateDefinition($definition);
36
        
37
        $this->definitions[] = $definition;
38
    }
39
40
    public function getIterator()
41
    {
42
        return new \ArrayIterator($this->definitions);
43
    }
44
45
    /**
46
     * @param $definitionFactory
47
     */
48
    private function validateFactory($definitionFactory)
49
    {
50
        if (!$definitionFactory instanceof Factory) {
51
            throw new \InvalidArgumentException("Definition collection elements needs to implement Definition\\Factory.");
52
        }
53
    }
54
55
    /**
56
     * @param $definition
57
     */
58
    private function validateDefinition($definition)
59
    {
60
        if (!$definition instanceof Definition) {
61
            throw new \RuntimeException("Definition\\Factory needs to create Entity\\Definition instance.");
62
        }
63
    }
64
}
65