Skip to content

Instantly share code, notes, and snippets.

@drupol
Last active June 8, 2022 04:53
Show Gist options
  • Save drupol/d4f94fec4c2e25a0cc4eb227a8dd2c49 to your computer and use it in GitHub Desktop.
Save drupol/d4f94fec4c2e25a0cc4eb227a8dd2c49 to your computer and use it in GitHub Desktop.
loophp/typed-generators test
<?php
declare(strict_types=1);
namespace Snippet;
use Faker\Generator;
use loophp\TypedGenerators\TG;
include __DIR__ . '/vendor/autoload.php';
$iterator = TG::array(TG::static(TG::string(), 'id'), TG::int(6))
->add(
TG::static(TG::string(), 'uuid'),
TG::uniqid()
)
->add(
TG::static(TG::string(), 'firstName'),
TG::faker(
TG::string(),
static fn (Generator $faker): string => $faker->firstName()
)
)
->add(
TG::static(TG::string(), 'country'),
TG::faker(
TG::string(),
static fn (Generator $faker): string => $faker->country()
)
)
->add(
TG::static(TG::string(), 'isCitizen'),
TG::bool()
)
->add(
TG::static(TG::string(), 'hometowm'),
TG::faker(
TG::string(),
static fn (Generator $faker): string => $faker->city()
)
)
->add(
TG::static(TG::string(), 'lastSeen'),
TG::nullable(TG::datetime())
)
->add(
TG::static(TG::string(), 'foo'),
TG::array(
TG::string(),
TG::bool()
)->add(
TG::int(),
TG::custom(TG::string(), static fn () => 'Hello!')
)
);
\PHPStan\dumpType($iterator());
// This produces:
// Array
// (
// [id] => 505975
// [uuid] => 62a02a3890ded
// [firstName] => Judd
// [country] => Japan
// [isCitizen] => true
// [hometowm] => Hintzport
// [lastSeen] => DateTimeImmutable Object
// (
// [date] => 2020-05-31 17:33:19.000000
// [timezone_type] => 3
// [timezone] => UTC
// )
// [foo] => Array
// (
// [+] => 1
// [6] => Hello!
// )
// )
// PHPStan:
// Note: Using configuration file /home/pol/Code/loophp/typed-generators/phpstan.neon.dist.
// 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
// ------ ------------------------------------------------------------------------------------------------
// Line test-gen.php
// ------ ------------------------------------------------------------------------------------------------
// 57 Dumped type: array<string, array<string, bool|string>|bool|DateTimeInterface|int|string|null>
// ------ ------------------------------------------------------------------------------------------------
// PSalm:
// ERROR: Trace - test-gen.php:70:0 - $v: array<string, DateTimeInterface|(array<TKey:loophp\TypedGenerators\Types\Core\ArrayType as array-key, T:loophp\TypedGenerators\Types\Core\ArrayType as mixed>)|bool|int|null|string> (see https://psalm.dev/224)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment