Skip to content

Commit

Permalink
Setup PHP CS Fixer (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
marmichalski authored Dec 20, 2023
1 parent 53688f1 commit cae6fe4
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 123 deletions.
24 changes: 12 additions & 12 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/benchmark export-ignore
/static-analysis export-ignore
/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php_cs export-ignore
.travis.yml export-ignore
composer.lock export-ignore
phpbench.json export-ignore
phpunit.xml.dist export-ignore
psalm.xml export-ignore
rector.php export-ignore
/benchmark export-ignore
/static-analysis export-ignore
/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php-cs-fixer.php export-ignore
.travis.yml export-ignore
composer.lock export-ignore
phpbench.json export-ignore
phpunit.xml.dist export-ignore
psalm.xml export-ignore
rector.php export-ignore
4 changes: 4 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ jobs:
with:
dependency-versions: "${{ matrix.dependencies }}"

- name: "Check code style"
if: ${{ matrix.dependencies == 'highest' && matrix.php == '8.1' }}
run: composer code-style:check

- name: "Run psalm"
run: vendor/bin/psalm

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.phpunit.result.cache
.php-cs-fixer.cache
/phpunit.xml
/vendor/
17 changes: 17 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

$finder = \PhpCsFixer\Finder::create()
->in(__DIR__)
;

return (new \PhpCsFixer\Config())->setRiskyAllowed(true)
->setRules([
'@PHP81Migration' => true,
'@Symfony' => true,
'yoda_style' => false,

])
->setFinder($finder)
;
15 changes: 0 additions & 15 deletions .php_cs

This file was deleted.

5 changes: 2 additions & 3 deletions benchmark/BaseShorteningBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Benchmark\Keiko\Uuid\Shortener;

use Keiko\Uuid\Shortener\Shortener;
use function array_map;

abstract class BaseShorteningBench
{
Expand Down Expand Up @@ -145,7 +144,7 @@ public function benchShorteningOfHugeUuid(): void

public function benchShorteningOfPromiscuousUuids(): void
{
array_map($this->shortener->reduce(...), self::UUIDS_TO_BE_SHORTENED);
\array_map($this->shortener->reduce(...), self::UUIDS_TO_BE_SHORTENED);
}

public function benchExpandingOfTinyUuid(): void
Expand All @@ -160,6 +159,6 @@ public function benchExpandingOfHugeUuid(): void

public function benchExpandingOfPromiscuousUuids(): void
{
array_map($this->shortener->expand(...), $this->shortenedPromoscuousUuids);
\array_map($this->shortener->expand(...), $this->shortenedPromoscuousUuids);
}
}
2 changes: 1 addition & 1 deletion benchmark/GMPShortenerBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class GMPShortenerBench extends BaseShorteningBench
{
protected function newShortener() : Shortener
protected function newShortener(): Shortener
{
return new GMPShortener(Dictionary::createUnmistakable());
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/ShortenerBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class ShortenerBench extends BaseShorteningBench
{
protected function newShortener() : Shortener
protected function newShortener(): Shortener
{
return new Shortener(Dictionary::createUnmistakable(), new Converter());
}
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"phpunit/phpunit": "^8.5.26",
"vimeo/psalm": "^4.3.1",
"phpbench/phpbench": "^1.1.1",
"rector/rector": "^0.18.12"
"rector/rector": "^0.18.12",
"php-cs-fixer/shim": "^3.41"
},
"suggest": {
"ramsey/uuid": "A PHP 5.4+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID)",
Expand All @@ -49,6 +50,8 @@
}
},
"scripts": {
"code-style:check": "php-cs-fixer fix --dry-run --diff --ansi",
"code-style:fix": "php-cs-fixer fix --diff --ansi",
"rector:check": "rector --dry-run --ansi",
"rector:fix": "rector --ansi"
}
Expand Down
54 changes: 53 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 4 additions & 19 deletions src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
namespace Keiko\Uuid\Shortener;

use Keiko\Uuid\Shortener\Exception\DictionaryException;
use function array_flip;
use function count_chars;
use function str_split;
use function strlen;

/** @psalm-immutable */
final class Dictionary
Expand All @@ -34,25 +30,23 @@ final class Dictionary
*/
public function __construct(string $charsSet)
{
$length = strlen($charsSet);
$length = \strlen($charsSet);

if ($length <= 16) {
throw DictionaryException::charsSetTooShort();
}

$uniqueChars = count_chars($charsSet, 3);
if (strlen($uniqueChars) !== $length) {
$uniqueChars = \count_chars($charsSet, 3);
if (\strlen($uniqueChars) !== $length) {
throw DictionaryException::nonUniqueChars();
}

$this->charsSet = $charsSet;
$this->length = $length;
$this->charIndexes = array_flip(str_split($charsSet));
$this->charIndexes = \array_flip(\str_split($charsSet));
}

/**
* @return Dictionary
*
* @psalm-pure
*/
public static function createUnmistakable(): Dictionary
Expand All @@ -61,27 +55,20 @@ public static function createUnmistakable(): Dictionary
}

/**
* @return Dictionary
*
* @psalm-pure
*/
public static function createAlphanumeric(): Dictionary
{
return new self(self::DICTIONARY_ALPHANUMERIC);
}

/**
* @return int
*/
public function getLength(): int
{
return $this->length;
}

/**
* @throws DictionaryException
*
* @return string
*/
public function getCharAt(int $index): string
{
Expand All @@ -94,8 +81,6 @@ public function getCharAt(int $index): string

/**
* @throws DictionaryException
*
* @return int
*/
public function getCharIndex(string $char): int
{
Expand Down
51 changes: 20 additions & 31 deletions src/GMPShortener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,13 @@
namespace Keiko\Uuid\Shortener;

use Keiko\Uuid\Shortener\Exception\DictionaryException;
use function array_flip;
use function gmp_init;
use function gmp_strval;
use function preg_match;
use function preg_quote;
use function str_pad;
use function str_replace;
use function strrev;
use function strtr;
use function substr;
use const STR_PAD_LEFT;

/** @psalm-immutable */
final class GMPShortener extends Shortener
{
/**
* @var string[] a map of character replacements to be used when translating a number that is
* in base(length(Dictionary)) (keys) to the dictionary characters (values).
* in base(length(Dictionary)) (keys) to the dictionary characters (values)
*
* @psalm-var non-empty-array<non-empty-string, non-empty-string>
*/
Expand All @@ -35,48 +24,48 @@ public function __construct(private readonly Dictionary $dictionary)
$replacements = [];

foreach (range(0, $dictionary->length - 1) as $characterIndex) {
$replacements[gmp_strval(gmp_init((string) $characterIndex, 10), $dictionary->length)] = $dictionary->charsSet[$characterIndex];
$replacements[\gmp_strval(\gmp_init((string) $characterIndex, 10), $dictionary->length)] = $dictionary->charsSet[$characterIndex];
}

$this->baseReplacements = $replacements;
$this->allowedCharactersMatcher = '/^['. preg_quote($dictionary->charsSet, '/').']+$/';
$this->allowedCharactersMatcher = '/^['.\preg_quote($dictionary->charsSet, '/').']+$/';
}

public function reduce(string $uuid): string
{
return strrev(strtr(
gmp_strval(gmp_init(str_replace('-', '', $uuid), 16), $this->dictionary->length),
return \strrev(\strtr(
\gmp_strval(\gmp_init(\str_replace('-', '', $uuid), 16), $this->dictionary->length),
$this->baseReplacements
));
}

public function expand(string $shortUuid): string
{
if (1 !== preg_match($this->allowedCharactersMatcher, $shortUuid)) {
if (1 !== \preg_match($this->allowedCharactersMatcher, $shortUuid)) {
throw DictionaryException::indexOutOfBounds();
}

$base16Uuid = str_pad(
gmp_strval(
gmp_init(
strtr(strrev($shortUuid), array_flip($this->baseReplacements)),
$base16Uuid = \str_pad(
\gmp_strval(
\gmp_init(
\strtr(\strrev($shortUuid), \array_flip($this->baseReplacements)),
$this->dictionary->length
),
16
),
32,
'0',
STR_PAD_LEFT
\STR_PAD_LEFT
);

return substr($base16Uuid,0, 8)
. '-'
. substr($base16Uuid, 8, 4)
. '-'
. substr($base16Uuid, 12, 4)
. '-'
. substr($base16Uuid, 16, 4)
. '-'
. substr($base16Uuid, 20, 12);
return \substr($base16Uuid, 0, 8)
.'-'
.\substr($base16Uuid, 8, 4)
.'-'
.\substr($base16Uuid, 12, 4)
.'-'
.\substr($base16Uuid, 16, 4)
.'-'
.\substr($base16Uuid, 20, 12);
}
}
Loading

0 comments on commit cae6fe4

Please sign in to comment.