Avoid PHP 8.5 deprecation notice when loading the tracker - #156
Merged
Conversation
PHP 8.5 deprecated the predefined locally scoped $http_response_header variable, and emits the notice at compile time. The stream transport's fallback read therefore triggered it merely by loading MatomoTracker.php, even on 8.5 where http_get_last_response_headers() is used instead - so it could neither be suppressed nor avoided by the function_exists() guard, and tools turning diagnostics into exceptions (e.g. Psalm) aborted while autoloading the class. Assigning the variable before reading it silences the diagnostic and keeps the PHP < 8.5 fallback working. Fixes #155
sgiehl
marked this pull request as ready for review
August 3, 2026 11:29
tzi
approved these changes
Aug 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
PHP 8.5 deprecated the predefined locally scoped
$http_response_headervariable, and reports it at compile time. The stream transport's pre-8.5 fallback read therefore made merely loadingMatomoTracker.phpemit a deprecation — even on 8.5, where the branch that actually runs ishttp_get_last_response_headers():Because it fires on include rather than on use, it can neither be suppressed with
@nor avoided by thefunction_exists('http_get_last_response_headers')guard. Tools that turn diagnostics into exceptions abort while autoloading the class — the reporter hit this with Psalm 6.16.1, which crashes evaluatingclass_exists('\MatomoTracker')inPiwikTracker.php:17— and applications that log deprecations get one entry per include.Reported by @Baffos in #155.
Fix
Assign the variable before the fallback reads it. That silences the compile-time diagnostic while keeping the pre-8.5 fallback working, and is the shape 3.4.0 used. Header handling is otherwise unchanged:
http_get_last_response_headers()is still preferred whenever it exists.Alternatives were checked and rejected:
file_get_contents(), so a helper reading it sees nothing.get_defined_vars()['http_response_header'] ?? []is a trap — it silences the 8.5 diagnostic but returns nothing on 8.1–8.5 alike (no CV exists), silently breaking incomingSet-Cookieparsing on PHP < 8.5.Note the fix is order-sensitive: moving the assignment below the read reintroduces the notice (verified — an assignment the compiler has not yet seen does not help). That's what the new test guards.
Notes
php:8.5-cli: the deprecation is emitted onmasterand gone with this change. Also verified the pre-assignment does not mask real headers — with a sentinel value, PHP replaces it with the real response headers (incl.Set-Cookie) on both 8.1 and 8.5, and end-to-end incoming-cookie parsing over the stream transport still works on both.testLoadingTheTrackerEmitsNoDeprecationNotice, which loads the tracker in a clean subprocess and asserts it emits nothing. Confirmed it fails on 8.5 without the fix and passes with it; CI already covers 8.5. It runs with-nso an unrelatedphp.iniextension line in a contributor's environment can't turn into a false failure (confirmed both ways).phpunit(156 tests),phpstan(level max) andphpcsare clean; the suite passes on 8.1, 8.3 and 8.5.phpunit.xml.disthas nofailOn*flags, and--fail-on-deprecationalone would have caught this bug class in-process (verified: it exits 1 onmasterunder 8.5). AddingfailOnDeprecation/failOnWarning/failOnNoticepasses cleanly with this fix, but it changes strictness for the whole suite, so it seemed like your call rather than a drive-by.