From 42cd190d90efba5d06475e046f717b4b3395c57a Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 19 Dec 2020 12:24:14 +0100 Subject: [PATCH 01/16] Helper functions to use our custom validators --- src/Faker/Generator.php | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index e01db1cdbb..7cfcfd3e8d 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -565,9 +565,15 @@ class Generator private $container; + /** + * @var UniqueGenerator + */ + private $uniqueGenerator; + public function __construct(ContainerInterface $container = null) { $this->container = $container ?: Extension\ContainerBuilder::getDefault(); + $this->uniqueGenerator = new UniqueGenerator($this); } /** @@ -610,6 +616,46 @@ public function getProviders() return $this->providers; } + /** + * With the unique generator you are guaranteed to never get the same two + * values. + * + * @return Generator The UniqueGenerator is a proxy + */ + public function withUnique(): Generator + { + return $this->uniqueGenerator; + } + + /** + * Get a value only some percentage of the time. + * + * @param float $weight A probability between 0 and 1, 0 means that we always get the default value. + * + * @return self + */ + public function withMaybe(float $weight = 0.5, $default = null) + { + if (mt_rand(1, 100) <= (100*$weight)) { + return $this; + } + + return new DefaultGenerator($default); + } + + /** + * To make sure the value meet some criteria, pass a callable that verifies the + * output. If the validator fails, the generator will try again. + * + * @example $faker->withValid(fn($v) => strlen($v) > 3))->name(); + * + * @return self + */ + public function withValid(\Closure $validator, int $maxRetries = 10000) + { + return new ValidGenerator($this, $validator, $maxRetries); + } + public function seed($seed = null) { if ($seed === null) { From 38707269e00ea8266d145ffe083157f9c165bcc1 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 7 Aug 2021 19:43:08 -0700 Subject: [PATCH 02/16] Update name --- src/Faker/DefaultGenerator.php | 4 +++- src/Faker/Generator.php | 20 ++++---------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/Faker/DefaultGenerator.php b/src/Faker/DefaultGenerator.php index 81aa4f6452..ba501dbf54 100644 --- a/src/Faker/DefaultGenerator.php +++ b/src/Faker/DefaultGenerator.php @@ -11,10 +11,12 @@ class DefaultGenerator { protected $default; + private $generator; - public function __construct($default = null) + public function __construct(Generator $generator, $default = null) { $this->default = $default; + $this->generator = $generator; } /** diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index 7cfcfd3e8d..3fa1081dec 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -122,18 +122,6 @@ * * @method string toUpper($string = '') * - * @property mixed $optional - * - * @method mixed optional($weight = null, $default = null) - * - * @property UniqueGenerator $unique - * - * @method UniqueGenerator unique($reset = false, $maxRetries = 10000) - * - * @property ValidGenerator $valid - * - * @method ValidGenerator valid($validator = null, $maxRetries = 10000) - * * @property int $biasedNumberBetween * * @method int biasedNumberBetween($min = 0, $max = 100, $function = 'sqrt') @@ -622,7 +610,7 @@ public function getProviders() * * @return Generator The UniqueGenerator is a proxy */ - public function withUnique(): Generator + public function unique(): Generator { return $this->uniqueGenerator; } @@ -634,13 +622,13 @@ public function withUnique(): Generator * * @return self */ - public function withMaybe(float $weight = 0.5, $default = null) + public function optional(float $weight = 0.5, $default = null) { if (mt_rand(1, 100) <= (100*$weight)) { return $this; } - return new DefaultGenerator($default); + return new DefaultGenerator($this, $default); } /** @@ -651,7 +639,7 @@ public function withMaybe(float $weight = 0.5, $default = null) * * @return self */ - public function withValid(\Closure $validator, int $maxRetries = 10000) + public function valid(\Closure $validator, int $maxRetries = 10000) { return new ValidGenerator($this, $validator, $maxRetries); } From ee8c1d040f475766bc4d39d1b4a512b2206df3b8 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 7 Aug 2021 20:02:16 -0700 Subject: [PATCH 03/16] Make sure we support extensions --- src/Faker/ChanceGenerator.php | 60 ++++++++++++++++++++++++++++++++++ src/Faker/DefaultGenerator.php | 11 ++++--- src/Faker/Generator.php | 14 ++++++++ src/Faker/UniqueGenerator.php | 17 +++++----- src/Faker/ValidGenerator.php | 10 +++++- 5 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 src/Faker/ChanceGenerator.php diff --git a/src/Faker/ChanceGenerator.php b/src/Faker/ChanceGenerator.php new file mode 100644 index 0000000000..26c90eb784 --- /dev/null +++ b/src/Faker/ChanceGenerator.php @@ -0,0 +1,60 @@ +default = $default; + $this->generator = $generator; + $this->weight = $weight; + } + + public function ext(string $id) + { + return new self($this->generator->ext($id), $this->weight, $this->default); + } + + /** + * Catch and proxy all generator calls but return only valid values + * + * @param string $attribute + * + * @deprecated Use a method instead. + */ + public function __get($attribute) + { + trigger_deprecation('fakerphp/faker', '1.14', 'Accessing property "%s" is deprecated, use "%s()" instead.', $attribute, $attribute); + + return $this->__call($attribute, []); + } + + /** + * @param string $method + * @param array $attributes + */ + public function __call($method, $attributes) + { + if (mt_rand(1, 100) <= (100*$this->weight)) { + return $this->generator; + } + + return $this->default; + } +} diff --git a/src/Faker/DefaultGenerator.php b/src/Faker/DefaultGenerator.php index ba501dbf54..ab818ddc5b 100644 --- a/src/Faker/DefaultGenerator.php +++ b/src/Faker/DefaultGenerator.php @@ -4,19 +4,22 @@ /** * This generator returns a default value for all called properties - * and methods. It works with Faker\Generator\Base->optional(). + * and methods. * * @mixin Generator */ class DefaultGenerator { protected $default; - private $generator; - public function __construct(Generator $generator, $default = null) + public function __construct($default = null) { $this->default = $default; - $this->generator = $generator; + } + + public function ext() + { + return $this; } /** diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index 3fa1081dec..966bf7a9ad 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -615,6 +615,18 @@ public function unique(): Generator return $this->uniqueGenerator; } + /** + * Get a value only some percentage of the time. + * + * @param float $weight A probability between 0 and 1, 0 means that we always get the default value. + * + * @return self + */ + public function chance(float $weight = 0.5, $default = null) + { + return new ChanceGenerator($this, $weight, $default); + } + /** * Get a value only some percentage of the time. * @@ -624,6 +636,8 @@ public function unique(): Generator */ public function optional(float $weight = 0.5, $default = null) { + trigger_deprecation('fakerphp/faker', '1.16', 'Method "%s::optional()" is deprecated, use "%s::chance()" instead.', __CLASS__, __CLASS__); + if (mt_rand(1, 100) <= (100*$weight)) { return $this; } diff --git a/src/Faker/UniqueGenerator.php b/src/Faker/UniqueGenerator.php index 8dddeec283..a4db8ee1f0 100644 --- a/src/Faker/UniqueGenerator.php +++ b/src/Faker/UniqueGenerator.php @@ -2,6 +2,8 @@ namespace Faker; +use Faker\Extension\Extension; + /** * Proxy for other generators that returns only unique values. * @@ -11,14 +13,7 @@ */ class UniqueGenerator { - /** - * @var Generator - */ protected $generator; - - /** - * @var int - */ protected $maxRetries; /** @@ -34,14 +29,20 @@ class UniqueGenerator protected $uniques = []; /** + * @param Generator|Extension $generator * @param int $maxRetries */ - public function __construct(Generator $generator, $maxRetries = 10000) + public function __construct($generator, $maxRetries = 10000) { $this->generator = $generator; $this->maxRetries = $maxRetries; } + public function ext(string $id) + { + return new self($this->generator->ext($id), $this->maxRetries, $this->maxRetries); + } + /** * Catch and proxy all generator calls but return only unique values * diff --git a/src/Faker/ValidGenerator.php b/src/Faker/ValidGenerator.php index 691d46dda9..c42ce93697 100644 --- a/src/Faker/ValidGenerator.php +++ b/src/Faker/ValidGenerator.php @@ -2,6 +2,8 @@ namespace Faker; +use Faker\Extension\Extension; + /** * Proxy for other generators, to return only valid values. Works with * Faker\Generator\Base->valid() @@ -15,10 +17,11 @@ class ValidGenerator protected $maxRetries; /** + * @param Generator|Extension $generator * @param callable|null $validator * @param int $maxRetries */ - public function __construct(Generator $generator, $validator = null, $maxRetries = 10000) + public function __construct($generator, $validator = null, $maxRetries = 10000) { if (null === $validator) { $validator = static function () { @@ -32,6 +35,11 @@ public function __construct(Generator $generator, $validator = null, $maxRetries $this->maxRetries = $maxRetries; } + public function ext(string $id) + { + return new self($this->generator->ext($id), $this->validator, $this->maxRetries); + } + /** * Catch and proxy all generator calls but return only valid values * From ab98df55011a2511fcab02ecc2e027f9e066d21d Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 7 Aug 2021 20:33:41 -0700 Subject: [PATCH 04/16] Updated some tests --- src/Faker/Generator.php | 13 ++++++++----- test/Faker/Provider/BaseTest.php | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index 966bf7a9ad..b4e41e5244 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -561,7 +561,6 @@ class Generator public function __construct(ContainerInterface $container = null) { $this->container = $container ?: Extension\ContainerBuilder::getDefault(); - $this->uniqueGenerator = new UniqueGenerator($this); } /** @@ -608,10 +607,14 @@ public function getProviders() * With the unique generator you are guaranteed to never get the same two * values. * - * @return Generator The UniqueGenerator is a proxy + * @return self */ - public function unique(): Generator + public function unique($reset = false, $maxRetries = 10000) { + if ($reset || $this->uniqueGenerator === null) { + $this->uniqueGenerator = new UniqueGenerator($this, $maxRetries); + } + return $this->uniqueGenerator; } @@ -642,7 +645,7 @@ public function optional(float $weight = 0.5, $default = null) return $this; } - return new DefaultGenerator($this, $default); + return new DefaultGenerator($default); } /** @@ -653,7 +656,7 @@ public function optional(float $weight = 0.5, $default = null) * * @return self */ - public function valid(\Closure $validator, int $maxRetries = 10000) + public function valid(\Closure $validator = null, int $maxRetries = 10000) { return new ValidGenerator($this, $validator, $maxRetries); } diff --git a/test/Faker/Provider/BaseTest.php b/test/Faker/Provider/BaseTest.php index 2e874eafa5..706313019e 100644 --- a/test/Faker/Provider/BaseTest.php +++ b/test/Faker/Provider/BaseTest.php @@ -532,11 +532,11 @@ public function testValidThrowsExceptionWhenNoValidValueCanBeGenerated() } } - public function testValidThrowsExceptionWhenParameterIsNotCollable() + public function testValidThrowsExceptionWhenParameterIsNotCallable() { - $this->expectException(\InvalidArgumentException::class); $faker = new \Faker\Generator(); $faker->addProvider(new \Faker\Provider\Base($faker)); + $this->expectException(\TypeError::class); $faker->valid(12)->randomElement([1, 3, 5, 7, 9]); } From b43dfd722a7c66933cbdbeb7e977ebeb8470acad Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 7 Aug 2021 20:36:14 -0700 Subject: [PATCH 05/16] deprecated DefaultGenerator --- src/Faker/DefaultGenerator.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Faker/DefaultGenerator.php b/src/Faker/DefaultGenerator.php index ab818ddc5b..2f09ee43fc 100644 --- a/src/Faker/DefaultGenerator.php +++ b/src/Faker/DefaultGenerator.php @@ -7,6 +7,7 @@ * and methods. * * @mixin Generator + * @deprecated Use ChanceGenerator instead */ class DefaultGenerator { @@ -14,6 +15,8 @@ class DefaultGenerator public function __construct($default = null) { + trigger_deprecation('fakerphp/faker', '1.16', 'Class "%s" is deprecated, use "%s" instead.', __CLASS__, ChanceGenerator::class); + $this->default = $default; } From b8a5b8234ffbbf1009e3424877e16dc5962602a8 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 8 Aug 2021 23:03:59 -0700 Subject: [PATCH 06/16] Name it back to "optional" --- src/Faker/Generator.php | 54 ++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index b4e41e5244..5c4422d6d4 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -607,7 +607,18 @@ public function getProviders() * With the unique generator you are guaranteed to never get the same two * values. * - * @return self + * + * // will never return twice the same value + * $faker->unique()->randomElement(array(1, 2, 3)); + * + * + * @param bool $reset If set to true, resets the list of existing values + * @param int $maxRetries Maximum number of retries to find a unique value, + * After which an OverflowException is thrown. + * + * @throws \OverflowException When no unique value can be found by iterating $maxRetries times + * + * @return self A proxy class returning only non-existing values */ public function unique($reset = false, $maxRetries = 10000) { @@ -618,18 +629,6 @@ public function unique($reset = false, $maxRetries = 10000) return $this->uniqueGenerator; } - /** - * Get a value only some percentage of the time. - * - * @param float $weight A probability between 0 and 1, 0 means that we always get the default value. - * - * @return self - */ - public function chance(float $weight = 0.5, $default = null) - { - return new ChanceGenerator($this, $weight, $default); - } - /** * Get a value only some percentage of the time. * @@ -639,22 +638,33 @@ public function chance(float $weight = 0.5, $default = null) */ public function optional(float $weight = 0.5, $default = null) { - trigger_deprecation('fakerphp/faker', '1.16', 'Method "%s::optional()" is deprecated, use "%s::chance()" instead.', __CLASS__, __CLASS__); - - if (mt_rand(1, 100) <= (100*$weight)) { - return $this; - } - - return new DefaultGenerator($default); + return new ChanceGenerator($this, $weight, $default); } /** * To make sure the value meet some criteria, pass a callable that verifies the * output. If the validator fails, the generator will try again. * - * @example $faker->withValid(fn($v) => strlen($v) > 3))->name(); + * The value validity is determined by a function passed as first argument. * - * @return self + * + * $values = array(); + * $evenValidator = function ($digit) { + * return $digit % 2 === 0; + * }; + * for ($i=0; $i < 10; $i++) { + * $values []= $faker->valid($evenValidator)->randomDigit; + * } + * print_r($values); // [0, 4, 8, 4, 2, 6, 0, 8, 8, 6] + * + * + * @param Closure $validator A function returning true for valid values + * @param int $maxRetries Maximum number of retries to find a valid value, + * After which an OverflowException is thrown. + * + * @throws \OverflowException When no valid value can be found by iterating $maxRetries times + * + * @return self A proxy class returning only valid values */ public function valid(\Closure $validator = null, int $maxRetries = 10000) { From 1663cf5f88909292b646fff2cbc797171fbae857 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 8 Aug 2021 23:14:17 -0700 Subject: [PATCH 07/16] Fixed tests --- src/Faker/ChanceGenerator.php | 8 ++++---- src/Faker/Generator.php | 5 +++++ test/Faker/Provider/BaseTest.php | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Faker/ChanceGenerator.php b/src/Faker/ChanceGenerator.php index 26c90eb784..35af5c594d 100644 --- a/src/Faker/ChanceGenerator.php +++ b/src/Faker/ChanceGenerator.php @@ -46,13 +46,13 @@ public function __get($attribute) } /** - * @param string $method - * @param array $attributes + * @param string $name + * @param array $arguments */ - public function __call($method, $attributes) + public function __call($name, $arguments) { if (mt_rand(1, 100) <= (100*$this->weight)) { - return $this->generator; + return call_user_func_array([$this->generator, $name], $arguments); } return $this->default; diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index 5c4422d6d4..bdd29692b5 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -638,6 +638,11 @@ public function unique($reset = false, $maxRetries = 10000) */ public function optional(float $weight = 0.5, $default = null) { + if ($weight > 1) { + trigger_deprecation('fakerphp/faker', '1.16', 'First argument ($weight) to method "optional()" must be between 0 and 1. You passed %f, we assume you meant %f.', $weight, $weight / 100); + $weight = $weight / 100; + } + return new ChanceGenerator($this, $weight, $default); } diff --git a/test/Faker/Provider/BaseTest.php b/test/Faker/Provider/BaseTest.php index 706313019e..9e1f8ef59f 100644 --- a/test/Faker/Provider/BaseTest.php +++ b/test/Faker/Provider/BaseTest.php @@ -401,7 +401,7 @@ public function testOptionalAllowsChainingProviderCallRandomlyReturnNull() $values = []; for ($i = 0; $i < 10; ++$i) { - $values[] = $faker->optional(50)->randomDigit; + $values[] = $faker->optional(0.5)->randomDigit; } self::assertContains(null, $values); } From 7396f900b75c40ae18470755ec04a00c6ba2b9fb Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 8 Aug 2021 23:16:30 -0700 Subject: [PATCH 08/16] fix --- test/Faker/DefaultGeneratorTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/Faker/DefaultGeneratorTest.php b/test/Faker/DefaultGeneratorTest.php index e009c5ccef..23eced28ea 100644 --- a/test/Faker/DefaultGeneratorTest.php +++ b/test/Faker/DefaultGeneratorTest.php @@ -25,6 +25,9 @@ public function testGeneratorReturnsDefaultValueForAnyPropertyGet() self::assertNotNull($generator->bar); } + /** + * @group legacy + */ public function testGeneratorReturnsDefaultValueForAnyMethodCall() { $generator = new DefaultGenerator(123); From c22e450d72060894554674582f65108a69d37d7b Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 8 Aug 2021 23:18:12 -0700 Subject: [PATCH 09/16] cs --- src/Faker/ChanceGenerator.php | 4 ++-- src/Faker/DefaultGenerator.php | 1 + src/Faker/UniqueGenerator.php | 4 ++-- src/Faker/ValidGenerator.php | 6 +++--- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Faker/ChanceGenerator.php b/src/Faker/ChanceGenerator.php index 35af5c594d..25aaa4c6e6 100644 --- a/src/Faker/ChanceGenerator.php +++ b/src/Faker/ChanceGenerator.php @@ -17,7 +17,7 @@ class ChanceGenerator protected $default; /** - * @param Generator|Extension $generator + * @param Extension|Generator $generator */ public function __construct($generator, float $weight, $default = null) { @@ -51,7 +51,7 @@ public function __get($attribute) */ public function __call($name, $arguments) { - if (mt_rand(1, 100) <= (100*$this->weight)) { + if (mt_rand(1, 100) <= (100 * $this->weight)) { return call_user_func_array([$this->generator, $name], $arguments); } diff --git a/src/Faker/DefaultGenerator.php b/src/Faker/DefaultGenerator.php index 2f09ee43fc..688f4766a8 100644 --- a/src/Faker/DefaultGenerator.php +++ b/src/Faker/DefaultGenerator.php @@ -7,6 +7,7 @@ * and methods. * * @mixin Generator + * * @deprecated Use ChanceGenerator instead */ class DefaultGenerator diff --git a/src/Faker/UniqueGenerator.php b/src/Faker/UniqueGenerator.php index a4db8ee1f0..55a849f4d7 100644 --- a/src/Faker/UniqueGenerator.php +++ b/src/Faker/UniqueGenerator.php @@ -29,8 +29,8 @@ class UniqueGenerator protected $uniques = []; /** - * @param Generator|Extension $generator - * @param int $maxRetries + * @param Extension|Generator $generator + * @param int $maxRetries */ public function __construct($generator, $maxRetries = 10000) { diff --git a/src/Faker/ValidGenerator.php b/src/Faker/ValidGenerator.php index c42ce93697..bf40945697 100644 --- a/src/Faker/ValidGenerator.php +++ b/src/Faker/ValidGenerator.php @@ -17,9 +17,9 @@ class ValidGenerator protected $maxRetries; /** - * @param Generator|Extension $generator - * @param callable|null $validator - * @param int $maxRetries + * @param Extension|Generator $generator + * @param callable|null $validator + * @param int $maxRetries */ public function __construct($generator, $validator = null, $maxRetries = 10000) { From 598bec18cb6be6a0d803ac427e7b34756cdef96b Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 8 Aug 2021 23:22:15 -0700 Subject: [PATCH 10/16] cs --- src/Faker/UniqueGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Faker/UniqueGenerator.php b/src/Faker/UniqueGenerator.php index 55a849f4d7..daf0d85e9c 100644 --- a/src/Faker/UniqueGenerator.php +++ b/src/Faker/UniqueGenerator.php @@ -40,7 +40,7 @@ public function __construct($generator, $maxRetries = 10000) public function ext(string $id) { - return new self($this->generator->ext($id), $this->maxRetries, $this->maxRetries); + return new self($this->generator->ext($id), $this->maxRetries); } /** From 619d5728d7dfb90a533d39041b003a33a4d86bad Mon Sep 17 00:00:00 2001 From: Bram Ceulemans Date: Tue, 18 Jan 2022 11:43:13 +0100 Subject: [PATCH 11/16] Fix UniqueGenerator not storing previously generated values when calling ->ext() method --- src/Faker/UniqueGenerator.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Faker/UniqueGenerator.php b/src/Faker/UniqueGenerator.php index daf0d85e9c..fef167b6d1 100644 --- a/src/Faker/UniqueGenerator.php +++ b/src/Faker/UniqueGenerator.php @@ -29,18 +29,20 @@ class UniqueGenerator protected $uniques = []; /** - * @param Extension|Generator $generator - * @param int $maxRetries + * @param Extension|Generator $generator + * @param int $maxRetries + * @param array> $uniques */ - public function __construct($generator, $maxRetries = 10000) + public function __construct($generator, $maxRetries = 10000, &$uniques = []) { $this->generator = $generator; $this->maxRetries = $maxRetries; + $this->uniques = &$uniques; } public function ext(string $id) { - return new self($this->generator->ext($id), $this->maxRetries); + return new self($this->generator->ext($id), $this->maxRetries, $this->uniques); } /** From beaad6c84839da72ac7db3f05b29721f3827e9b8 Mon Sep 17 00:00:00 2001 From: Bram Ceulemans Date: Tue, 18 Jan 2022 14:06:51 +0100 Subject: [PATCH 12/16] Fix types in randomFloat --- src/Faker/Core/Number.php | 2 +- src/Faker/Extension/NumberExtension.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Faker/Core/Number.php b/src/Faker/Core/Number.php index 17903052d7..f7af0c2960 100644 --- a/src/Faker/Core/Number.php +++ b/src/Faker/Core/Number.php @@ -40,7 +40,7 @@ public function randomDigitNotZero(): int return mt_rand(1, 9); } - public function randomFloat(int $nbMaxDecimals = null, float $min = 0, float $max = null): float + public function randomFloat(?int $nbMaxDecimals = null, float $min = 0, ?float $max = null): float { if (null === $nbMaxDecimals) { $nbMaxDecimals = $this->randomDigit(); diff --git a/src/Faker/Extension/NumberExtension.php b/src/Faker/Extension/NumberExtension.php index 7d0b45860b..ebfa8c3113 100644 --- a/src/Faker/Extension/NumberExtension.php +++ b/src/Faker/Extension/NumberExtension.php @@ -37,7 +37,7 @@ public function randomDigitNotZero(): int; * * @example 48.8932 */ - public function randomFloat(?int $nbMaxDecimals, float $min, float $max): float; + public function randomFloat(?int $nbMaxDecimals, float $min, ?float $max): float; /** * Returns a random integer with 0 to $nbDigits digits. From b90deae16c35a6c1b9d23fea921608f91ba0fbfc Mon Sep 17 00:00:00 2001 From: Bram Ceulemans Date: Tue, 18 Jan 2022 14:14:29 +0100 Subject: [PATCH 13/16] Generate baseline for proxy classes --- phpstan-baseline.neon | 24 ++++++++++++++++++++++++ psalm.baseline.xml | 7 +------ src/Faker/Generator.php | 10 ++++------ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 62eeaf6ac5..69f1d7d141 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -20,6 +20,21 @@ parameters: count: 1 path: src/Faker/Extension/ContainerBuilder.php + - + message: "#^Method Faker\\\\Generator\\:\\:optional\\(\\) should return Faker\\\\Generator but returns Faker\\\\ChanceGenerator\\.$#" + count: 1 + path: src/Faker/Generator.php + + - + message: "#^Method Faker\\\\Generator\\:\\:unique\\(\\) should return Faker\\\\Generator but returns Faker\\\\UniqueGenerator\\.$#" + count: 1 + path: src/Faker/Generator.php + + - + message: "#^Method Faker\\\\Generator\\:\\:valid\\(\\) should return Faker\\\\Generator but returns Faker\\\\ValidGenerator\\.$#" + count: 1 + path: src/Faker/Generator.php + - message: "#^Unreachable statement \\- code above always terminates\\.$#" count: 1 @@ -735,6 +750,15 @@ parameters: count: 2 path: src/Faker/Provider/Base.php + - + message: + """ + #^Instantiation of deprecated class Faker\\\\DefaultGenerator\\: + Use ChanceGenerator instead$# + """ + count: 1 + path: src/Faker/Provider/Base.php + - message: "#^Negated boolean expression is always false\\.$#" count: 1 diff --git a/psalm.baseline.xml b/psalm.baseline.xml index 3908786358..410abbb3af 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -1,5 +1,5 @@ - + 0 @@ -8,11 +8,6 @@ string - - - ContainerExceptionInterface - - TableRegistry diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index bdd29692b5..3d7ce6a6c9 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -2,7 +2,6 @@ namespace Faker; -use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; /** @@ -568,7 +567,6 @@ public function __construct(ContainerInterface $container = null) * * @param class-string $id * - * @throws ContainerExceptionInterface * @throws Extension\ExtensionNotFound * * @return T @@ -663,15 +661,15 @@ public function optional(float $weight = 0.5, $default = null) * print_r($values); // [0, 4, 8, 4, 2, 6, 0, 8, 8, 6] * * - * @param Closure $validator A function returning true for valid values - * @param int $maxRetries Maximum number of retries to find a valid value, - * After which an OverflowException is thrown. + * @param ?\Closure $validator A function returning true for valid values + * @param int $maxRetries Maximum number of retries to find a valid value, + * After which an OverflowException is thrown. * * @throws \OverflowException When no valid value can be found by iterating $maxRetries times * * @return self A proxy class returning only valid values */ - public function valid(\Closure $validator = null, int $maxRetries = 10000) + public function valid(?\Closure $validator = null, int $maxRetries = 10000) { return new ValidGenerator($this, $validator, $maxRetries); } From 6bb85fea0c33a4bce0ebb9ac4a19c4138686f9dd Mon Sep 17 00:00:00 2001 From: Bram Ceulemans Date: Tue, 18 Jan 2022 14:25:40 +0100 Subject: [PATCH 14/16] Add baseline for psalm --- psalm.baseline.xml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/psalm.baseline.xml b/psalm.baseline.xml index 410abbb3af..04ca4ec424 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -8,6 +8,18 @@ string + + + $this->uniqueGenerator + new ChanceGenerator($this, $weight, $default) + new ValidGenerator($this, $validator, $maxRetries) + + + self + self + self + + TableRegistry From 65111e1980447e47346d9ba104d21cc8830010a6 Mon Sep 17 00:00:00 2001 From: Bram Ceulemans Date: Tue, 18 Jan 2022 14:48:03 +0100 Subject: [PATCH 15/16] Add regression test for UniqueGenerator --- test/Faker/UniqueGeneratorTest.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/Faker/UniqueGeneratorTest.php diff --git a/test/Faker/UniqueGeneratorTest.php b/test/Faker/UniqueGeneratorTest.php new file mode 100644 index 0000000000..1912e4a50c --- /dev/null +++ b/test/Faker/UniqueGeneratorTest.php @@ -0,0 +1,27 @@ +expectException(\OverflowException::class); + + for ($i = 0; $i < 5; ++$i) { + $this->faker->unique()->ext(NumberExtension::class)->numberBetween(0, 3); + } + } + + public function testUniqueGeneratorRetries(): void + { + for ($i = 0; $i < 10; ++$i) { + $this->faker->unique()->ext(NumberExtension::class)->numberBetween(0, 9); + } + } +} From 78f602500d21f948b623b754b1f00c7625de1566 Mon Sep 17 00:00:00 2001 From: Bram Ceulemans Date: Tue, 18 Jan 2022 14:58:52 +0100 Subject: [PATCH 16/16] Add ValidGeneratorTest.php --- test/Faker/UniqueGeneratorTest.php | 4 ++-- test/Faker/ValidGeneratorTest.php | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/Faker/ValidGeneratorTest.php diff --git a/test/Faker/UniqueGeneratorTest.php b/test/Faker/UniqueGeneratorTest.php index 1912e4a50c..bf7b147c8d 100644 --- a/test/Faker/UniqueGeneratorTest.php +++ b/test/Faker/UniqueGeneratorTest.php @@ -13,8 +13,8 @@ public function testUniqueGeneratorKeepsUniquesAcrossExtensions(): void { $this->expectException(\OverflowException::class); - for ($i = 0; $i < 5; ++$i) { - $this->faker->unique()->ext(NumberExtension::class)->numberBetween(0, 3); + for ($i = 0; $i < 3; ++$i) { + $this->faker->unique()->ext(NumberExtension::class)->numberBetween(0, 1); } } diff --git a/test/Faker/ValidGeneratorTest.php b/test/Faker/ValidGeneratorTest.php new file mode 100644 index 0000000000..e9fafe967f --- /dev/null +++ b/test/Faker/ValidGeneratorTest.php @@ -0,0 +1,19 @@ +expectException(\OverflowException::class); + + $validator = static function ($value) { + return $value !== 0; + }; + $this->faker->valid($validator, 10)->numberBetween(0, 0); + } +}