Automattic / Automattic/VIP-Coding-Standards

switch_to_blog() - flag for missing subsequent restore_current_blog()

Open
#651 3 comments 0 reactions 0 assignees View on GitHub
Type: Enhancement
Dominant language
PHP
Stars
261
Forks
44
Avg merge
19m
Merged PRs (30d)
1

Description

## Describe the solution you'd like

From https://github.com/Automattic/VIP-Coding-Standards/pull/649#discussion_r606174833, it would be worth flagging that `restore_current_blog()` should be called subsequently (if not already).

## What code should be reported as a violation?

```
function switch_blog() {
$id = get_current_blog_id();

if ( $id === 2 ) {
return;
}

switch_to_blog( 2 );
}
```

## What code should *not* be reported as a violation?

```
function switch_blog() {
$id = get_current_blog_id();

if ( $id === 2 ) {
return;
}

switch_to_blog( 2 );
restore_current_blog();
}
```

Contributor guide

Open the contributing guide

Research direction

Locate the existing rule or sniff that handles switch_to_blog(), then review how it determines whether a call is balanced. Add coverage for the two examples in the issue: report a switch without a subsequent restore_current_blog(), and do not report the restored case.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.