Completed
Push — master ( 31a468...e56ae9 )
by Nicolaas
01:22
created

BuildController::MyCanMethodBuilder()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 2
1
<?php
2
3
4
namespace SunnySideUp\BuildDataObject;
5
6
7
8
abstract class BuildController extends \Controller {
9
10
11
    private static $form_data_session_variable = 'SunnySideUp\BuildDataObject\DataObjectBuildController';
0 ignored issues
show
Unused Code introduced by
The property $form_data_session_variable is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
13
    private static $url_segment = 'build';
0 ignored issues
show
Unused Code introduced by
The property $url_segment is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
15
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
        'primaryformstart' => true,
17
        'PrimaryForm' => true,
18
        'doprimaryform' => true,
19
        'secondaryformstart' => true,
20
        'SecondaryForm' => true,
21
        'dosecondaryform' => true,
22
        'results' => true,
23
        'startover' => true,
24
        'debug' => true
25
    ];
26
27
    protected $myBaseClass = 'DataObject';
28
29
    protected $apiProvider = 'SunnySideUp\BuildDataObject\API';
30
31
    abstract protected function primaryThingsToBuild();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
32
33
    abstract protected function secondaryThingsToBuild();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
34
35
    public function Link($action = null)
36
    {
37
        if($action) {
38
            $action .= '/';
39
        }
40
        return
41
            '/'.$this->Config()->get('url_segment').
42
            '/'.strtolower($this->myBaseClass).
43
            '/'.$action;
44
    }
45
46
    public function Title()
47
    {
48
        return 'Build a '.$this->myBaseClass.' - Step '.$this->step.' of 2';
49
    }
50
51
52
    public function jQueryLink()
53
    {
54
        return \Director::absoluteURL('/framework/thirdparty/jquery/jquery.js');
55
    }
56
57
    public function startover()
58
    {
59
        $this->saveData('_PrimaryForm', null);
60
        $this->saveData('_SecondaryForm', null);
61
        return $this->redirect($this->link('primaryformstart'));
62
    }
63
64
    /**
65
     *
66
     * @var Form
67
     */
68
    protected $step = 1;
69
70
    /**
71
     *
72
     * @var Form
73
     */
74
    protected $form = null;
75
76
    /**
77
     *
78
     * @var Form
79
     */
80
    protected $prevLink = null;
81
82
    /**
83
     *
84
     * @var ArrayList
85
     */
86
    protected $finalData = null;
87
88
    function index()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89
    {
90
        return $this->redirect($this->Link('primaryformstart'));
91
    }
92
93
    function primaryformstart()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
94
    {
95
        $this->PrimaryForm();
96
        $this->prevLink = $this->Link('startover');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->Link('startover') of type string is incompatible with the declared type object<SunnySideUp\BuildDataObject\Form> of property $prevLink.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
97
98
        return $this->renderWith('BuildControllerForm');
99
    }
100
101
    function PrimaryForm()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
102
    {
103
        $this->form = $this->createForm('PrimaryForm', 'Build Model');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createForm('PrimaryForm', 'Build Model') of type object<Form> is incompatible with the declared type object<SunnySideUp\BuildDataObject\Form> of property $form.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
104
105
        return $this->form;
106
    }
107
108
    function doprimaryform($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
109
    {
110
        $this->saveData('_PrimaryForm', $data);
111
112
        return $this->redirect($this->Link('secondaryformstart'));
113
    }
114
115
116
    function secondaryformstart()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
117
    {
118
        $this->step = 2;
0 ignored issues
show
Documentation Bug introduced by
It seems like 2 of type integer is incompatible with the declared type object<SunnySideUp\BuildDataObject\Form> of property $step.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
119
        $this->SecondaryForm();
120
        $this->prevLink = $this->Link('primaryformstart');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->Link('primaryformstart') of type string is incompatible with the declared type object<SunnySideUp\BuildDataObject\Form> of property $prevLink.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
121
122
        return $this->renderWith('BuildControllerForm');
123
    }
124
125
    function SecondaryForm()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
126
    {
127
        $this->form = $this->createForm('SecondaryForm', 'Download Example Class');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createForm('Secon...ownload Example Class') of type object<Form> is incompatible with the declared type object<SunnySideUp\BuildDataObject\Form> of property $form.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
128
129
        return $this->form;
130
    }
131
132
133
    function dosecondaryform($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134
    {
135
        $this->saveData('_SecondaryForm', $data);
136
137
        return $this->redirect($this->Link('results'));
138
    }
139
140
    function results()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
141
    {
142
        $this->finalData = $this->processedFormData($this->retrieveData());
143
        return \SS_HTTPRequest::send_file(
144
            $this->renderWith($this->resultsTemplateForBuilder()),
145
            $this->finalData->Name.'.php'
146
        );
147
    }
148
149
    function debug()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
150
    {
151
        $this->finalData = $this->processedFormData($this->retrieveData());
152
        print_r($this->CanMethodBuilder('canEdit'));
0 ignored issues
show
Bug introduced by
The method CanMethodBuilder() does not exist on SunnySideUp\BuildDataObject\BuildController. Did you maybe mean MyCanMethodBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
153
        print_r($this->finalData);
154
        die('-----------------------------');
0 ignored issues
show
Coding Style Compatibility introduced by
The method debug() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
155
    }
156
157
    function Form()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
158
    {
159
        return $this->form;
160
    }
161
162
    function FinalData()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
163
    {
164
        return $this->finalData;
165
    }
166
167
    function PrevLink()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
168
    {
169
        return $this->prevLink;
170
    }
171
172
    function MyCanMethodBuilder($type, $value) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
173
        if($value === 'parent') {
174
            return null;
175
        } elseif($value === 'one') {
176
            $str = 'self::get()->count() ? false : true;';
177
        } elseif($value === 'true') {
178
            $str = 'true;';
179
        } elseif($value === 'false') {
180
            $str = 'false;';
181
        } else {
182
            $str = 'Permission::check(\''.$value.'\', \'any\', $member);';
183
        }
184
185
        return \DBField::create_field('Varchar', $str);
186
    }
187
188
189
    protected function createForm($formName, $actionTitle)
190
    {
191
        if($formName === 'PrimaryForm') {
192
            $isPrimary = true;
193
            $isSecond = false;
0 ignored issues
show
Unused Code introduced by
$isSecond is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
194
        } elseif($formName === 'SecondaryForm') {
195
            $isPrimary = false;
196
            $isSecond = true;
0 ignored issues
show
Unused Code introduced by
$isSecond is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
197
        } else {
198
            user_error('Set right form type: '.$formName.' is not valid');
199
        }
200
201
        $finalFields = \FieldList::create();
202
203
        if($isPrimary) {
0 ignored issues
show
Bug introduced by
The variable $isPrimary does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
204
            $toBuild = $this->primaryThingsToBuild();
205
            $finalFields->push(\HeaderField::create('Name your '.$this->myBaseClass));
206
            $finalFields->push(\TextField::create('Name', ''));
207
            $finalFields->push(\HeaderField::create('Extends'));
208
            $possibleExtensions = $this->myAPI()->PossibleRelationsWithBaseClass($this->myBaseClass);
209
            asort($possibleExtensions);
210
            $finalFields->push(
211
                \DropdownField::create(
212
                    'Extends',
213
                    '',
214
                    $possibleExtensions
215
                )
216
            );
217
            $additionalFields = $this->additionalPrimaryFields();
218
            foreach($additionalFields as $additionalField) {
219
                $finalFields->push($additionalField);
220
            }
221
        } else {
222
            $toBuild = $this->secondaryThingsToBuild();
223
        }
224
        $formFields = [];
225
        $formFieldsWithMultiple = [];
226
227
        $count = 0;
228
        //build fields ...
229
        foreach($toBuild as $item) {
230
            $name = $item[0];
231
            $sourceMethod1 = $item[1];
232
            $sourceMethod2 = $item[2];
233
            $isMultiple = $item[3];
234
235
236
            //work out style
237
            $hasKeyAndValue = false;
238
            if($sourceMethod1 && $sourceMethod2) {
239
                $hasKeyAndValue = true;
240
            }
241
            $formFields[$count] = [];
242
            if($isMultiple) {
243
                $max = 12;
244
            } else {
245
                $max = 1;
246
            }
247
            $formFields[$count][0] = [
248
                $name.'_HEADER',
249
                'HeaderField',
250
                $name
251
            ];
252
253
254
            //work out sources
255
            if($sourceMethod1 && $this->myAPI()->hasMethod($sourceMethod1)) {
256
                $source1 = $this->myAPI()->$sourceMethod1();
257
            } else {
258
                $source1 = null;
259
            }
260
            if($sourceMethod2 && $this->myAPI()->hasMethod($sourceMethod2)) {
261
                $source2 = $this->myAPI()->$sourceMethod2();
262
            } elseif($sourceMethod2) {
263
                $source2 = null;
264
            } else {
265
                $source2 = 'ignore';
266
            }
267
268
            //work out field names
269
270
            for($i = 1; $i <= $max; $i++) {
271
                if($hasKeyAndValue) {
272
                    if($isMultiple) {
273
                        $nameKey = $name.'__KEY__'.$i;
274
                        $nameValue = $name.'__VALUE__'.$i;
275
                        $formFieldsWithMultiple[$nameKey] = $nameKey;
276
                    } else {
277
                        $nameKey = $name.'__KEY__';
278
                        $nameValue = $name.'__VALUE__';
279
                    }
280
                } else {
281
                    if($isMultiple) {
282
                        $nameKey = $name.$i;
283
                        $nameValue = '';
284
                        $formFieldsWithMultiple[$nameKey] = $nameKey;
285
                    } else {
286
                        $nameKey = $name;
287
                        $nameValue = '';
288
                    }
289
                }
290
                if($hasKeyAndValue) {
291
                    //key field
292 View Code Duplication
                    if($source1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
293
                        $formFields[$count][$i]['KEY'] = [
294
                            $nameKey,
295
                            'DropdownField',
296
                            $source1
297
                        ];
298
                    } else {
299
                        $formFields[$count][$i]['KEY'] = [
300
                            $nameKey,
301
                            'TextField'
302
                        ];
303
                    }
304
305
                    //value field
306 View Code Duplication
                    if($source2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
307
                        $formFields[$count][$i]['VALUE'] = [
308
                            $nameValue,
309
                            'DropdownField',
310
                            $source2
311
                        ];
312
                    } else {
313
                        $formFields[$count][$i]['VALUE'] = [
314
                            $nameValue,
315
                            'TextField'
316
                        ];
317
                    }
318 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
319
                    //keys only!
320
                    if($source1) {
321
                        $formFields[$count][$i] = [
322
                            $nameKey,
323
                            'DropdownField',
324
                            $source1
325
                        ];
326
                    } else {
327
                        $formFields[$count][$i] = [
328
                            $nameKey,
329
                            'TextField'
330
                        ];
331
                    }
332
                }
333
            }
334
            if($i > 2) {
335
                $formFields[$count][$i + 1] = [
336
                    $name.'_ADD_'.$i,
337
                    'LiteralField',
338
                    '
339
                    <div class="CompositeField add-and-remove">
340
                        <a href="#" class="add first-add"><i class="material-icons">add_circle_outline</i></a>
341
                        <a href="#" class="remove"><i class="material-icons">remove_circle_outline</i></a>
342
                    </div>
343
                    '
344
                ];
345
            }
346
            $count++;
347
        }
348
        //create fields ...
349
        $count = 0;
350
        foreach($formFields as $outerCount => $subFieldList) {
351
            $count++;
352
            $compositeField = \CompositeField::create();
353
            $compositeField->addExtraClass('OuterComposite pos'.$count);
354
            $innerCount = 0;
355
            foreach($subFieldList as $innerCount => $fieldDetails) {
356
                $innerCount++;
357
                if(isset($fieldDetails['KEY']) && isset($fieldDetails['VALUE'])) {
358
                    $subCompositeField = \CompositeField::create();
359
                    $subCompositeField->addExtraClass('InnerComposite pos'.$innerCount);
360
                    foreach($fieldDetails as $fieldDetailsInner) {
361
                        $fieldName = $fieldDetailsInner[0];
362
                        $fieldType = $fieldDetailsInner[1];
363
                        $additionalClasses = [];
364
                        if(strpos($fieldName, '__KEY__')) {
365
                            $additionalClasses[] = 'mykey';
366
                        }
367
                        if(strpos($fieldName, '__VALUE__')) {
368
                            $additionalClasses[] = 'myvalue';
369
                        }
370
                        if(isset($fieldDetailsInner[2])) {
371
                            $source = $fieldDetailsInner[2];
372
                            asort($source);
373
                            $source = $this->prependNullOption( $source );
374
                            $tempField = $fieldType::create($fieldName, '', $source);
375
                        } else {
376
                            $tempField = $fieldType::create($fieldName, '');
377
                        }
378
                        if(count($additionalClasses)) {
379
                            $classes = implode(' ', $additionalClasses);
380
                            $tempField->addExtraClass($classes);
381
                        }
382
                        $subCompositeField->push($tempField);
383
                    }
384
                    $compositeField->push($subCompositeField);
385
                } else {
386
                    $fieldName = $fieldDetails[0];
387
                    if(isset($formFieldsWithMultiple[$fieldName])) {
388
                        $subCompositeField = \CompositeField::create();
389
                        $subCompositeField->addExtraClass('InnerComposite pos'.$innerCount);
390
                    } else {
391
                        $subCompositeField = null;
392
                    }
393
                    $fieldType = $fieldDetails[1];
394
                    if($fieldType === 'DropdownField') {
395
                        $source = $fieldDetails[2];
396
                        asort($source);
397
                        $source = $this->prependNullOption($source);
398
                        $myTempfield = $fieldType::create($fieldName, '', $source);
399
                    } elseif($fieldType === 'HeaderField') {
400
                        $title = str_replace('_', ' ', $fieldDetails[2]);
401
                        $myTempfield = $fieldType::create($fieldName, $title);
402
                    } elseif($fieldType === 'LiteralField') {
403
                        $title = $fieldDetails[2];
404
                        $myTempfield = $fieldType::create($fieldName, $title);
405
                    } else {
406
                        $myTempfield = $fieldType::create($fieldName, '');
407
                    }
408
                    if($subCompositeField) {
409
                        $subCompositeField->push($myTempfield);
410
                        $compositeField->push($subCompositeField);
411
                    } else {
412
                        $compositeField->push($myTempfield);
413
                    }
414
                }
415
            }
416
            $finalFields->push($compositeField);
417
        }
418
        $actions = \FieldList::create(
419
            [\FormAction::create('do'.strtolower($formName), $actionTitle)]
420
        );
421
422
        $form = \Form::create($this, $formName, $finalFields, $actions);
423
        $form->setFormAction($this->Link($formName));
424
        $form->loadDataFrom($this->retrieveData());
425
426
        return $form;
427
    }
428
429
    /**
430
     * returns an array of fields
431
     * @return array
432
     */
433
    protected function additionalPrimaryFields()
434
    {
435
        return [];
436
    }
437
438
    protected function saveData($name, $data)
439
    {
440
        $var = $this->Config()->get('form_data_session_variable');
441
        \Session::set($var.$name, $data);
442
        \Session::save();
443
    }
444
445
    private $_data = null;
446
447
    protected function retrieveData()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
448
    {
449
        if(! $this->_data) {
450
            $var = $this->Config()->get('form_data_session_variable');
451
            $retrieveDataPrimary = \Session::get($var.'_PrimaryForm');
452
            if ($retrieveDataPrimary && (is_array($retrieveDataPrimary) || is_object($retrieveDataPrimary))) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
453
                //do nothing
454
            } else {
455
                $retrieveDataPrimary = [];
456
            }
457
            $retrieveDataSecondary = \Session::get($var.'_SecondaryForm');
458
            if ($retrieveDataSecondary && (is_array($retrieveDataSecondary) || is_object($retrieveDataSecondary))) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
459
                //do nothing
460
            } else {
461
                $retrieveDataSecondary = [];
462
            }
463
            $this->_data = $retrieveDataPrimary + $retrieveDataSecondary;
464
        }
465
466
        return $this->_data;
467
    }
468
469
    private $_processed_data = null;
470
471
    protected function processedFormData($data = null) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
472
        if(! $this->_processed_data) {
473
            if(! $data) {
474
                $data = $this->retrieveData();
475
            }
476
            $array = [];
477
            foreach($data as $key => $value) {
478
                if($key && $value) {
479
                    if(
480
                        strpos($key, '__KEY__') ||
481
                        strpos($key, '__VALUE__')
482
                    ) {
483
                        $parts = explode('__', $key);
484
                        if(!isset($array[$parts[0]])) {
485
                            $array[$parts[0]] = [];
486
                        }
487
                        if(! isset($array[$parts[0]][$parts[2]])) {
488
                            $array[$parts[0]][$parts[2]] = [];
489
                        }
490
                        $array[$parts[0]][$parts[2]][$parts[1]] = $value;
491
                    } elseif(substr($key, 0, 3) === 'can') {
492
                        $array[$key] = $this->MyCanMethodBuilder($key, $value);
493
                    } else {
494
                        $array[$key] = $value;
495
                    }
496
                }
497
            }
498
            foreach($array as $field => $values) {
499
                $alInner = \ArrayList::create();
500
                if(is_array($values)) {
501
                    foreach($values as $key => $valuePairs) {
502
                        if(isset($valuePairs['KEY']) && isset($valuePairs['VALUE'])) {
503
                            if($valuePairs['VALUE'] == 'true') {
504
                                $valuePairArray = [
505
                                    'Key' => $valuePairs['KEY'],
506
                                    'UnquotedValue' => $valuePairs['VALUE'],
507
                                ];
508
                            } else {
509
                                $valuePairArray = [
510
                                    'Key' => $valuePairs['KEY'],
511
                                    'Value' => $valuePairs['VALUE'],
512
                                ];
513
                            }
514
                            $alInner->push(\ArrayData::create($valuePairArray));
515
                        }
516
                    }
517
                    $array[$field] = $alInner;
518
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
519
                    //do nothing
520
                }
521
            }
522
            $this->_processed_data = \ArrayData::create($array);
523
        }
524
525
        return $this->_processed_data;
526
    }
527
528
529
    protected function resultsTemplateForBuilder()
530
    {
531
        return str_replace(__NAMESPACE__ .'\\', '', $this->class).'Results';
532
    }
533
534
535
    protected function prependNullOption($source)
536
    {
537
        $source = ['' => '--- Please Select ---'] + $source;
538
539
        return $source;
540
    }
541
542
543
    protected function myAPI()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
544
    {
545
        $class = $this->apiProvider;
546
547
        return $class::inst($this->myBaseClass, $this->processedFormData());
548
    }
549
550
551
}
552