feat: make msls_get_switcher() $attr optional#644
Conversation
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>
There was a problem hiding this comment.
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.
What
Gives the
$attrparameter ofmsls_get_switcher()a default ofarray()sothe documented public template tag can be called with no arguments.
Closes #643.
Why
The body already coerces any non-array value to
array(), so$attrwaseffectively optional. But without a default, the natural template-tag call
msls_get_switcher()threw a fatalArgumentCountError("Too few arguments… 0 passed and exactly 1 expected"). The sibling tag
msls_the_switcher( array $arr = array() )already defaults, so the two wereinconsistent. A downstream theme calling
msls_get_switcher()with no argscurrently has to pass
array()purely to satisfy the signature.Backward compatibility
No behavior change for any existing caller:
is_array()coercion is preserved.This keeps the
[sc_msls]shortcode(
add_shortcode( 'sc_msls', 'msls_get_switcher' )) working — WordPresspasses the empty string
''when a shortcode has no attributes, which anarraytype hint would have rejected.msls_get_switcher( $attr )calls are unaffected.get_the_msls( $attr )shim is unaffected.Tests
Adds
tests/phpunit/TestApi.phpcovering three cases:''when no output object is registered;set_tags();'', the shortcode's no-attribute case) → coerced toarray().The test runs in a separate process so loading
includes/api.phpdoes notcollide with the Brain Monkey function stubs used elsewhere in the suite.
Full suite green (431 tests).
composer phpcspasses;composer phpstanshows no new findings (the pre-existing errors are in unrelated files).
The
@sincetag uses3.0.0, the current unreleased version onmaster.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.