Add strict types and type hints across API
Apply strict_types and extensive type declarations throughout the API and models, improving type safety and error handling. Key changes: add declare(strict_types=1) to many files; convert properties, method parameters and return values to typed signatures (PDO, arrays, ints, strings, bools, nullables); switch exception handling to Throwable in index and Router; improve Router, controllers and model method signatures and nullability handling; refine file/image serving security checks and headers in ImageController; strengthen Database typing and initialization methods; return explicit types from BaseModel CRUD helpers and counting; update Media/Cast/Adult/Game/Console/Settings controllers and models to use typed methods, better validation, and clearer update/create return types. Also add AGENTS.md (agent skills index), update README with Swagger/OpenAPI usage instructions, and add /.windsurf to .gitignore. These changes aim to harden runtime correctness, make intended contracts explicit, and prepare the codebase for easier maintenance and static analysis.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../database.php';
|
||||
|
||||
class ApiLogger {
|
||||
private static $instance = null;
|
||||
private $pdo;
|
||||
private $enabled;
|
||||
private static ?ApiLogger $instance = null;
|
||||
private PDO $pdo;
|
||||
private bool $enabled;
|
||||
|
||||
private function __construct() {
|
||||
$this->enabled = API_LOGGING_ENABLED;
|
||||
@@ -12,14 +15,14 @@ class ApiLogger {
|
||||
$this->pdo = $db->getConnection();
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
public static function getInstance(): ApiLogger {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function logRequest($method, $path, $params = [], $body = null) {
|
||||
public function logRequest(string|array $method, string|array $path, array $params = [], string|array|null $body = null): void {
|
||||
if (!$this->enabled) {
|
||||
return;
|
||||
}
|
||||
@@ -29,7 +32,7 @@ class ApiLogger {
|
||||
INSERT INTO api_logs (type, method, path, params, body)
|
||||
VALUES ('REQUEST', :method, :path, :params, :body)
|
||||
");
|
||||
|
||||
|
||||
$methodValue = is_array($method) ? (json_encode($method) ?: '[array]') : (string)$method;
|
||||
$pathValue = is_array($path) ? (json_encode($path) ?: '[array]') : (string)$path;
|
||||
$paramsValue = is_array($params) ? (json_encode($params) ?: '[array]') : (string)$params;
|
||||
@@ -37,19 +40,19 @@ class ApiLogger {
|
||||
if ($body) {
|
||||
$bodyValue = is_array($body) ? (json_encode($body) ?: '[array]') : (string)$body;
|
||||
}
|
||||
|
||||
|
||||
$stmt->execute([
|
||||
':method' => $methodValue,
|
||||
':path' => $pathValue,
|
||||
':params' => $paramsValue,
|
||||
':body' => $bodyValue
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
} catch (Throwable $e) {
|
||||
error_log('Failed to log request: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function logResponse($method, $path, $statusCode, $response) {
|
||||
public function logResponse(string|array $method, string|array $path, int $statusCode, array $response): void {
|
||||
if (!$this->enabled) {
|
||||
return;
|
||||
}
|
||||
@@ -65,12 +68,12 @@ class ApiLogger {
|
||||
':status_code' => $statusCode,
|
||||
':response' => (json_encode($response) ?: '[encoding_failed]')
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
} catch (Throwable $e) {
|
||||
error_log('Failed to log response: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function logError($method, $path, $error) {
|
||||
public function logError(string|array $method, string|array $path, string|array $error): void {
|
||||
if (!$this->enabled) {
|
||||
return;
|
||||
}
|
||||
@@ -85,12 +88,12 @@ class ApiLogger {
|
||||
':path' => is_array($path) ? (json_encode($path) ?: '[array]') : (string)$path,
|
||||
':error' => is_array($error) ? (json_encode($error) ?: '[array]') : (string)$error
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
} catch (Throwable $e) {
|
||||
error_log('Failed to log error: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function logDebug($message) {
|
||||
public function logDebug(string|array $message): void {
|
||||
if (!$this->enabled) {
|
||||
return;
|
||||
}
|
||||
@@ -103,7 +106,7 @@ class ApiLogger {
|
||||
$stmt->execute([
|
||||
':message' => is_array($message) ? (json_encode($message) ?: '[array]') : (string)$message
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
} catch (Throwable $e) {
|
||||
error_log('Failed to log debug: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user