Skip to content

Commit 7f6270b

Browse files
committed
added support for DNF types
1 parent 17ce530 commit 7f6270b

File tree

7 files changed

+63
-7
lines changed

7 files changed

+63
-7
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

+8-4
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,14 @@ public static function validateType(?string $type, bool &$nullable): ?string
174174
return null;
175175
}
176176

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

tests/PhpGenerator/ClassType.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ $method->addParameter('res', null)
131131
->setType(Type::union(Type::Array, 'null'));
132132

133133
$method->addParameter('bar', null)
134-
->setType('stdClass|string')
135-
->setNullable(true);
134+
->setNullable(true)
135+
->setType('stdClass|string');
136136

137137
$class->addTrait('foo');
138138
$class->removeTrait('foo');
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)