Mark data classes and functions with types

This commit is contained in:
2025-12-17 03:41:40 +09:00
parent da1113341a
commit f8acc4b815
20 changed files with 496 additions and 343 deletions

View File

@@ -4,6 +4,9 @@ declare(strict_types=1);
namespace pvv\side;
use DateTimeImmutable;
use PDO;
class Door {
private $pdo;
@@ -11,64 +14,74 @@ class Door {
$this->pdo = $pdo;
}
public function getAll() {
$query = 'SELECT time, open FROM door ORDER BY time DESC';
/**
* @return array{time: int, open: bool}[]
*/
public function getAll(): array {
$query = "SELECT time, open FROM door ORDER BY time DESC";
$statement = $this->pdo->prepare($query);
$statement->execute();
$doorEvents = [];
foreach ($statement->fetchAll() as $row) {
$doorEvents[] = [
'time' => (int) $row['time'],
'open' => (bool) $row['open'],
"time" => (int) $row["time"],
"open" => (bool) $row["open"],
];
}
return $doorEvents;
}
public function getEntriesAfter($startTime) {
$query = 'SELECT time, open FROM door WHERE time > :startTime ORDER BY time DESC';
/**
* @return array{time: int, open: bool}[]
*/
public function getEntriesAfter(DateTimeImmutable $startTime): array {
$query =
"SELECT time, open FROM door WHERE time > :startTime ORDER BY time DESC";
$statement = $this->pdo->prepare($query);
$statement->bindParam(':startTime', $startTime, \PDO::PARAM_STR);
$statement->bindParam(":startTime", $startTime, \PDO::PARAM_STR);
$statement->execute();
$doorEvents = [];
foreach ($statement->fetchAll() as $row) {
$doorEvents[] = [
'time' => (int) $row['time'],
'open' => (bool) $row['open'],
"time" => (int) $row["time"],
"open" => (bool) $row["open"],
];
}
return $doorEvents;
}
public function getCurrent() {
$query = 'SELECT time, open FROM door ORDER BY time DESC LIMIT 1';
/**
* @return array{time: int, open: bool}
*/
public function getCurrent(): array {
$query = "SELECT time, open FROM door ORDER BY time DESC LIMIT 1";
$statement = $this->pdo->prepare($query);
$statement->execute();
$row = $statement->fetch();
return [
'time' => (int) $row['time'],
'open' => (bool) $row['open'],
"time" => (int) $row["time"],
"open" => (bool) $row["open"],
];
}
private function removeOld(): void {
$firstValidTime = time() - 60 * 60 * 24 * 7; // One week before now
$query = 'DELETE FROM door WHERE time < :firstValid';
$query = "DELETE FROM door WHERE time < :firstValid";
$statement = $this->pdo->prepare($query);
$statement->bindParam(':firstValid', $firstValidTime, \PDO::PARAM_STR);
$statement->bindParam(":firstValid", $firstValidTime, \PDO::PARAM_STR);
$statement->execute();
}
public function createEvent($time, $open): void {
$query = 'INSERT INTO door(time, open) VALUES (:time, :open)';
public function createEvent(DateTimeImmutable $time, bool $open): void {
$query = "INSERT INTO door(time, open) VALUES (:time, :open)";
$statement = $this->pdo->prepare($query);
$statement->bindParam(':time', $time, \PDO::PARAM_STR);
$statement->bindParam(':open', $open, \PDO::PARAM_STR);
$statement->bindParam(":time", $time, \PDO::PARAM_STR);
$statement->bindParam(":open", $open, \PDO::PARAM_STR);
$statement->execute();
$this->removeOld();