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,46 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/MediaType.php';
|
||||
|
||||
class Music extends MediaType {
|
||||
protected function getType() {
|
||||
protected function getType(): string {
|
||||
return 'Album';
|
||||
}
|
||||
|
||||
protected function getTypeSpecificFields() {
|
||||
|
||||
protected function getTypeSpecificFields(): array {
|
||||
return ['artist', 'album_type', 'release_date'];
|
||||
}
|
||||
|
||||
protected function validateTypeSpecificFields($data) {
|
||||
|
||||
protected function validateTypeSpecificFields(array $data): array {
|
||||
// Music spezifische Validierung
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function search($filters = [], $page = 1, $limit = 20) {
|
||||
|
||||
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
|
||||
// Nur Music suchen
|
||||
$filters['type'] = 'Album';
|
||||
return parent::search($filters, $page, $limit);
|
||||
}
|
||||
|
||||
public function getWithTracks($id) {
|
||||
|
||||
public function getWithTracks(?int $id): ?array {
|
||||
$media = $this->getWithRelations($id);
|
||||
if (!$media) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
$media['tracks'] = $this->getTracks($id);
|
||||
|
||||
|
||||
return $media;
|
||||
}
|
||||
|
||||
public function getTracks($mediaId) {
|
||||
|
||||
public function getTracks(?int $mediaId): array {
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT * FROM tracks WHERE media_id = ? ORDER BY track_number
|
||||
");
|
||||
$stmt->execute([$mediaId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public function addTrack($mediaId, $trackData) {
|
||||
|
||||
public function addTrack(?int $mediaId, array $trackData): int {
|
||||
$stmt = $this->pdo->prepare("
|
||||
INSERT INTO tracks (media_id, track_number, title, artist)
|
||||
VALUES (?, ?, ?, ?)
|
||||
@@ -52,20 +55,20 @@ class Music extends MediaType {
|
||||
//$trackData['duration'] ?? null,
|
||||
$trackData['artist'] ?? null
|
||||
]);
|
||||
return $this->pdo->lastInsertId();
|
||||
return (int)$this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
public function updateTrack($trackId, $trackData) {
|
||||
|
||||
public function updateTrack(?int $trackId, array $trackData): bool {
|
||||
$fields = [];
|
||||
$params = [];
|
||||
|
||||
|
||||
foreach (['track_number', 'title', 'artist'] as $field) {
|
||||
if (array_key_exists($field, $trackData)) {
|
||||
$fields[] = "$field = ?";
|
||||
$params[] = $trackData[$field];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!empty($fields)) {
|
||||
$params[] = $trackId;
|
||||
$stmt = $this->pdo->prepare("UPDATE tracks SET " . implode(', ', $fields) . " WHERE id = ?");
|
||||
@@ -74,29 +77,29 @@ class Music extends MediaType {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function deleteTrack($trackId) {
|
||||
|
||||
public function deleteTrack(?int $trackId): bool {
|
||||
$stmt = $this->pdo->prepare("DELETE FROM tracks WHERE id = ?");
|
||||
$stmt->execute([$trackId]);
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
|
||||
public function createWithRelations($data) {
|
||||
|
||||
public function createWithRelations(array $data): int {
|
||||
$mediaId = parent::createWithRelations($data);
|
||||
|
||||
|
||||
// Tracks speichern
|
||||
if (isset($data['tracks']) && is_array($data['tracks'])) {
|
||||
foreach ($data['tracks'] as $track) {
|
||||
$this->addTrack($mediaId, $track);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $mediaId;
|
||||
}
|
||||
|
||||
public function updateWithRelations($id, $data) {
|
||||
|
||||
public function updateWithRelations(int $id, array $data): bool {
|
||||
parent::updateWithRelations($id, $data);
|
||||
|
||||
|
||||
// Tracks aktualisieren
|
||||
if (isset($data['tracks']) && is_array($data['tracks'])) {
|
||||
// Alle existierenden Tracks löschen
|
||||
@@ -106,7 +109,6 @@ class Music extends MediaType {
|
||||
$this->addTrack($id, $track);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user