Skip to content

Commit cb1cb9a

Browse files
committed
Use psalm 6, fix newly found issues
1 parent cc9cfa7 commit cb1cb9a

18 files changed

+94
-108
lines changed

HTML/QuickForm2/Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ protected function getChildValues($filtered = false)
140140
}
141141
$valueAry =& $valueAry[$token];
142142
} while (count($tokens) > 1);
143-
if ('' !== ($token = array_shift($tokens))) {
143+
if ('' !== ($token = (string)array_shift($tokens))) {
144144
$valueAry[$token] = $value;
145145
} else {
146146
if (!isset($forceKeys[$name])) {

HTML/QuickForm2/Container/Group.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,9 @@ public function __toString()
339339
{
340340
// pear-package-only HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_Renderer');
341341

342-
/** @var HTML_QuickForm2_Renderer_Default $renderer */
343-
$renderer = $this->render(
344-
HTML_QuickForm2_Renderer::factory('default')
345-
->setTemplateForId($this->getId(), '{content}')
346-
);
342+
/** @var HTML_QuickForm2_Renderer_Default&HTML_QuickForm2_Renderer_Proxy $renderer */
343+
$renderer = HTML_QuickForm2_Renderer::factory('default');
344+
$this->render($renderer->setTemplateForId((string)$this->getId(), '{content}'));
347345
return $renderer->__toString()
348346
. $renderer->getJavascriptBuilder()->getSetupCode(null, true);
349347
}

HTML/QuickForm2/Container/Repeat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ private function _guessIndexField()
283283
&& !$child->getAttribute('multiple'))
284284
|| $child instanceof HTML_QuickForm2_Element_Textarea
285285
) {
286-
$this->indexField = substr($name, 0, $pos);
286+
$this->indexField = (string)substr($name, 0, $pos);
287287
return true;
288288
}
289289
}

HTML/QuickForm2/DataSource/SuperGlobal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function getUpload($name)
110110
}
111111

112112
do {
113-
$token = array_shift($tokens);
113+
$token = (string)array_shift($tokens);
114114
if (!isset($value['name'][$token])) {
115115
return null;
116116
}

HTML/QuickForm2/Element/Date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ private function _shortHourCallback(&$v, $k)
320320
protected function createOptionList($start, $end, $step = 1)
321321
{
322322
for ($i = $start, $options = []; $start > $end? $i >= $end: $i <= $end; $i += $step) {
323-
$options[$i] = sprintf('%02d', $i);
323+
$options[$i] = (string)sprintf('%02d', $i);
324324
}
325325
return $options;
326326
}

HTML/QuickForm2/Element/Hierselect.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ private function _loadChildOptions()
220220
$keys = $this->_values;
221221
$array =& $this->options[$idx++];
222222
while (!empty($keys)) {
223-
$key = array_shift($keys);
223+
if (null === $key = array_shift($keys)) {
224+
continue;
225+
}
224226
if (!isset($array[$key])) {
225227
if (!empty($keys)) {
226228
$array[$key] = [];
@@ -312,19 +314,13 @@ protected function updateValue()
312314
* @link http://pear.php.net/bugs/bug.php?id=16603
313315
* @return array Array with separate options and texts
314316
*/
315-
private function _prepareOptions($ary, $depth)
317+
private function _prepareOptions(array $ary, $depth)
316318
{
317-
if (!is_array($ary)) {
318-
$ret = $ary;
319-
} elseif (0 == $depth) {
320-
$ret = ['values' => array_keys($ary), 'texts' => array_values($ary)];
321-
} else {
322-
$ret = [];
323-
foreach ($ary as $k => $v) {
324-
$ret[$k] = $this->_prepareOptions($v, $depth - 1);
325-
}
326-
}
327-
return $ret;
319+
return 0 === $depth
320+
? ['values' => array_keys($ary), 'texts' => array_values($ary)]
321+
: array_map(function ($v) use ($depth) {
322+
return $this->_prepareOptions($v, $depth - 1);
323+
}, $ary);
328324
}
329325

330326
/**
@@ -336,7 +332,7 @@ private function _generateInlineScript()
336332
{
337333
// we store values and options with id of first select rather than with
338334
// the element's name since the former has more chances to be unique
339-
$selectId = reset($this->elements)->getId();
335+
$selectId = (reset($this->elements) ?: $this)->getId();
340336
$cr = self::getOption(self::OPTION_LINEBREAK);
341337
$js = "qf.elements.hierselect.defaults['{$selectId}'] = " .
342338
HTML_QuickForm2_JavascriptBuilder::encode($this->_values) . ";{$cr}";

HTML/QuickForm2/Element/InputCheckbox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public function __construct($name = null, $attributes = null, array $data = [])
5555
protected function updateValue()
5656
{
5757
$name = (string)$this->getName();
58-
if ('[]' == substr($name, -2)) {
59-
$name = substr($name, 0, -2);
58+
if ('[]' === substr($name, -2)) {
59+
$name = (string)substr($name, 0, -2);
6060
}
6161
foreach ($this->getDataSources() as $ds) {
6262
if (null !== ($value = $ds->getValue($name))

HTML/QuickForm2/Element/InputFile.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ protected function validate()
203203
? $this->messageProvider->get(['file', $this->value['error']], $this->language)
204204
: call_user_func($this->messageProvider, ['file', $this->value['error']], $this->language);
205205
if (UPLOAD_ERR_INI_SIZE == $this->value['error']) {
206-
$iniSize = ini_get('upload_max_filesize');
206+
$iniSize = ini_get('upload_max_filesize') ?: '0';
207207
$size = intval($iniSize);
208-
switch (strtoupper(substr($iniSize, -1))) {
208+
switch (strtoupper((string)substr($iniSize, -1))) {
209209
case 'G': $size *= 1024;
210210
case 'M': $size *= 1024;
211211
case 'K': $size *= 1024;
@@ -219,7 +219,7 @@ protected function validate()
219219
}
220220
}
221221
}
222-
$this->error = isset($size)? sprintf($errorMessage, $size): $errorMessage;
222+
$this->error = (string)(isset($size)? sprintf($errorMessage, $size): $errorMessage);
223223
return false;
224224
}
225225
return parent::validate();

HTML/QuickForm2/Element/InputImage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ protected function updateValue()
123123
return;
124124

125125
} elseif (false !== strpos($name, '[')) {
126-
if ('[]' == substr($name, -2)) {
127-
$name = substr($name, 0, -2);
126+
if ('[]' === substr($name, -2)) {
127+
$name = (string)substr($name, 0, -2);
128128
}
129129
if (null !== ($value = $ds->getValue($name))) {
130130
$this->coordinates = [

HTML/QuickForm2/Element/Select.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function __toString()
112112
} else {
113113
$this->attributes['name'] .= '[]';
114114
$attrString = $this->getAttributes(true);
115-
$this->attributes['name'] = substr($this->attributes['name'], 0, -2);
115+
$this->attributes['name'] = (string)substr($this->attributes['name'], 0, -2);
116116
}
117117
$indent = $this->getIndent();
118118
return $indent . '<select' . $attrString . '>' .

HTML/QuickForm2/JavascriptBuilder.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,14 @@ public function __construct($defaultWebPath = 'js/', $defaultAbsPath = null)
105105
if (null === $defaultAbsPath) {
106106
$defaultAbsPath = '@data_dir@' . DIRECTORY_SEPARATOR . 'HTML_QuickForm2'
107107
. DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR;
108-
// package was probably not installed, use relative path
108+
// Not a PEAR installation, use relative path
109109
if (0 === strpos($defaultAbsPath, '@' . 'data_dir@')) {
110-
$defaultAbsPath = realpath(
111-
dirname(dirname(__DIR__))
112-
. DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'js'
113-
) . DIRECTORY_SEPARATOR;
110+
if (false === $defaultAbsPath = realpath(__DIR__ . '/../../data/js')) {
111+
throw new HTML_QuickForm2_Exception(
112+
'Unable to locate directory containing JavaScript files, please provide one explicitly'
113+
);
114+
}
115+
$defaultAbsPath .= DIRECTORY_SEPARATOR;
114116
}
115117
}
116118
$this->defaultAbsPath = $defaultAbsPath;

HTML/QuickForm2/MessageProvider/Default.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function get(array $messageId, $langId = null)
298298
*/
299299
public function set(array $messageId, $langId, $message)
300300
{
301-
$key = array_shift($messageId);
301+
$key = array_shift($messageId) ?: 0;
302302
if (empty($this->messages[$key])) {
303303
$this->messages[$key] = [];
304304
}
@@ -307,7 +307,7 @@ public function set(array $messageId, $langId, $message)
307307
}
308308
$messageHolder =& $this->messages[$key][$langId];
309309
while (!empty($messageId)) {
310-
$key = array_shift($messageId);
310+
$key = array_shift($messageId) ?: 0;
311311
if (empty($messageHolder[$key])) {
312312
$messageHolder[$key] = [];
313313
}

HTML/QuickForm2/MessageProvider/Strftime.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ class HTML_QuickForm2_MessageProvider_Strftime implements HTML_QuickForm2_Messag
5757
public function __construct()
5858
{
5959
for ($i = 1; $i <= 12; $i++) {
60-
$names = explode("\n", strftime("%b\n%B", mktime(12, 0, 0, $i, 1, 2011)));
60+
$names = explode("\n", (string)strftime("%b\n%B", mktime(12, 0, 0, $i, 1, 2011)));
6161
$this->messages['months_short'][] = $names[0];
6262
$this->messages['months_long'][] = $names[1];
6363
}
6464
for ($i = 0; $i < 7; $i++) {
65-
$names = explode("\n", strftime("%a\n%A", mktime(12, 0, 0, 1, 2 + $i, 2011)));
65+
$names = explode("\n", (string)strftime("%a\n%A", mktime(12, 0, 0, 1, 2 + $i, 2011)));
6666
$this->messages['weekdays_short'][] = $names[0];
6767
$this->messages['weekdays_long'][] = $names[1];
6868
}

HTML/QuickForm2/Node.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ protected static function generateId($elementName)
246246
$container =& $container[$token];
247247
} while (!empty($tokens));
248248

249-
return substr($id, 1);
249+
return (string)substr($id, 1);
250250
}
251251

252252

HTML/QuickForm2/Renderer/Callback.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public static function _renderForm(
185185
'<form'.$form->getAttributes(true).'><div>' .
186186
call_user_func($renderer->hiddenGroupCallback, $renderer, $form)
187187
];
188-
$html[] = implode($break, array_pop($renderer->html));
188+
$html[] = implode($break, (array)array_pop($renderer->html));
189189
$html[] = '</div></form>';
190190
$html[] = call_user_func($renderer->requiredNoteCallback, $renderer, $form);
191191
$script = $renderer->getJavascriptBuilder()->getFormJavascript($form->getId());
@@ -232,19 +232,15 @@ public static function _renderErrorsGroup(
232232
$html = [];
233233
if (!empty($renderer->errors)) {
234234
$html[] = '<div class="errors">';
235-
if (($prefix = $renderer->getOption('errors_prefix'))
236-
&& !empty($prefix)
237-
) {
235+
if ($prefix = $renderer->getOption('errors_prefix')) {
238236
$html[] = '<p>' . $prefix . '</p>';
239237
}
240238
$html[] = '<ul>';
241239
foreach ($renderer->errors as $error) {
242240
$html[] = '<li>' . $error . '</li>';
243241
}
244242
$html[] = '</ul>';
245-
if (($suffix = $renderer->getOption('errors_suffix'))
246-
&& !empty($suffix)
247-
) {
243+
if ($suffix = $renderer->getOption('errors_suffix')) {
248244
$html[] = '<p>' . $suffix . '</p>';
249245
}
250246
$html[] = '</div>';
@@ -278,7 +274,7 @@ public static function _renderRequiredNote(
278274
) {
279275
/** @var HTML_QuickForm2_Renderer_Callback $renderer */
280276
if ($renderer->hasRequired && !$form->toggleFrozen(null)) {
281-
if (($note = $renderer->getOption('required_note')) && !empty($note)) {
277+
if ($note = $renderer->getOption('required_note')) {
282278
return '<div class="reqnote">'.$note.'</div>';
283279
}
284280
}
@@ -290,7 +286,7 @@ public static function _renderContainer(
290286
) {
291287
/** @var HTML_QuickForm2_Renderer_Callback $renderer */
292288
$break = HTML_Common2::getOption(HTML_Common2::OPTION_LINEBREAK);
293-
return implode($break, array_pop($renderer->html));
289+
return implode($break, (array)array_pop($renderer->html));
294290
}
295291

296292
public static function _renderGroup(
@@ -314,7 +310,7 @@ public static function _renderGroup(
314310
}
315311

316312
$separator = $group->getSeparator();
317-
$elements = array_pop($renderer->html);
313+
$elements = (array)array_pop($renderer->html);
318314
if (!is_array($separator)) {
319315
$content = implode((string)$separator, $elements);
320316
} else {
@@ -345,8 +341,8 @@ public static function _renderRepeat(
345341
if (!empty($label[0])) {
346342
$html[] = '<p>'.array_shift($label).'</p>';
347343
}
348-
$elements = array_pop($renderer->html);
349-
$content = implode($break, $elements);
344+
$elements = (array)array_pop($renderer->html);
345+
$content = implode($break, $elements);
350346
$html[] = $content;
351347
$html[] = '</div>';
352348
return implode($break, $html) . $break;
@@ -360,15 +356,15 @@ public static function _renderFieldset(
360356
$break = HTML_Common2::getOption(HTML_Common2::OPTION_LINEBREAK);
361357
$html = ['<fieldset'.$fieldset->getAttributes(true).'>'];
362358
$label = $fieldset->getLabel();
363-
$mainLabel = is_array($label) ? array_shift($label) : (string)$label;
359+
$mainLabel = (string)(is_array($label) ? array_shift($label) : $label);
364360
if ('' !== $mainLabel) {
365361
$html[] = sprintf(
366362
'<legend id="%s-legend">%s</legend>',
367363
(string)$fieldset->getId(),
368364
$mainLabel
369365
);
370366
}
371-
$elements = array_pop($renderer->html);
367+
$elements = (array)array_pop($renderer->html);
372368
$html[] = implode($break, $elements);
373369
$html[] = '</fieldset>';
374370
return implode($break, $html) . $break;
@@ -728,15 +724,16 @@ public function findCallback(HTML_QuickForm2_Node $element, $default = null)
728724
if (!empty($this->callbacksForId[$elementId])) {
729725
return $this->callbacksForId[$elementId];
730726
}
731-
$class = strtolower(get_class($element));
727+
$class = get_class($element);
732728
$groupId = end($this->groupId);
733729
$elementClasses = [];
734730
do {
735-
if (empty($groupId) && !empty($this->callbacksForClass[$class])) {
736-
return $this->callbacksForClass[$class];
731+
$lower = strtolower($class);
732+
if (empty($groupId) && !empty($this->callbacksForClass[$lower])) {
733+
return $this->callbacksForClass[$lower];
737734
}
738-
$elementClasses[$class] = true;
739-
} while ($class = strtolower(get_parent_class($class)));
735+
$elementClasses[$lower] = true;
736+
} while (false !== $class = get_parent_class($class));
740737

741738
if (!empty($groupId)) {
742739
if (!empty($this->elementCallbacksForGroupId[$groupId])) {
@@ -748,16 +745,17 @@ public function findCallback(HTML_QuickForm2_Node $element, $default = null)
748745
}
749746

750747
if (null !== ($group = $element->getContainer())) {
751-
$grClass = strtolower(get_class($group));
748+
$grClass = get_class($group);
752749
do {
753-
if (!empty($this->elementCallbacksForGroupClass[$grClass])) {
750+
$lower = strtolower($grClass);
751+
if (!empty($this->elementCallbacksForGroupClass[$lower])) {
754752
foreach (array_keys($elementClasses) as $elClass) {
755-
if (!empty($this->elementCallbacksForGroupClass[$grClass][$elClass])) {
756-
return $this->elementCallbacksForGroupClass[$grClass][$elClass];
753+
if (!empty($this->elementCallbacksForGroupClass[$lower][$elClass])) {
754+
return $this->elementCallbacksForGroupClass[$lower][$elClass];
757755
}
758756
}
759757
}
760-
} while ($grClass = strtolower(get_parent_class($grClass)));
758+
} while (false !== $grClass = get_parent_class($grClass));
761759
}
762760
}
763761
// default may be null, so we try to return a callback in any case

0 commit comments

Comments
 (0)