From 7ff277d4617290d82928acf78ada7e8272cbe6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Wed, 29 Nov 2023 16:12:00 +0100 Subject: [PATCH] Move some namespaces on generator --- phpstan.neon | 1 + psalm.xml | 2 + src/Bundle/Resources/config/services.xml | 1 + .../Generator/RandomnessGenerator.php | 50 +------------- .../RandomnessGeneratorInterface.php | 11 ++- .../Generator/RandomnessGeneratorSpec.php | 40 ++--------- .../src/Generator/RandomnessGenerator.php | 68 +++++++++++++++++++ .../RandomnessGeneratorInterface.php | 25 +++++++ .../Generator/RandomnessGeneratorSpec.php | 63 +++++++++++++++++ 9 files changed, 175 insertions(+), 86 deletions(-) create mode 100644 src/Component/src/Generator/RandomnessGenerator.php create mode 100644 src/Component/src/Generator/RandomnessGeneratorInterface.php create mode 100644 src/Component/tests/spec/Generator/RandomnessGeneratorSpec.php diff --git a/phpstan.neon b/phpstan.neon index aab9e9172..c678c126b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -26,6 +26,7 @@ parameters: - %currentWorkingDirectory%/src/Component/Annotation/* - %currentWorkingDirectory%/src/Component/Factory/* - %currentWorkingDirectory%/src/Component/Metadata/* + - %currentWorkingDirectory%/src/Component/Generator/* - %currentWorkingDirectory%/src/Component/Reflection/ClassReflection.php - %currentWorkingDirectory%/src/Component/spec/* - %currentWorkingDirectory%/src/Component/Storage/* diff --git a/psalm.xml b/psalm.xml index 649693ea7..c6e7aa762 100644 --- a/psalm.xml +++ b/psalm.xml @@ -188,6 +188,7 @@ + @@ -214,6 +215,7 @@ + diff --git a/src/Bundle/Resources/config/services.xml b/src/Bundle/Resources/config/services.xml index 7488f11dc..62dbd17ba 100644 --- a/src/Bundle/Resources/config/services.xml +++ b/src/Bundle/Resources/config/services.xml @@ -37,6 +37,7 @@ + digits = implode(range(0, 9)); - - $this->uriSafeAlphabet = - implode(range(0, 9)) - . implode(range('a', 'z')) - . implode(range('A', 'Z')) - . implode(['-', '_', '~']) - ; - } - - public function generateUriSafeString(int $length): string +if (false) { + final class RandomnessGenerator extends \Sylius\Resource\Generator\RandomnessGenerator { - return $this->generateStringOfLength($length, $this->uriSafeAlphabet); - } - - public function generateNumeric(int $length): string - { - return $this->generateStringOfLength($length, $this->digits); - } - - public function generateInt(int $min, int $max): int - { - return random_int($min, $max); - } - - private function generateStringOfLength(int $length, string $alphabet): string - { - $alphabetMaxIndex = strlen($alphabet) - 1; - - Assert::greaterThanEq($alphabetMaxIndex, 1); - - $randomString = ''; - - for ($i = 0; $i < $length; ++$i) { - $index = random_int(0, $alphabetMaxIndex); - $randomString .= $alphabet[$index]; - } - - return $randomString; } } diff --git a/src/Component/Generator/RandomnessGeneratorInterface.php b/src/Component/Generator/RandomnessGeneratorInterface.php index b2033b03d..ec3382a6b 100644 --- a/src/Component/Generator/RandomnessGeneratorInterface.php +++ b/src/Component/Generator/RandomnessGeneratorInterface.php @@ -13,11 +13,10 @@ namespace Sylius\Component\Resource\Generator; -interface RandomnessGeneratorInterface -{ - public function generateUriSafeString(int $length): string; +interface_exists(\Sylius\Resource\Generator\RandomnessGeneratorInterface::class); - public function generateNumeric(int $length): string; - - public function generateInt(int $min, int $max): int; +if (false) { + interface RandomnessGeneratorInterface extends \Sylius\Resource\Generator\RandomnessGeneratorInterface + { + } } diff --git a/src/Component/spec/Generator/RandomnessGeneratorSpec.php b/src/Component/spec/Generator/RandomnessGeneratorSpec.php index 017b96259..9611a3974 100644 --- a/src/Component/spec/Generator/RandomnessGeneratorSpec.php +++ b/src/Component/spec/Generator/RandomnessGeneratorSpec.php @@ -14,7 +14,9 @@ namespace spec\Sylius\Component\Resource\Generator; use PhpSpec\ObjectBehavior; -use Sylius\Component\Resource\Generator\RandomnessGeneratorInterface; +use Sylius\Component\Resource\Generator\RandomnessGeneratorInterface as LegacyRandomnessGeneratorInterface; +use Sylius\Resource\Generator\RandomnessGenerator as NewRandomnessGenerator; +use Sylius\Resource\Generator\RandomnessGeneratorInterface; final class RandomnessGeneratorSpec extends ObjectBehavior { @@ -23,41 +25,13 @@ function it_implements_randomness_generator_interface(): void $this->shouldImplement(RandomnessGeneratorInterface::class); } - function it_generates_random_uri_safe_string_of_length(): void + function it_implements_legacy_randomness_generator_interface(): void { - $length = 9; - - $this->generateUriSafeString($length)->shouldBeString(); - $this->generateUriSafeString($length)->shouldHaveLength($length); - } - - function it_generates_random_numeric_string_of_length(): void - { - $length = 12; - - $this->generateNumeric($length)->shouldBeString(); - $this->generateNumeric($length)->shouldBeNumeric(); - $this->generateNumeric($length)->shouldHaveLength($length); - } - - function it_generates_random_int_in_range(): void - { - $min = 12; - $max = 2000000; - - $this->generateInt($min, $max)->shouldBeInt(); - $this->generateInt($min, $max)->shouldBeInRange($min, $max); + $this->shouldImplement(LegacyRandomnessGeneratorInterface::class); } - public function getMatchers(): array + function it_should_be_an_alias_of_randomness_generator(): void { - return [ - 'haveLength' => function ($subject, $length) { - return $length === strlen($subject); - }, - 'beInRange' => function ($subject, $min, $max) { - return $subject >= $min && $subject <= $max; - }, - ]; + $this->shouldBeAnInstanceOf(NewRandomnessGenerator::class); } } diff --git a/src/Component/src/Generator/RandomnessGenerator.php b/src/Component/src/Generator/RandomnessGenerator.php new file mode 100644 index 000000000..fa70eed07 --- /dev/null +++ b/src/Component/src/Generator/RandomnessGenerator.php @@ -0,0 +1,68 @@ +digits = implode(range(0, 9)); + + $this->uriSafeAlphabet = + implode(range(0, 9)) + . implode(range('a', 'z')) + . implode(range('A', 'Z')) + . implode(['-', '_', '~']) + ; + } + + public function generateUriSafeString(int $length): string + { + return $this->generateStringOfLength($length, $this->uriSafeAlphabet); + } + + public function generateNumeric(int $length): string + { + return $this->generateStringOfLength($length, $this->digits); + } + + public function generateInt(int $min, int $max): int + { + return random_int($min, $max); + } + + private function generateStringOfLength(int $length, string $alphabet): string + { + $alphabetMaxIndex = strlen($alphabet) - 1; + + Assert::greaterThanEq($alphabetMaxIndex, 1); + + $randomString = ''; + + for ($i = 0; $i < $length; ++$i) { + $index = random_int(0, $alphabetMaxIndex); + $randomString .= $alphabet[$index]; + } + + return $randomString; + } +} + +class_alias(RandomnessGenerator::class, \Sylius\Component\Resource\Generator\RandomnessGenerator::class); diff --git a/src/Component/src/Generator/RandomnessGeneratorInterface.php b/src/Component/src/Generator/RandomnessGeneratorInterface.php new file mode 100644 index 000000000..64b5a14a8 --- /dev/null +++ b/src/Component/src/Generator/RandomnessGeneratorInterface.php @@ -0,0 +1,25 @@ +shouldImplement(RandomnessGeneratorInterface::class); + } + + function it_generates_random_uri_safe_string_of_length(): void + { + $length = 9; + + $this->generateUriSafeString($length)->shouldBeString(); + $this->generateUriSafeString($length)->shouldHaveLength($length); + } + + function it_generates_random_numeric_string_of_length(): void + { + $length = 12; + + $this->generateNumeric($length)->shouldBeString(); + $this->generateNumeric($length)->shouldBeNumeric(); + $this->generateNumeric($length)->shouldHaveLength($length); + } + + function it_generates_random_int_in_range(): void + { + $min = 12; + $max = 2000000; + + $this->generateInt($min, $max)->shouldBeInt(); + $this->generateInt($min, $max)->shouldBeInRange($min, $max); + } + + public function getMatchers(): array + { + return [ + 'haveLength' => function ($subject, $length) { + return $length === strlen($subject); + }, + 'beInRange' => function ($subject, $min, $max) { + return $subject >= $min && $subject <= $max; + }, + ]; + } +}