Skip to content

Commit 4051ff7

Browse files
committed
Update imports
1 parent ca69f1f commit 4051ff7

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

runtests.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
];
1212

1313
// Capture the output
14-
\ob_start();
14+
ob_start();
1515
$command = new Command();
1616
$command->run($argv, false);
17-
$output = \ob_get_clean();
17+
$output = ob_get_clean();
1818

1919
// Print the output
2020
echo $output;

src/VerifierServer/PersistentState.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use Dotenv\Dotenv;
66

7+
use Exception;
8+
use PDO;
9+
use PDOException;
10+
711
/**
812
* Class PersistentState
913
*
@@ -17,7 +21,7 @@
1721
* @package VerifierServer
1822
*/
1923
class PersistentState {
20-
private \PDO $pdo;
24+
private PDO $pdo;
2125
private array $verifyList;
2226

2327
public function __construct(
@@ -46,26 +50,26 @@ public function __construct(
4650
* - discord: TEXT
4751
* - create_time: TEXT
4852
*
49-
* @throws \PDOException if the table creation fails.
53+
* @throws PDOException if the table creation fails.
5054
*/
5155
private function initializeDatabase(): void
5256
{
5357
$env = self::loadEnvConfig();
54-
$this->pdo = new \PDO(
58+
$this->pdo = new PDO(
5559
$env['DB_DSN'],
5660
$env['DB_USERNAME'],
5761
$env['DB_PASSWORD'],
5862
isset($env['DB_OPTIONS']) ? json_decode($env['DB_OPTIONS'], true) : null
5963
);
60-
if (strpos($this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME), 'mysql') !== false) {
64+
if (strpos($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME), 'mysql') !== false) {
6165
if ($this->pdo->exec("CREATE TABLE IF NOT EXISTS verify_list (
6266
id INT AUTO_INCREMENT PRIMARY KEY,
6367
ss13 VARCHAR(255),
6468
discord VARCHAR(255),
6569
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
6670
)") === false) {
6771
$errorInfo = $this->pdo->errorInfo();
68-
throw new \PDOException("Failed to create table: " . implode(", ", $errorInfo));
72+
throw new PDOException("Failed to create table: " . implode(", ", $errorInfo));
6973
}
7074
} else {
7175
if ($this->pdo->exec("CREATE TABLE IF NOT EXISTS verify_list (
@@ -75,7 +79,7 @@ private function initializeDatabase(): void
7579
create_time TEXT
7680
)") === false) {
7781
$errorInfo = $this->pdo->errorInfo();
78-
throw new \PDOException("Failed to create table: " . implode(", ", $errorInfo));
82+
throw new PDOException("Failed to create table: " . implode(", ", $errorInfo));
7983
}
8084
}
8185
}
@@ -88,7 +92,7 @@ private function initializeDatabase(): void
8892
*
8993
* @return array The verification list.
9094
*
91-
* @throws \PDOException If there is an error executing the query or fetching the data from the database.
95+
* @throws PDOException If there is an error executing the query or fetching the data from the database.
9296
*/
9397
public function getVerifyList(bool $getLocalCache = false): array
9498
{
@@ -100,12 +104,12 @@ public function getVerifyList(bool $getLocalCache = false): array
100104
$stmt = $this->pdo->query("SELECT * FROM verify_list");
101105
if ($stmt === false) {
102106
$errorInfo = $this->pdo->errorInfo();
103-
throw new \PDOException("Failed to execute query: " . implode(", ", $errorInfo));
107+
throw new PDOException("Failed to execute query: " . implode(", ", $errorInfo));
104108
}
105-
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
109+
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
106110
if ($result === false) {
107111
$errorInfo = $this->pdo->errorInfo();
108-
throw new \PDOException("Failed to fetch data: " . implode(", ", $errorInfo));
112+
throw new PDOException("Failed to fetch data: " . implode(", ", $errorInfo));
109113
}
110114
return $this->verifyList = $result;
111115
}
@@ -121,21 +125,21 @@ public function getVerifyList(bool $getLocalCache = false): array
121125
* with keys 'ss13', 'discord', and 'create_time'.
122126
* @param bool $write Whether to write the list to the database. Default is true.
123127
*
124-
* @throws \PDOException If there is an error deleting from the verify_list table, preparing the insert statement,
128+
* @throws PDOException If there is an error deleting from the verify_list table, preparing the insert statement,
125129
* or executing the insert statement.
126130
*/
127131
public function setVerifyList(array $list, bool $write = true): void
128132
{
129133
if ($write && $this->storageType !== 'filesystem') {
130134
if ($this->pdo->exec("DELETE FROM verify_list") === false) {
131-
throw new \PDOException("Failed to delete from verify_list: " . implode(", ", $this->pdo->errorInfo()));
135+
throw new PDOException("Failed to delete from verify_list: " . implode(", ", $this->pdo->errorInfo()));
132136
}
133137
$stmt = $this->pdo->prepare("INSERT INTO verify_list (ss13, discord, create_time) VALUES (:ss13, :discord, :create_time)");
134138
if ($stmt === false) {
135-
throw new \PDOException("Failed to prepare statement.");
139+
throw new PDOException("Failed to prepare statement.");
136140
}
137141
foreach ($list as $item) if (!$stmt->execute($item)) {
138-
throw new \PDOException("Failed to execute statement.");
142+
throw new PDOException("Failed to execute statement.");
139143
}
140144
}
141145
$this->verifyList = $list;
@@ -230,7 +234,7 @@ public static function loadEnvConfig(): array
230234
* If the file cannot be read, it throws an exception.
231235
*
232236
* @return array|null The decoded JSON data as an associative array, or an empty array if the file is empty or invalid.
233-
* @throws \Exception If the file cannot be read.
237+
* @throws Exception If the file cannot be read.
234238
*/
235239
public static function loadVerifyFile(string $json_path): ?array
236240
{
@@ -243,7 +247,7 @@ public static function loadVerifyFile(string $json_path): ?array
243247
}
244248
$data = file_get_contents($json_path);
245249
if ($data === false) {
246-
throw new \Exception("Failed to read {$json_path}");
250+
throw new Exception("Failed to read {$json_path}");
247251
}
248252
return json_decode($data, true) ?: [];
249253
}

src/VerifierServer/Server.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Monolog\Level;
66
use Monolog\Logger;
7+
use Monolog\Handler\StreamHandler;
78
use Psr\Http\Message\ResponseInterface;
89
use Psr\Http\Message\ServerRequestInterface;
910
use Psr\Log\LoggerInterface;
@@ -15,6 +16,10 @@
1516
use VerifierServer\Endpoints\VerifiedEndpoint;
1617
use VerifierServer\Endpoints\EndpointInterface;
1718

19+
use Exception;
20+
use Throwable;
21+
use function pcntl_signal_dispatch;
22+
1823
/**
1924
* Class Server
2025
*
@@ -46,7 +51,7 @@ public function __construct(
4651
/**
4752
* Logs an error message with details about the exception.
4853
*
49-
* @param \Exception $e The exception to log.
54+
* @param Exception $e The exception to log.
5055
* @param bool $fatal Optional. Indicates whether the error is fatal. Defaults to false.
5156
* If true, the server will stop after logging the error.
5257
*
@@ -64,15 +69,15 @@ public function logError($e, bool $fatal = false): void
6469
/**
6570
* Initializes the server by creating a stream socket server and setting it to non-blocking mode.
6671
*
67-
* @throws \Exception If the server fails to be created.
72+
* @throws Exception If the server fails to be created.
6873
*/
6974
public function init(?LoopInterface $loop = null, bool $stream_socket_server = false): void
7075
{
7176
if ($this->running) return;
7277
if ($stream_socket_server) {
7378
$this->server = stream_socket_server("{$this->hostAddr}", $errno, $errstr);
7479
if (! is_resource($this->server)) {
75-
throw new \Exception("Failed to create server: $errstr ($errno)");
80+
throw new Exception("Failed to create server: $errstr ($errno)");
7681
}
7782
} else {
7883
$this->server = new HttpServer(
@@ -81,7 +86,7 @@ public function init(?LoopInterface $loop = null, bool $stream_socket_server = f
8186
: Loop::get(),
8287
fn($request) => $this->handleReact($request)
8388
);
84-
$this->server->on('error', fn(\Throwable $e) => $this->logError($e, true));
89+
$this->server->on('error', fn(Throwable $e) => $this->logError($e, true));
8590
$this->socket = new SocketServer($this->hostAddr, [], $this->loop);
8691
}
8792
$this->initialized = true;
@@ -106,7 +111,7 @@ public function start(bool $start_loop = false): void
106111
$this->running = true;
107112
while ($this->running) {
108113
if (stripos(PHP_OS, 'WIN') === false && extension_loaded('pcntl')) {
109-
\pcntl_signal_dispatch();
114+
pcntl_signal_dispatch();
110115
}
111116
if ($client = @stream_socket_accept($this->server, 0)) {
112117
$this->handleResource($client);
@@ -160,7 +165,7 @@ public function getLogger(): ?LoggerInterface
160165
public function setLogger(LoggerInterface|true|null $logger): void
161166
{
162167
$this->logger = $logger === true
163-
? new Logger('VerifierServer', [new \Monolog\Handler\StreamHandler('php://stdout', Level::Info)])
168+
? new Logger('VerifierServer', [new StreamHandler('php://stdout', Level::Info)])
164169
: $logger;
165170
}
166171

@@ -203,9 +208,9 @@ public static function arrayToRequestString(array $formData): string
203208
/**
204209
* Handles an incoming resource request from a client and generates appropriate responses.
205210
*
206-
* @param \Psr\Http\Message\ServerRequestInterface $client The incoming client request.
211+
* @param ServerRequestInterface $client The incoming client request.
207212
*
208-
* @return \Psr\Http\Message\ResponseInterface The generated HTTP response.
213+
* @return ResponseInterface The generated HTTP response.
209214
*/
210215
private function handleReact(ServerRequestInterface $client): ResponseInterface
211216
{
@@ -239,7 +244,7 @@ private function handleReact(ServerRequestInterface $client): ResponseInterface
239244
*
240245
* @param resource $client The client socket resource to handle the request from.
241246
*
242-
* @throws \Exception If reading from or writing to the client fails.
247+
* @throws Exception If reading from or writing to the client fails.
243248
*
244249
* @return null Always returns null after processing the request.
245250
*/
@@ -256,7 +261,7 @@ private function handleResource($client): null
256261
}
257262

258263
if ($request === false) {
259-
throw new \Exception("Failed to read from client");
264+
throw new Exception("Failed to read from client");
260265
}
261266
$lines = explode(PHP_EOL, trim($request));
262267
$firstLine = explode(' ', $lines[0]);
@@ -298,7 +303,7 @@ private function handleResource($client): null
298303
$response = "HTTP/1.1 $response $statusText";
299304
}
300305
if (fwrite($client, $response . PHP_EOL . implode(PHP_EOL, array_map(fn($key, $value) => "$key: $value", array_keys($content_type), $content_type)) . PHP_EOL . PHP_EOL . $body) === false) {
301-
throw new \Exception("Failed to write to client");
306+
throw new Exception("Failed to write to client");
302307
}
303308
fclose($client);
304309
return null;

0 commit comments

Comments
 (0)