Skip to content

Commit e0741f4

Browse files
committed
added validator for types
1 parent 7329ec2 commit e0741f4

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,23 @@ public static function createObject(string $class, array $props)
117117
{
118118
return Dumper::createObject($class, $props);
119119
}
120+
121+
122+
public static function validateType(?string $type, bool &$nullable): ?string
123+
{
124+
if ($type === '' || $type === null) {
125+
return null;
126+
}
127+
if (!preg_match('#(?:
128+
\?[\w\\\\]+|
129+
[\w\\\\]+ (?: (&[\w\\\\]+)* | (\|[\w\\\\]+)* )
130+
)()$#xAD', $type)) {
131+
throw new Nette\InvalidArgumentException("Value '$type' is not valid type.");
132+
}
133+
if ($type[0] === '?') {
134+
$nullable = true;
135+
return substr($type, 1);
136+
}
137+
return $type;
138+
}
120139
}

src/PhpGenerator/Parameter.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ public function isReference(): bool
5656
/** @return static */
5757
public function setType(?string $type): self
5858
{
59-
if ($type && $type[0] === '?') {
60-
$type = substr($type, 1);
61-
$this->nullable = true;
62-
}
63-
$this->type = $type;
59+
$this->type = Helpers::validateType($type, $this->nullable);
6460
return $this;
6561
}
6662

@@ -74,15 +70,14 @@ public function getType(): ?string
7470
/** @deprecated use setType() */
7571
public function setTypeHint(?string $type): self
7672
{
77-
$this->type = $type;
78-
return $this;
73+
return $this->setType($type);
7974
}
8075

8176

8277
/** @deprecated use getType() */
8378
public function getTypeHint(): ?string
8479
{
85-
return $this->type;
80+
return $this->getType();
8681
}
8782

8883

src/PhpGenerator/Property.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ public function isStatic(): bool
7676
/** @return static */
7777
public function setType(?string $type): self
7878
{
79-
if ($type && $type[0] === '?') {
80-
$type = substr($type, 1);
81-
$this->nullable = true;
82-
}
83-
$this->type = $type;
79+
$this->type = Helpers::validateType($type, $this->nullable);
8480
return $this;
8581
}
8682

src/PhpGenerator/Traits/FunctionLike.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,7 @@ public function isVariadic(): bool
127127
/** @return static */
128128
public function setReturnType(?string $type): self
129129
{
130-
if ($type && $type[0] === '?') {
131-
$type = substr($type, 1);
132-
$this->returnNullable = true;
133-
}
134-
$this->returnType = $type;
130+
$this->returnType = Nette\PhpGenerator\Helpers::validateType($type, $this->returnNullable);
135131
return $this;
136132
}
137133

tests/PhpGenerator/invalidNames.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ Assert::exception(function () {
109109
new Nette\PhpGenerator\Property('*');
110110
}, Nette\InvalidArgumentException::class);
111111

112+
Assert::exception(function () {
113+
(new Nette\PhpGenerator\Property('foo'))->setType('a b');
114+
}, Nette\InvalidArgumentException::class);
115+
112116

113117
Assert::noError(function () {
114118
new Nette\PhpGenerator\Parameter('Iñtërnâtiônàlizætiøn');
@@ -130,6 +134,10 @@ Assert::exception(function () {
130134
new Nette\PhpGenerator\Parameter('$test');
131135
}, Nette\InvalidArgumentException::class);
132136

137+
Assert::exception(function () {
138+
(new Nette\PhpGenerator\Parameter('foo'))->setType('a b');
139+
}, Nette\InvalidArgumentException::class);
140+
133141

134142
Assert::noError(function () {
135143
new Nette\PhpGenerator\Method('Iñtërnâtiônàlizætiøn');
@@ -147,6 +155,10 @@ Assert::exception(function () {
147155
new Nette\PhpGenerator\Method('*');
148156
}, Nette\InvalidArgumentException::class);
149157

158+
Assert::exception(function () {
159+
(new Nette\PhpGenerator\Method('foo'))->setReturnType('a b');
160+
}, Nette\InvalidArgumentException::class);
161+
150162

151163
Assert::noError(function () {
152164
new Nette\PhpGenerator\GlobalFunction('Iñtërnâtiônàlizætiøn');

0 commit comments

Comments
 (0)