Passed
Push — master ( 03d969...bf76ef )
by Vitalii
08:57 queued 16s
created

sb_file_select()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 9
c 1
b 0
f 1
nc 6
nop 2
dl 0
loc 12
rs 9.6111
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
    if (!$form_desc) {
25
        error_page('parse error in JSON file');
26
    }
27
    page_head_select2($form_desc->title);
28
    form_start($form_desc->handler);
29
    foreach ($form_desc->fields as $field) {
30
        switch ($field->type) {
31
        case 'text':
32
        case 'integer':
33
        case 'float':
34
            form_input_text($field->title, $field->name);
35
            break;
36
        case 'file_select':
37
            $items = sb_file_select($user, $field->suffix);
38
            form_select($field->title, $field->name, $items);
39
            break;
40
        case 'file_select_multi':
41
            $items = sb_file_select($user, $field->suffix);
42
            form_select2_multi(
43
                $field->title, $field->name, $items, null, "id=$field->name"
44
            );
45
            break;
46
        case 'select':
47
            form_select($field->title, $field->name, $field->items);
48
            break;
49
        case 'select_multi':
50
            form_select2_multi(
51
                $field->title, $field->name, $field->items,
52
                null, "id=$field->name"
53
            );
54
            break;
55
        default:
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
56
            echo "unknown field type $field->type";
57
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
58
        case 'radio':
0 ignored issues
show
Coding Style introduced by
Empty CASE statements are not allowed
Loading history...
59
            break;
60
        }
61
    }
62
    form_submit('OK');
63
    form_end();
64
    page_tail();
65
}
66
67
$json_fname = get_str('json_fname');
68
if (!is_valid_filename($json_fname)) {
69
    error_page('filename');
70
}
71
$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...
72
form($user, $json_fname);
73
?>
74