fix: use strict null checks and explicit property initialization#617
Merged
rowan-m merged 1 commit intogoogle:mainfrom Mar 29, 2026
Merged
fix: use strict null checks and explicit property initialization#617rowan-m merged 1 commit intogoogle:mainfrom
rowan-m merged 1 commit intogoogle:mainfrom
Conversation
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.
Hi!
This is a small, strict-typing and safety improvement for ReCaptcha.php.
While auditing the class, I noticed that the 5 optional configuration properties (
$hostname,$apkPackageName,$action,$threshold,$timeoutSeconds) are strictly typed but never explicitly initialized. Right now, the class relies onisset()in the verify() method to act as a guard rail.While that technically works, it leaves a potential footgun: if anyone ever adds a new internal method that tries to read one of those properties without wrapping it in
isset(), PHP 8 will crash with a fatal\Error("Typed property must not be accessed before initialization").This PR makes things explicitly safe and native to PHP 8:
?type = nullso they always have a predictable, safe state.isset()checks withnull !== $this->propertycomparisons. The behavior is the same, but it doesn't rely on error-suppression masking."0"Edge Case: I noticed we were using empty($secret) and empty($response). Because empty("0") returnstruein PHP, the library would actually reject the string"0"as invalid input! Since the parameters are already strictly typed asstringin the method signature, I replaced empty() with a strict'' ===check. The string"0"is now correctly accepted as a valid string value. (I added two specific unit tests to prove and lock in this fix!)!is_null($requestMethod)withnull !== $requestMethodjust to keep the coding style consistent throughout the file.What Changed
"0"is treated as a valid stringAll tests pass (up from 66 to 68 tests, 183 assertions) and PHPStan Level Max is completely green.