Skip to content

Commit

Permalink
Expect::from() supports union types
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 25, 2020
1 parent f9e81f5 commit 34baf9e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coding-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.1
php-version: 8.0
coverage: none

- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:


- name: Nette Code Checker
php: 8.0snapshot
install:
- travis_retry composer create-project nette/code-checker temp/code-checker ^3 --no-progress
script:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"require": {
"php": ">=7.1 <8.1",
"nette/utils": "^3.1"
"nette/utils": "^3.1.4"
},
"require-dev": {
"nette/tester": "^2.2",
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public static function merge($value, $base)

public static function getPropertyType(\ReflectionProperty $prop): ?string
{
if ($type = Reflection::getPropertyType($prop)) {
return ($prop->getType()->allowsNull() ? '?' : '') . $type;
if ($types = Reflection::getPropertyTypes($prop)) {
return implode('|', $types);
} elseif ($type = preg_replace('#\s.*#', '', (string) self::parseAnnotation($prop, 'var'))) {
$class = Reflection::getPropertyDeclaringClass($prop);
return preg_replace_callback('#[\w\\\\]+#', function ($m) use ($class) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Schema/Expect.from.php74.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Assert::with(Structure::class, function () {
Assert::type(Structure::class, $schema);
Assert::equal([
'dsn' => Expect::string('mysql'),
'user' => Expect::type('?string')->required(),
'password' => Expect::type('?string'),
'user' => Expect::type('string|null')->required(),
'password' => Expect::type('string|null'),
'options' => Expect::array(),
'debugger' => Expect::bool(true),
'mixed' => Expect::mixed(),
Expand Down
39 changes: 39 additions & 0 deletions tests/Schema/Expect.from.php80.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* @phpversion 8.0
*/

declare(strict_types=1);

use Nette\Schema\Elements\Structure;
use Nette\Schema\Expect;
use Tester\Assert;


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


Assert::with(Structure::class, function () {
$schema = Expect::from(new class {
public string $dsn = 'mysql';
public ?string $user;
public ?string $password = null;
public array|int $options = [];
public bool $debugger = true;
public mixed $mixed;
public array $arr = [1];
});

Assert::type(Structure::class, $schema);
Assert::equal([
'dsn' => Expect::string('mysql'),
'user' => Expect::type('string|null')->required(),
'password' => Expect::type('string|null'),
'options' => Expect::type('array|int')->default([]),
'debugger' => Expect::bool(true),
'mixed' => Expect::mixed()->required(),
'arr' => Expect::type('array')->default([1]),
], $schema->items);
Assert::type('string', $schema->castTo);
});
6 changes: 3 additions & 3 deletions tests/Schema/Helpers.getPropertyType.php74.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ Assert::same('string', Helpers::getPropertyType(new \ReflectionProperty(NS\A::cl

Assert::same('NS\A', Helpers::getPropertyType(new \ReflectionProperty(NS\A::class, 'selfType')));

Assert::same('?Test\B', Helpers::getPropertyType(new \ReflectionProperty(NS\A::class, 'nullableClassType')));
Assert::same('Test\B|null', Helpers::getPropertyType(new \ReflectionProperty(NS\A::class, 'nullableClassType')));

Assert::same('?string', Helpers::getPropertyType(new \ReflectionProperty(NS\A::class, 'nullableNativeType')));
Assert::same('string|null', Helpers::getPropertyType(new \ReflectionProperty(NS\A::class, 'nullableNativeType')));

Assert::same('?NS\A', Helpers::getPropertyType(new \ReflectionProperty(NS\A::class, 'nullableSelfType')));
Assert::same('NS\A|null', Helpers::getPropertyType(new \ReflectionProperty(NS\A::class, 'nullableSelfType')));

Assert::same('Test\B', Helpers::getPropertyType(new \ReflectionProperty(NS\A::class, 'annotationClassType')));

Expand Down

0 comments on commit 34baf9e

Please sign in to comment.