Skip to content

Commit

Permalink
chore: fix php notices
Browse files Browse the repository at this point in the history
  • Loading branch information
JoyceBabu committed May 27, 2023
1 parent 01e74fa commit 4419022
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 34 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Snowflake ID Generator that does not require a daemon",
"type": "library",
"require": {
"php": ">=5.6.0"
"php": ">=7.4"
},
"license": "MIT",
"authors": [
Expand Down
6 changes: 5 additions & 1 deletion src/Snowflake/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

declare(strict_types=1);

namespace Ennexa\Snowflake\Exception;

use Ennexa\Snowflake\ExceptionInterface;

class InvalidArgumentException
extends \InvalidArgumentException
implements \Ennexa\Snowflake\ExceptionInterface {
implements ExceptionInterface {
}
6 changes: 5 additions & 1 deletion src/Snowflake/Exception/InvalidSystemClockException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

declare(strict_types=1);

namespace Ennexa\Snowflake\Exception;

use Ennexa\Snowflake\ExceptionInterface;

class InvalidSystemClockException
extends \Exception
implements \Ennexa\Snowflake\ExceptionInterface {
implements ExceptionInterface {
}
6 changes: 5 additions & 1 deletion src/Snowflake/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

declare(strict_types=1);

namespace Ennexa\Snowflake\Exception;

use Ennexa\Snowflake\ExceptionInterface;

class RuntimeException
extends \RuntimeException
implements \Ennexa\Snowflake\ExceptionInterface {
implements ExceptionInterface {

}
4 changes: 2 additions & 2 deletions src/Snowflake/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Ennexa\Snowflake;

interface ExceptionInterface {

interface ExceptionInterface extends \Throwable
{
}
48 changes: 22 additions & 26 deletions src/Snowflake/Generator.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
<?php

declare(strict_types=1);

namespace Ennexa\Snowflake;

use Exception\InvalidArgumentException;
use Exception\InvalidSystemClockException;
use Ennexa\Snowflake\Exception\InvalidArgumentException;
use Ennexa\Snowflake\Exception\InvalidSystemClockException;

class Generator {
class Generator
{
const NODE_LEN = 8;
const WORKER_LEN = 8;
const SEQUENCE_LEN = 8;

private $instanceId = 0;
private $startEpoch = 1546300800000;
private $sequenceMax;
private $store;
private int $instanceId = 0;
private int $startEpoch = 1546300800000;
private int $sequenceMask;
private int $sequenceMax;
private StoreInterface $store;
private int $tickShift;

private static function getMaxValue(int $len)
private static function getMaxValue(int $len): int
{
return -1 ^ (-1 << $len);
}

public static function generateInstanceId($nodeId = 0, $workerId = 0)
{
$nodeIdMax = $this->getMaxValue(self::NODE_LEN);
$nodeIdMax = self::getMaxValue(self::NODE_LEN);
if ($nodeId < 0 || $nodeId > $nodeIdMax) {
throw InvalidArgumentException("Node ID should be between 0 and $nodeIdMax");
throw new InvalidArgumentException("Node ID should be between 0 and $nodeIdMax");
}

$workerIdMax = $this->getMaxValue(self::WORKER_LEN);
$workerIdMax = self::getMaxValue(self::WORKER_LEN);
if ($workerId < 0 || $workerId > $workerIdMax) {
throw InvalidArgumentException("Worker ID should be between 0 and $workerIdMax");
throw new InvalidArgumentException("Worker ID should be between 0 and $workerIdMax");
}

return $nodeId << self::WORKER_LEN | $workerId;
Expand All @@ -51,33 +56,24 @@ public function __construct(StoreInterface $store, int $instanceId = 0, ?int $st

/**
* Set the sequence store
*
* @param int Instance Id
* @return void
*/
public function setStore(StoreInterface $store)
public function setStore(StoreInterface $store): void
{
$this->store = $store;
}

/**
* Get the current generator instance id
*
* @param int Instance Id
* @return void
*/
public function getInstanceId()
public function getInstanceId(): int
{
return $this->instanceId >> self::SEQUENCE_LEN;
}

/**
* Set the instance id for the generator instance
*
* @param int Instance Id
* @return void
*/
public function setInstanceId(int $instanceId)
public function setInstanceId(int $instanceId): void
{
$this->instanceId = $instanceId << self::SEQUENCE_LEN;
}
Expand All @@ -95,10 +91,10 @@ public function nextSequence()
/**
* Generate a unique id based on the epoch and instance id
*
* @return int unique 64-bit id
* @return string unique 64-bit id
* @throws InvalidSystemClockException
*/
public function nextId()
public function nextId(): string
{
list($timestamp, $sequence) = $this->nextSequence();

Expand Down
6 changes: 4 additions & 2 deletions src/Snowflake/StoreInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Ennexa\Snowflake;

interface StoreInterface {
public function next(int $instanceId):array;
interface StoreInterface
{
public function next(int $instanceId): array;
}

0 comments on commit 4419022

Please sign in to comment.