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,23 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
abstract class BaseModel {
|
||||
protected $pdo;
|
||||
protected $table;
|
||||
|
||||
public function __construct($pdo) {
|
||||
protected PDO $pdo;
|
||||
protected string $table;
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
protected function findById($id) {
|
||||
|
||||
protected function findById(int $id): array|false {
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
|
||||
protected function findAll($conditions = [], $orderBy = 'createdAt DESC', $limit = null, $offset = null) {
|
||||
|
||||
protected function findAll(array $conditions = [], string $orderBy = 'createdAt DESC', ?int $limit = null, ?int $offset = null): array {
|
||||
$query = "SELECT * FROM {$this->table} WHERE 1=1";
|
||||
$params = [];
|
||||
|
||||
|
||||
foreach ($conditions as $field => $value) {
|
||||
if (is_array($value)) {
|
||||
// LIKE Operator
|
||||
@@ -28,26 +30,26 @@ abstract class BaseModel {
|
||||
$params[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$query .= " ORDER BY $orderBy";
|
||||
|
||||
if ($limit) {
|
||||
|
||||
if ($limit !== null) {
|
||||
$query .= " LIMIT " . (int)$limit;
|
||||
}
|
||||
|
||||
if ($offset) {
|
||||
|
||||
if ($offset !== null) {
|
||||
$query .= " OFFSET " . (int)$offset;
|
||||
}
|
||||
|
||||
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
protected function count($conditions = []) {
|
||||
|
||||
protected function count(array $conditions = []): int|false {
|
||||
$query = "SELECT COUNT(*) FROM {$this->table} WHERE 1=1";
|
||||
$params = [];
|
||||
|
||||
|
||||
foreach ($conditions as $field => $value) {
|
||||
if (is_array($value)) {
|
||||
$query .= " AND $field LIKE ?";
|
||||
@@ -57,42 +59,42 @@ abstract class BaseModel {
|
||||
$params[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
protected function create($data) {
|
||||
|
||||
protected function create(array $data): int|false {
|
||||
$fields = array_keys($data);
|
||||
$placeholders = array_fill(0, count($fields), '?');
|
||||
|
||||
|
||||
$query = "INSERT INTO {$this->table} (" . implode(', ', $fields) . ") VALUES (" . implode(', ', $placeholders) . ")";
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute(array_values($data));
|
||||
|
||||
|
||||
return $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
protected function update($id, $data) {
|
||||
|
||||
protected function update(int $id, array $data): bool {
|
||||
$fields = [];
|
||||
$params = [];
|
||||
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
$fields[] = "$field = ?";
|
||||
$params[] = $value;
|
||||
}
|
||||
|
||||
|
||||
$params[] = $id;
|
||||
|
||||
|
||||
$query = "UPDATE {$this->table} SET " . implode(', ', $fields) . " WHERE id = ?";
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
|
||||
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
|
||||
protected function delete($id) {
|
||||
|
||||
protected function delete(int $id): bool {
|
||||
$stmt = $this->pdo->prepare("DELETE FROM {$this->table} WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
return $stmt->rowCount() > 0;
|
||||
|
||||
Reference in New Issue
Block a user