MethodCollection::getIterator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
 * Method Collection ClassGeneration
18
 * @author Antonio Spinelli <[email protected]>
19
 */
20
class MethodCollection extends ArrayCollection
21
{
22
23
    /**
24
     * Adds a new Method on collection.
25
     *
26
     * @param MethodInterface $method
27
     *
28
     * @return bool
29
     *
30
     * @throws \InvalidArgumentException
31
     */
32 16
    public function add($method)
33
    {
34 16
        if (!$method instanceof MethodInterface) {
35 1
            throw new \InvalidArgumentException('This Method must be a instance of \ClassGeneration\MethodInterface');
36
        }
37 16
        if ($method->getName() === null) {
38 9
            $method->setName('method' . ($this->count() + 1));
39 9
        }
40
41 16
        parent::set($method->getName(), $method);
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...
42 16
        return true;
43
    }
44
45
    /**
46
     * Gets the Method Iterator.
47
     * @return MethodIterator|MethodInterface[]
48
     */
49 12
    public function getIterator()
50
    {
51 12
        return new MethodIterator($this);
52
    }
53
54
    /**
55
     * Parse the Method Collection to string.
56
     * @return string
57
     */
58 10
    public function toString()
59
    {
60 10
        $string = '';
61 10
        $methods = $this->getIterator();
62 10
        foreach ($methods as $method) {
63 10
            $string .= $method->toString();
64 10
        }
65
66 10
        return $string;
67
    }
68
69
    /**
70
     * Removes tags by name.
71
     *
72
     * @param string $methodName
73
     *
74
     * @return MethodCollection Returns a collection with removed methods.
75
     */
76 1
    public function removeByName($methodName)
77
    {
78 1
        $removedList = new self();
79 1
        $list = $this->getIterator();
80 1
        foreach ($list as $index => $method) {
81 1
            if ((is_array($methodName) && in_array($method->getName(), $methodName))
82 1
                || ($method->getName() === $methodName)
83 1
            ) {
84 1
                $removedList->add($method);
85 1
                $this->remove($index);
86 1
            }
87 1
        }
88
89 1
        return $removedList;
90
    }
91
}
92