Skip to content

Fix PHP 8.4 deprecated implicit nullable parameters and use constant for Anthropic API version#40

Merged
felixarntz merged 6 commits intofelixarntz:mainfrom
JosephGabito:joseph/removed-deprecated-nullable-constructor-parameter
Jul 8, 2025
Merged

Fix PHP 8.4 deprecated implicit nullable parameters and use constant for Anthropic API version#40
felixarntz merged 6 commits intofelixarntz:mainfrom
JosephGabito:joseph/removed-deprecated-nullable-constructor-parameter

Conversation

@JosephGabito
Copy link
Copy Markdown
Contributor

@JosephGabito JosephGabito commented Jul 6, 2025

Description

This PR fixes PHP 8.4 deprecated nullable parameters by replacing the unsupported $authentication = null pattern with proper nullable type hints (?Authentication) across all API clients and service registration context. Additionally, replaces the hardcoded Anthropic API version with a class constant for better maintainability.

What Changed

  • Fixed PHP 8.4 deprecated nullable parameters:
    Replaced Authentication $authentication = null with ?Authentication $authentication = null across all relevant files.

  • Added Anthropic API version constant:
    Defined ANTHROPIC_API_VERSION = '2023-06-01' to replace hardcoded version string.

Why This Is Safe

  • No breaking changes: All existing code paths continue to work exactly as before.
  • Future-proof: Eliminates PHP 8.4 deprecation warnings.
  • Type-safe: Uses modern PHP nullable syntax instead of deprecated patterns.
  • Comprehensive: All relevant files updated consistently.

Copy link
Copy Markdown
Owner

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JosephGabito Thank you for the PR, and great catch!

I have one point of feedback for the proposed approach. Also, there are a few other places in the codebase where a similar change would need to be made:

  • Felix_Arntz\AI_Services\Google\Google_AI_API_Client
  • Felix_Arntz\AI_Services\Services\Base\Generic_AI_API_Client
  • Felix_Arntz\AI_Services\Services\Service_Registration_Context

It would be great if you could align the approach throughout these classes.

Comment thread includes/Anthropic/Anthropic_AI_API_Client.php Outdated
Comment thread includes/Anthropic/Anthropic_AI_API_Client.php
Updated the Anthropic_AI_API_Client constructor to accept an optional Authentication instance. The code now checks for the presence of the authentication object before setting the custom header, improving flexibility and preventing potential errors when authentication is not provided.
Corrected the docblock for the $authentication parameter in the constructor to clarify the default value wording.
Changed the type hint for the Authentication parameter in the Google_AI_API_Client constructor to allow null values. This improves flexibility when instantiating the client without authentication.
Updated constructors in Generic_AI_API_Client and Service_Registration_Context to accept nullable Authentication parameters. This change improves type safety and clarifies that Authentication is optional.
@JosephGabito JosephGabito changed the title Remove Deprecated Nullable Authentication Parameter from Anthropic_AI_API_Client for PHP 8.4 Compatibility Fix PHP 8.4 deprecated nullable parameters and use constant for Anthropic API version Jul 7, 2025
Updated constructors and methods across several service classes to use explicit nullable type hints (e.g., ?Request_Handler, ?Option_Encrypter, ?callable) for optional parameters. This improves type safety and clarity in the codebase.
@JosephGabito
Copy link
Copy Markdown
Contributor Author

JosephGabito commented Jul 7, 2025

@felixarntz

Thanks for reviewing the PR. Updated both PR title and description to better align with the changes.

I also did change things to other locations to better align with everything :)

  1. ‎includes/Anthropic/Anthropic_AI_API_Client.php
  2. ‎includes/Google/Google_AI_API_Client.php
  3. includes/Google/Google_AI_Service.php
  4. ‎includes/OpenAI/OpenAI_AI_Service.php
  5. ‎includes/Perplexity/Perplexity_AI_Service.php
  6. ‎includes/Services/API/Candidates_Stream_Processor.php
  7. ‎includes/Services/Base/Generic_AI_API_Client.php
  8. ‎includes/Services/Service_Registration_Context.php
  9. ‎includes/Services/Services_API.php
  10. ‎includes/XAI/XAI_AI_Service.php

Cheers

@JosephGabito JosephGabito requested a review from felixarntz July 7, 2025 02:10
Copy link
Copy Markdown
Owner

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JosephGabito Thank you for the updates!

The PR looks great as is. Just noticed in the tests that there is one more case of inexplicit nullable parameters in the codebase, Felix_Arntz\AI_Services\Services\Util\Data_Encryption::__construct(), see https://github.com/felixarntz/ai-services/actions/runs/16105825834/job/45523564678?pr=40. I'm happy to merge this as is, but if you're willing to update that last case too, I'd appreciate it!

The other flagged case comes from the library https://github.com/felixarntz/wp-oop-plugin-lib that this plugin uses, so I'll make sure to fix those there and update!

@felixarntz felixarntz added the bug Something isn't working label Jul 8, 2025
@felixarntz
Copy link
Copy Markdown
Owner

Update: See felixarntz/wp-oop-plugin-lib@1fdfe74 for the upstream fix.

@felixarntz felixarntz merged commit a32214e into felixarntz:main Jul 8, 2025
24 of 46 checks passed
@felixarntz felixarntz changed the title Fix PHP 8.4 deprecated nullable parameters and use constant for Anthropic API version Fix PHP 8.4 deprecated implicit nullable parameters and use constant for Anthropic API version Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants