From b3a24d7df6e1dd5a18aac72be32ca7636a57a8a8 Mon Sep 17 00:00:00 2001 From: Marcin Gil Date: Sun, 8 Mar 2026 13:19:26 +0100 Subject: [PATCH] Fix PHP 8.1+ deprecation warnings for null parameters to internal functions PHP 8.1 deprecated passing null to non-nullable string parameters of internal functions (strtoupper, explode, preg_match, ltrim, trim, in_array, str_replace, etc). This will become a TypeError in PHP 9.0. Fixed files: - Support/Vanilla/Clockwork.php: $_SERVER keys missing in CLI context - Request/IncomingRequest.php: null host passed to explode() - Request/ShouldCollect.php: null uri/method passed to preg_match/strtoupper - Request/Request.php: null query passed to ltrim in database query counters - DataSource/PhpDataSource.php: null host passed to trim - DataSource/LumenDataSource.php: null $_SERVER/$_POST values - DataSource/EloquentDataSource.php: null query passed to ltrim - Helpers/StackFilter.php: null frame properties passed to in_array Co-Authored-By: Claude Opus 4.6 --- Clockwork/DataSource/EloquentDataSource.php | 2 +- Clockwork/DataSource/LumenDataSource.php | 6 +++--- Clockwork/DataSource/PhpDataSource.php | 2 +- Clockwork/Helpers/StackFilter.php | 16 ++++++++-------- Clockwork/Request/IncomingRequest.php | 2 +- Clockwork/Request/Request.php | 10 +++++----- Clockwork/Request/ShouldCollect.php | 6 +++--- Clockwork/Support/Vanilla/Clockwork.php | 8 +++++--- 8 files changed, 27 insertions(+), 25 deletions(-) diff --git a/Clockwork/DataSource/EloquentDataSource.php b/Clockwork/DataSource/EloquentDataSource.php index 877d3675..aaea3018 100644 --- a/Clockwork/DataSource/EloquentDataSource.php +++ b/Clockwork/DataSource/EloquentDataSource.php @@ -324,7 +324,7 @@ protected function quoteBinding($binding, $connection) // Increment query counts for collected query protected function incrementQueryCount($query) { - $sql = ltrim($query['query']); + $sql = ltrim($query['query'] ?? ''); $this->count['total']++; diff --git a/Clockwork/DataSource/LumenDataSource.php b/Clockwork/DataSource/LumenDataSource.php index 5d3b65b3..9329441b 100644 --- a/Clockwork/DataSource/LumenDataSource.php +++ b/Clockwork/DataSource/LumenDataSource.php @@ -118,9 +118,9 @@ protected function getRequestMethod() if ($this->app->bound('request')) { return $this->app['request']->getMethod(); } elseif (isset($_POST['_method'])) { - return strtoupper($_POST['_method']); + return strtoupper((string) $_POST['_method']); } else { - return $_SERVER['REQUEST_METHOD']; + return $_SERVER['REQUEST_METHOD'] ?? null; } } @@ -188,7 +188,7 @@ protected function getPathInfo() return $this->app['request']->getPathInfo(); } else { $query = $_SERVER['QUERY_STRING'] ?? ''; - return '/' . trim(str_replace("?{$query}", '', $_SERVER['REQUEST_URI']), '/'); + return '/' . trim(str_replace("?{$query}", '', $_SERVER['REQUEST_URI'] ?? ''), '/'); } } } diff --git a/Clockwork/DataSource/PhpDataSource.php b/Clockwork/DataSource/PhpDataSource.php index f08e1c7b..0951bc6b 100644 --- a/Clockwork/DataSource/PhpDataSource.php +++ b/Clockwork/DataSource/PhpDataSource.php @@ -106,7 +106,7 @@ protected function getRequestUrl() $port = (! $https && $port != 80 || $https && $port != 443) ? ":{$port}" : ''; // remove port number from the host - $host = $host ? preg_replace('/:\d+$/', '', trim($host)) : null; + $host = $host !== null ? preg_replace('/:\d+$/', '', trim($host)) : null; return "{$scheme}://{$host}{$port}{$uri}"; } diff --git a/Clockwork/Helpers/StackFilter.php b/Clockwork/Helpers/StackFilter.php index f36d6442..68584941 100644 --- a/Clockwork/Helpers/StackFilter.php +++ b/Clockwork/Helpers/StackFilter.php @@ -101,24 +101,24 @@ public function closure() protected function matchesClass(StackFrame $frame) { - if (count($this->classes) && ! in_array($frame->class, $this->classes)) return false; - if (count($this->notClasses) && in_array($frame->class, $this->notClasses)) return false; + if (count($this->classes) && ! in_array($frame->class ?? '', $this->classes)) return false; + if (count($this->notClasses) && in_array($frame->class ?? '', $this->notClasses)) return false; return true; } protected function matchesFile(StackFrame $frame) { - if (count($this->files) && ! in_array($frame->file, $this->files)) return false; - if (count($this->notFiles) && in_array($frame->file, $this->notFiles)) return false; + if (count($this->files) && ! in_array($frame->file ?? '', $this->files)) return false; + if (count($this->notFiles) && in_array($frame->file ?? '', $this->notFiles)) return false; return true; } protected function matchesFunction(StackFrame $frame) { - if (count($this->functions) && ! in_array($frame->function, $this->functions)) return false; - if (count($this->notFunctions) && in_array($frame->function, $this->notFunctions)) return false; + if (count($this->functions) && ! in_array($frame->function ?? '', $this->functions)) return false; + if (count($this->notFunctions) && in_array($frame->function ?? '', $this->notFunctions)) return false; return true; } @@ -140,8 +140,8 @@ protected function matchesNamespace(StackFrame $frame) protected function matchesVendor(StackFrame $frame) { - if (count($this->vendors) && ! in_array($frame->vendor, $this->vendors)) return false; - if (count($this->notVendors) && in_array($frame->vendor, $this->notVendors)) return false; + if (count($this->vendors) && ! in_array($frame->vendor ?? '', $this->vendors)) return false; + if (count($this->notVendors) && in_array($frame->vendor ?? '', $this->notVendors)) return false; return true; } diff --git a/Clockwork/Request/IncomingRequest.php b/Clockwork/Request/IncomingRequest.php index a889a57c..eaecbebb 100644 --- a/Clockwork/Request/IncomingRequest.php +++ b/Clockwork/Request/IncomingRequest.php @@ -39,7 +39,7 @@ public function input($key, $default = null) // Returns true, if HTTP host is one of the common domains used for local development public function hasLocalHost() { - $segments = explode('.', $this->host); + $segments = explode('.', $this->host ?? ''); $tld = $segments[count($segments) - 1]; return $this->host == '127.0.0.1' diff --git a/Clockwork/Request/Request.php b/Clockwork/Request/Request.php index f4a06658..523575cf 100644 --- a/Clockwork/Request/Request.php +++ b/Clockwork/Request/Request.php @@ -569,7 +569,7 @@ public function getDatabaseSlowQueriesCount() public function getDatabaseSelects() { return count(array_filter($this->databaseQueries, function ($query) { - return preg_match('/^select\b/i', ltrim($query['query'])); + return preg_match('/^select\b/i', ltrim($query['query'] ?? '')); })); } @@ -577,7 +577,7 @@ public function getDatabaseSelects() public function getDatabaseInserts() { return count(array_filter($this->databaseQueries, function ($query) { - return preg_match('/^insert\b/i', ltrim($query['query'])); + return preg_match('/^insert\b/i', ltrim($query['query'] ?? '')); })); } @@ -585,7 +585,7 @@ public function getDatabaseInserts() public function getDatabaseUpdates() { return count(array_filter($this->databaseQueries, function ($query) { - return preg_match('/^update\b/i', ltrim($query['query'])); + return preg_match('/^update\b/i', ltrim($query['query'] ?? '')); })); } @@ -593,7 +593,7 @@ public function getDatabaseUpdates() public function getDatabaseDeletes() { return count(array_filter($this->databaseQueries, function ($query) { - return preg_match('/^delete\b/i', ltrim($query['query'])); + return preg_match('/^delete\b/i', ltrim($query['query'] ?? '')); })); } @@ -601,7 +601,7 @@ public function getDatabaseDeletes() public function getDatabaseOthers() { return count(array_filter($this->databaseQueries, function ($query) { - return ! preg_match('/^(select|insert|update|delete)\b/i', ltrim($query['query'])); + return ! preg_match('/^(select|insert|update|delete)\b/i', ltrim($query['query'] ?? '')); })); } diff --git a/Clockwork/Request/ShouldCollect.php b/Clockwork/Request/ShouldCollect.php index bccd249f..e8433b9a 100644 --- a/Clockwork/Request/ShouldCollect.php +++ b/Clockwork/Request/ShouldCollect.php @@ -78,7 +78,7 @@ protected function passExcept(IncomingRequest $request) if (! count($this->except)) return true; foreach ($this->except as $pattern) { - if (preg_match('#' . str_replace('#', '\#', $pattern) . '#', $request->uri)) return false; + if (preg_match('#' . str_replace('#', '\#', $pattern) . '#', $request->uri ?? '')) return false; } return true; @@ -89,7 +89,7 @@ protected function passOnly(IncomingRequest $request) if (! count($this->only)) return true; foreach ($this->only as $pattern) { - if (preg_match('#' . str_replace('#', '\#', $pattern) . '#', $request->uri)) return true; + if (preg_match('#' . str_replace('#', '\#', $pattern) . '#', $request->uri ?? '')) return true; } return false; @@ -99,7 +99,7 @@ protected function passExceptPreflight(IncomingRequest $request) { if (! $this->exceptPreflight) return true; - return strtoupper($request->method) != 'OPTIONS'; + return strtoupper($request->method ?? '') != 'OPTIONS'; } protected function passCallback(IncomingRequest $request) diff --git a/Clockwork/Support/Vanilla/Clockwork.php b/Clockwork/Support/Vanilla/Clockwork.php index abc330d6..be5116f0 100644 --- a/Clockwork/Support/Vanilla/Clockwork.php +++ b/Clockwork/Support/Vanilla/Clockwork.php @@ -497,13 +497,15 @@ protected function incomingRequest() // Creates an incoming request instance from globals protected function incomingRequestFromGlobals() { + $host = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? $_SERVER['SERVER_ADDR'] ?? ''; + return new IncomingRequest([ - 'method' => $_SERVER['REQUEST_METHOD'], - 'uri' => $_SERVER['REQUEST_URI'], + 'method' => $_SERVER['REQUEST_METHOD'] ?? null, + 'uri' => $_SERVER['REQUEST_URI'] ?? null, 'headers' => $_SERVER, 'input' => array_merge($_GET, $_POST, (array) json_decode(file_get_contents('php://input'), true)), 'cookies' => $_COOKIE, - 'host' => explode(':', $_SERVER['HTTP_HOST'] ?: $_SERVER['SERVER_NAME'] ?: $_SERVER['SERVER_ADDR'])[0] + 'host' => explode(':', $host)[0] ]); }