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,5 +1,7 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../models/Media.php';
require_once __DIR__ . '/../models/Series.php';
require_once __DIR__ . '/../models/Music.php';
@@ -7,24 +9,24 @@ require_once __DIR__ . '/../models/Game.php';
require_once __DIR__ . '/../services/ApiLogger.php';
class MediaController {
private $media;
private $series;
private $music;
private $game;
private $logger;
public function __construct($pdo) {
private Media $media;
private Series $series;
private Music $music;
private Game $game;
private ApiLogger $logger;
public function __construct(PDO $pdo) {
$this->media = new Media($pdo);
$this->series = new Series($pdo);
$this->music = new Music($pdo);
$this->game = new Game($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;
// Sub-Endpunkte für Episoden und Tracks
if ($id && $subResource) {
if ($subResource === 'episodes') {
@@ -34,7 +36,7 @@ class MediaController {
return $this->handleTracks($method, $id, $segments);
}
}
switch ($method) {
case 'GET':
return $id ? $this->getOne($id) : $this->getAll();
@@ -50,7 +52,7 @@ class MediaController {
}
}
private function handleEpisodes($method, $mediaId, $segments) {
private function handleEpisodes(string $method, ?int $mediaId, array $segments): array {
$episodeId = isset($segments[3]) ? (int)$segments[3] : null;
switch ($method) {
@@ -71,7 +73,7 @@ class MediaController {
}
}
private function handleTracks($method, $mediaId, $segments) {
private function handleTracks(string $method, ?int $mediaId, array $segments): array {
$trackId = isset($segments[3]) ? (int)$segments[3] : null;
switch ($method) {
@@ -92,7 +94,7 @@ class MediaController {
}
}
private function getEpisodes($mediaId) {
private function getEpisodes(?int $mediaId): array {
$season = isset($_GET['season']) ? (int)$_GET['season'] : null;
$episodes = $this->series->getEpisodes($mediaId, $season);
return ['success' => true, 'data' => ['items' => $episodes]];
@@ -103,7 +105,7 @@ class MediaController {
* @param int $mediaId Media ID
* @return array Created episode ID
*/
private function addEpisode($mediaId) {
private function addEpisode(?int $mediaId): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
@@ -120,7 +122,7 @@ class MediaController {
* @param int $episodeId Episode ID
* @return array Updated episode ID
*/
private function updateEpisode($episodeId) {
private function updateEpisode(?int $episodeId): array {
if (!$episodeId) {
http_response_code(400);
return ['success' => false, 'error' => 'Episode ID required'];
@@ -141,7 +143,7 @@ class MediaController {
* @param int $episodeId Episode ID
* @return array Success message
*/
private function deleteEpisode($episodeId) {
private function deleteEpisode(?int $episodeId): array {
if (!$episodeId) {
http_response_code(400);
return ['success' => false, 'error' => 'Episode ID required'];
@@ -160,7 +162,7 @@ class MediaController {
* @param int $episodeId Episode ID
* @return array Episode data
*/
private function getEpisode($episodeId) {
private function getEpisode(?int $episodeId): array {
// Episode direkt aus Datenbank abrufen
$stmt = $this->series->getConnection()->prepare("SELECT * FROM episodes WHERE id = ?");
$stmt->execute([$episodeId]);
@@ -173,12 +175,12 @@ class MediaController {
return ['success' => true, 'data' => $episode];
}
private function getTracks($mediaId) {
private function getTracks(?int $mediaId): array {
$tracks = $this->music->getTracks($mediaId);
return ['success' => true, 'data' => ['items' => $tracks]];
}
private function addTrack($mediaId) {
private function addTrack(?int $mediaId): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
@@ -190,7 +192,7 @@ class MediaController {
return ['success' => true, 'data' => ['id' => $trackId]];
}
private function updateTrack($trackId) {
private function updateTrack(?int $trackId): array {
if (!$trackId) {
http_response_code(400);
return ['success' => false, 'error' => 'Track ID required'];
@@ -206,7 +208,7 @@ class MediaController {
return ['success' => true, 'data' => ['id' => $trackId]];
}
private function deleteTrack($trackId) {
private function deleteTrack(?int $trackId): array {
if (!$trackId) {
http_response_code(400);
return ['success' => false, 'error' => 'Track ID required'];
@@ -220,7 +222,7 @@ class MediaController {
return ['success' => true, 'message' => 'Track deleted successfully'];
}
private function getTrack($trackId) {
private function getTrack(?int $trackId): array {
// Track direkt aus Datenbank abrufen
$stmt = $this->music->getConnection()->prepare("SELECT * FROM tracks WHERE id = ?");
$stmt->execute([$trackId]);
@@ -238,7 +240,7 @@ class MediaController {
* @param int $id Media ID
* @return array Media object with relations
*/
private function getOne($id) {
private function getOne(?int $id): array {
// Zuerst Basis-Media abrufen um Typ zu bestimmen
$baseMedia = $this->media->getBase($id);
if (!$baseMedia) {
@@ -268,7 +270,7 @@ class MediaController {
* Get all media items with filtering and pagination
* @return array Paginated media list
*/
private function getAll() {
private function getAll(): array {
$filters = [];
if (isset($_GET['category'])) $filters['category'] = $_GET['category'];
if (isset($_GET['type'])) $filters['type'] = $_GET['type'];
@@ -286,7 +288,7 @@ class MediaController {
* Create a new media item
* @return array Created media ID
*/
private function create() {
private function create(): array {
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
@@ -341,7 +343,7 @@ class MediaController {
* @param int $id Media ID
* @return array Updated media ID
*/
private function update($id) {
private function update(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];
@@ -375,7 +377,7 @@ class MediaController {
* @param int $id Media ID
* @return array Success message
*/
private function delete($id) {
private function delete(?int $id): array {
if (!$id) {
http_response_code(400);
return ['success' => false, 'error' => 'ID required'];