@@ -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 {
0 commit comments