diff --git a/.github/workflows/codestyle.yml b/.github/workflows/codestyle.yml index 127c849..8911fb4 100644 --- a/.github/workflows/codestyle.yml +++ b/.github/workflows/codestyle.yml @@ -32,7 +32,7 @@ jobs: run: composer require "laravel/pint" - name: Code Style - run: vendor/bin/pint --test + run: vendor/bin/pint --test --config ./pint.json - name: Collect code coverage with phpunit run: vendor/bin/phpunit --coverage-clover=coverage.xml diff --git a/pint.json b/pint.json index 03f610a..578307a 100644 --- a/pint.json +++ b/pint.json @@ -2,6 +2,7 @@ "rules": { "header_comment": { "header": "This file is part of the godruoyi/php-snowflake.\n \n(c) Godruoyi \n \nThis source file is subject to the MIT license that is bundled." - } + }, + "no_superfluous_phpdoc_tags": false } } \ No newline at end of file diff --git a/src/SequenceResolver.php b/src/SequenceResolver.php index 6058074..f391fc6 100644 --- a/src/SequenceResolver.php +++ b/src/SequenceResolver.php @@ -15,7 +15,7 @@ interface SequenceResolver /** * The snowflake. * - * @param int|string $currentTime current request ms + * @param int|string $currentTime current timestamp: milliseconds * @return int */ public function sequence(int $currentTime); diff --git a/src/Snowflake.php b/src/Snowflake.php index 1e95d17..d7f1ddb 100644 --- a/src/Snowflake.php +++ b/src/Snowflake.php @@ -84,10 +84,10 @@ public function __construct(int $datacenter = null, int $workerid = null) */ public function id() { - $currentTime = $this->getCurrentMicrotime(); + $currentTime = $this->getCurrentMillisecond(); while (($sequence = $this->callResolver($currentTime)) > (-1 ^ (-1 << self::MAX_SEQUENCE_LENGTH))) { usleep(1); - $currentTime = $this->getCurrentMicrotime(); + $currentTime = $this->getCurrentMillisecond(); } $workerLeftMoveLength = self::MAX_SEQUENCE_LENGTH; @@ -120,7 +120,11 @@ public function parseId(string $id, bool $transform = false): array } /** - * Get current microtime timestamp. + * Get current millisecond time. + * + * @deprecated the method name is wrong, use getCurrentMillisecond instead, will be removed in next major version. + * + * @codeCoverageIgnore * * @return int */ @@ -129,6 +133,16 @@ public function getCurrentMicrotime() return floor(microtime(true) * 1000) | 0; } + /** + * Get current millisecond time. + * + * @return int + */ + public function getCurrentMillisecond(): int + { + return floor(microtime(true) * 1000) | 0; + } + /** * Set start time (millisecond). * @@ -136,7 +150,7 @@ public function getCurrentMicrotime() */ public function setStartTimeStamp(int $millisecond) { - $missTime = $this->getCurrentMicrotime() - $millisecond; + $missTime = $this->getCurrentMillisecond() - $millisecond; if ($missTime < 0) { throw new Exception('The start time cannot be greater than the current time'); diff --git a/src/Sonyflake.php b/src/Sonyflake.php index eef6849..c631f57 100644 --- a/src/Sonyflake.php +++ b/src/Sonyflake.php @@ -78,7 +78,7 @@ public function id() */ public function setStartTimeStamp(int $millisecond) { - $elapsedTime = floor(($this->getCurrentMicrotime() - $millisecond) / 10) | 0; + $elapsedTime = floor(($this->getCurrentMillisecond() - $millisecond) / 10) | 0; if ($elapsedTime < 0) { throw new Exception('The start time cannot be greater than the current time'); } @@ -131,7 +131,7 @@ public function getDefaultSequenceResolver(): SequenceResolver */ private function elapsedTime(): int { - return floor(($this->getCurrentMicrotime() - $this->getStartTimeStamp()) / 10) | 0; + return floor(($this->getCurrentMillisecond() - $this->getStartTimeStamp()) / 10) | 0; } /** diff --git a/tests/SnowflakeTest.php b/tests/SnowflakeTest.php index b89609d..379eae2 100644 --- a/tests/SnowflakeTest.php +++ b/tests/SnowflakeTest.php @@ -148,11 +148,11 @@ public function testParseId() $this->assertSame($payloads['sequence'], '0'); } - public function testGetCurrentMicrotime() + public function testgetCurrentMillisecond() { $snowflake = new Snowflake(999, 20); $now = floor(microtime(true) * 1000) | 0; - $time = $snowflake->getCurrentMicrotime(); + $time = $snowflake->getCurrentMillisecond(); $this->assertTrue($time >= $now); } diff --git a/tests/SonyflakeTest.php b/tests/SonyflakeTest.php index 629cc36..582df3d 100644 --- a/tests/SonyflakeTest.php +++ b/tests/SonyflakeTest.php @@ -170,11 +170,11 @@ public function testGetStartTimeStamp() $this->assertTrue(1 === $snowflake->getStartTimeStamp()); } - public function testGetCurrentMicrotime() + public function testgetCurrentMillisecond() { $snowflake = new Sonyflake(9990); $now = floor(microtime(true) * 1000) | 0; - $time = $snowflake->getCurrentMicrotime(); + $time = $snowflake->getCurrentMillisecond(); $this->assertTrue($now - $time >= 0); } diff --git a/tests/TimeTest.php b/tests/TimeTest.php index 9412918..fa2478e 100644 --- a/tests/TimeTest.php +++ b/tests/TimeTest.php @@ -19,11 +19,11 @@ public function testTime() $s = new Snowflake(); $a = 0; - while (($s1 = $s->getcurrentMicrotime()) && $a < 10) { - $s2 = $s->getcurrentMicrotime(); + while (($s1 = $s->getCurrentMillisecond()) && $a < 10) { + $s2 = $s->getCurrentMillisecond(); while ($s1 == $s2) { usleep(1); - $s2 = $s->getcurrentMicrotime(); + $s2 = $s->getCurrentMillisecond(); } $a++; $this->assertTrue($s1 != $s2);