Skip to content

Commit c5181de

Browse files
committed
improved validation error messages
1 parent a758e6f commit c5181de

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,14 +600,14 @@ public function hasMethod(string $name): bool
600600
/** @throws Nette\InvalidStateException */
601601
public function validate(): void
602602
{
603-
if ($this->abstract && $this->final) {
604-
throw new Nette\InvalidStateException('Class cannot be abstract and final.');
605-
606-
} elseif ($this->isEnum() && ($this->abstract || $this->final || $this->extends || $this->properties)) {
607-
throw new Nette\InvalidStateException('Enum cannot be abstract or final or extends class or have properties.');
603+
if ($this->isEnum() && ($this->abstract || $this->final || $this->extends || $this->properties)) {
604+
throw new Nette\InvalidStateException("Enum '$this->name' cannot be abstract or final or extends class or have properties.");
608605

609606
} elseif (!$this->name && ($this->abstract || $this->final)) {
610607
throw new Nette\InvalidStateException('Anonymous class cannot be abstract or final.');
608+
609+
} elseif ($this->abstract && $this->final) {
610+
throw new Nette\InvalidStateException("Class '$this->name' cannot be abstract and final at the same time.");
611611
}
612612
}
613613

src/PhpGenerator/Method.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function addPromotedParameter(string $name, $defaultValue = null): Promot
137137
public function validate(): void
138138
{
139139
if ($this->abstract && ($this->final || $this->visibility === ClassType::VISIBILITY_PRIVATE)) {
140-
throw new Nette\InvalidStateException('Method cannot be abstract and final or private.');
140+
throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time.");
141141
}
142142
}
143143
}

tests/PhpGenerator/ClassType.validate.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ Assert::exception(function () {
1313
$class = new ClassType('A');
1414
$class->setFinal(true)->setAbstract(true);
1515
$class->validate();
16-
}, Nette\InvalidStateException::class, 'Class cannot be abstract and final.');
16+
}, Nette\InvalidStateException::class, "Class 'A' cannot be abstract and final at the same time.");
1717

1818
Assert::exception(function () {
1919
$class = new ClassType('A');
2020
$class->setAbstract(true)->setFinal(true);
2121
$class->validate();
22-
}, Nette\InvalidStateException::class, 'Class cannot be abstract and final.');
22+
}, Nette\InvalidStateException::class, "Class 'A' cannot be abstract and final at the same time.");
2323

2424
Assert::exception(function () {
2525
$class = new ClassType;

tests/PhpGenerator/Method.validate.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ Assert::exception(function () {
1313
$method = new Method('foo');
1414
$method->setFinal(true)->setAbstract(true);
1515
$method->validate();
16-
}, Nette\InvalidStateException::class, 'Method cannot be abstract and final or private.');
16+
}, Nette\InvalidStateException::class, 'Method foo() cannot be abstract and final or private at the same time.');
1717

1818
Assert::exception(function () {
1919
$method = new Method('foo');
2020
$method->setAbstract(true)->setFinal(true);
2121
$method->validate();
22-
}, Nette\InvalidStateException::class, 'Method cannot be abstract and final or private.');
22+
}, Nette\InvalidStateException::class, 'Method foo() cannot be abstract and final or private at the same time.');
2323

2424
Assert::exception(function () {
2525
$method = new Method('foo');
2626
$method->setAbstract(true)->setVisibility('private');
2727
$method->validate();
28-
}, Nette\InvalidStateException::class, 'Method cannot be abstract and final or private.');
28+
}, Nette\InvalidStateException::class, 'Method foo() cannot be abstract and final or private at the same time.');

tests/PhpGenerator/Printer.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ Assert::exception(function () {
8787
$class = new ClassType;
8888
$class->setFinal(true)->setAbstract(true);
8989
(new Printer)->printClass($class);
90-
}, Nette\InvalidStateException::class, 'Class cannot be abstract and final.');
90+
}, Nette\InvalidStateException::class, 'Anonymous class cannot be abstract or final.');

0 commit comments

Comments
 (0)