Skip to content

Add null safety checks to prevent PHP warnings#4905

Open
GaryJones wants to merge 1 commit intobuddyboss:releasefrom
GaryJones:fix/null-safety-php-warnings-v2
Open

Add null safety checks to prevent PHP warnings#4905
GaryJones wants to merge 1 commit intobuddyboss:releasefrom
GaryJones:fix/null-safety-php-warnings-v2

Conversation

@GaryJones
Copy link
Copy Markdown

Problem

Four places in the codebase lack null/undefined checks, producing PHP warnings or deprecation notices on PHP 8.1+.

1. Undefined title_required property on activity objects

In bp-groups/bp-groups-activity.php, when updating a group activity, the code accesses $activity->title_required without a fallback. If the property doesn't exist on the activity object, PHP emits an undefined property warning.

Fix: Add ?? false null-coalescing fallback.

2. Null passed to wp_kses_post() from notification icon

In bp-notifications/bp-notifications-functions.php, bb_notification_small_icon() can return null, which is then passed directly to wp_kses_post(). On PHP 8.1+, this triggers a deprecation warning since wp_kses_post() expects a string.

Fix: Add ?? '' null-coalescing fallback.

3. Missing progress data in profile completion widget

In class-bp-xprofile-profile-completion-widget.php, the widget accesses $user_progress['completion_percentage'] without checking whether $user_progress is populated or the key exists. When no applicable profile fields are configured, this produces undefined key warnings.

Fix: Add an early return when $user_progress is empty or missing the completion_percentage key.

4. Uninitialised $result variable in topics manager

In class-bb-topics-manager.php, the $result variable is only assigned inside a conditional block (if ( 'migrate' === $migrate_type && $new_topic_id )). When the condition is false, $result is undefined for any subsequent code that references it.

Fix: Initialise $result = array() before the conditional.

Test plan

  • Update a group activity where title_required is not set on the activity object — no PHP warning
  • View notifications where bb_notification_small_icon() returns null — no deprecation notice
  • Display the profile completion widget with no applicable xprofile fields configured — no undefined key warnings
  • Use the topics manager with a non-migrate action — no undefined variable notice

Four small fixes for undefined property/variable warnings and null
parameter deprecations on PHP 8.1+:

1. Null-coalescing fallback for missing title_required property on
   activity objects (bp-groups-activity.php).

2. Null-coalescing fallback before passing bb_notification_small_icon()
   result to wp_kses_post() (bp-notifications-functions.php).

3. Early return in profile completion widget when user_progress is
   empty or missing completion_percentage key
   (class-bp-xprofile-profile-completion-widget.php).

4. Initialise $result to empty array before conditional assignment
   in topics manager (class-bb-topics-manager.php).
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.

2 participants