Skip to content

Commit dde1e16

Browse files
authored
Chore: deprecated getCurrentMicrotime (#45)
* Chore: deprecated getCurrentMicrotime * update pint use custom config
1 parent 18ceb83 commit dde1e16

File tree

8 files changed

+31
-16
lines changed

8 files changed

+31
-16
lines changed

.github/workflows/codestyle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
run: composer require "laravel/pint"
3333

3434
- name: Code Style
35-
run: vendor/bin/pint --test
35+
run: vendor/bin/pint --test --config ./pint.json
3636

3737
- name: Collect code coverage with phpunit
3838
run: vendor/bin/phpunit --coverage-clover=coverage.xml

pint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"rules": {
33
"header_comment": {
44
"header": "This file is part of the godruoyi/php-snowflake.\n \n(c) Godruoyi <[email protected]> \n \nThis source file is subject to the MIT license that is bundled."
5-
}
5+
},
6+
"no_superfluous_phpdoc_tags": false
67
}
78
}

src/SequenceResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface SequenceResolver
1515
/**
1616
* The snowflake.
1717
*
18-
* @param int|string $currentTime current request ms
18+
* @param int|string $currentTime current timestamp: milliseconds
1919
* @return int
2020
*/
2121
public function sequence(int $currentTime);

src/Snowflake.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ public function __construct(int $datacenter = null, int $workerid = null)
8484
*/
8585
public function id()
8686
{
87-
$currentTime = $this->getCurrentMicrotime();
87+
$currentTime = $this->getCurrentMillisecond();
8888
while (($sequence = $this->callResolver($currentTime)) > (-1 ^ (-1 << self::MAX_SEQUENCE_LENGTH))) {
8989
usleep(1);
90-
$currentTime = $this->getCurrentMicrotime();
90+
$currentTime = $this->getCurrentMillisecond();
9191
}
9292

9393
$workerLeftMoveLength = self::MAX_SEQUENCE_LENGTH;
@@ -120,7 +120,11 @@ public function parseId(string $id, bool $transform = false): array
120120
}
121121

122122
/**
123-
* Get current microtime timestamp.
123+
* Get current millisecond time.
124+
*
125+
* @deprecated the method name is wrong, use getCurrentMillisecond instead, will be removed in next major version.
126+
*
127+
* @codeCoverageIgnore
124128
*
125129
* @return int
126130
*/
@@ -129,14 +133,24 @@ public function getCurrentMicrotime()
129133
return floor(microtime(true) * 1000) | 0;
130134
}
131135

136+
/**
137+
* Get current millisecond time.
138+
*
139+
* @return int
140+
*/
141+
public function getCurrentMillisecond(): int
142+
{
143+
return floor(microtime(true) * 1000) | 0;
144+
}
145+
132146
/**
133147
* Set start time (millisecond).
134148
*
135149
* @throws Exception
136150
*/
137151
public function setStartTimeStamp(int $millisecond)
138152
{
139-
$missTime = $this->getCurrentMicrotime() - $millisecond;
153+
$missTime = $this->getCurrentMillisecond() - $millisecond;
140154

141155
if ($missTime < 0) {
142156
throw new Exception('The start time cannot be greater than the current time');

src/Sonyflake.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function id()
7878
*/
7979
public function setStartTimeStamp(int $millisecond)
8080
{
81-
$elapsedTime = floor(($this->getCurrentMicrotime() - $millisecond) / 10) | 0;
81+
$elapsedTime = floor(($this->getCurrentMillisecond() - $millisecond) / 10) | 0;
8282
if ($elapsedTime < 0) {
8383
throw new Exception('The start time cannot be greater than the current time');
8484
}
@@ -131,7 +131,7 @@ public function getDefaultSequenceResolver(): SequenceResolver
131131
*/
132132
private function elapsedTime(): int
133133
{
134-
return floor(($this->getCurrentMicrotime() - $this->getStartTimeStamp()) / 10) | 0;
134+
return floor(($this->getCurrentMillisecond() - $this->getStartTimeStamp()) / 10) | 0;
135135
}
136136

137137
/**

tests/SnowflakeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ public function testParseId()
148148
$this->assertSame($payloads['sequence'], '0');
149149
}
150150

151-
public function testGetCurrentMicrotime()
151+
public function testgetCurrentMillisecond()
152152
{
153153
$snowflake = new Snowflake(999, 20);
154154
$now = floor(microtime(true) * 1000) | 0;
155-
$time = $snowflake->getCurrentMicrotime();
155+
$time = $snowflake->getCurrentMillisecond();
156156

157157
$this->assertTrue($time >= $now);
158158
}

tests/SonyflakeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ public function testGetStartTimeStamp()
170170
$this->assertTrue(1 === $snowflake->getStartTimeStamp());
171171
}
172172

173-
public function testGetCurrentMicrotime()
173+
public function testgetCurrentMillisecond()
174174
{
175175
$snowflake = new Sonyflake(9990);
176176
$now = floor(microtime(true) * 1000) | 0;
177-
$time = $snowflake->getCurrentMicrotime();
177+
$time = $snowflake->getCurrentMillisecond();
178178

179179
$this->assertTrue($now - $time >= 0);
180180
}

tests/TimeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public function testTime()
1919
$s = new Snowflake();
2020
$a = 0;
2121

22-
while (($s1 = $s->getcurrentMicrotime()) && $a < 10) {
23-
$s2 = $s->getcurrentMicrotime();
22+
while (($s1 = $s->getCurrentMillisecond()) && $a < 10) {
23+
$s2 = $s->getCurrentMillisecond();
2424
while ($s1 == $s2) {
2525
usleep(1);
26-
$s2 = $s->getcurrentMicrotime();
26+
$s2 = $s->getCurrentMillisecond();
2727
}
2828
$a++;
2929
$this->assertTrue($s1 != $s2);

0 commit comments

Comments
 (0)