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
|
|
|
* Argument Collection ClassGeneration |
18
|
|
|
* @author Antonio Spinelli <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ArgumentCollection extends ArrayCollection |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Add a Argument on collection.<br /> |
25
|
|
|
* The index is a argument name, then will replace |
26
|
|
|
* if exist a index with the same name. |
27
|
|
|
* |
28
|
|
|
* @param ArgumentInterface $argument |
29
|
|
|
* |
30
|
|
|
* @throws \InvalidArgumentException |
31
|
|
|
* @return boolean |
32
|
|
|
*/ |
33
|
13 |
|
public function add($argument) |
34
|
|
|
{ |
35
|
13 |
|
if (!$argument instanceof ArgumentInterface) { |
36
|
1 |
|
throw new \InvalidArgumentException( |
37
|
|
|
'This Argument must be a instance of \ClassGeneration\ArgumentInterface' |
38
|
1 |
|
); |
39
|
|
|
} |
40
|
13 |
|
if ($argument->getName() === null) { |
41
|
2 |
|
$argument->setName('param' . ($this->count() + 1)); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
13 |
|
parent::offsetSet($argument->getName(), $argument); |
|
|
|
|
45
|
|
|
|
46
|
13 |
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets Argument Iterator |
51
|
|
|
* @return ArgumentIterator|Argument[] |
52
|
|
|
*/ |
53
|
20 |
|
public function getIterator() |
54
|
|
|
{ |
55
|
20 |
|
return new ArgumentIterator($this); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the arguments in string. |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
18 |
|
public function toString() |
63
|
|
|
{ |
64
|
18 |
|
$arguments = $this->getIterator(); |
65
|
18 |
|
$params = array(); |
66
|
18 |
|
$optionals = array(); |
67
|
18 |
|
foreach ($arguments as $argument) { |
68
|
10 |
|
if ($argument->isOptional()) { |
69
|
1 |
|
$optionals[] = $argument->toString(); |
70
|
1 |
|
} else { |
71
|
10 |
|
$params[] = $argument->toString(); |
72
|
|
|
} |
73
|
18 |
|
} |
74
|
|
|
|
75
|
18 |
|
return implode(', ', array_merge($params, $optionals)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Removes tags by name. |
80
|
|
|
* |
81
|
|
|
* @param string|array|Argument $argumentName |
82
|
|
|
* |
83
|
|
|
* @return \ClassGeneration\ArgumentCollection |
84
|
|
|
*/ |
85
|
1 |
|
public function removeByName($argumentName) |
86
|
|
|
{ |
87
|
1 |
|
$removedList = new self(); |
88
|
1 |
|
$list = $this->getIterator(); |
89
|
1 |
|
foreach ($list as $index => $argument) { |
90
|
1 |
|
if (($argumentName instanceof Argument && $argumentName->getName() != $argument->getName()) |
91
|
1 |
|
|| (is_string($argumentName) && $argument->getName() !== $argumentName) |
92
|
1 |
|
) { |
93
|
1 |
|
continue; |
94
|
|
|
} |
95
|
1 |
|
$removedList->add(clone $argument); |
96
|
1 |
|
$this->remove($index); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
return $removedList; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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 theSon
calls the wrong method in the parent class.