chore: PHP 8.4+ modernization and housekeeping#607
Conversation
add declare(strict_types=1) to all source and test files, fix curl_close resource leak in CurlPost using try/finally, modernize autoload.php to use str_starts_with(), update GitHub Actions from v3 to v4, and update phpunit.xml.dist schema URL to PHPUnit 13. also add namespace mock for curl_close in CurlPostTest.php to keep PHPStan happy, and move int(0) test case from emptySecretProvider to invalidSecretProvider since strict_types now causes a TypeError before the empty-string check can throw RuntimeException. All 56 tests pass (147 assertions), PHPStan level max: 0 errors.
rowan-m
left a comment
There was a problem hiding this comment.
Nice. I sense an AI contribution, but it's solid. In future, I would split something like this up into multiple PRs for each type of fix, e.g. update the actions, add the strict types, tweak the tests, tweak the autoloader.
Thanks for the quick review and merge, Rowan! |
Summary
This PR introduces a suite of non-breaking, high-impact housekeeping improvements. The primary focus is bringing the library into full compliance with PHP 8.4+ standards and modernizing the surrounding CI/CD toolchain. These are safe, additive changes that resolve minor technical debt and enforce stricter type safety across the board.
Detailed Changes
1. Add
declare(strict_types=1)to all source and test filesAlthough the project requires PHP
>=8.4and utilizes typed properties and return types extensively, strict typing was not enforced at the file level. This PR adds thedeclare(strict_types=1)directive to all 8 source files and 6 test files.Impact & Test Updates:
By enforcing strict types, the constructor
ReCaptcha::__construct()will now immediately throw aTypeErrorif a non-string$secret(e.g., an integer) is passed, rather than coercing it and later throwing aRuntimeExceptionduring the internal empty-string guard clause.To reflect this accurate type enforcement, the test suite was updated. Specifically, the
int(0)test case was moved from emptySecretProvider (which asserts aRuntimeException) to invalidSecretProvider (which correctly asserts aTypeError).2. Resolve
CurlPost::submit()resource leak and ensure reliable cleanupThe
CurlPost::submit()method previously relied on PHP's Garbage Collector to eventually clean up the cURL resource because curl_close($handle) was missing.I have wrapped the execution block in a
try/finallystructure. This guarantees curl_close($handle) is decisively executed on all code paths, whether the network request succeeds or fails.Test Updates:
To satisfy static analysis (PHPStan) and maintain consistency with the existing mocking pattern in CurlPostTest.php (where curl_init(), curl_setopt_array(), and curl_exec() are mocked in the local
ReCaptcha\RequestMethodnamespace rather than relying on an external mock library), a corresponding curl_close(\stdClass $ch): void no-op mock was added.3. Modernize autoload.php with
str_starts_with()Replaced the older string matching comparison (
substr($class, 0, 10) !== 'ReCaptcha\\') with PHP 8.0's native!str_starts_with($class, 'ReCaptcha\\'). This makes the autoloader significantly more readable and idiomatic without altering its behavior.(Note:
declare(strict_types=1)was intentionally omitted from the autoloader file so as not to impose strict typing behavior on downstream consumer logic unexpectedly).4. Update GitHub Actions workflows to v4
The repository's .github/workflows/php.yml was using deprecated checkout mechanisms (
actions/checkout@v3andactions/cache@v3). These were upgraded to@v4. This is critical as v3 relies on Node 16 (which is now end-of-life), while v4 utilizes Node 20.5. Update phpunit.xml.dist schema directly to PHPUnit 13.0
The phpunit.xml.dist XML configurations still referenced a significantly outdated XSD schema URL (
https://schema.phpunit.de/10.5/phpunit.xsd). Since the codebase usesphpunit/phpunit ^13.0, this schema mismatch can cause warnings in modern IDEs.I have updated the schema definition URL precisely to
https://schema.phpunit.de/13.0/phpunit.xsdto mirror the installed PHPUnit 13.0.5 environment, eliminating all validation warnings.Verification Checklist
vendor/bin/php-cs-fixer fix --dry-run)try/finallyimplemented safely for guaranteed resource cleanupTest Output