DefinitionCollection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 60
loc 60
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\LazyObjects;
4
5
use Isolate\Framework\LazyObjects\Definition\Factory;
6
use Isolate\LazyObjects\Proxy\Definition;
7
use Traversable;
8
9 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...
10
{
11
    /**
12
     * @var Definition[]|array
13
     */
14
    private $definitions;
15
16
    public function __construct(array $elements = array())
17
    {
18
        $this->definitions = [];
19
        
20
        foreach ($elements as $definitionFactory) {
21
            $this->validateFactory($definitionFactory);
22
            $definition = $definitionFactory->createDefinition();
23
            $this->validateDefinition($definition);
24
25
            $this->definitions[] = $definition;
26
        }
27
    }
28
29
    /**
30
     * @param mixed $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
31
     * @return bool
32
     */
33
    public function add(Factory $definitionFactory)
34
    {
35
        $definition = $definitionFactory->createDefinition();
36
        $this->validateDefinition($definition);
37
        
38
        $this->definitions[] = $definition;
39
    }
40
41
    /**
42
     * @return \ArrayIterator
43
     */
44
    public function getIterator()
45
    {
46
        return new \ArrayIterator($this->definitions);
47
    }
48
49
    /**
50
     * @param $definitionFactory
51
     */
52
    private function validateFactory($definitionFactory)
53
    {
54
        if (!$definitionFactory instanceof Factory) {
55
            throw new \InvalidArgumentException("Definition collection elements needs to implement Definition\\Factory.");
56
        }
57
    }
58
59
    /**
60
     * @param $definition
61
     */
62
    private function validateDefinition($definition)
63
    {
64
        if (!$definition instanceof Definition) {
65
            throw new \RuntimeException("Definition\\Factory needs to create Entity\\Definition instance.");
66
        }
67
    }
68
}
69