Skip to content

Commit 0ba04ff

Browse files
committed
added Property and Parameter validation
1 parent c5181de commit 0ba04ff

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/PhpGenerator/Parameter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,9 @@ public function hasDefaultValue(): bool
131131
{
132132
return $this->hasDefaultValue;
133133
}
134+
135+
136+
public function validate(): void
137+
{
138+
}
134139
}

src/PhpGenerator/Printer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
154154

155155
$properties = [];
156156
foreach ($class->getProperties() as $property) {
157+
$property->validate();
157158
$type = $property->getType();
158159
$def = (($property->getVisibility() ?: 'public')
159160
. ($property->isStatic() ? ' static' : '')
@@ -289,6 +290,7 @@ public function printParameters($function, PhpNamespace $namespace = null, int $
289290
$special = false;
290291

291292
foreach ($list as $param) {
293+
$param->validate();
292294
$variadic = $function->isVariadic() && $param === end($list);
293295
$type = $param->getType();
294296
$promoted = $param instanceof PromotedParameter ? $param : null;

src/PhpGenerator/PromotedParameter.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Nette\PhpGenerator;
1111

12+
use Nette;
13+
1214

1315
/**
1416
* Promoted parameter in constructor.
@@ -34,4 +36,13 @@ public function isReadOnly(): bool
3436
{
3537
return $this->readOnly;
3638
}
39+
40+
41+
/** @throws Nette\InvalidStateException */
42+
public function validate(): void
43+
{
44+
if ($this->readOnly && !$this->getType()) {
45+
throw new Nette\InvalidStateException("Property \${$this->getName()}: Read-only properties are only supported on typed property.");
46+
}
47+
}
3748
}

src/PhpGenerator/Property.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,13 @@ public function isReadOnly(): bool
131131
{
132132
return $this->readOnly;
133133
}
134+
135+
136+
/** @throws Nette\InvalidStateException */
137+
public function validate(): void
138+
{
139+
if ($this->readOnly && !$this->type) {
140+
throw new Nette\InvalidStateException("Property \$$this->name: Read-only properties are only supported on typed property.");
141+
}
142+
}
134143
}

0 commit comments

Comments
 (0)