Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
operating-system: ['ubuntu-latest', 'macos-latest']
php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
php-versions: ['8.1', '8.2']

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
php-versions: ['8.1', '8.2']
php-versions: ['8.1', '8.3']

by now it can be lower/upper set to 8.3

phpunit-versions: ['latest']

steps:
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
}
],
"require": {
"php": "~5.6|~7.0|~8.0",
"paragonie/random_compat": ">=2.0"
"php": ">=8.1"
},
"require-dev": {
"phpunit/phpunit" : ">=5.6",
"squizlabs/php_codesniffer": "^2.3"
"phpunit/phpunit" : ">=10.0",
"squizlabs/php_codesniffer": "^3.7"
},
"archive": {
"exclude": [
Expand Down
34 changes: 10 additions & 24 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Hidehalo\Nanoid;

class Client
Expand All @@ -16,27 +16,25 @@ class Client
* @param string $alphabet Symbols to be used in ID.
* @param integer $size number of symbols in ID.
*/
protected $alphabet;
protected $size;
protected string $alphabet;
protected int $size;

/**
* @var CoreInterface $core Core dynamic random
*/
private $core;
private CoreInterface $core;

/**
* @var GeneratorInterface $generator Random Btyes Generator
*/
protected $generator;
protected GeneratorInterface $generator;

/**
* Constructor of Client
*
* @codeCoverageIgnore
* @param integer $size
* @param GeneratorInterface $generator
*/
public function __construct($size = 21, GeneratorInterface $generator = null)
public function __construct(int $size = 21, GeneratorInterface $generator = null)
{
$this->size = $size > 0 ? $size : 21;
$this->generator = $generator ?: new Generator();
Expand All @@ -47,11 +45,9 @@ public function __construct($size = 21, GeneratorInterface $generator = null)
/**
* Generate nanoid via optional modes
*
* @param integer $size
* @param integer $mode Client::MODE_NORMAL|Client::MODE_DYNAMIC
* @return string
*/
public function generateId($size = 0, $mode = self::MODE_NORMAL)
public function generateId(int $size = 0, int $mode = self::MODE_NORMAL): string
{
$size = $size > 0 ? $size : $this->size;
switch ($mode) {
Expand All @@ -67,12 +63,9 @@ public function generateId($size = 0, $mode = self::MODE_NORMAL)
* you have been implements your custom GeneratorInterface as correctly.
* Otherwise use the build-in default random bytes generator
*
* @param GeneratorInterface $generator
* @param integer $size
* @param string $alphabet default CoreInterface::SAFE_SYMBOLS
* @return string
*/
public function formattedId($alphabet, $size = 0, GeneratorInterface $generator = null)
public function formattedId(string $alphabet, int $size = 0, GeneratorInterface $generator = null): string
{
$alphabet = $alphabet ?: CoreInterface::SAFE_SYMBOLS;
$size = $size > 0 ? $size : $this->size;
Expand All @@ -84,14 +77,9 @@ public function formattedId($alphabet, $size = 0, GeneratorInterface $generator
/**
* Backwards-compatible method name.
*
* @param string $alphabet
* @param integer $size
* @param GeneratorInterface $generator
*
* @return string
* @since 1.0.0
*/
public function formatedId($alphabet, $size = 0, GeneratorInterface $generator = null)
public function formatedId(string $alphabet, int $size = 0, GeneratorInterface $generator = null): string
{
$size = $size > 0 ? $size : $this->size;

Expand All @@ -104,10 +92,8 @@ public function formatedId($alphabet, $size = 0, GeneratorInterface $generator =
* as UUID v4.
*
* @see https://github.com/ai/nanoid/blob/master/non-secure/index.js#L19
* @param integer $size
* @return string
*/
private function normalRandom($size)
private function normalRandom(int $size): string
{
$id = '';
while (1 <= $size--) {
Expand Down
9 changes: 6 additions & 3 deletions src/Core.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Hidehalo\Nanoid;

class Core implements CoreInterface
Expand All @@ -7,8 +7,11 @@ class Core implements CoreInterface
* @inheritDoc
* @see https://github.com/ai/nanoid/blob/master/async/index.browser.js#L4
*/
public function random(GeneratorInterface $generator, $size, $alphabet = CoreInterface::SAFE_SYMBOLS)
{
public function random(
GeneratorInterface $generator,
int $size,
string $alphabet = CoreInterface::SAFE_SYMBOLS
): string {
$len = strlen($alphabet);
$mask = (2 << (int) (log($len - 1) / M_LN2)) - 1;
$step = (int) ceil(1.6 * $mask * $size / $len);
Expand Down
12 changes: 6 additions & 6 deletions src/CoreInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Hidehalo\Nanoid;

interface CoreInterface
Expand All @@ -10,10 +10,10 @@ interface CoreInterface
* Secure random string generator with custom alphabet.
* Alphabet must contain 256 symbols or less. Otherwise, the generator
* will not be secure.
*
* @param GeneratorInterface $generator
* @param string $alphabet
* @param integer $size
*/
public function random(GeneratorInterface $generator, $size, $alphabet = CoreInterface::SAFE_SYMBOLS);
public function random(
GeneratorInterface $generator,
int $size,
string $alphabet = CoreInterface::SAFE_SYMBOLS
): string;
}
4 changes: 2 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
<?php declare(strict_types=1);
namespace Hidehalo\Nanoid;

class Generator implements GeneratorInterface
{
/**
* @inheritDoc
*/
public function random($size)
public function random(int $size): array
{
return unpack('C*', \random_bytes($size));
}
Expand Down
7 changes: 2 additions & 5 deletions src/GeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<?php
<?php declare(strict_types=1);
namespace Hidehalo\Nanoid;

interface GeneratorInterface
{
/**
* Return random bytes array
*
* @param integer $size
* @return array
*/
public function random($size);
public function random(int $size): array;
}
15 changes: 5 additions & 10 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Hidehalo\Nanoid\Test;

use Hidehalo\Nanoid\Client;
Expand All @@ -10,9 +10,8 @@ class ClientTest extends TestCase
/**
* @group passed
* @dataProvider clientProvider
* @param Client $client
*/
public function testGenerateId(Client $client)
public function testGenerateId(Client $client): void
{
$size = 7;
$normalRandom = $client->generateId($size);
Expand All @@ -29,9 +28,8 @@ public function testGenerateId(Client $client)
/**
* @group passed
* @dataProvider clientProvider
* @param Client $client
*/
public function testFormattedId(Client $client)
public function testFormattedId(Client $client): void
{
$size = 10;
$alphabet = '0123456789abcdefghi';
Expand All @@ -47,9 +45,8 @@ public function testFormattedId(Client $client)
/**
* @group passed
* @dataProvider clientProvider
* @param Client $client
*/
public function testFormatedId(Client $client)
public function testFormatedId(Client $client): void
{
$size = 10;
$alphabet = '0123456789abcdefghi';
Expand All @@ -64,10 +61,8 @@ public function testFormatedId(Client $client)

/**
* Client Provider
*
* @return mixed
*/
public function clientProvider()
public static function clientProvider(): array
{
return [
[new Client()]
Expand Down
10 changes: 3 additions & 7 deletions tests/CoreTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Hidehalo\Nanoid\Test;

use Hidehalo\Nanoid\Core;
Expand All @@ -12,10 +12,8 @@ class CoreTest extends TestCase
/**
* @group passed
* @dataProvider coreProvider
* @param CoreInterface $core
* @param GeneratorInterface $generator
*/
public function testRandom(CoreInterface $core, GeneratorInterface $generator)
public function testRandom(CoreInterface $core, GeneratorInterface $generator): void
{
$alph = 'abcdefghijk0123456789';
$size = 5;
Expand All @@ -25,10 +23,8 @@ public function testRandom(CoreInterface $core, GeneratorInterface $generator)

/**
* Core Provider
*
* @return mixed
*/
public function coreProvider()
public static function coreProvider(): array
{
return [
[new Core(), new Generator()]
Expand Down
9 changes: 3 additions & 6 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Hidehalo\Nanoid\Test;

use PHPUnit\Framework\TestCase;
Expand All @@ -11,9 +11,8 @@ class GeneratorTest extends TestCase
/**
* @group passed
* @dataProvider generatorProvider
* @param CoreInterface $core
*/
public function testRandom(GeneratorInterface $generator)
public function testRandom(GeneratorInterface $generator): void
{
$size = 5;
$ret = $generator->random($size);
Expand All @@ -22,10 +21,8 @@ public function testRandom(GeneratorInterface $generator)

/**
* Generator Provider
*
* @return mixed
*/
public function generatorProvider()
public static function generatorProvider(): array
{
return [
[new Generator()]
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/DummyGenerator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Hidehalo\Nanoid\Test\Support;

use Hidehalo\Nanoid\GeneratorInterface;
Expand All @@ -8,7 +8,7 @@ class DummyGenerator implements GeneratorInterface
/**
* @inheritDoc
*/
public function random($size)
public function random(int $size): array
{
$ret = [];
while ($size--) {
Expand Down