Skip to content

Commit 4419022

Browse files
committed
chore: fix php notices
1 parent 01e74fa commit 4419022

7 files changed

+44
-34
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Snowflake ID Generator that does not require a daemon",
44
"type": "library",
55
"require": {
6-
"php": ">=5.6.0"
6+
"php": ">=7.4"
77
},
88
"license": "MIT",
99
"authors": [
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Ennexa\Snowflake\Exception;
46

7+
use Ennexa\Snowflake\ExceptionInterface;
8+
59
class InvalidArgumentException
610
extends \InvalidArgumentException
7-
implements \Ennexa\Snowflake\ExceptionInterface {
11+
implements ExceptionInterface {
812
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Ennexa\Snowflake\Exception;
46

7+
use Ennexa\Snowflake\ExceptionInterface;
8+
59
class InvalidSystemClockException
610
extends \Exception
7-
implements \Ennexa\Snowflake\ExceptionInterface {
11+
implements ExceptionInterface {
812
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Ennexa\Snowflake\Exception;
46

7+
use Ennexa\Snowflake\ExceptionInterface;
8+
59
class RuntimeException
610
extends \RuntimeException
7-
implements \Ennexa\Snowflake\ExceptionInterface {
11+
implements ExceptionInterface {
812

913
}

src/Snowflake/ExceptionInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace Ennexa\Snowflake;
44

5-
interface ExceptionInterface {
6-
5+
interface ExceptionInterface extends \Throwable
6+
{
77
}

src/Snowflake/Generator.php

+22-26
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Ennexa\Snowflake;
46

5-
use Exception\InvalidArgumentException;
6-
use Exception\InvalidSystemClockException;
7+
use Ennexa\Snowflake\Exception\InvalidArgumentException;
8+
use Ennexa\Snowflake\Exception\InvalidSystemClockException;
79

8-
class Generator {
10+
class Generator
11+
{
912
const NODE_LEN = 8;
1013
const WORKER_LEN = 8;
1114
const SEQUENCE_LEN = 8;
1215

13-
private $instanceId = 0;
14-
private $startEpoch = 1546300800000;
15-
private $sequenceMax;
16-
private $store;
16+
private int $instanceId = 0;
17+
private int $startEpoch = 1546300800000;
18+
private int $sequenceMask;
19+
private int $sequenceMax;
20+
private StoreInterface $store;
21+
private int $tickShift;
1722

18-
private static function getMaxValue(int $len)
23+
private static function getMaxValue(int $len): int
1924
{
2025
return -1 ^ (-1 << $len);
2126
}
2227

2328
public static function generateInstanceId($nodeId = 0, $workerId = 0)
2429
{
25-
$nodeIdMax = $this->getMaxValue(self::NODE_LEN);
30+
$nodeIdMax = self::getMaxValue(self::NODE_LEN);
2631
if ($nodeId < 0 || $nodeId > $nodeIdMax) {
27-
throw InvalidArgumentException("Node ID should be between 0 and $nodeIdMax");
32+
throw new InvalidArgumentException("Node ID should be between 0 and $nodeIdMax");
2833
}
2934

30-
$workerIdMax = $this->getMaxValue(self::WORKER_LEN);
35+
$workerIdMax = self::getMaxValue(self::WORKER_LEN);
3136
if ($workerId < 0 || $workerId > $workerIdMax) {
32-
throw InvalidArgumentException("Worker ID should be between 0 and $workerIdMax");
37+
throw new InvalidArgumentException("Worker ID should be between 0 and $workerIdMax");
3338
}
3439

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

5257
/**
5358
* Set the sequence store
54-
*
55-
* @param int Instance Id
56-
* @return void
5759
*/
58-
public function setStore(StoreInterface $store)
60+
public function setStore(StoreInterface $store): void
5961
{
6062
$this->store = $store;
6163
}
6264

6365
/**
6466
* Get the current generator instance id
65-
*
66-
* @param int Instance Id
67-
* @return void
6867
*/
69-
public function getInstanceId()
68+
public function getInstanceId(): int
7069
{
7170
return $this->instanceId >> self::SEQUENCE_LEN;
7271
}
7372

7473
/**
7574
* Set the instance id for the generator instance
76-
*
77-
* @param int Instance Id
78-
* @return void
7975
*/
80-
public function setInstanceId(int $instanceId)
76+
public function setInstanceId(int $instanceId): void
8177
{
8278
$this->instanceId = $instanceId << self::SEQUENCE_LEN;
8379
}
@@ -95,10 +91,10 @@ public function nextSequence()
9591
/**
9692
* Generate a unique id based on the epoch and instance id
9793
*
98-
* @return int unique 64-bit id
94+
* @return string unique 64-bit id
9995
* @throws InvalidSystemClockException
10096
*/
101-
public function nextId()
97+
public function nextId(): string
10298
{
10399
list($timestamp, $sequence) = $this->nextSequence();
104100

src/Snowflake/StoreInterface.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
2+
23
namespace Ennexa\Snowflake;
34

4-
interface StoreInterface {
5-
public function next(int $instanceId):array;
5+
interface StoreInterface
6+
{
7+
public function next(int $instanceId): array;
68
}

0 commit comments

Comments
 (0)