|
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); |
|
|
|
|
|
|
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
|
|
|
|
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:
The
getFirstName()method in theSoncalls the wrong method in the parent class.