Add PHP 7.4+ type hints to helpers, models, and middleware

Added strict typing with parameter types, return types, and property
types across all core classes:
- helpers: Database, ErrorHandler, CacheHelper
- models: TicketModel, UserModel, WorkflowModel, TemplateModel, UserPreferencesModel
- middleware: RateLimitMiddleware, CsrfMiddleware, SecurityHeadersMiddleware

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-29 11:04:36 -05:00
parent 8a8b1b0258
commit 37be81b3e2
11 changed files with 118 additions and 119 deletions

View File

@@ -6,15 +6,15 @@
* such as workflow rules, user preferences, and configuration data.
*/
class CacheHelper {
private static $cacheDir = null;
private static $memoryCache = [];
private static ?string $cacheDir = null;
private static array $memoryCache = [];
/**
* Get the cache directory path
*
* @return string Cache directory path
*/
private static function getCacheDir() {
private static function getCacheDir(): string {
if (self::$cacheDir === null) {
self::$cacheDir = sys_get_temp_dir() . '/tinker_tickets_cache';
if (!is_dir(self::$cacheDir)) {
@@ -31,7 +31,7 @@ class CacheHelper {
* @param mixed $identifier Unique identifier
* @return string Cache key
*/
private static function makeKey($prefix, $identifier = null) {
private static function makeKey(string $prefix, $identifier = null): string {
$key = $prefix;
if ($identifier !== null) {
$key .= '_' . md5(serialize($identifier));
@@ -47,7 +47,7 @@ class CacheHelper {
* @param int $ttl Time-to-live in seconds (default 300 = 5 minutes)
* @return mixed|null Cached data or null if not found/expired
*/
public static function get($prefix, $identifier = null, $ttl = 300) {
public static function get(string $prefix, $identifier = null, int $ttl = 300) {
$key = self::makeKey($prefix, $identifier);
// Check memory cache first (fastest)
@@ -88,7 +88,7 @@ class CacheHelper {
* @param mixed $data Data to cache
* @return bool Success
*/
public static function set($prefix, $identifier, $data) {
public static function set(string $prefix, $identifier, $data): bool {
$key = self::makeKey($prefix, $identifier);
$cached = [
'time' => time(),
@@ -110,7 +110,7 @@ class CacheHelper {
* @param mixed $identifier Unique identifier (null to delete all with prefix)
* @return bool Success
*/
public static function delete($prefix, $identifier = null) {
public static function delete(string $prefix, $identifier = null): bool {
if ($identifier !== null) {
$key = self::makeKey($prefix, $identifier);
unset(self::$memoryCache[$key]);
@@ -140,7 +140,7 @@ class CacheHelper {
*
* @return bool Success
*/
public static function clearAll() {
public static function clearAll(): bool {
self::$memoryCache = [];
$files = glob(self::getCacheDir() . '/*.json');
@@ -160,7 +160,7 @@ class CacheHelper {
* @param int $ttl Time-to-live in seconds
* @return mixed Cached or freshly fetched data
*/
public static function remember($prefix, $identifier, $callback, $ttl = 300) {
public static function remember(string $prefix, $identifier, callable $callback, int $ttl = 300) {
$data = self::get($prefix, $identifier, $ttl);
if ($data === null) {
@@ -178,7 +178,7 @@ class CacheHelper {
*
* @param int $maxAge Maximum age in seconds (default 1 hour)
*/
public static function cleanup($maxAge = 3600) {
public static function cleanup(int $maxAge = 3600): void {
$files = glob(self::getCacheDir() . '/*.json');
$now = time();

View File

@@ -6,7 +6,7 @@
* Provides a singleton connection for the request lifecycle.
*/
class Database {
private static $connection = null;
private static ?mysqli $connection = null;
/**
* Get database connection (singleton pattern)
@@ -14,7 +14,7 @@ class Database {
* @return mysqli Database connection
* @throws Exception If connection fails
*/
public static function getConnection() {
public static function getConnection(): mysqli {
if (self::$connection === null) {
self::$connection = self::createConnection();
}
@@ -33,7 +33,7 @@ class Database {
* @return mysqli Database connection
* @throws Exception If connection fails
*/
private static function createConnection() {
private static function createConnection(): mysqli {
// Ensure config is loaded
if (!isset($GLOBALS['config'])) {
require_once dirname(__DIR__) . '/config/config.php';
@@ -59,7 +59,7 @@ class Database {
/**
* Close the database connection
*/
public static function close() {
public static function close(): void {
if (self::$connection !== null) {
self::$connection->close();
self::$connection = null;
@@ -71,7 +71,7 @@ class Database {
*
* @return bool Success
*/
public static function beginTransaction() {
public static function beginTransaction(): bool {
return self::getConnection()->begin_transaction();
}
@@ -80,7 +80,7 @@ class Database {
*
* @return bool Success
*/
public static function commit() {
public static function commit(): bool {
return self::getConnection()->commit();
}
@@ -89,7 +89,7 @@ class Database {
*
* @return bool Success
*/
public static function rollback() {
public static function rollback(): bool {
return self::getConnection()->rollback();
}
@@ -101,7 +101,7 @@ class Database {
* @param array $params Parameters to bind
* @return mysqli_result|bool Query result
*/
public static function query($sql, $types = '', $params = []) {
public static function query(string $sql, string $types = '', array $params = []) {
$conn = self::getConnection();
if (empty($types) || empty($params)) {
@@ -130,7 +130,7 @@ class Database {
* @param array $params Parameters to bind
* @return int Affected rows (-1 on failure)
*/
public static function execute($sql, $types = '', $params = []) {
public static function execute(string $sql, string $types = '', array $params = []): int {
$conn = self::getConnection();
$stmt = $conn->prepare($sql);
@@ -158,7 +158,7 @@ class Database {
*
* @return int Last insert ID
*/
public static function lastInsertId() {
public static function lastInsertId(): int {
return self::getConnection()->insert_id;
}
@@ -168,7 +168,7 @@ class Database {
* @param string $string String to escape
* @return string Escaped string
*/
public static function escape($string) {
public static function escape(string $string): string {
return self::getConnection()->real_escape_string($string);
}
}

View File

@@ -6,15 +6,15 @@
* across the application.
*/
class ErrorHandler {
private static $logFile = null;
private static $initialized = false;
private static ?string $logFile = null;
private static bool $initialized = false;
/**
* Initialize error handling
*
* @param bool $displayErrors Whether to display errors (false in production)
*/
public static function init($displayErrors = false) {
public static function init(bool $displayErrors = false): void {
if (self::$initialized) {
return;
}
@@ -45,7 +45,7 @@ class ErrorHandler {
* @param int $errline Line number
* @return bool
*/
public static function handleError($errno, $errstr, $errfile, $errline) {
public static function handleError(int $errno, string $errstr, string $errfile, int $errline): bool {
// Don't handle suppressed errors
if (!(error_reporting() & $errno)) {
return false;
@@ -69,7 +69,7 @@ class ErrorHandler {
*
* @param Throwable $exception
*/
public static function handleException($exception) {
public static function handleException(Throwable $exception): void {
$message = sprintf(
"Uncaught %s: %s in %s on line %d\nStack trace:\n%s",
get_class($exception),
@@ -94,7 +94,7 @@ class ErrorHandler {
/**
* Handle fatal errors on shutdown
*/
public static function handleShutdown() {
public static function handleShutdown(): void {
$error = error_get_last();
if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
@@ -120,7 +120,7 @@ class ErrorHandler {
* @param int $level Error level
* @param array $context Additional context
*/
public static function log($message, $level = E_USER_NOTICE, $context = []) {
public static function log(string $message, int $level = E_USER_NOTICE, array $context = []): void {
$timestamp = date('Y-m-d H:i:s');
$levelName = self::getErrorTypeName($level);
@@ -140,7 +140,7 @@ class ErrorHandler {
* @param int $httpCode HTTP status code
* @param Throwable|null $exception Original exception (for debug info)
*/
public static function sendErrorResponse($message, $httpCode = 500, $exception = null) {
public static function sendErrorResponse(string $message, int $httpCode = 500, ?Throwable $exception = null): void {
http_response_code($httpCode);
if (!headers_sent()) {
@@ -172,7 +172,7 @@ class ErrorHandler {
* @param array $errors Array of validation errors
* @param string $message Overall error message
*/
public static function sendValidationError($errors, $message = 'Validation failed') {
public static function sendValidationError(array $errors, string $message = 'Validation failed'): void {
http_response_code(422);
if (!headers_sent()) {
@@ -192,7 +192,7 @@ class ErrorHandler {
*
* @param string $message Error message
*/
public static function sendNotFoundError($message = 'Resource not found') {
public static function sendNotFoundError(string $message = 'Resource not found'): void {
self::sendErrorResponse($message, 404);
}
@@ -201,7 +201,7 @@ class ErrorHandler {
*
* @param string $message Error message
*/
public static function sendUnauthorizedError($message = 'Authentication required') {
public static function sendUnauthorizedError(string $message = 'Authentication required'): void {
self::sendErrorResponse($message, 401);
}
@@ -210,7 +210,7 @@ class ErrorHandler {
*
* @param string $message Error message
*/
public static function sendForbiddenError($message = 'Access denied') {
public static function sendForbiddenError(string $message = 'Access denied'): void {
self::sendErrorResponse($message, 403);
}
@@ -220,7 +220,7 @@ class ErrorHandler {
* @param int $errno Error number
* @return string Error type name
*/
private static function getErrorTypeName($errno) {
private static function getErrorTypeName(int $errno): string {
$types = [
E_ERROR => 'ERROR',
E_WARNING => 'WARNING',
@@ -248,7 +248,7 @@ class ErrorHandler {
* @param int $lines Number of lines to return
* @return array Log entries
*/
public static function getRecentErrors($lines = 50) {
public static function getRecentErrors(int $lines = 50): array {
if (self::$logFile === null || !file_exists(self::$logFile)) {
return [];
}