Passed
Push — dpa_json_form ( 96f4b2 )
by David
09:41
created

form()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 37
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 32
c 1
b 0
f 1
nc 10
nop 2
dl 0
loc 37
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// generate a form from a JSON spec
4
5
require_once('../inc/util.inc');
6
require_once('../inc/sandbox.inc');
7
8
function sb_file_select($user, $suffix=null) {
9
    $sbfiles = sandbox_file_names($user);
10
    $sbitems = [];
11
    $exp = null;
12
    if ($suffix) {
13
        $exp = sprintf('/%s$/', $suffix);
14
    }
15
    foreach ($sbfiles as $f) {
16
        if ($exp && !preg_match($exp, $f)) continue;
17
        $sbitems[] = [$f, $f];
18
    }
19
    return $sbitems;
20
}
21
22
function form($user, $json_fname) {
23
    $form_desc = json_decode(file_get_contents($json_fname));
24
    page_head_select2($form_desc->title);
25
    form_start($form_desc->handler);
26
    foreach ($form_desc->fields as $field) {
27
        switch ($field->type) {
28
        case 'text':
29
        case 'integer':
30
        case 'float':
31
            form_input_text($field->title, $field->name);
32
            break;
33
        case 'file_select':
34
            $items = sb_file_select($user, $field->suffix);
35
            form_select($field->title, $field->name, $items);
36
            break;
37
        case 'file_select_multi':
38
            $items = sb_file_select($user, $field->suffix);
39
            form_select2_multi(
40
                $field->title, $field->name, $items, null, "id=$field->name"
41
            );
42
            break;
43
        case 'select':
44
            form_select($field->title, $field->name, $field->items);
45
            break;
46
        case 'select_multi':
47
            form_select2_multi(
48
                $field->title, $field->name, $field->items,
49
                null, "id=$field->name"
50
            );
51
            break;
52
        case 'radio':
0 ignored issues
show
Coding Style introduced by
Empty CASE statements are not allowed
Loading history...
53
            break;
54
        }
55
    }
56
    form_submit('OK');
57
    form_end();
58
    page_tail();
59
}
60
61
$json_fname = get_str('json_fname');
62
$user = get_logged_in_user();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as get_logged_in_user() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
form($user, $json_fname);
64
?>
65