Skip to content

Commit a47a595

Browse files
committed
Checks whether names are valid [Closes #24]
1 parent ff01f24 commit a47a595

File tree

5 files changed

+162
-2
lines changed

5 files changed

+162
-2
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ public function getNamespace()
141141
*/
142142
public function setName($name)
143143
{
144+
if ($name !== NULL && !Helpers::isIdentifier($name)) {
145+
throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
146+
}
144147
$this->name = $name;
145148
return $this;
146149
}

src/PhpGenerator/Method.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ public function __construct($name)
5555
{
5656
if ($name === NULL) {
5757
throw new Nette\DeprecatedException('For closures use Nette\PhpGenerator\Closure instead of Nette\PhpGenerator\Method.');
58+
} elseif (!Helpers::isIdentifier($name)) {
59+
throw new Nette\InvalidArgumentException("Value '$name' is not valid name.");
5860
}
59-
$this->name = (string) $name;
61+
$this->name = $name;
6062
}
6163

6264

src/PhpGenerator/PhpNamespace.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class PhpNamespace
4242
*/
4343
public function __construct($name = NULL)
4444
{
45+
if ($name && !Helpers::isNamespace($name)) {
46+
throw new Nette\InvalidArgumentException("Value '$name' is not valid name.");
47+
}
4548
$this->name = (string) $name;
4649
}
4750

src/PhpGenerator/Traits/NameAware.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Nette\PhpGenerator\Traits;
99

10+
use Nette;
11+
1012

1113
/**
1214
* @internal
@@ -22,7 +24,10 @@ trait NameAware
2224
*/
2325
public function __construct($name)
2426
{
25-
$this->name = (string) $name;
27+
if (!Nette\PhpGenerator\Helpers::isIdentifier($name)) {
28+
throw new Nette\InvalidArgumentException("Value '$name' is not valid name.");
29+
}
30+
$this->name = $name;
2631
}
2732

2833

tests/PhpGenerator/invalidNames.phpt

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator for files.
5+
*/
6+
7+
use Tester\Assert;
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
Assert::noError(function () {
13+
new Nette\PhpGenerator\PhpNamespace(''); // global namespace
14+
new Nette\PhpGenerator\PhpNamespace(NULL); // global namespace for back compatibility
15+
new Nette\PhpGenerator\PhpNamespace('Iñtërnâti\ônàlizætiøn');
16+
});
17+
18+
Assert::exception(function () {
19+
new Nette\PhpGenerator\PhpNamespace('*');
20+
}, Nette\InvalidArgumentException::class);
21+
22+
Assert::exception(function () {
23+
new Nette\PhpGenerator\PhpNamespace('abc abc');
24+
}, Nette\InvalidArgumentException::class);
25+
26+
Assert::exception(function () {
27+
new Nette\PhpGenerator\PhpNamespace('abc\\');
28+
}, Nette\InvalidArgumentException::class);
29+
30+
Assert::exception(function () {
31+
new Nette\PhpGenerator\PhpNamespace('\\abc');
32+
}, Nette\InvalidArgumentException::class);
33+
34+
35+
Assert::noError(function () {
36+
new Nette\PhpGenerator\ClassType(NULL); // anonymous class
37+
new Nette\PhpGenerator\ClassType('Iñtërnâtiônàlizætiøn');
38+
});
39+
40+
Assert::exception(function () {
41+
new Nette\PhpGenerator\ClassType('');
42+
}, Nette\InvalidArgumentException::class);
43+
44+
Assert::exception(function () {
45+
new Nette\PhpGenerator\ClassType('*');
46+
}, Nette\InvalidArgumentException::class);
47+
48+
Assert::exception(function () {
49+
new Nette\PhpGenerator\ClassType('abc abc');
50+
}, Nette\InvalidArgumentException::class);
51+
52+
Assert::exception(function () {
53+
new Nette\PhpGenerator\ClassType('abc\\abc');
54+
}, Nette\InvalidArgumentException::class);
55+
56+
Assert::exception(function () {
57+
new Nette\PhpGenerator\ClassType('\\abc');
58+
}, Nette\InvalidArgumentException::class);
59+
60+
61+
Assert::noError(function () {
62+
new Nette\PhpGenerator\Property('Iñtërnâtiônàlizætiøn');
63+
});
64+
65+
Assert::exception(function () {
66+
new Nette\PhpGenerator\Property(NULL);
67+
}, Nette\InvalidArgumentException::class);
68+
69+
Assert::exception(function () {
70+
new Nette\PhpGenerator\Property('');
71+
}, Nette\InvalidArgumentException::class);
72+
73+
Assert::exception(function () {
74+
new Nette\PhpGenerator\Property('*');
75+
}, Nette\InvalidArgumentException::class);
76+
77+
78+
Assert::noError(function () {
79+
new Nette\PhpGenerator\Parameter('Iñtërnâtiônàlizætiøn');
80+
});
81+
82+
Assert::exception(function () {
83+
new Nette\PhpGenerator\Parameter('');
84+
}, Nette\InvalidArgumentException::class);
85+
86+
Assert::exception(function () {
87+
new Nette\PhpGenerator\Parameter(NULL);
88+
}, Nette\InvalidArgumentException::class);
89+
90+
Assert::exception(function () {
91+
new Nette\PhpGenerator\Parameter('*');
92+
}, Nette\InvalidArgumentException::class);
93+
94+
Assert::exception(function () {
95+
new Nette\PhpGenerator\Parameter('$test');
96+
}, Nette\InvalidArgumentException::class);
97+
98+
99+
Assert::noError(function () {
100+
new Nette\PhpGenerator\Method('Iñtërnâtiônàlizætiøn');
101+
});
102+
103+
Assert::exception(function () {
104+
new Nette\PhpGenerator\Method('');
105+
}, Nette\InvalidArgumentException::class);
106+
107+
Assert::exception(function () {
108+
new Nette\PhpGenerator\Method(NULL);
109+
}, Nette\DeprecatedException::class);
110+
111+
Assert::exception(function () {
112+
new Nette\PhpGenerator\Method('*');
113+
}, Nette\InvalidArgumentException::class);
114+
115+
116+
Assert::noError(function () {
117+
new Nette\PhpGenerator\GlobalFunction('Iñtërnâtiônàlizætiøn');
118+
});
119+
120+
Assert::exception(function () {
121+
new Nette\PhpGenerator\GlobalFunction('');
122+
}, Nette\InvalidArgumentException::class);
123+
124+
Assert::exception(function () {
125+
new Nette\PhpGenerator\GlobalFunction(NULL);
126+
}, Nette\InvalidArgumentException::class);
127+
128+
Assert::exception(function () {
129+
new Nette\PhpGenerator\GlobalFunction('*');
130+
}, Nette\InvalidArgumentException::class);
131+
132+
133+
Assert::noError(function () {
134+
new Nette\PhpGenerator\Constant('Iñtërnâtiônàlizætiøn');
135+
});
136+
137+
Assert::exception(function () {
138+
new Nette\PhpGenerator\Constant(NULL);
139+
}, Nette\InvalidArgumentException::class);
140+
141+
Assert::exception(function () {
142+
new Nette\PhpGenerator\Constant('');
143+
}, Nette\InvalidArgumentException::class);
144+
145+
Assert::exception(function () {
146+
new Nette\PhpGenerator\Constant('*');
147+
}, Nette\InvalidArgumentException::class);

0 commit comments

Comments
 (0)