Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type inference for MultiCheckbox Value Options #247

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Element/MultiCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

use const E_USER_DEPRECATED;

/**
* @psalm-type ValueOptionSpec = array<string, string>|list<array{
* label: non-empty-string,
* value: non-empty-string,
* selected?: bool,
* disabled?: bool,
* attributes?: array<string, scalar|null>,
* label_attributes?: array<string, scalar|null>,
* }>
*/
class MultiCheckbox extends Checkbox
{
/** @var array<string, scalar|null> */
Expand All @@ -32,18 +42,19 @@ class MultiCheckbox extends Checkbox
/** @var null|string */
protected $uncheckedValue;

/** @var array */
/** @var ValueOptionSpec */
protected $valueOptions = [];

/**
* @return array
* @return ValueOptionSpec
*/
public function getValueOptions(): array
{
return $this->valueOptions;
}

/**
* @param ValueOptionSpec $options
* @return $this
*/
public function setValueOptions(array $options)
Expand All @@ -61,6 +72,11 @@ public function setValueOptions(array $options)
}

/**
* Unset a value option
*
* This method will only unset a value option when the element was created with a simple array of key-value pairs
* for value options, for example ['value1' => 'label1', 'value2' => 'label2']
*
* @return $this
*/
public function unsetValueOption(string $key)
Expand Down
75 changes: 75 additions & 0 deletions test/StaticAnalysis/MultiCheckboxValueOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Form\StaticAnalysis;

use Laminas\Form\Element\MultiCheckbox;

/** @psalm-suppress UnusedClass, UnusedMethod */
final class MultiCheckboxValueOptions
{
public function checkValueOptionsVariations(): void
{
$element = new MultiCheckbox('foo');
$element->setValueOptions([
'a' => 'A',
'b' => 'B',
]);

$element = new MultiCheckbox('foo');
$element->setValueOptions([
[
'label' => 'Foo',
'value' => 'Bar',
],
[
'label' => 'Foo',
'value' => 'Bar',
'disabled' => false,
],
[
'label' => 'Foo',
'value' => 'Bar',
'disabled' => true,
'selected' => true,
],
[
'label' => 'Foo',
'value' => 'Bar',
'attributes' => [
'readonly' => true,
'something' => 'else',
],
],
[
'label' => 'Foo',
'value' => 'Bar',
'label_attributes' => [
'inert' => true,
'class' => 'baz',
],
],
]);

$element = new MultiCheckbox('foo');
$element->setValueOptions([
'foo' => 'bar',
[
'label' => 'Foo',
'value' => 'Bar',
'disabled' => true,
'selected' => true,
'attributes' => [
'readonly' => true,
'something' => 'else',
],
'label_attributes' => [
'inert' => true,
'class' => 'baz',
],
],
'bing' => 'bong',
]);
}
}
6 changes: 6 additions & 0 deletions test/View/Helper/FormMultiCheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function testUsesOptionsAttributeToGenerateCheckBoxes(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('>%s</label>', $label), $markup);
self::assertStringContainsString(sprintf('value="%s"', $value), $markup);
}
Expand Down Expand Up @@ -120,6 +122,8 @@ public function testGenerateCheckBoxesAndHiddenElement(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('>%s</label>', $label), $markup);
self::assertStringContainsString(sprintf('value="%s"', $value), $markup);
}
Expand Down Expand Up @@ -157,6 +161,8 @@ public function testAllowsSpecifyingLabelPosition(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('<label>%s<', $label), $markup);
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/View/Helper/FormRadioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function testUsesOptionsAttributeToGenerateRadioOptions(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('>%s</label>', $label), $markup);
self::assertStringContainsString(sprintf('value="%s"', $value), $markup);
}
Expand Down Expand Up @@ -117,6 +119,8 @@ public function testGenerateRadioOptionsAndHiddenElement(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('>%s</label>', $label), $markup);
self::assertStringContainsString(sprintf('value="%s"', $value), $markup);
}
Expand Down Expand Up @@ -154,6 +158,8 @@ public function testAllowsSpecifyingLabelPosition(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('<label>%s<', $label), $markup);
}
}
Expand Down