Skip to content

Commit

Permalink
Merge pull request #2 from tiny-blocks/release/1.0.0
Browse files Browse the repository at this point in the history
Release/1.0.0
  • Loading branch information
gustavofreze authored Jun 8, 2023
2 parents f10f27d + c356a6d commit 64162da
Show file tree
Hide file tree
Showing 15 changed files with 600 additions and 2 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on:
push:
pull_request:

permissions:
contents: read

jobs:
auto-review:
name: Auto review
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install dependencies
run: composer update --no-progress --optimize-autoloader

- name: Run phpcs
run: composer phpcs

- name: Run phpmd
run: composer phpmd

tests:
name: Tests
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install dependencies
run: composer update --no-progress --optimize-autoloader

- name: Run unit tests
env:
XDEBUG_MODE: coverage
run: composer test

- name: Run mutation tests
run: composer test-mutation
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
vendor
report
composer.lock
.phpunit.result.cache
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022-2023 Tiny Blocks

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.2.6

.PHONY: configure test test-no-coverage review show-reports clean

configure:
@${DOCKER_RUN} composer update --optimize-autoloader

test: review
@${DOCKER_RUN} composer tests

test-no-coverage: review
@${DOCKER_RUN} composer tests-no-coverage

review:
@${DOCKER_RUN} composer review

show-reports:
@sensible-browser report/coverage/coverage-html/index.html report/coverage/mutation-report.html

clean:
@sudo chown -R ${USER}:${USER} ${PWD}
@rm -rf report vendor
65 changes: 63 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,63 @@
# ksuid
K-Sortable Unique Identifier for PHP.
# Ksuid

[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)

* [Overview](#overview)
* [Installation](#installation)
* [How to use](#how-to-use)
* [License](#license)
* [Contributing](#contributing)

<div id='overview'></div>

## Overview

Ksuid stands for [K-Sortable Unique Identifier](https://segment.com/blog/a-brief-history-of-the-uuid). It's a way to
generate globally unique IDs which are partially chronologically sortable.

<div id='installation'></div>

## Installation

```bash
composer require tiny-blocks/ksuid
```

<div id='how-to-use'></div>

## How to use

The library exposes a concrete implementation through the `Ksuid` class.

With the `random` method, a new instance of type `Ksuid` is created from a timestamp (_current unix timestamp - EPOCH_)
and a payload (_cryptographically secure pseudo-random bytes_).

```php
$ksuid = Ksuid::random();

echo $ksuid->getValue(); # 2QvY47aUlV3cSyYcpo53FQxgSFg
echo $ksuid->getPayload(); # bdf0a2329620aa70cebe4026ca9ff49c
echo $ksuid->getTimestamp(); # 286235327
```

You can also choose from other factory models.

```php

Ksuid::from(payload: hex2bin("9850EEEC191BF4FF26F99315CE43B0C8"), timestamp: 286235327);

Ksuid::fromPayload(value: '0o5Fs0EELR0fUjHjbCnEtdUwQe3');

Ksuid::fromTimestamp(value: 286235327);
```

## License

Math is licensed under [MIT](/LICENSE).

<div id='contributing'></div>

## Contributing

Please follow the [contributing guidelines](https://github.com/tiny-blocks/tiny-blocks/blob/main/CONTRIBUTING.md) to
contribute to the project.
71 changes: 71 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "tiny-blocks/ksuid",
"type": "library",
"license": "MIT",
"homepage": "https://github.com/tiny-blocks/ksuid",
"description": "K-Sortable Unique Identifier for PHP.",
"prefer-stable": true,
"minimum-stability": "stable",
"keywords": [
"psr",
"psr-4",
"ksuid",
"psr-12",
"base62",
"unique",
"identifier",
"tiny-blocks"
],
"authors": [
{
"name": "Gustavo Freze de Araujo Santos",
"homepage": "https://github.com/gustavofreze"
}
],
"config": {
"sort-packages": true,
"allow-plugins": {
"infection/extension-installer": true
}
},
"autoload": {
"psr-4": {
"TinyBlocks\\Ksuid\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"TinyBlocks\\Ksuid\\": "tests/"
}
},
"require": {
"php": "^8.2",
"tiny-blocks/encoder": "^1"
},
"require-dev": {
"infection/infection": "^0.26",
"phpmd/phpmd": "^2.12",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.7"
},
"scripts": {
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --ignore-violations-on-exit",
"test": "phpunit --log-junit=report/coverage/junit.xml --coverage-xml=report/coverage/coverage-xml --coverage-html=report/coverage/coverage-html tests",
"test-mutation": "infection --only-covered --logger-html=report/coverage/mutation-report.html --coverage=report/coverage --min-msi=100 --min-covered-msi=100 --threads=4",
"test-no-coverage": "phpunit --no-coverage",
"test-mutation-no-coverage": "infection --only-covered --min-msi=100 --threads=4",
"review": [
"@phpcs",
"@phpmd"
],
"tests": [
"@test",
"@test-mutation"
],
"tests-no-coverage": [
"@test-no-coverage",
"@test-mutation-no-coverage"
]
}
}
30 changes: 30 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"timeout": 10,
"testFramework": "phpunit",
"tmpDir": "report/",
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "report/logs/infection-text.log",
"summary": "report/logs/infection-summary.log"
},
"mutators": {
"@default": true,
"Plus": false,
"Minus": false,
"Concat": false,
"CastArray": false,
"GreaterThan": false,
"UnwrapSubstr": false,
"UnwrapStrRepeat": false,
"IncrementInteger": false,
"DecrementInteger": false
},
"phpUnit": {
"configDir": "",
"customPath": "./vendor/bin/phpunit"
}
}
57 changes: 57 additions & 0 deletions phpmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0"?>
<ruleset name="PHPMD Custom rules"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>PHPMD Custom rules</description>

<rule ref="rulesets/cleancode.xml/ElseExpression"/>
<rule ref="rulesets/cleancode.xml/BooleanArgumentFlag"/>

<rule ref="rulesets/codesize.xml/TooManyFields"/>
<rule ref="rulesets/codesize.xml/TooManyMethods"/>
<rule ref="rulesets/codesize.xml/NPathComplexity"/>
<rule ref="rulesets/codesize.xml/CyclomaticComplexity"/>
<rule ref="rulesets/codesize.xml/ExcessivePublicCount"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassLength"/>
<rule ref="rulesets/codesize.xml/TooManyPublicMethods"/>
<rule ref="rulesets/codesize.xml/ExcessiveMethodLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveParameterList"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity"/>

<rule ref="rulesets/controversial.xml/Superglobals"/>
<rule ref="rulesets/controversial.xml/CamelCaseClassName"/>
<rule ref="rulesets/controversial.xml/CamelCaseMethodName"/>
<rule ref="rulesets/controversial.xml/CamelCasePropertyName"/>
<rule ref="rulesets/controversial.xml/CamelCaseVariableName"/>
<rule ref="rulesets/controversial.xml/CamelCaseParameterName"/>

<rule ref="rulesets/design.xml/GotoStatement"/>
<rule ref="rulesets/design.xml/ExitExpression"/>
<rule ref="rulesets/design.xml/EvalExpression"/>
<rule ref="rulesets/design.xml/NumberOfChildren"/>
<rule ref="rulesets/design.xml/DepthOfInheritance"/>
<rule ref="rulesets/design.xml/CouplingBetweenObjects"/>
<rule ref="rulesets/design.xml/DevelopmentCodeFragment"/>

<rule ref="rulesets/naming.xml/LongVariable"/>
<rule ref="rulesets/naming.xml/ShortVariable">
<properties>
<property name="minimum" value="2"/>
</properties>
</rule>
<rule ref="rulesets/naming.xml/ShortMethodName">
<properties>
<property name="minimum" value="2"/>
</properties>
</rule>
<rule ref="rulesets/naming.xml/BooleanGetMethodName"/>
<rule ref="rulesets/naming.xml/ConstantNamingConventions"/>
<rule ref="rulesets/naming.xml/ConstructorWithNameAsEnclosingClass"/>

<rule ref="rulesets/unusedcode.xml/UnusedPrivateField"/>
<rule ref="rulesets/unusedcode.xml/UnusedLocalVariable"/>
<rule ref="rulesets/unusedcode.xml/UnusedPrivateMethod"/>
<rule ref="rulesets/unusedcode.xml/UnusedFormalParameter"/>
</ruleset>
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
cacheResultFile="report/.phpunit.result.cache"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
14 changes: 14 additions & 0 deletions src/Internal/Exceptions/InvalidPayloadSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace TinyBlocks\Ksuid\Internal\Exceptions;

use RuntimeException;

final class InvalidPayloadSize extends RuntimeException
{
public function __construct(private readonly int $currentSize, private readonly int $payloadBytes)
{
$template = 'Current length is <%s> bytes. Payload size must be exactly <%s> bytes.';
parent::__construct(sprintf($template, $this->currentSize, $this->payloadBytes));
}
}
Loading

0 comments on commit 64162da

Please sign in to comment.