Skip to content

Commit 8959ed2

Browse files
committed
added support for DNF types
1 parent 5de8c7f commit 8959ed2

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"require-dev": {
2222
"nette/tester": "^2.4",
23-
"nikic/php-parser": "^4.14",
23+
"nikic/php-parser": "^4.15",
2424
"tracy/tracy": "^2.8",
2525
"phpstan/phpstan": "^1.0"
2626
},

src/PhpGenerator/Helpers.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,19 @@ public static function createObject(string $class, array $props): object
170170

171171
public static function validateType(?string $type, bool &$nullable): ?string
172172
{
173+
$nullable = false;
173174
if ($type === '' || $type === null) {
174175
return null;
175176
}
176177

177-
if (!preg_match('#(?:
178-
\?[\w\\\\]+|
179-
[\w\\\\]+ (?: (&[\w\\\\]+)* | (\|[\w\\\\]+)* )
180-
)()$#xAD', $type)) {
178+
if (!preg_match(<<<'XX'
179+
~(?n)
180+
(
181+
\?? (?<type> [\w\\]+)|
182+
(?<intersection> (?&type) (& (?&type))+ )|
183+
(?<upart> (?&type) | \( (?&intersection) \) ) (\| (?&upart) )+
184+
)$~xAD
185+
XX, $type)) {
181186
throw new Nette\InvalidArgumentException("Value '$type' is not valid type.");
182187
}
183188

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\Helpers;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
$foo = false;
13+
Assert::null(Helpers::validateType('', $foo));
14+
Assert::null(Helpers::validateType(null, $foo));
15+
Assert::same('Foo', Helpers::validateType('Foo', $foo));
16+
Assert::same('Foo\Bar', Helpers::validateType('Foo\Bar', $foo));
17+
Assert::same('\Foo\Bar', Helpers::validateType('\Foo\Bar', $foo));
18+
Assert::same('Foo', Helpers::validateType('?Foo', $foo));
19+
Assert::true($foo);
20+
Assert::same('Foo|Bar', Helpers::validateType('Foo|Bar', $foo));
21+
Assert::same('Foo&Bar\X', Helpers::validateType('Foo&Bar\X', $foo));
22+
Assert::same('(Foo&Bar\X)|Baz', Helpers::validateType('(Foo&Bar\X)|Baz', $foo));
23+
Assert::same('Abc\C|(Abc\X&Abc\D)|null', Helpers::validateType('Abc\C|(Abc\X&Abc\D)|null', $foo));
24+
25+
Assert::exception(
26+
fn() => Helpers::validateType('-', $foo),
27+
Nette\InvalidArgumentException::class,
28+
);
29+
30+
Assert::exception(
31+
fn() => Helpers::validateType('?Foo|Bar', $foo),
32+
Nette\InvalidArgumentException::class,
33+
);
34+
35+
Assert::exception(
36+
fn() => Helpers::validateType('(Foo)', $foo),
37+
Nette\InvalidArgumentException::class,
38+
);
39+
40+
Assert::exception(
41+
fn() => Helpers::validateType('(Foo&Bar)', $foo),
42+
Nette\InvalidArgumentException::class,
43+
);

tests/PhpGenerator/expected/ClassType.from.82.expect

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
readonly class Class13
22
{
3+
public function func(C|(X&D)|null $foo): (A&B)|null
4+
{
5+
}
36
}
47

58
trait Trait13

tests/PhpGenerator/expected/Extractor.classes.82.expect

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ namespace Abc;
66

77
readonly class Class13
88
{
9+
public function func(C|(X&D)|null $foo): (A&B)|null
10+
{
11+
}
912
}
1013

1114
trait Trait13

tests/PhpGenerator/fixtures/classes.82.php

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
readonly class Class13
88
{
9+
public function func(C|(X&D)|null $foo): (A&B)|null
10+
{
11+
}
912
}
1013

1114

0 commit comments

Comments
 (0)