Skip to content

Commit 3db5dec

Browse files
committed
ClassType: method names are case insensitive
1 parent 90082c1 commit 3db5dec

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public function addMember($member): self
395395
if ($this->isInterface()) {
396396
$member->setBody(null);
397397
}
398-
$this->methods[$member->getName()] = $member;
398+
$this->methods[strtolower($member->getName())] = $member;
399399

400400
} elseif ($member instanceof Property) {
401401
$this->properties[$member->getName()] = $member;
@@ -563,8 +563,8 @@ public function setMethods(array $methods): self
563563
{
564564
(function (Method ...$methods) {})(...array_values($methods));
565565
$this->methods = [];
566-
foreach ($methods as $v) {
567-
$this->methods[$v->getName()] = $v;
566+
foreach ($methods as $m) {
567+
$this->methods[strtolower($m->getName())] = $m;
568568
}
569569
return $this;
570570
}
@@ -573,16 +573,21 @@ public function setMethods(array $methods): self
573573
/** @return Method[] */
574574
public function getMethods(): array
575575
{
576-
return $this->methods;
576+
$res = [];
577+
foreach ($this->methods as $m) {
578+
$res[$m->getName()] = $m;
579+
}
580+
return $res;
577581
}
578582

579583

580584
public function getMethod(string $name): Method
581585
{
582-
if (!isset($this->methods[$name])) {
586+
$m = $this->methods[strtolower($name)] ?? null;
587+
if (!$m) {
583588
throw new Nette\InvalidArgumentException("Method '$name' not found.");
584589
}
585-
return $this->methods[$name];
590+
return $m;
586591
}
587592

588593

@@ -594,21 +599,21 @@ public function addMethod(string $name): Method
594599
} else {
595600
$method->setPublic();
596601
}
597-
return $this->methods[$name] = $method;
602+
return $this->methods[strtolower($name)] = $method;
598603
}
599604

600605

601606
/** @return static */
602607
public function removeMethod(string $name): self
603608
{
604-
unset($this->methods[$name]);
609+
unset($this->methods[strtolower($name)]);
605610
return $this;
606611
}
607612

608613

609614
public function hasMethod(string $name): bool
610615
{
611-
return isset($this->methods[$name]);
616+
return isset($this->methods[strtolower($name)]);
612617
}
613618

614619

tests/PhpGenerator/ClassType.addMember.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Assert::exception(function () {
1616

1717

1818
$class = (new ClassType('Example'))
19+
->addMember($method = new Nette\PhpGenerator\Method('GETHANDLE'))
1920
->addMember($method = new Nette\PhpGenerator\Method('getHandle'))
2021
->addMember($property = new Nette\PhpGenerator\Property('handle'))
2122
->addMember($const = new Nette\PhpGenerator\Constant('ROLE'))

tests/PhpGenerator/ClassType.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ $class->removeProperty('b')->removeProperty('c');
186186
Assert::same(['a'], array_keys($class->getProperties()));
187187

188188
$class->addMethod('a');
189+
$class->addMethod('A');
189190
$class->addMethod('b');
190-
$class->removeMethod('b')->removeMethod('c');
191+
$class->removeMethod('B')->removeMethod('c');
191192

192-
Assert::same(['a'], array_keys($class->getMethods()));
193+
Assert::same(['A'], array_keys($class->getMethods()));

0 commit comments

Comments
 (0)