PropertyCollection   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 2 Features 1
Metric Value
wmc 17
c 9
b 2
f 1
lcom 1
cbo 3
dl 0
loc 105
ccs 43
cts 43
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 15 3
A current() 0 4 1
A getIterator() 0 4 1
A toString() 0 10 2
B getByName() 0 14 5
B removeByName() 0 15 5
1
<?php
2
3
/*
4
 * This file is part of the ClassGeneration package.
5
 *
6
 * (c) Antonio Spinelli <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ClassGeneration;
13
14
use ClassGeneration\Collection\ArrayCollection;
15
16
/**
17
 * Property Collection ClassGeneration
18
 * @author Antonio Spinelli <[email protected]>
19
 */
20
class PropertyCollection extends ArrayCollection
21
{
22
23
    /**
24
     * Adds a new Property on the collection.
25
     *
26
     * @param PropertyInterface $property
27
     *
28
     * @return bool
29
     * @throws \InvalidArgumentException
30
     */
31 11
    public function add($property)
32
    {
33 11
        if (!$property instanceof PropertyInterface) {
34 1
            throw new \InvalidArgumentException(
35
                'This Property must be a instance of \ClassGeneration\PropertyInterface'
36 1
            );
37
        }
38 11
        if ($property->getName() === null) {
39 5
            $property->setName('property' . ($this->count() + 1));
40 5
        }
41
42 11
        parent::set($property->getName(), $property);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (set() instead of add()). Are you sure this is correct? If so, you might want to change this to $this->set().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
43
44 11
        return true;
45
    }
46
47
    /**
48
     * Gets the current Property.
49
     * @return Property
50
     */
51 5
    public function current()
52
    {
53 5
        return parent::current();
54
    }
55
56
    /**
57
     * Gets the Property Iterator.
58
     * @return PropertyIterator|Property[]
59
     */
60 15
    public function getIterator()
61
    {
62 15
        return new PropertyIterator($this);
63
    }
64
65
    /**
66
     * Parse the Property Collection to string.
67
     * @return string
68
     */
69 10
    public function toString()
70
    {
71 10
        $string = '';
72 10
        $properties = $this->getIterator();
73 10
        foreach ($properties as $property) {
74 6
            $string .= $property->toString();
75 10
        }
76
77 10
        return $string;
78
    }
79
80
    /**
81
     * Find the properties by name.
82
     *
83
     * @param string $propertyName
84
     *
85
     * @return PropertyCollection
86
     */
87 2
    public function getByName($propertyName)
88
    {
89 2
        $foundList = new self();
90 2
        $list = $this->getIterator();
91 2
        foreach ($list as $property) {
92 2
            if ((is_array($propertyName) && in_array($property->getName(), $propertyName))
93 2
                || ($property->getName() === $propertyName)
94 2
            ) {
95 2
                $foundList->add($property);
96 2
            }
97 2
        }
98
99 2
        return $foundList;
100
    }
101
102
    /**
103
     * Removes tags by name.
104
     *
105
     * @param string $propertyName
106
     *
107
     * @return PropertyCollection
108
     */
109 1
    public function removeByName($propertyName)
110
    {
111 1
        $removedList = new self();
112 1
        $list = $this->getIterator();
113 1
        foreach ($list as $index => $property) {
114 1
            if ((is_array($propertyName) && in_array($property->getName(), $propertyName))
115 1
                || ($property->getName() === $propertyName)
116 1
            ) {
117 1
                $removedList->add(clone $property);
118 1
                $this->remove($index);
119 1
            }
120 1
        }
121
122 1
        return $removedList;
123
    }
124
}
125