Skip to content

Commit 6ec5256

Browse files
committed
Fix nulls used as array offsets (deprecated in PHP 8.5)
These were caught by tests and psalm. Also made a few comparisons more explicit, updated types and initializers for some properties.
1 parent 1c6b798 commit 6ec5256

7 files changed

Lines changed: 26 additions & 23 deletions

File tree

HTML/QuickForm2/Container/Group.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function setValue($value)
112112
foreach ($nameParts as $i => $tokens) {
113113
$val = [$k => $v];
114114
do {
115-
$token = array_shift($tokens);
115+
$token = array_shift($tokens) ?? '';
116116
$numeric = false;
117117
if ($token == "") {
118118
// special case for a group of checkboxes

HTML/QuickForm2/Element/Select.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class HTML_QuickForm2_Element_Select extends HTML_QuickForm2_Element
5555
*
5656
* A value is considered possible if it is present as a value attribute of
5757
* some option and that option is not disabled.
58-
* @var array
58+
* @var array<string, true>
5959
*/
6060
protected $possibleValues = [];
6161

@@ -171,20 +171,21 @@ protected function getFrozenHtml()
171171
*/
172172
public function getRawValue()
173173
{
174-
if (!empty($this->attributes['disabled']) || 0 == count($this->values)
174+
if (!empty($this->attributes['disabled'])
175+
|| [] === $this->values
175176
|| ($this->data['intrinsic_validation']
176-
&& (0 == count($this->optionContainer) || 0 == count($this->possibleValues)))
177+
&& (0 === count($this->optionContainer) || [] === $this->possibleValues))
177178
) {
178179
return null;
179180
}
180181

181182
$values = [];
182183
foreach ($this->values as $value) {
183-
if (!$this->data['intrinsic_validation'] || !empty($this->possibleValues[$value])) {
184+
if (!$this->data['intrinsic_validation'] || !empty($this->possibleValues[(string)$value])) {
184185
$values[] = $value;
185186
}
186187
}
187-
if (0 == count($values)) {
188+
if ([] === $values) {
188189
return null;
189190
} elseif (!empty($this->attributes['multiple'])) {
190191
return $values;

HTML/QuickForm2/Element/Select/Optgroup.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class HTML_QuickForm2_Element_Select_Optgroup
4545
/**
4646
* Class constructor
4747
*
48-
* @param array &$values Reference to values of parent <select> element
49-
* @param array &$possibleValues Reference to possible values of parent <select> element
50-
* @param string $label 'label' attribute for optgroup tag
51-
* @param string|array $attributes Additional attributes for <optgroup> tag
52-
* (either as a string or as an associative array)
48+
* @param array &$values Reference to values of parent <select> element
49+
* @param array<string, true> &$possibleValues Reference to possible values of parent <select> element
50+
* @param string $label 'label' attribute for optgroup tag
51+
* @param string|array $attributes Additional attributes for <optgroup> tag
52+
* (either as a string or as an associative array)
5353
*/
5454
public function __construct(&$values, &$possibleValues, $label, $attributes = null)
5555
{

HTML/QuickForm2/Element/Select/OptionContainer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ class HTML_QuickForm2_Element_Select_OptionContainer extends HTML_Common2
6262

6363
/**
6464
* Reference to parent <select>'s possible values
65-
* @var array
65+
* @var array<string, true>
6666
*/
6767
protected $possibleValues;
6868

6969

7070
/**
7171
* Class constructor
7272
*
73-
* @param array &$values Reference to values of parent <select> element
74-
* @param array &$possibleValues Reference to possible values of parent <select> element
73+
* @param array &$values Reference to values of parent <select> element
74+
* @param array<string, true> &$possibleValues Reference to possible values of parent <select> element
7575
*/
7676
public function __construct(&$values, &$possibleValues)
7777
{

HTML/QuickForm2/JavascriptBuilder.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ class HTML_QuickForm2_JavascriptBuilder
4141
* Client-side rules
4242
* @var array<string, string[]>
4343
*/
44-
protected $rules = [];
44+
protected $rules = ['' => []];
4545

4646
/**
4747
* Elements' setup code
4848
* @var array<string, string[]>
4949
*/
50-
protected $scripts = [];
50+
protected $scripts = ['' => []];
5151

5252
/**
5353
* Whether to generate a validator object for the form if no rules are present
@@ -56,7 +56,7 @@ class HTML_QuickForm2_JavascriptBuilder
5656
*
5757
* @var array<string, bool>
5858
*/
59-
protected $forceValidator = [];
59+
protected $forceValidator = ['' => false];
6060

6161
/**
6262
* Javascript libraries
@@ -86,7 +86,7 @@ class HTML_QuickForm2_JavascriptBuilder
8686
* Current form ID
8787
* @var string
8888
*/
89-
protected $formId;
89+
protected $formId = '';
9090

9191

9292
/**
@@ -257,7 +257,7 @@ public function getSetupCode($formId = null, $addScriptTags = false)
257257
{
258258
$js = '';
259259
foreach ($this->scripts as $id => $scripts) {
260-
if ((null === $formId || $id == $formId) && !empty($scripts)) {
260+
if ((null === $formId || $id === $formId) && [] !== $scripts) {
261261
$js .= ('' == $js? '': "\n") . implode("\n", $scripts);
262262
}
263263
}
@@ -277,8 +277,8 @@ public function getValidator($formId = null, $addScriptTags = false)
277277
{
278278
$js = '';
279279
foreach ($this->rules as $id => $rules) {
280-
if ((null === $formId || $id == $formId)
281-
&& (!empty($rules) || !empty($this->forceValidator[$id]))
280+
if ((null === $formId || $id === $formId)
281+
&& ([] !== $rules || $this->forceValidator[$id])
282282
) {
283283
$js .= ('' == $js ? '' : "\n")
284284
. "new qf.Validator(document.getElementById('{$id}'), [\n"

HTML/QuickForm2/MessageProvider/Default.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,11 @@ public function get(array $messageId, $langId = null)
272272
$langId = HTML_Common2::getOption(HTML_QuickForm2_Node::OPTION_LANGUAGE);
273273
}
274274
$key = array_shift($messageId);
275-
if (empty($this->messages[$key]) || empty($this->messages[$key][$langId])) {
275+
if (null === $key || empty($this->messages[$key]) || empty($this->messages[$key][$langId])) {
276276
return null;
277277
}
278278
$message = $this->messages[$key][$langId];
279-
while (!empty($messageId)) {
279+
while ([] !== $messageId) {
280280
$key = array_shift($messageId);
281281
if (empty($message[$key])) {
282282
return null;

tests/QuickForm2/Element/InputCheckboxTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
/** Sets up includes */
2323
require_once dirname(dirname(__DIR__)) . '/TestHelper.php';
2424

25+
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
2526
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
2627

2728
/**
@@ -90,6 +91,7 @@ public function testRequest16806()
9091
* @see http://pear.php.net/bugs/bug.php?id=16816
9192
* @doesNotPerformAssertions
9293
*/
94+
#[DoesNotPerformAssertions]
9395
public function testBug16816()
9496
{
9597
$box = new HTML_QuickForm2_Element_InputCheckbox(

0 commit comments

Comments
 (0)