Skip to content

Commit 2f64aa3

Browse files
committed
Method::setParameters() & ClassType::setProperties() & setMethods() retain names in keys
1 parent 2c08cae commit 2f64aa3

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,13 @@ public function addConst($name, $value)
432432
*/
433433
public function setProperties(array $props)
434434
{
435+
$this->properties = [];
435436
foreach ($props as $v) {
436437
if (!$v instanceof Property) {
437438
throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Property[].');
438439
}
440+
$this->properties[$v->getName()] = $v;
439441
}
440-
$this->properties = $props;
441442
return $this;
442443
}
443444

@@ -480,12 +481,13 @@ public function addProperty($name, $value = NULL)
480481
*/
481482
public function setMethods(array $methods)
482483
{
484+
$this->methods = [];
483485
foreach ($methods as $v) {
484486
if (!$v instanceof Method) {
485487
throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Method[].');
486488
}
489+
$this->methods[$v->getName()] = $v;
487490
}
488-
$this->methods = $methods;
489491
return $this;
490492
}
491493

src/PhpGenerator/Method.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,13 @@ public function getName()
164164
*/
165165
public function setParameters(array $val)
166166
{
167+
$this->parameters = [];
167168
foreach ($val as $v) {
168169
if (!$v instanceof Parameter) {
169170
throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Parameter[].');
170171
}
172+
$this->parameters[$v->getName()] = $v;
171173
}
172-
$this->parameters = $val;
173174
return $this;
174175
}
175176

tests/PhpGenerator/ClassType.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,20 @@ $method->addParameter('res', NULL)
6565
->setTypeHint('array');
6666

6767
Assert::matchFile(__DIR__ . '/ClassType.expect', (string) $class);
68+
69+
70+
// global setters & getters
71+
$methods = $class->getMethods();
72+
Assert::count(3, $methods);
73+
$class->setMethods(array_values($methods));
74+
Assert::same($methods, $class->getMethods());
75+
76+
$properties = $class->getProperties();
77+
Assert::count(3, $properties);
78+
$class->setProperties(array_values($properties));
79+
Assert::same($properties, $class->getProperties());
80+
81+
$parameters = $method->getParameters();
82+
Assert::count(2, $parameters);
83+
$method->setParameters(array_values($parameters));
84+
Assert::same($parameters, $method->getParameters());

0 commit comments

Comments
 (0)