Skip to content

Commit

Permalink
Move some namespaces on generator
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Nov 29, 2023
1 parent e96fec0 commit 7ff277d
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 86 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
<directory name="src/Component/Annotation" />
<directory name="src/Component/Factory" />
<directory name="src/Component/Metadata" />
<directory name="src/Component/Generator" />
<directory name="src/Component/Storage" />
<file name="src/Bundle/Event/ResourceControllerEvent.php" />
</errorLevel>
Expand All @@ -214,6 +215,7 @@
<errorLevel type="suppress">
<directory name="src/Component/Factory" />
<directory name="src/Component/Metadata" />
<directory name="src/Component/Generator" />
<directory name="src/Component/Storage" />
</errorLevel>
</UnrecognizedStatement>
Expand Down
1 change: 1 addition & 0 deletions src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<defaults public="true" />

<service id="sylius.random_generator" class="Sylius\Component\Resource\Generator\RandomnessGenerator" />
<service id="Sylius\Resource\Generator\RandomnessGeneratorInterface" alias="sylius.random_generator" />
<service id="Sylius\Component\Resource\Generator\RandomnessGeneratorInterface" alias="sylius.random_generator" />

<service id="sylius.form.type_extension.form.request_handler"
Expand Down
50 changes: 3 additions & 47 deletions src/Component/Generator/RandomnessGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,10 @@

namespace Sylius\Component\Resource\Generator;

use Webmozart\Assert\Assert;
class_exists(\Sylius\Resource\Generator\RandomnessGenerator::class);

final class RandomnessGenerator implements RandomnessGeneratorInterface
{
private string $uriSafeAlphabet;

private string $digits;

public function __construct()
{
$this->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;
}
}
11 changes: 5 additions & 6 deletions src/Component/Generator/RandomnessGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
}
40 changes: 7 additions & 33 deletions src/Component/spec/Generator/RandomnessGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
68 changes: 68 additions & 0 deletions src/Component/src/Generator/RandomnessGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Resource\Generator;

use Webmozart\Assert\Assert;

final class RandomnessGenerator implements RandomnessGeneratorInterface
{
private string $uriSafeAlphabet;

private string $digits;

public function __construct()
{
$this->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);
25 changes: 25 additions & 0 deletions src/Component/src/Generator/RandomnessGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Resource\Generator;

interface RandomnessGeneratorInterface
{
public function generateUriSafeString(int $length): string;

public function generateNumeric(int $length): string;

public function generateInt(int $min, int $max): int;
}

class_alias(RandomnessGeneratorInterface::class, \Sylius\Component\Resource\Generator\RandomnessGeneratorInterface::class);
63 changes: 63 additions & 0 deletions src/Component/tests/spec/Generator/RandomnessGeneratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Resource\Generator;

use PhpSpec\ObjectBehavior;
use Sylius\Resource\Generator\RandomnessGeneratorInterface;

final class RandomnessGeneratorSpec extends ObjectBehavior
{
function it_implements_randomness_generator_interface(): void
{
$this->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;
},
];
}
}

0 comments on commit 7ff277d

Please sign in to comment.