Test Setup Failed
Pull Request — master (#190)
by
unknown
08:43
created

privilege   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaultOptions() 0 4 1
A configureOptions() 0 13 1
A buildView() 0 6 1
B get_effective_value() 0 21 5
A render_choices() 0 18 3
A search_for_object() 0 20 4
A getName() 0 4 1
A getBlockPrefix() 0 4 1
A getParent() 0 4 1
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\extension\type;
7
8
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
12
use midcom\datamanager\extension\compat;
13
use Symfony\Component\Form\Extension\Core\Type\RadioType;
14
use midcom;
15
use midcom_core_user;
16
use Symfony\Component\Form\FormView;
17
use Symfony\Component\Form\FormInterface;
18
use midcom\datamanager\storage\container\dbacontainer;
19
/**
20
 * Experimental privilege type
21
 */
22
23
class privilege extends RadioType
24
{
25
    public $res;
26
27
    public $defaultChoices = array(
28
                'widget privilege: allow' => MIDCOM_PRIVILEGE_ALLOW,
29
                'widget privilege: deny' => MIDCOM_PRIVILEGE_DENY,
30
                'widget privilege: inherit' => MIDCOM_PRIVILEGE_INHERIT,
31
            );
32
    /**
33
     *  Symfony 2.6 compat
34
     *
35
     * {@inheritDoc}
36
     */
37
    public function setDefaultOptions(OptionsResolverInterface $resolver)
38
    {
39
        $this->configureOptions($resolver);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function configureOptions(OptionsResolver $resolver)
46
    {
47
        $this->res = $resolver;
48
        $map_privilege = function (Options $options) {
49
            $return_options = $this->defaultChoices;
50
            return $return_options;
51
        };
52
        $resolver->setDefaults(array(
53
            'choices' => $map_privilege,
54
            'choices_as_values' => true,
55
            'expanded' => true,
56
        ));
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function buildView(FormView $view, FormInterface $form, array $options)
63
    {
64
        parent::buildView($view, $form, $options);
65
        $view->vars['privilege'] = $this;
66
        $view->vars['type_conf'] = $options['type_config'];
67
    }
68
69
    public function get_effective_value(array $options,$object = null)
70
    {
71
        if (!$object)
72
        {
73
            $defaults = midcom::get()->auth->acl->get_default_privileges();
74
            return(($defaults[$options['privilege_name']] === MIDCOM_PRIVILEGE_ALLOW));
75
        }
76
        if ($options['assignee']== 'SELF') {
77
            if ($object instanceof midcom_db_group) {
78
                //There's no sane way to query group privileges in auth right now, so we only return defaults
79
                $defaults = midcom::get()->auth->acl->get_default_privileges();
80
                return(($defaults[$options['privilege_name']] === MIDCOM_PRIVILEGE_ALLOW));
81
            }
82
            return(midcom::get()->auth->can_user_do($options['privilege_name'],
83
                    new midcom_core_user($object->__object->id),$options['classname']));
84
        }
85
        if ($principal = midcom::get()->auth->get_assignee($options['assignee'])) {
86
            return $object->can_do($options['privilege_name'], $principal);
87
        }
88
        return $object->can_do($options['privilege_name'], $options['assignee']);
89
    }
90
91
    public function render_choices(array $options,$object = null)
92
    {
93
            $l10n = midcom::get()->i18n->get_l10n('midcom.datamanager');
94
            $inherit = $this->get_effective_value($options,$object);
95
            $allow = 'widget privilege: allow';
96
            $deny = 'widget privilege: deny';
97
98
            if ($inherit === true) {
99
                $base = $l10n->get('widget privilege: inherit %s');
100
                $allow = $l10n->get($allow);
101
                $d = sprintf($base,$allow);
102
            } elseif ($inherit === false) {
103
                $base = $l10n->get('widget privilege: inherit %s');
104
                $deny = $l10n->get($deny);
105
                $d = sprintf($base,$deny);
106
            }
107
            return $d;
0 ignored issues
show
Bug introduced by
The variable $d 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...
108
    }
109
110
    public function search_for_object($object)
111
    {
112
        $object_found = false;
113
        $help_obj = $object;
114
115
        while (!$object_found)
116
        {
117
            if ($help_obj instanceof dbacontainer) {
118
                $object_found = true;
0 ignored issues
show
Unused Code introduced by
$object_found 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...
119
                return $help_obj->get_value();
120
            } else {
121
                if (!empty($help_obj->parent)) {
122
                    $help_obj = $help_obj->parent->vars['data'];
123
                } else {
124
                    return null;
125
                }
126
            }
127
        }
128
        return null;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     *
134
     * Symfony < 2.8 compat
135
     */
136
    public function getName()
137
    {
138
        return $this->getBlockPrefix();
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getBlockPrefix()
145
    {
146
        return 'privilege';
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getParent()
153
    {
154
        return compat::get_type_name('radiocheckselect');
155
    }
156
}