Driver_Plugin::change()   B
last analyzed

Complexity

Conditions 8
Paths 32

Size

Total Lines 61
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 24
nc 32
nop 1
dl 0
loc 61
rs 7.0047
c 0
b 0
f 0

How to fix   Long Method   

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
/**
4
 * Provides XML file handling for plugin schemas
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   XML
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\xml;
17
18
/**
19
 * Provides XML file handling for plugin schemas
20
 *
21
 * @category  Core
22
 * @package   XML
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Driver_Plugin extends Base
30
{
31
    /**
32
     * Determine the driver specific source file
33
     *
34
     * @param string $type Type of target, e.g. plugin
35
     * @param string $name Directory name of the target
36
     * @param string $lang Language if more than one is possible
37
     *
38
     * @return string
39
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
40
41
    protected function file($type, $name, $lang)
42
    {
43
        unset($lang);
44
45
        $file = '';
46
47
        if ($type == 'plugin') {
48
49
            $file = 'csphere/plugins/' . $name . '/plugin.xml';
50
        }
51
52
        return $file;
53
    }
54
55
    /**
56
     * Change data array for easier usage
57
     *
58
     * @param array $array Formated array generated earlier
59
     *
60
     * @return array
61
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
62
63
    protected function change(array $array)
64
    {
65
        // Shorten array depth for common elements
66
        $array = $this->common($array);
67
68
        // Shorten array depth for array elements
69
        $array['required'] = $array['required'][0];
70
71
        if (isset($array['required']['attr'][0]['php'])) {
72
73
            $array['required']['php'] = $array['required']['attr'][0]['php'];
74
        }
75
76
        // Check for required extensions
77
        if (empty($array['required']['extension'])) {
78
79
            $array['required']['extension'] = [];
80
        }
81
82
        // Shorten optional content if found
83
        if (!isset($array['environment'][0]['needed'])) {
84
85
            $array['environment'][0]['needed'] = [];
86
87
        } else {
88
89
            $env = [];
90
91
            foreach ($array['environment'][0]['needed'] AS $needed) {
92
93
                if (!isset($needed['version_max'])) {
94
95
                    $needed['version_max'] = '';
96
                }
97
98
                $env[] = $needed;
99
            }
100
101
            $array['environment'][0]['needed'] = $env;
102
        }
103
104
        if (!isset($array['environment'][0]['extend'])) {
105
106
            $array['environment'][0]['extend'] = [];
107
        }
108
109
        // Handle special case for entries
110
        if (!isset($array['entries'][0]['target'])) {
111
112
            $array['entries'][0]['target'] = [];
113
        }
114
115
        $array['entries'] = $array['entries'][0];
116
117
        $array = $this->_changeAccess($array);
118
119
        // Set icon URL
120
        $array['icon']['url'] = '';
121
122
        return $array;
123
    }
124
125
    /**
126
     * Change data array for easier usage, special part for changing the access part
127
     *
128
     * @param array $array Formated array generated earlier
129
     *
130
     * @return array
131
     **/
132
    private function _changeAccess($array)
133
    {
134
        // Handle access rules
135
        if (!isset($array['routes'][0])) {
136
137
            $array['routes'] = [];
138
139
        } else {
140
141
            $env = [];
142
143
            foreach ($array['routes'][0]['define'] AS $action) {
144
                $env[$action['value']] = $action['type'];
145
            }
146
147
            $array['routes'] = $env;
148
        }
149
150
        return $array;
151
    }
152
}
153