Skip to content

Commit d645813

Browse files
committed
Add Nullable hybrid type.
1 parent 18771de commit d645813

File tree

6 files changed

+254
-6
lines changed

6 files changed

+254
-6
lines changed

src/TypedGen.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
use loophp\TypedGenerators\Types\Core\ObjectType;
2525
use loophp\TypedGenerators\Types\Core\StringType;
2626
use loophp\TypedGenerators\Types\Hybrid\Compound;
27-
use loophp\TypedGenerators\Types\Hybrid\FakerType;
27+
use loophp\TypedGenerators\Types\Hybrid\Faker;
28+
use loophp\TypedGenerators\Types\Hybrid\Nullable;
2829
use loophp\TypedGenerators\Types\TypeGenerator;
2930

3031
final class TypedGen
@@ -78,11 +79,11 @@ public static function datetime(?DateTimeInterface $from = null, ?DateTimeInterf
7879
* @param TypeGenerator<V> $type
7980
* @param Closure(Generator): V $fakerGenerator
8081
*
81-
* @return FakerType<V>
82+
* @return Faker<V>
8283
*/
83-
public static function faker(TypeGenerator $type, Closure $fakerGenerator, ?Generator $faker = null): FakerType
84+
public static function faker(TypeGenerator $type, Closure $fakerGenerator, ?Generator $faker = null): Faker
8485
{
85-
return FakerType::new($type, $fakerGenerator, $faker);
86+
return Faker::new($type, $fakerGenerator, $faker);
8687
}
8788

8889
public static function float(): FloatType
@@ -126,6 +127,18 @@ public static function null(): NullType
126127
return NullType::new();
127128
}
128129

130+
/**
131+
* @template W
132+
*
133+
* @param TypeGenerator<W> $type
134+
*
135+
* @return Nullable<W>
136+
*/
137+
public static function nullable(TypeGenerator $type): Nullable
138+
{
139+
return Nullable::new($type);
140+
}
141+
129142
public static function object(): ObjectType
130143
{
131144
return ObjectType::new();

src/Types/Hybrid/Nullable.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* For the full copyright and license information, please view
5+
* the LICENSE file that was distributed with this source code.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace loophp\TypedGenerators\Types\Hybrid;
11+
12+
use Iterator;
13+
use loophp\TypedGenerators\Types\Core\BoolType;
14+
use loophp\TypedGenerators\Types\Core\NullType;
15+
use loophp\TypedGenerators\Types\TypeGenerator;
16+
17+
/**
18+
* @template T
19+
*
20+
* @implements TypeGenerator<T|null>
21+
*/
22+
final class Nullable implements TypeGenerator
23+
{
24+
/**
25+
* @var TypeGenerator<T>
26+
*/
27+
private TypeGenerator $type;
28+
29+
/**
30+
* @param TypeGenerator<T> $type
31+
*/
32+
public function __construct(
33+
TypeGenerator $type
34+
) {
35+
$this->type = $type;
36+
}
37+
38+
/**
39+
* @return T|null
40+
*/
41+
public function __invoke()
42+
{
43+
return (new BoolType())() ? ($this->type)() : NullType::new()();
44+
}
45+
46+
/**
47+
* @return Iterator<int, T|null>
48+
*/
49+
public function getIterator(): Iterator
50+
{
51+
// @phpstan-ignore-next-line
52+
while (true) {
53+
yield $this->__invoke();
54+
}
55+
}
56+
57+
/**
58+
* @param T|null $input
59+
*
60+
* @return T|null
61+
*/
62+
public function identity($input)
63+
{
64+
return $input;
65+
}
66+
67+
/**
68+
* @template V
69+
*
70+
* @param TypeGenerator<V> $type
71+
*
72+
* @return Nullable<V>
73+
*/
74+
public static function new(TypeGenerator $type): self
75+
{
76+
return new self($type);
77+
}
78+
}

tests/static-analysis/Types/Core/IteratorType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Faker\Generator;
1111
use loophp\TypedGenerators\Types\Core\IteratorType;
1212
use loophp\TypedGenerators\Types\Core\StringType;
13-
use loophp\TypedGenerators\Types\Hybrid\FakerType;
13+
use loophp\TypedGenerators\Types\Hybrid\Faker;
1414

1515
/**
1616
* @param iterable<string, string> $iterable
@@ -19,7 +19,7 @@ function foobar(iterable $iterable): void
1919
{
2020
}
2121

22-
$fakerType = FakerType::new(
22+
$fakerType = Faker::new(
2323
StringType::new(),
2424
static function (Generator $faker): string {
2525
return $faker->city();

tests/unit/TypeGenTest.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
/**
4+
* For the full copyright and license information, please view
5+
* the LICENSE file that was distributed with this source code.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace tests\loophp\TypedGenerators;
11+
12+
use Faker\Generator;
13+
use loophp\TypedGenerators\TypedGen;
14+
use loophp\TypedGenerators\Types\Core\ArrayType;
15+
use loophp\TypedGenerators\Types\Core\BoolType;
16+
use loophp\TypedGenerators\Types\Core\ClosureType;
17+
use loophp\TypedGenerators\Types\Core\DateTimeType;
18+
use loophp\TypedGenerators\Types\Core\FloatType;
19+
use loophp\TypedGenerators\Types\Core\IntType;
20+
use loophp\TypedGenerators\Types\Core\IteratorType;
21+
use loophp\TypedGenerators\Types\Core\ListType;
22+
use loophp\TypedGenerators\Types\Core\NullType;
23+
use loophp\TypedGenerators\Types\Core\ObjectType;
24+
use loophp\TypedGenerators\Types\Core\StringType;
25+
use loophp\TypedGenerators\Types\Hybrid\Compound;
26+
use loophp\TypedGenerators\Types\Hybrid\Faker;
27+
use loophp\TypedGenerators\Types\Hybrid\Nullable;
28+
use PHPUnit\Framework\TestCase;
29+
30+
/**
31+
* @internal
32+
* @coversDefaultClass \loophp\TypedGenerators
33+
*/
34+
final class TypeGenTest extends TestCase
35+
{
36+
/**
37+
* @dataProvider typeProvider
38+
*/
39+
public function testStaticFactories(string $method, array $arguments, string $class)
40+
{
41+
self::assertInstanceOf($class, TypedGen::{$method}(...$arguments));
42+
}
43+
44+
public function typeProvider()
45+
{
46+
yield [
47+
'method' => 'array',
48+
'arguments' => [
49+
new StringType(),
50+
new StringType(),
51+
],
52+
'class' => ArrayType::class,
53+
];
54+
55+
yield [
56+
'method' => 'bool',
57+
'arguments' => [],
58+
'class' => BoolType::class,
59+
];
60+
61+
yield [
62+
'method' => 'closure',
63+
'arguments' => [],
64+
'class' => ClosureType::class,
65+
];
66+
67+
yield [
68+
'method' => 'compound',
69+
'arguments' => [
70+
new StringType(),
71+
new IntType(),
72+
],
73+
'class' => Compound::class,
74+
];
75+
76+
yield [
77+
'method' => 'datetime',
78+
'arguments' => [],
79+
'class' => DateTimeType::class,
80+
];
81+
82+
yield [
83+
'method' => 'faker',
84+
'arguments' => [
85+
new StringType(),
86+
static fn (Generator $faker): string => $faker->city(),
87+
],
88+
'class' => Faker::class,
89+
];
90+
91+
yield [
92+
'method' => 'float',
93+
'arguments' => [],
94+
'class' => FloatType::class,
95+
];
96+
97+
yield [
98+
'method' => 'int',
99+
'arguments' => [],
100+
'class' => IntType::class,
101+
];
102+
103+
yield [
104+
'method' => 'iterator',
105+
'arguments' => [
106+
new StringType(),
107+
new IntType(),
108+
],
109+
'class' => IteratorType::class,
110+
];
111+
112+
yield [
113+
'method' => 'list',
114+
'arguments' => [
115+
new StringType(),
116+
],
117+
'class' => ListType::class,
118+
];
119+
120+
yield [
121+
'method' => 'null',
122+
'arguments' => [],
123+
'class' => NullType::class,
124+
];
125+
126+
yield [
127+
'method' => 'nullable',
128+
'arguments' => [
129+
new StringType(),
130+
],
131+
'class' => Nullable::class,
132+
];
133+
134+
yield [
135+
'method' => 'object',
136+
'arguments' => [],
137+
'class' => ObjectType::class,
138+
];
139+
140+
yield [
141+
'method' => 'string',
142+
'arguments' => [],
143+
'class' => StringType::class,
144+
];
145+
}
146+
}

tests/unit/Types/Core/IteratorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use loophp\TypedGenerators\Types\Core\NullType;
2222
use loophp\TypedGenerators\Types\Core\ObjectType;
2323
use loophp\TypedGenerators\Types\Core\StringType;
24+
use loophp\TypedGenerators\Types\Hybrid\Nullable;
2425
use loophp\TypedGenerators\Types\TypeGenerator;
2526
use PHPUnit\Framework\TestCase;
2627

@@ -87,6 +88,11 @@ public function typeProvider()
8788
new IntType(),
8889
];
8990

91+
yield [
92+
new Nullable(new StringType()),
93+
new Nullable(new StringType()),
94+
];
95+
9096
yield [
9197
new NullType(),
9298
new NullType(),

tests/unit/Types/Core/ListTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use loophp\TypedGenerators\Types\Core\NullType;
2121
use loophp\TypedGenerators\Types\Core\ObjectType;
2222
use loophp\TypedGenerators\Types\Core\StringType;
23+
use loophp\TypedGenerators\Types\Hybrid\Nullable;
2324
use loophp\TypedGenerators\Types\TypeGenerator;
2425
use PHPUnit\Framework\TestCase;
2526

@@ -78,6 +79,10 @@ public function typeProvider()
7879
new IntType(),
7980
];
8081

82+
yield [
83+
new Nullable(new StringType()),
84+
];
85+
8186
yield [
8287
new NullType(),
8388
];

0 commit comments

Comments
 (0)