Skip to content

Commit

Permalink
added support for DNF types
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2022
1 parent 17ce530 commit 7f6270b
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"require-dev": {
"nette/tester": "^2.4",
"nikic/php-parser": "^4.14",
"nikic/php-parser": "^4.15",
"tracy/tracy": "^2.8",
"phpstan/phpstan": "^1.0"
},
Expand Down
12 changes: 8 additions & 4 deletions src/PhpGenerator/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,14 @@ public static function validateType(?string $type, bool &$nullable): ?string
return null;
}

if (!preg_match('#(?:
\?[\w\\\\]+|
[\w\\\\]+ (?: (&[\w\\\\]+)* | (\|[\w\\\\]+)* )
)()$#xAD', $type)) {
if (!preg_match(<<<'XX'
~(?n)
(
\?? (?<type> [\w\\]+)|
(?<intersection> (?&type) (& (?&type))+ )|
(?<upart> (?&type) | \( (?&intersection) \) ) (\| (?&upart) )+
)$~xAD
XX, $type)) {
throw new Nette\InvalidArgumentException("Value '$type' is not valid type.");
}

Expand Down
4 changes: 2 additions & 2 deletions tests/PhpGenerator/ClassType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ $method->addParameter('res', null)
->setType(Type::union(Type::Array, 'null'));

$method->addParameter('bar', null)
->setType('stdClass|string')
->setNullable(true);
->setNullable(true)
->setType('stdClass|string');

$class->addTrait('foo');
$class->removeTrait('foo');
Expand Down
43 changes: 43 additions & 0 deletions tests/PhpGenerator/Helpers.validateType.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

use Nette\PhpGenerator\Helpers;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$foo = false;
Assert::null(Helpers::validateType('', $foo));
Assert::null(Helpers::validateType(null, $foo));
Assert::same('Foo', Helpers::validateType('Foo', $foo));
Assert::same('Foo\Bar', Helpers::validateType('Foo\Bar', $foo));
Assert::same('\Foo\Bar', Helpers::validateType('\Foo\Bar', $foo));
Assert::same('Foo', Helpers::validateType('?Foo', $foo));
Assert::true($foo);
Assert::same('Foo|Bar', Helpers::validateType('Foo|Bar', $foo));
Assert::same('Foo&Bar\X', Helpers::validateType('Foo&Bar\X', $foo));
Assert::same('(Foo&Bar\X)|Baz', Helpers::validateType('(Foo&Bar\X)|Baz', $foo));
Assert::same('Abc\C|(Abc\X&Abc\D)|null', Helpers::validateType('Abc\C|(Abc\X&Abc\D)|null', $foo));

Assert::exception(
fn() => Helpers::validateType('-', $foo),
Nette\InvalidArgumentException::class,
);

Assert::exception(
fn() => Helpers::validateType('?Foo|Bar', $foo),
Nette\InvalidArgumentException::class,
);

Assert::exception(
fn() => Helpers::validateType('(Foo)', $foo),
Nette\InvalidArgumentException::class,
);

Assert::exception(
fn() => Helpers::validateType('(Foo&Bar)', $foo),
Nette\InvalidArgumentException::class,
);
3 changes: 3 additions & 0 deletions tests/PhpGenerator/expected/ClassType.from.82.expect
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
readonly class Class13
{
public function func(C|(X&D)|null $foo): (A&B)|null
{
}
}

trait Trait13
Expand Down
3 changes: 3 additions & 0 deletions tests/PhpGenerator/expected/Extractor.classes.82.expect
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace Abc;

readonly class Class13
{
public function func(C|(X&D)|null $foo): (A&B)|null
{
}
}

trait Trait13
Expand Down
3 changes: 3 additions & 0 deletions tests/PhpGenerator/fixtures/classes.82.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

readonly class Class13
{
public function func(C|(X&D)|null $foo): (A&B)|null
{
}
}


Expand Down

0 comments on commit 7f6270b

Please sign in to comment.