Passed
Push — master ( 40899a...60cef9 )
by Andreas
22:38
created

midgard_admin_asgard_copytree::_is_collapsed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midgard.admin.asgard
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * Copy/delete tree branch viewer
11
 *
12
 * @package midgard.admin.asgard
13
 */
14
class midgard_admin_asgard_copytree extends midgard_admin_asgard_navigation
15
{
16
    /**
17
     * Switch to determine if the whole tree should be copied
18
     *
19
     * @var boolean
20
     */
21
    public $copy_tree = false;
22
23
    /**
24
     * Switch to determine the visibility of inputs
25
     *
26
     * @var boolean
27
     */
28
    public $inputs = true;
29
30
    /**
31
     * Choose the target type
32
     *
33
     * @var String
34
     */
35
    public $input_type;
36
37
    /**
38
     * Choose the target name for the form
39
     *
40
     * @var String
41
     */
42
    public $input_name;
43
44
    /**
45
     * Show the link to view the object
46
     *
47
     * @var boolean
48
     */
49
    public $view_link = false;
50
51
    /**
52
     * Show the link to view the object
53
     *
54
     * @var boolean
55
     */
56
    public $edit_link = false;
57
58
    /**
59
     * Page prefix
60
     *
61
     * @var String
62
     */
63
    public $page_prefix = '';
64
65
    /**
66
     * Constructor, connect to the parent class constructor.
67
     *
68
     * @param mixed $object
69
     * @param array $request_data
70
     */
71 2
    public function __construct($object, &$request_data)
72
    {
73 2
        parent::__construct($object, $request_data);
74 2
        $this->page_prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
0 ignored issues
show
Documentation Bug introduced by
It seems like midcom_core_context::get...M_CONTEXT_ANCHORPREFIX) can also be of type false. However, the property $page_prefix is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75 2
    }
76
77 1
    protected function _draw_element($object, string $label, int $level, bool $autoexpand = false)
78
    {
79 1
        $ref = midcom_helper_reflector_tree::get($object);
80 1
        $span_class = '';
81 1
        $css_class = $this->get_css_classes($object, $ref->mgdschema_class);
0 ignored issues
show
Bug introduced by
It seems like $ref->mgdschema_class can also be of type null; however, parameter $mgdschema_class of midgard_admin_asgard_navigation::get_css_classes() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        $css_class = $this->get_css_classes($object, /** @scrutinizer ignore-type */ $ref->mgdschema_class);
Loading history...
82 1
        $this->shown_objects[$object->guid] = true;
83
84 1
        echo "<li class=\"{$css_class}\">\n";
85
86 1
        $label = htmlspecialchars($ref->get_object_label($object));
87 1
        $icon = $ref->get_object_icon($object);
88 1
        if (empty($label)) {
89
            $label = "#{$object->id}";
90
        }
91
92 1
        $checked = ($this->copy_tree) ? ' checked="checked"' : '';
93
94 1
        if ($this->inputs) {
95
            // This value is used for compiling the exclusion list: if the object is found from this list, but not from the selection list,
96
            // it means that the selection did not include the object GUID
97 1
            echo "<input type=\"hidden\" name=\"all_objects[]\" value=\"{$object->guid}\" />\n";
98
99 1
            echo "<label for=\"item_{$object->guid}\">\n";
100 1
            echo "<input id=\"item_{$object->guid}\" type=\"{$this->input_type}\" name=\"{$this->input_name}\" value=\"{$object->guid}\"{$checked} />\n";
101
        }
102
103 1
        echo "<span class=\"title{$span_class}\" title=\"" . sprintf($this->_l10n->get('%s (%s)'), $label, $ref->get_class_label()) . "\">{$icon}{$label}</span>\n";
104
105
        // Show the link to the object
106 1
        if ($this->view_link) {
107 1
            echo "<a href=\"{$this->page_prefix}__mfa/asgard/object/view/{$object->guid}/\" class=\"thickbox\" target=\"_blank\" title=\"" . $this->_l10n->get('view object') . "\">\n";
108 1
            echo "<i class=\"fa fa-eye\"></i>\n";
109 1
            echo "</a>\n";
110
        }
111
112
        // Show the link to the object
113 1
        if ($this->edit_link) {
114
            echo "<a href=\"{$this->page_prefix}__mfa/asgard/object/edit/{$object->guid}/\" target='_blank' title=\"" . $this->_l10n->get('edit object') . "\">\n";
115
            echo "<i class=\"fa fa-pencil\"> </i>\n";
116
            echo "</a>\n";
117
        }
118
119 1
        if ($this->inputs) {
120 1
            echo "</label>\n";
121
        }
122
123
        // List the child elements
124 1
        $this->_list_child_elements($object, $level + 1);
125
126 1
        echo "</li>\n";
127 1
    }
128
129 1
    protected function _is_collapsed($type, $total) : bool
130
    {
131 1
        if ($this->inputs) {
132 1
            return false;
133
        }
134
        return parent::_is_collapsed($type, $total);
135
    }
136
137
    /**
138
     * Draw the tree selector
139
     */
140 2
    public function draw()
141
    {
142 2
        if (!$this->input_type) {
143 2
            $this->input_type = 'checkbox';
144
        }
145
146 2
        if (!$this->input_name) {
147 2
            if ($this->input_type === 'checkbox') {
148 2
                $this->input_name = 'selected[]';
149
            } else {
150
                $this->input_name = 'target';
151
            }
152
        }
153
154 2
        $root_object = $this->_object;
155 2
        $this->_list_child_elements($root_object);
156 2
    }
157
}
158