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 @@
* 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 [];
}