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,34 +1,37 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/MediaType.php';
require_once __DIR__ . '/../services/ImageHandler.php';
class Adult extends MediaType {
private $imageHandler;
private $isUpdate = false;
private $mediaId = null;
public function __construct($pdo) {
private ImageHandler $imageHandler;
private bool $isUpdate = false;
private ?int $mediaId = null;
public function __construct(PDO $pdo) {
parent::__construct($pdo);
$this->imageHandler = new ImageHandler();
}
protected function getType() {
protected function getType(): string {
return 'Adult';
}
protected function getTypeSpecificFields() {
protected function getTypeSpecificFields(): array {
return [];
}
protected function validateTypeSpecificFields($data) {
protected function validateTypeSpecificFields(array $data): array {
// Adult spezifische Validierung
// Eventuell Altersverifikation etc.
return $data;
}
protected function processPosterField($data) {
protected function processPosterField(array $data): array {
error_log("Adult::processPosterField - Checking for poster field, isUpdate: " . ($this->isUpdate ? 'yes' : 'no'));
if ($this->isUpdate && $this->mediaId && isset($data['poster']) && !empty($data['poster'])) {
$currentMedia = $this->findById($this->mediaId);
if ($currentMedia && !empty($currentMedia['poster'])) {
@@ -39,15 +42,15 @@ class Adult extends MediaType {
}
}
}
if (isset($data['poster']) && !empty($data['poster'])) {
error_log("Adult::processPosterField - Poster found, length: " . strlen($data['poster']));
if (strpos($data['poster'], '/images/') === 0 || filter_var($data['poster'], FILTER_VALIDATE_URL)) {
error_log("Adult::processPosterField - Poster is already a path or URL, skipping processing");
return $data;
}
$posterPath = $this->imageHandler->saveBase64Image($data['poster'], 'adult/poster');
error_log("Adult::processPosterField - ImageHandler returned: " . ($posterPath ?: 'null'));
if ($posterPath) {
@@ -61,23 +64,23 @@ class Adult extends MediaType {
}
return $data;
}
public function createWithRelations($data) {
public function createWithRelations(array $data): int {
$data['type'] = 'Adult';
$this->validateTypeSpecificFields($data);
$data = $this->processPosterField($data);
return parent::createWithRelations($data);
}
public function updateWithRelations($id, $data) {
public function updateWithRelations(int $id, array $data): bool {
$this->isUpdate = true;
$this->mediaId = $id;
$this->validateTypeSpecificFields($data);
$data = $this->processPosterField($data);
parent::updateWithRelations($id, $data);
return parent::updateWithRelations($id, $data);
}
public function search($filters = [], $page = 1, $limit = 20) {
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
// Nur Adult Content suchen
$filters['type'] = 'Adult';
return parent::search($filters, $page, $limit);