Skip to content

Commit

Permalink
callable generators
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyBel committed Jul 12, 2023
1 parent ffd18cf commit 5828df3
Show file tree
Hide file tree
Showing 28 changed files with 50 additions and 124 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ A config file can be passed in `--config` option:
PhpInvariant allows writing custom generators to generate data for you needs

To write a generator you need to create two classes and add they in config file:
1. `Type` class. This class uses as attribute and store settings for generator It must implements `TypeInterface`.
1. `Generator` class. It generates data based on settings from `Type` class. Must implements `GeneratorInterface`
1. `Type` class. This class uses as attribute and store settings for generator
1. `Generator` class. It generates data based on settings from `Type` class. Must implements `__invoke` method
1. Add classes in `generators` configuration file section
```yml
parameters:
Expand Down
8 changes: 3 additions & 5 deletions custom/CustomGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\Random\Random;

class CustomGenerator implements GeneratorInterface
class CustomGenerator
{
public function __construct(
private Random $random
) {
}


/**
* @param CustomType $type
*/
public function generate(TypeInterface $type): string

public function __invoke(CustomType $type): string
{
return $type->prefix.' '.$this->random->getInt(1, 100);

Expand Down
2 changes: 1 addition & 1 deletion custom/CustomType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PhpInvariant\Generator\TypeInterface;

#[Attribute]
class CustomType implements TypeInterface
class CustomType
{
public function __construct(public string $prefix)
{
Expand Down
3 changes: 1 addition & 2 deletions src/CheckMethodRunner/CheckMethodCaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PhpInvariant\CheckMethodRunner;

use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\CheckMethodRunner\Dto\MethodRunResult;
use ReflectionClass;
use ReflectionException;
Expand All @@ -16,7 +15,7 @@ public function __construct(
}

/**
* @param array<TypeInterface> $types
* @param array<mixed> $types
* @throws ReflectionException
*/
public function callMethod(ReflectionClass $checkClass, ReflectionMethod $checkMethod, array $types, MethodRunResult $result): void
Expand Down
5 changes: 2 additions & 3 deletions src/CheckMethodRunner/CheckMethodGeneratorsCaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PhpInvariant\BaseInvariant\Exception\PhpInvariantAssertException;
use PhpInvariant\Generator\GeneratorFactory;
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\CheckMethodRunner\Dto\ErrorRunResult;
use PhpInvariant\CheckMethodRunner\Dto\CheckMethodCallResult;
use PHPUnit\Framework\ExpectationFailedException;
Expand All @@ -20,7 +19,7 @@ public function __construct(
}

/**
* @param array<TypeInterface> $types
* @param array<mixed> $types
* @throws ReflectionException
* @throws ExpectationFailedException
*/
Expand All @@ -29,7 +28,7 @@ public function callMethod(ReflectionClass $checkClass, ReflectionMethod $checkM
$parameters = [];
foreach ($types as $type) {
$generator = $this->generatorFactory->getGenerator($type);
$parameters[] = $generator->generate($type);
$parameters[] = $generator($type);
}
$result = new CheckMethodCallResult();

Expand Down
3 changes: 1 addition & 2 deletions src/CheckMethodRunner/Condition/CountCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PhpInvariant\CheckMethodRunner\Condition;

use PhpInvariant\Finish\FinishCount;
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\CheckMethodRunner\Dto\MethodRunResult;
use PhpInvariant\CheckMethodRunner\CheckMethodCaller;
use ReflectionClass;
Expand All @@ -18,7 +17,7 @@ public function __construct(
}

/**
* @param array<TypeInterface> $types
* @param array<mixed> $types
* @throws ReflectionException
*/
public function run(ReflectionClass $checkClass, ReflectionMethod $checkMethod, array $types, FinishCount $finishCondition): MethodRunResult
Expand Down
3 changes: 1 addition & 2 deletions src/CheckMethodRunner/Condition/TimeCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PhpInvariant\CheckMethodRunner\Condition;

use PhpInvariant\Finish\FinishTime;
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\CheckMethodRunner\Dto\MethodRunResult;
use PhpInvariant\CheckMethodRunner\CheckMethodCaller;
use ReflectionClass;
Expand All @@ -19,7 +18,7 @@ public function __construct(


/**
* @param array<TypeInterface> $types
* @param array<mixed> $types
* @throws ReflectionException
*/
public function run(ReflectionClass $checkClass, ReflectionMethod $checkMethod, array $types, FinishTime $finishCondition): MethodRunResult
Expand Down
4 changes: 0 additions & 4 deletions src/Generator/Exception/GeneratorNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@ public static function notFoundForType(string $typeName): self
return new self(sprintf('can not find generator for type %s', $typeName));
}

public static function notImplementInterface(string $generatorName): self
{
return new self(sprintf('generator %s must implement GeneratorInterface', $generatorName));
}

}
10 changes: 4 additions & 6 deletions src/Generator/Generator/Arrays/ArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,29 @@

namespace PhpInvariant\Generator\Generator\Arrays;

use PhpInvariant\Generator\Generator\GeneratorInterface;
use PhpInvariant\Generator\GeneratorFactory;
use PhpInvariant\Generator\Type\Arrays\ArrayType;
use PhpInvariant\Generator\TypeInterface;

class ArrayGenerator implements GeneratorInterface
class ArrayGenerator
{
public function __construct(
private GeneratorFactory $generatorFactory
) {
}

/**
* @param ArrayType $type
* @return array<mixed>
*/
public function generate(TypeInterface $type): array
public function __invoke(ArrayType $type): array
{
$generator = $this->generatorFactory->getGenerator($type->elementType);
$data = [];
for ($i = 0; $i < $type->count; $i++) {
$data[] = $generator->generate($type->elementType);
$data[] = $generator($type->elementType);
}
return $data;
}



}
7 changes: 2 additions & 5 deletions src/Generator/Generator/Arrays/FromArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

namespace PhpInvariant\Generator\Generator\Arrays;

use PhpInvariant\Generator\Generator\GeneratorInterface;
use PhpInvariant\Generator\Type\Arrays\FromArrayType;
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\Random\Random;

class FromArrayGenerator implements GeneratorInterface
class FromArrayGenerator
{
public function __construct(
private Random $random
) {
}

/**
* @param FromArrayType $type
* @return array<mixed>
*/
public function generate(TypeInterface $type): array
public function __invoke(FromArrayType $type): array
{
$result = [];
for ($i = 0; $i < $type->count; $i++) {
Expand Down
12 changes: 4 additions & 8 deletions src/Generator/Generator/Combine/OneOfGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@

namespace PhpInvariant\Generator\Generator\Combine;

use PhpInvariant\Generator\Generator\GeneratorInterface;
use PhpInvariant\Generator\GeneratorFactory;
use PhpInvariant\Generator\Type\Combine\OneOfType;
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\Random\Random;

class OneOfGenerator implements GeneratorInterface
class OneOfGenerator
{
public function __construct(
private GeneratorFactory $generatorFactory,
private Random $random
) {
}

/**
* @param OneOfType $type
*/
public function generate(TypeInterface $type): mixed

public function __invoke(OneOfType $type): mixed
{
$data = $type->types;
$type = $data[$this->random->getInt(0, count($data) - 1)];
$generator = $this->generatorFactory->getGenerator($type);
return $generator->generate($type);
return $generator($type);

}
}
10 changes: 3 additions & 7 deletions src/Generator/Generator/DateTime/DateTimeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@
namespace PhpInvariant\Generator\Generator\DateTime;

use DateTime;
use PhpInvariant\Generator\Generator\GeneratorInterface;
use PhpInvariant\Generator\Type\DateTime\DateTimeType;
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\Random\Random;

class DateTimeGenerator implements GeneratorInterface
class DateTimeGenerator
{
public function __construct(
private Random $random
) {
}

/**
* @param DateTimeType $type
*/
public function generate(TypeInterface $type): DateTime

public function __invoke(DateTimeType $type): DateTime
{
$from = !is_null($type->from) ? $type->from->getTimestamp() : 0;
$before = !is_null($type->before) ? $type->before->getTimestamp() : 0xffffffff;
Expand Down
10 changes: 0 additions & 10 deletions src/Generator/Generator/GeneratorInterface.php

This file was deleted.

10 changes: 3 additions & 7 deletions src/Generator/Generator/Scalar/Boolean/BooleanGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

namespace PhpInvariant\Generator\Generator\Scalar\Boolean;

use PhpInvariant\Generator\Generator\GeneratorInterface;
use PhpInvariant\Generator\Type\Scalar\Boolean\BooleanType;
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\Random\Random;

class BooleanGenerator implements GeneratorInterface
class BooleanGenerator
{
public function __construct(
private Random $random
) {
}

/**
* @param BooleanType $type
*/
public function generate(TypeInterface $type): bool

public function __invoke(BooleanType $type): bool
{
return (bool)($this->random->getInt(0, 1));
}
Expand Down
10 changes: 3 additions & 7 deletions src/Generator/Generator/Scalar/Float/FloatGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

namespace PhpInvariant\Generator\Generator\Scalar\Float;

use PhpInvariant\Generator\Generator\GeneratorInterface;
use PhpInvariant\Generator\Type\Scalar\Float\FloatType;
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\Random\Random;

class FloatGenerator implements GeneratorInterface
class FloatGenerator
{
public function __construct(
private Random $random
) {
}

/**
* @param FloatType $type
*/
public function generate(TypeInterface $type): float

public function __invoke(FloatType $type): float
{
$maxIntegerPart = (int)floor($type->max);
$minIntegerPart = (int)floor($type->min);
Expand Down
9 changes: 2 additions & 7 deletions src/Generator/Generator/Scalar/Integer/IntegerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@

namespace PhpInvariant\Generator\Generator\Scalar\Integer;

use PhpInvariant\Generator\Generator\GeneratorInterface;
use PhpInvariant\Generator\Type\Scalar\Integer\IntegerType;
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\Random\Random;

class IntegerGenerator implements GeneratorInterface
class IntegerGenerator
{
public function __construct(
private Random $random
) {
}

/**
* @param IntegerType $type
*/
public function generate(TypeInterface $type): int
public function __invoke(IntegerType $type): int
{
return $this->random->getInt($type->min, $type->max);
}
Expand Down
10 changes: 3 additions & 7 deletions src/Generator/Generator/Scalar/String/StringGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

namespace PhpInvariant\Generator\Generator\Scalar\String;

use PhpInvariant\Generator\Generator\GeneratorInterface;
use PhpInvariant\Generator\Type\Scalar\String\StringType;
use PhpInvariant\Generator\TypeInterface;
use PhpInvariant\Random\Random;

class StringGenerator implements GeneratorInterface
class StringGenerator
{
public function __construct(
private Random $random
) {
}

/**
* @param StringType $type
*/
public function generate(TypeInterface $type): string

public function __invoke(StringType $type): string
{
$alphabet = [];
for ($char = 32; $char <= 127; $char++) {
Expand Down
Loading

0 comments on commit 5828df3

Please sign in to comment.