Skip to content

A free open source IT asset/license management system

Always return an array from selectValues method

Marcus Moore7f153b32e4ac
diff --git a/app/Models/ReportTemplate.php b/app/Models/ReportTemplate.php
index 4aa3d17161..8c0fa6091b 100644
--- a/app/Models/ReportTemplate.php
+++ b/app/Models/ReportTemplate.php
@@ -76,17 +76,17 @@ class ReportTemplate extends Model
return $this->options[$property] ?? null;
}
- public function selectValues(string $property, string $model = null)
+ public function selectValues(string $property, string $model = null): iterable
{
if (!isset($this->options[$property])) {
- return null;
+ return [];
}
// @todo: I think this was added to support the null object pattern
- // @todo: Check if this is still needed and if so, add a test for it.
- if ($this->options[$property] === [null]) {
- return null;
- }
+ // @todo: Check if this is still needed and if so, add a test for it (testParsingSelectValues()).
+// if ($this->options[$property] === [null]) {
+// return null;
+// }
// If a model is provided then we should ensure we only return
// the ids of models that exist and are not deleted.
diff --git a/resources/views/partials/forms/edit/department-select.blade.php b/resources/views/partials/forms/edit/department-select.blade.php
index eede9dce0f..1356261f64 100644
--- a/resources/views/partials/forms/edit/department-select.blade.php
+++ b/resources/views/partials/forms/edit/department-select.blade.php
@@ -5,7 +5,7 @@
<div class="col-md-6">
<select class="js-data-ajax" data-endpoint="departments" data-placeholder="{{ trans('general.select_department') }}" name="{{ $fieldname }}" style="width: 100%" id="department_select" aria-label="{{ $fieldname }}"{{ (isset($multiple) && ($multiple=='true')) ? " multiple='multiple'" : '' }}>
@isset ($selected)
- @if (!is_array($selected))
+ @if (!is_iterable($selected))
@php
$selected = [$selected];
@endphp
diff --git a/tests/Unit/ReportTemplateTest.php b/tests/Unit/ReportTemplateTest.php
index 14287b2984..5d930484d6 100644
--- a/tests/Unit/ReportTemplateTest.php
+++ b/tests/Unit/ReportTemplateTest.php
@@ -80,14 +80,17 @@ class ReportTemplateTest extends TestCase
{
$template = ReportTemplate::factory()->create([
'options' => [
- 'is_an_array' => ['2', '3', '4'],
- 'is_an_array_containing_null' => [null],
+ 'an_array' => ['2', '3', '4'],
+ 'an_empty_array'=> [],
+ 'an_array_containing_null' => [null],
],
]);
- $this->assertEquals(['2', '3', '4'], $template->selectValues('is_an_array'));
- $this->assertEquals(null, $template->selectValues('non_existent_key'));
- $this->assertNull($template->selectValues('is_an_array_containing_null'));
+ $this->assertEquals(['2', '3', '4'], $template->selectValues('an_array'));
+ $this->assertEquals([], $template->selectValues('an_empty_array'));
+ // @todo: should this actually be []?
+ $this->assertEquals([null], $template->selectValues('an_array_containing_null'));
+ $this->assertEquals([], $template->selectValues('non_existent_key'));
}
public function testSelectValueDoesNotIncludeDeletedOrNonExistentModels()