Skip to content

fix: Default PhpK8sException message parameter to empty string#501

Open
dshafik wants to merge 1 commit into
renoki-co:masterfrom
dshafik:patch-1
Open

fix: Default PhpK8sException message parameter to empty string#501
dshafik wants to merge 1 commit into
renoki-co:masterfrom
dshafik:patch-1

Conversation

@dshafik
Copy link
Copy Markdown

@dshafik dshafik commented May 5, 2026

This avoids an E_DEPRECATED error when calling the parent::__construct() method.

Alternatively, line 24 could be changed to $message ?? ""

Summary by Sourcery

Bug Fixes:

  • Prevent E_DEPRECATED warnings by ensuring the PhpK8sException constructor always passes a string message to the parent Exception.

This avoids an E_DEPRECATED error when calling the `parent::__construct()` method.

Alternatively, line 24 could be changed to `$message ?? ""`
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 5, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts PhpK8sException’s constructor default message to an empty string to avoid deprecation notices when delegating to the base Exception constructor.

Class diagram for updated PhpK8sException constructor

classDiagram
    class PhpK8sException {
        <<exception>>
        +__construct(message string = "", code int = 0, payload array?)
    }

    class Exception {
        +__construct(message string = "", code int = 0)
    }

    PhpK8sException --|> Exception
Loading

File-Level Changes

Change Details Files
Default PhpK8sException constructor message parameter to an empty string instead of null to avoid E_DEPRECATED warnings when calling parent::__construct.
  • Change the PhpK8sException::__construct signature so that the $message parameter defaults to an empty string rather than null
  • Ensure that calls that omit the $message argument still pass a string value to the base Exception constructor, preventing deprecation notices in newer PHP versions
src/Exceptions/PhpK8sException.php

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

📝 Walkthrough

Summary

This change modifies the PhpK8sException constructor to default the $message parameter to an empty string ("") instead of null. This is a targeted fix for PHP 8.2+ deprecation warnings that occur when passing null to the parent Exception class constructor.

Assessment

What this fixes:

  • Eliminates E_DEPRECATED warnings in PHP 8.2+ when creating PhpK8sException instances without providing a message
  • Aligns with modern PHP typing conventions where empty string is the appropriate default for exception messages rather than null

Backwards compatibility:

  • This change is safe for existing code. Code calling new PhpK8sException() with no arguments now receives an empty string message instead of null (same runtime behavior for end users)
  • Code explicitly passing null will still work

What's not addressed:

  • The docblock comment on line 19 still declares the parameter as string|null, but the default is now hardcoded to "". This documentation inconsistency should be corrected to string to match the actual parameter signature

Realistic take:
This is a legitimate, minimal fix for PHP 8.2 compatibility. The change itself is sound, though the docblock update should have been included for consistency. The alternative approach mentioned in the PR description (using $message ?? "" in the parent constructor call) would have been equivalent but less explicit in the signature.

Walkthrough

The PhpK8sException class constructor's default value for the $message parameter was changed from null to an empty string "". No other logic or functionality was modified. The exception class behavior remains otherwise unchanged.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and accurately describes the main change: defaulting the PhpK8sException message parameter to an empty string.
Description check ✅ Passed The description directly addresses the changeset by explaining the rationale (avoiding E_DEPRECATED error) and even mentions an alternative implementation approach.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 PHPStan (2.1.54)

PHPStan was skipped because the sandbox runner could not parse its output.

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider using $message ?? "" when calling parent::__construct() instead of changing the default argument value, so that existing callers that explicitly pass null retain their behavior while still avoiding the deprecation warning.
  • The docblock still documents $message as string|null; if you intend to disallow null going forward, update it accordingly, otherwise keep accepting ?string and normalize only at the parent::__construct call site.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider using `$message ?? ""` when calling `parent::__construct()` instead of changing the default argument value, so that existing callers that explicitly pass `null` retain their behavior while still avoiding the deprecation warning.
- The docblock still documents `$message` as `string|null`; if you intend to disallow `null` going forward, update it accordingly, otherwise keep accepting `?string` and normalize only at the `parent::__construct` call site.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the PhpK8sException constructor to use an empty string as the default value for the message parameter to mitigate PHP deprecation warnings. The review feedback correctly identifies that this change does not cover cases where null is explicitly passed and suggests using the null coalescing operator in the parent constructor call to fully resolve the issue.

* @param int $code
*/
public function __construct($message = null, $code = 0, ?array $payload = null)
public function __construct($message = "", $code = 0, ?array $payload = null)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Changing the default value to an empty string only prevents the deprecation error when the $message argument is omitted. If null is explicitly passed to the constructor (which is still permitted by the docblock and the lack of a type hint), the E_DEPRECATED error will still occur when parent::__construct($message, $code) is called on line 24.

To fully resolve this for all cases, it is recommended to implement the alternative mentioned in the PR description by using the null coalescing operator in the parent call: parent::__construct($message ?? "", $code);. This ensures a string is always passed to the base Exception class.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Exceptions/PhpK8sException.php`:
- Around line 22-24: Update the docblock for PhpK8sException::__construct to
reflect the new non-null default for $message: change the `@param` annotation from
"string|null $message" to "string $message" so the docblock matches the
constructor signature and no longer suggests null is accepted.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5bbdc13a-12a6-4e58-a0b8-82a142288b8a

📥 Commits

Reviewing files that changed from the base of the PR and between 609ba22 and 24091aa.

📒 Files selected for processing (1)
  • src/Exceptions/PhpK8sException.php

Comment on lines +22 to 24
public function __construct($message = "", $code = 0, ?array $payload = null)
{
parent::__construct($message, $code);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Find all call sites of PhpK8sException constructor to check for explicit null being passed.
rg -n --type=php -C2 'new PhpK8sException\s*\(\s*null'

Repository: renoki-co/php-k8s

Length of output: 43


🏁 Script executed:

# First, locate and inspect the PhpK8sException class to check the docblock
cat -n src/Exceptions/PhpK8sException.php | head -40

Repository: renoki-co/php-k8s

Length of output: 1008


🏁 Script executed:

# Search for all instantiations of PhpK8sException (with or without null)
rg -n --type=php 'new PhpK8sException' -C 1

Repository: renoki-co/php-k8s

Length of output: 43


🏁 Script executed:

# Also check if there are any other ways the exception is thrown or constructed
rg -n --type=php 'PhpK8sException(' -C 1

Repository: renoki-co/php-k8s

Length of output: 144


Update the docblock to match the new default behavior.

The fix is correct and complete—no call sites in the codebase pass null explicitly to the constructor. However, the docblock on line 19 still declares @param string|null $message, which contradicts the new default of "" and suggests the parameter accepts null when the code will now reject it (passing null to parent::__construct would trigger E_DEPRECATED in PHP 8.1+).

Change the docblock from:

`@param`  string|null  $message

to:

`@param`  string  $message

This keeps the docblock honest—the parameter no longer accepts null in practice.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Exceptions/PhpK8sException.php` around lines 22 - 24, Update the docblock
for PhpK8sException::__construct to reflect the new non-null default for
$message: change the `@param` annotation from "string|null $message" to "string
$message" so the docblock matches the constructor signature and no longer
suggests null is accepted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant