Skip to content

feat: make msls_get_switcher() $attr optional#644

Open
apermo wants to merge 2 commits into
lloc:masterfrom
apermo:feature/msls-get-switcher-optional-attr
Open

feat: make msls_get_switcher() $attr optional#644
apermo wants to merge 2 commits into
lloc:masterfrom
apermo:feature/msls-get-switcher-optional-attr

Conversation

@apermo
Copy link
Copy Markdown
Contributor

@apermo apermo commented Jun 3, 2026

What

Gives the $attr parameter of msls_get_switcher() a default of array() so
the documented public template tag can be called with no arguments.

// includes/api.php
-function msls_get_switcher( $attr ): string {
+function msls_get_switcher( $attr = array() ): string {
     $arr = is_array( $attr ) ? $attr : array();
     $obj = apply_filters( 'msls_get_output', null );
     return ! is_null( $obj ) ? strval( $obj->set_tags( $arr ) ) : '';
 }

Closes #643.

Why

The body already coerces any non-array value to array(), so $attr was
effectively optional. But without a default, the natural template-tag call
msls_get_switcher() threw a fatal ArgumentCountError ("Too few arguments
… 0 passed and exactly 1 expected"). The sibling tag
msls_the_switcher( array $arr = array() ) already defaults, so the two were
inconsistent. A downstream theme calling msls_get_switcher() with no args
currently has to pass array() purely to satisfy the signature.

Backward compatibility

No behavior change for any existing caller:

  • The parameter stays untyped so the is_array() coercion is preserved.
    This keeps the [sc_msls] shortcode
    (add_shortcode( 'sc_msls', 'msls_get_switcher' )) working — WordPress
    passes the empty string '' when a shortcode has no attributes, which an
    array type hint would have rejected.
  • Existing msls_get_switcher( $attr ) calls are unaffected.
  • The deprecated get_the_msls( $attr ) shim is unaffected.

Tests

Adds tests/phpunit/TestApi.php covering three cases:

  • no-arg call → returns '' when no output object is registered;
  • explicit attribute array → forwarded to set_tags();
  • non-array value ('', the shortcode's no-attribute case) → coerced to
    array().

The test runs in a separate process so loading includes/api.php does not
collide with the Brain Monkey function stubs used elsewhere in the suite.
Full suite green (431 tests). composer phpcs passes; composer phpstan
shows no new findings (the pre-existing errors are in unrelated files).

The @since tag uses 3.0.0, the current unreleased version on master.

Disclosure

Claude (Claude Code) assisted with this change. I reviewed the diff, ran the
test suite, phpcs and phpstan locally, and take responsibility for the change.

apermo and others added 2 commits June 3, 2026 21:50
Give $attr a default of array() so the documented template tag
can be called with no arguments. The body already coerced any
non-array value to array(), so $attr was effectively optional;
the missing default made msls_get_switcher() throw an
ArgumentCountError on the natural no-arg call.

The parameter stays untyped to preserve the is_array() coercion,
which keeps the [sc_msls] shortcode working (WordPress passes ''
when a shortcode has no attributes).

Closes lloc#643

Co-Authored-By: Claude <noreply@anthropic.com>
Add an isolated test for includes/api.php covering the no-arg
call, an explicit attribute array, and coercion of a non-array
value (the '' the [sc_msls] shortcode passes). Runs in a separate
process so loading api.php does not collide with the Brain Monkey
function stubs used elsewhere in the suite.

Co-Authored-By: Claude <noreply@anthropic.com>
@apermo apermo requested a review from lloc as a code owner June 3, 2026 19:51
Copy link
Copy Markdown
Contributor

@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 msls_get_switcher function in includes/api.php to make the $attr parameter optional, defaulting to an empty array, and updates its docblock documentation. Additionally, a new PHPUnit test class TestApi is introduced in tests/phpunit/TestApi.php to verify the behavior of msls_get_switcher under various scenarios, including calls without arguments, with an array, and with non-array values. There are no review comments, so we have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

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.

msls_get_switcher() should accept no arguments — $attr is effectively optional but lacks a default

1 participant