Skip to content

Commit 0193ef3

Browse files
authored
feat: support phpstan (#62)
* feat: support phpstan
1 parent 940c346 commit 0193ef3

File tree

8 files changed

+85
-8
lines changed

8 files changed

+85
-8
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: static analysis
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- '*.x'
8+
pull_request:
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
tests:
15+
runs-on: ubuntu-22.04
16+
17+
strategy:
18+
fail-fast: true
19+
20+
name: Static Analysis
21+
22+
steps:
23+
- name: Checkout code
24+
uses: actions/checkout@v3
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: 8.2
30+
tools: composer:v2
31+
coverage: none
32+
33+
- name: Install dependencies
34+
uses: nick-fields/retry@v2
35+
with:
36+
timeout_minutes: 10
37+
max_attempts: 3
38+
command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress
39+
40+
- name: Execute type checking
41+
run: vendor/bin/phpstan

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit": "^10",
26-
"laravel/pint": "^1.10"
26+
"laravel/pint": "^1.10",
27+
"phpstan/phpstan": "^1.10"
2728
},
2829
"autoload-dev": {
2930
"psr-4": {
@@ -38,5 +39,10 @@
3839
"scripts": {
3940
"test": "vendor/bin/phpunit",
4041
"pint": "vendor/bin/pint"
42+
},
43+
"config": {
44+
"allow-plugins": {
45+
"pestphp/pest-plugin": true
46+
}
4147
}
4248
}

phpstan.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
paths:
3+
- src
4+
5+
level: 9

src/FileLockResolver.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ protected function getSequence(string $filePath, int $currentTime): int
6868
try {
6969
$f = @fopen($filePath, static::FileOpenMode);
7070

71+
if (! $f) {
72+
throw new SnowflakeException(sprintf('can not open this file %s', $filePath));
73+
}
74+
7175
// we always use exclusive lock to avoid the problem of concurrent access.
7276
// so we don't need to check the return value of flock.
7377
flock($f, static::FlockLockOperation);
@@ -97,7 +101,7 @@ protected function getSequence(string $filePath, int $currentTime): int
97101
/**
98102
* Unlock and close file.
99103
*
100-
* @param resource $f
104+
* @param resource|false|null $f
101105
*/
102106
protected function unlock($f): void
103107
{
@@ -108,7 +112,9 @@ protected function unlock($f): void
108112
}
109113

110114
/**
115+
* @param array<int, int> $contents
111116
* @param resource $f
117+
* @return bool
112118
*/
113119
public function updateContents(array $contents, $f): bool
114120
{
@@ -119,6 +125,9 @@ public function updateContents(array $contents, $f): bool
119125
/**
120126
* Increment sequence with specify time. if current time is not set in the lock file
121127
* set it to 1, otherwise increment it.
128+
*
129+
* @param array<int, int> $contents
130+
* @return array<int, int>
122131
*/
123132
public function incrementSequenceWithSpecifyTime(array $contents, int $currentTime): array
124133
{
@@ -129,6 +138,9 @@ public function incrementSequenceWithSpecifyTime(array $contents, int $currentTi
129138

130139
/**
131140
* Clean the old content, we only save the data generated within 10 minutes.
141+
*
142+
* @param array<int, int> $contents
143+
* @return array<int, int>
132144
*/
133145
public function cleanOldSequences(array $contents): array
134146
{
@@ -148,6 +160,10 @@ public function cleanAllLocksFile(): void
148160
{
149161
$files = glob($this->lockFileDir.'/*');
150162

163+
if (! $files) {
164+
return;
165+
}
166+
151167
foreach ($files as $file) {
152168
if (is_file($file) && preg_match('/snowflake-(\d+)\.lock$/', $file)) {
153169
unlink($file);
@@ -159,6 +175,7 @@ public function cleanAllLocksFile(): void
159175
* Get resource contents, If the contents are invalid json, return null.
160176
*
161177
* @param resource $f
178+
* @return array<int, int>|null
162179
*/
163180
public function getContents($f): ?array
164181
{

src/LaravelSequenceResolver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@ class LaravelSequenceResolver implements SequenceResolver
2424
/**
2525
* Init resolve instance, must be connected.
2626
*/
27-
public function __construct(protected Repository $cache)
27+
public function __construct(protected Repository $cache) // @phpstan-ignore-line
2828
{
2929
}
3030

3131
public function sequence(int $currentTime): int
3232
{
3333
$key = $this->prefix.$currentTime;
3434

35+
// @phpstan-ignore-next-line
3536
if ($this->cache->add($key, 1, 10)) {
3637
return 0;
3738
}
3839

40+
// @phpstan-ignore-next-line
3941
return $this->cache->increment($key, 1);
4042
}
4143

src/RedisSequenceResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function sequence(int $currentTime): int
4848
LUA;
4949

5050
// 10 seconds
51-
return $this->redis->eval($lua, [$this->prefix.$currentTime, '0', '10'], 1);
51+
return $this->redis->eval($lua, [$this->prefix.$currentTime, '0', '10'], 1) | 0;
5252
}
5353

5454
/**

src/Snowflake.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ class Snowflake
3838

3939
/**
4040
* The Sequence Resolver instance.
41+
*
42+
* @var Closure|SequenceResolver|null
4143
*/
42-
protected null|Closure|SequenceResolver $sequence = null;
44+
protected SequenceResolver|null|Closure $sequence = null;
4345

4446
/**
4547
* The start timestamp.
@@ -87,6 +89,8 @@ public function id(): string
8789

8890
/**
8991
* Parse snowflake id.
92+
*
93+
* @return array<string, float|int|string>
9094
*/
9195
public function parseId(string $id, bool $transform = false): array
9296
{
@@ -154,7 +158,7 @@ public function getStartTimeStamp(): float|int
154158
/**
155159
* Set Sequence Resolver.
156160
*/
157-
public function setSequenceResolver(callable|SequenceResolver $sequence): self
161+
public function setSequenceResolver(Closure|SequenceResolver $sequence): self
158162
{
159163
$this->sequence = $sequence;
160164

@@ -180,7 +184,7 @@ public function getDefaultSequenceResolver(): SequenceResolver
180184
/**
181185
* Call resolver.
182186
*/
183-
protected function callResolver(mixed $currentTime): int
187+
protected function callResolver(int $currentTime): int
184188
{
185189
$resolver = $this->getSequenceResolver();
186190

src/Sonyflake.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ public function setStartTimeStamp(int $millisecond): self
8282

8383
/**
8484
* Parse snowflake id.
85+
*
86+
* @return array<string, float|int|string>
8587
*/
86-
public function parseId(string $id, $transform = false): array
88+
public function parseId(string $id, bool $transform = false): array
8789
{
8890
$id = decbin((int) $id);
8991
$length = self::MAX_SEQUENCE_LENGTH + self::MAX_MACHINEID_LENGTH;

0 commit comments

Comments
 (0)