Skip to content

Commit

Permalink
⬆️ drop PHP 7
Browse files Browse the repository at this point in the history
  • Loading branch information
garak committed Oct 18, 2024
1 parent 9876083 commit db55ed2
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 83 deletions.
26 changes: 9 additions & 17 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,34 @@ on:

jobs:
phpstan:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
name: PHPStan
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: PHPStan
uses: docker://oskarstark/phpstan-ga
env:
REQUIRE_DEV: true
with:
args: analyse
cs-fixer:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
name: PHP-CS-Fixer
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Fix CS
uses: docker://oskarstark/php-cs-fixer-ga
tests:
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
strategy:
max-parallel: 1
matrix:
include:
- description: 'lowest'
php: '7.1'
php: '8.1'
composer_option: '--prefer-lowest'
- description: '7.2'
php: '7.2'
- description: '7.3'
php: '7.3'
- description: '7.4'
php: '7.4'
- description: '8.0'
php: '8.0'
- description: '8.1'
php: '8.1'
- description: '8.2'
Expand All @@ -51,10 +43,10 @@ jobs:
name: PHP ${{ matrix.php }} tests
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Cache
uses: actions/cache@v3
with:
uses: actions/cache@v4
with:
path: ~/.composer/cache/files
key: ${{ matrix.php }}-${{ matrix.composer_option }}
- name: Setup PHP
Expand Down
5 changes: 3 additions & 2 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHP71Migration:risky' => true,
'@PHPUnit75Migration:risky' => true,
'@PHP80Migration:risky' => true,
'@PHP81Migration' => true,
'@PHPUnit100Migration:risky' => true,
'declare_strict_types' => false,
'native_function_invocation' => ['include' => ['@all']],
'final_class' => true,
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $id = Shortid::generate();

For more control, you can customize the alphabet and length using the Factory class.

Default alphabet uses all letters (lowercase and uppercase), all numbers, underscore, and hypen.
The default alphabet uses all letters (lowercase and uppercase), all numbers, underscore, and hyphen.

``` php
use PUGX\Shortid\Factory;
Expand All @@ -52,14 +52,14 @@ $factory = new Factory();
// alphabet string must be 64 characters long
$factory->setAlphabet('é123456789àbcdefghìjklmnòpqrstùvwxyzABCDEFGHIJKLMNOPQRSTUVWX.!@|');
// length must be between 2 and 20 (default is 7)
// of course, a lower length is increasing clashing probability
// of course, a lower length increases the clashing probability
$factory->setLength(9);
Shortid::setFactory($factory);

$id = Shortid::generate();
```

As alternative, you can customize single generations:
As an alternative, you can customize single generations:

``` php
use PUGX\Shortid\Shortid;
Expand Down Expand Up @@ -104,7 +104,7 @@ $anotherFixedId = new Shortid('fooBarZ');

## Doctrine

If you want to use ShortId with Doctrine ORM, take a look to [ShortId Doctrine type][2].
If you want to use ShortId with Doctrine ORM, take a look at [ShortId Doctrine type][2].


## Doctrine and Symfony
Expand All @@ -121,7 +121,7 @@ native extension is not available.
If, instead, your environment is offering that extension, you can avoid installing
that polyfill by configuring a [replace][5] entry in your `composer.json`.

The same applies to the [randomLib][6] library: if you are using PHP 8.3 or higher,
The same applies to the [randomLib][6] library: if you are using PHP 8.3 or higher,
you can replace it, since this library uses the native `Random` class instead.

## Contributing
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@
}
],
"require": {
"php": "^7.1 || ^8.0",
"ext-json": "*",
"php": "^8.1",
"paragonie/random-lib": "^2.0",
"symfony/polyfill-mbstring": "^1.19"
},
"require-dev": {
"dg/bypass-finals": "^1.3",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5"
"phpunit/phpunit": "^9.6"
},
"config": {
"bin-dir": "bin"
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "1.x-dev"
}
},
"autoload": {
Expand Down
31 changes: 8 additions & 23 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,18 @@

final class Factory
{
/**
* @var int
*/
private $length = 7;

/**
* @var string
*/
private $alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-';

/**
* @var bool
*/
private $readable = false;

/**
* @var RandomLibFactory
*/
private static $factory;
private int $length = 7;
private string $alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-';
private bool $readable = false;
private static ?RandomLibFactory $factory = null;

/**
* @throws InvalidShortidException
*/
public function generate(int $length = null, string $alphabet = null, bool $readable = null): Shortid
public function generate(?int $length = null, ?string $alphabet = null, ?bool $readable = null): Shortid
{
$length = $length ?? $this->length;
$readable = $readable ?? $this->readable;
$length ??= $this->length;
$readable ??= $this->readable;
if (null === $alphabet && $readable) {
$alphabet = \str_replace(\str_split(Generator::AMBIGUOUS_CHARS), '', $this->alphabet);
$alphabet .= \str_repeat('_', \strlen(Generator::AMBIGUOUS_CHARS) / 2);
Expand Down Expand Up @@ -80,7 +65,7 @@ public static function getFactory(): RandomLibFactory
return self::$factory;
}

public function checkLength(int $length = null, bool $strict = false): void
public function checkLength(?int $length = null, bool $strict = false): void
{
if (null === $length && !$strict) {
return;
Expand Down
31 changes: 9 additions & 22 deletions src/Shortid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,18 @@

namespace PUGX\Shortid;

final class Shortid implements \JsonSerializable, \Serializable
final class Shortid implements \JsonSerializable, \Serializable, \Stringable
{
/**
* @var string
*/
private $id;

/**
* @var Factory|null
*/
private static $factory;
private static ?Factory $factory = null;

/**
* @throws InvalidShortidException
*/
public function __construct(string $id, int $length = null, string $alphabet = null)
public function __construct(private string $id, ?int $length = null, ?string $alphabet = null)
{
if (!self::isValid($id, $length, $alphabet)) {
throw new InvalidShortidException(\sprintf('Invalid shortid %s (length %d alphabet %s', $id, $length, $alphabet));
}

$this->id = $id;
}

public function __toString(): string
Expand All @@ -34,7 +24,7 @@ public function __toString(): string
/**
* @throws InvalidShortidException
*/
public static function generate(int $length = null, string $alphabet = null, bool $readable = false): self
public static function generate(?int $length = null, ?string $alphabet = null, bool $readable = false): self
{
if (null === $length) {
self::getFactory()->checkLength($length);
Expand All @@ -53,14 +43,14 @@ public static function getFactory(): Factory
return self::$factory;
}

public static function setFactory(Factory $factory = null): void
public static function setFactory(?Factory $factory = null): void
{
self::$factory = $factory;
}

public static function isValid(string $value, int $length = null, string $alphabet = null): bool
public static function isValid(string $value, ?int $length = null, ?string $alphabet = null): bool
{
$length = $length ?? self::getFactory()->getLength();
$length ??= self::getFactory()->getLength();
$alphabet = \preg_quote($alphabet ?: self::getFactory()->getAlphabet(), '/');
$matches = [];
$ok = \preg_match('/^(['.$alphabet.']{'.$length.'})$/', $value, $matches);
Expand All @@ -78,12 +68,9 @@ public function serialize(): string
return $this->id;
}

/**
* @param string $serialized
*/
public function unserialize($serialized): void
public function unserialize(string $data): void
{
$this->id = $serialized;
$this->id = $data;
}

/**
Expand Down
11 changes: 4 additions & 7 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

final class FactoryTest extends TestCase
{
/**
* @var Factory
*/
private $factory;
private Factory $factory;

protected function setUp(): void
{
Expand All @@ -25,14 +22,14 @@ public function testGenerate(): void
if (\method_exists($this, 'assertMatchesRegularExpression')) {
self::assertMatchesRegularExpression('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
} else {
self::assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
self::assertMatchesRegularExpression('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
}
}

/**
* @return string[][]
*/
public function alphabetsProvider(): array
public static function alphabetsProvider(): array
{
$alphabets = [];
$chars = [];
Expand Down Expand Up @@ -77,7 +74,7 @@ public function testSetWrongAlphabet(string $alphabet): void
/**
* @return array{test: array{'test'}, rand: array{string}}
*/
public function wrongAlphabetsProvider(): array
public static function wrongAlphabetsProvider(): array
{
return [
'test' => ['test'],
Expand Down
6 changes: 3 additions & 3 deletions tests/ShortidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGenerate(): void
if (\method_exists($this, 'assertMatchesRegularExpression')) {
self::assertMatchesRegularExpression('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
} else {
self::assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
self::assertMatchesRegularExpression('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
}
}

Expand All @@ -32,7 +32,7 @@ public function testGenerateWithReadable(): void
if (\method_exists($this, 'assertMatchesRegularExpression')) {
self::assertMatchesRegularExpression('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
} else {
self::assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
self::assertMatchesRegularExpression('/^[a-z0-9\_\-]{7}$/i', $generated->__toString());
}
}

Expand All @@ -43,7 +43,7 @@ public function testGenerateWithLength(): void
if (\method_exists($this, 'assertMatchesRegularExpression')) {
self::assertMatchesRegularExpression('/^[a-z0-9\_\-]{8}$/i', $generated->__toString());
} else {
self::assertRegExp('/^[a-z0-9\_\-]{8}$/i', $generated->__toString());
self::assertMatchesRegularExpression('/^[a-z0-9\_\-]{8}$/i', $generated->__toString());
}
}

Expand Down

0 comments on commit db55ed2

Please sign in to comment.