Skip to content

Commit b3cddd2

Browse files
committed
Refactor codes
1 parent f2d86b9 commit b3cddd2

File tree

4 files changed

+19
-28
lines changed

4 files changed

+19
-28
lines changed

src/Kris/LaravelFormBuilder/Fields/ChoiceType.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Kris\LaravelFormBuilder\Fields;
3+
namespace Kris\LaravelFormBuilder\Fields;
44

55
use Illuminate\Support\Arr;
66

@@ -24,7 +24,6 @@ protected function getTemplate()
2424
return 'choice';
2525
}
2626

27-
2827
/**
2928
* @inheritdoc
3029
*/
@@ -98,7 +97,7 @@ protected function createChildren()
9897
if (($data_override = $this->getOption('data_override')) && $data_override instanceof \Closure) {
9998
$this->options['choices'] = $data_override($this->options['choices'], $this);
10099
}
101-
100+
102101
$this->children = [];
103102
$this->determineChoiceField();
104103

@@ -126,7 +125,7 @@ protected function buildCheckableChildren($fieldType)
126125
{
127126
$multiple = $this->getOption('multiple') ? '[]' : '';
128127

129-
$attr = $this->options['attr']?? [];
128+
$attr = $this->options['attr'] ?? [];
130129
$attr = Arr::except($attr, ['class', 'multiple', 'id', 'name']);
131130
foreach ((array)$this->options['choices'] as $key => $choice) {
132131
$id = str_replace('.', '_', $this->getNameKey()) . '_' . $key;

src/Kris/LaravelFormBuilder/Fields/ParentType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function setValue($val)
5656
*/
5757
public function render(array $options = [], $showLabel = true, $showField = true, $showError = true)
5858
{
59-
if(!empty($options)) {
59+
if (!empty($options)) {
6060
$this->prepareOptions($options);
6161

6262
$this->createChildren();

src/Kris/LaravelFormBuilder/Form.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function rebuildForm()
171171
if ($this->isPlain()) {
172172
foreach ($this->fields as $name => $field) {
173173
// Remove any temp variables added in previous instance
174-
$options = Arr::except($field->getOptions(), 'tmp');
174+
$options = Arr::except($field->getOptions(), 'tmp');
175175
$this->add($name, $field->getType(), $options);
176176
}
177177
} else {
@@ -187,7 +187,7 @@ public function rebuildForm()
187187
*/
188188
protected function isPlain()
189189
{
190-
if($this->formBuilder === null) {
190+
if ($this->formBuilder === null) {
191191
throw new \RuntimeException('FormBuilder is not set');
192192
}
193193

@@ -251,7 +251,6 @@ protected function addField(FormField $field, $modify = false)
251251
$this->preventDuplicate($field->getRealName());
252252
}
253253

254-
255254
if ($field->getType() == 'file') {
256255
$this->formOptions['files'] = true;
257256
}
@@ -882,7 +881,7 @@ public function getData($name = null, $default = null)
882881
* will be switched to protected in 1.7.
883882
* @param $data
884883
* @return $this
885-
**/
884+
*/
886885
public function addData(array $data)
887886
{
888887
foreach ($data as $key => $value) {
@@ -992,7 +991,7 @@ public function setTranslationTemplate($template)
992991
* Render the form.
993992
*
994993
* @param array $options
995-
* @param string $fields
994+
* @param string[] $fields
996995
* @param bool $showStart
997996
* @param bool $showFields
998997
* @param bool $showEnd
@@ -1056,16 +1055,9 @@ protected function getTemplate()
10561055
*/
10571056
protected function getUnrenderedFields()
10581057
{
1059-
$unrenderedFields = [];
1060-
1061-
foreach ($this->fields as $field) {
1062-
if (!$field->isRendered()) {
1063-
$unrenderedFields[] = $field;
1064-
continue;
1065-
}
1066-
}
1067-
1068-
return $unrenderedFields;
1058+
return array_filter($this->fields, function ($field) {
1059+
return !$field->isRendered();
1060+
});
10691061
}
10701062

10711063
/**
@@ -1090,9 +1082,7 @@ protected function preventDuplicate($name)
10901082
*/
10911083
protected function getFieldType($type)
10921084
{
1093-
$fieldType = $this->formHelper->getFieldType($type);
1094-
1095-
return $fieldType;
1085+
return $this->formHelper->getFieldType($type);
10961086
}
10971087

10981088
/**
@@ -1484,6 +1474,7 @@ public function getFilters()
14841474
public function lockFiltering()
14851475
{
14861476
$this->lockFiltering = true;
1477+
14871478
return $this;
14881479
}
14891480

@@ -1495,6 +1486,7 @@ public function lockFiltering()
14951486
public function unlockFiltering()
14961487
{
14971488
$this->lockFiltering = false;
1489+
14981490
return $this;
14991491
}
15001492

@@ -1506,7 +1498,7 @@ public function unlockFiltering()
15061498
*/
15071499
public function isFilteringLocked()
15081500
{
1509-
return !$this->lockFiltering ? false : true;
1501+
return (bool)$this->lockFiltering;
15101502
}
15111503

15121504
/**

tests/Fields/ChoiceTypeTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function it_disables_select()
117117
];
118118
$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
119119
$children = $field->getChildren();
120-
120+
121121
// there shall be no 'disabled' attribute set beforehand
122122
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
123123
foreach ($children as $child) {
@@ -165,7 +165,7 @@ public function it_disables_checkbox_list()
165165
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
166166
}
167167
}
168-
168+
169169
/** @test */
170170
public function it_disables_radios_list()
171171
{
@@ -195,7 +195,7 @@ public function it_disables_radios_list()
195195
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
196196
}
197197
}
198-
198+
199199
/** @test */
200200
public function it_enables_select()
201201
{
@@ -256,7 +256,7 @@ public function it_enables_checkbox_list()
256256
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
257257
}
258258
}
259-
259+
260260
/** @test */
261261
public function it_enables_radios_list()
262262
{

0 commit comments

Comments
 (0)