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:
Lars Behrends
2026-04-16 16:40:31 +02:00
parent 728ca893b1
commit e38a6e1f7b
26 changed files with 545 additions and 419 deletions

View File

@@ -1,21 +1,23 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../models/Cast.php';
require_once __DIR__ . '/../models/AdultCast.php';
require_once __DIR__ . '/../services/ApiLogger.php';
class CastController {
private $cast;
private $adultCast;
private $logger;
public function __construct($pdo) {
private Cast $cast;
private AdultCast $adultCast;
private ApiLogger $logger;
public function __construct(PDO $pdo) {
$this->cast = new Cast($pdo);
$this->adultCast = new AdultCast($pdo);
$this->logger = ApiLogger::getInstance();
}
public function handleRequest($method, $segments) {
public function handleRequest(string $method, array $segments): array {
$id = isset($segments[1]) ? (int)$segments[1] : null;
$subResource = isset($segments[2]) ? $segments[2] : null;
@@ -26,7 +28,7 @@ class CastController {
// die("adult");
return $this->handleAdult($method, $id, $segments);
}
switch ($method) {
case 'GET':
return $id ? $this->getOne($id, $segments) : $this->getAll();
@@ -42,7 +44,7 @@ class CastController {
}
}
private function handleAdult($method, $id, $segments) {
private function handleAdult(string $method, ?int $id, array $segments): array {
switch ($method) {
case 'GET':
@@ -62,7 +64,7 @@ class CastController {
}
}
private function getAdultAll() {
private function getAdultAll(): array {
$filters = [];
if (isset($_GET['search'])) $filters['search'] = $_GET['search'];
if (isset($_GET['ethnicity'])) $filters['ethnicity'] = $_GET['ethnicity'];
@@ -75,7 +77,7 @@ class CastController {
return ['success' => true, 'data' => $result];
}
private function getAdultOne($id) {
private function getAdultOne(?int $id): array {
$cast = $this->adultCast->getWithAdultSpecifics($id);
if (!$cast) {
http_response_code(404);
@@ -84,7 +86,7 @@ class CastController {
return ['success' => true, 'data' => $cast];
}
private function createAdult() {
private function createAdult(): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
@@ -119,7 +121,7 @@ class CastController {
return ['success' => true, 'data' => ['id' => $castId]];
}
private function updateAdult($id) {
private function updateAdult(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];
@@ -137,7 +139,7 @@ class CastController {
return ['success' => true, 'data' => ['id' => $id]];
}
private function deleteAdultSpecifics($id) {
private function deleteAdultSpecifics(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];
@@ -152,7 +154,7 @@ class CastController {
$this->logger->logResponse('DELETE', "/api/cast/adult/$id", 200, ['message' => 'Adult specifics deleted successfully']);
return ['success' => true, 'message' => 'Adult specifics deleted successfully'];
}
private function getOne($id, $segments) {
private function getOne(?int $id, array $segments): array {
// Prüfen ob /media angehängt wurde
if (isset($segments[2]) && $segments[2] === 'media') {
return $this->getMedia($id);
@@ -169,12 +171,12 @@ class CastController {
return ['success' => true, 'data' => $cast];
}
private function getMedia($castId) {
private function getMedia(?int $castId): array {
$media = $this->cast->getMediaForCast($castId);
return ['success' => true, 'data' => ['items' => $media]];
}
private function getAll() {
private function getAll(): array {
$filters = [];
if (isset($_GET['search'])) $filters['search'] = $_GET['search'];
@@ -185,7 +187,7 @@ class CastController {
return ['success' => true, 'data' => $result];
}
private function create() {
private function create(): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
@@ -220,7 +222,7 @@ class CastController {
return ['success' => true, 'data' => ['id' => $castId]];
}
private function update($id) {
private function update(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];
@@ -238,7 +240,7 @@ class CastController {
return ['success' => true, 'data' => ['id' => $id]];
}
private function delete($id) {
private function delete(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];