Failed Conditions
Push — master ( 5c0b2e...874fc8 )
by Andreas
32:37 queued 29:04
created

DokuWiki_PluginTrait   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 232
rs 8.3396
c 0
b 0
f 0
wmc 44
lcom 1
cbo 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getInfo() 0 15 2
A isSingleton() 0 3 1
A loadHelper() 0 5 3
A getPluginType() 0 4 1
A getPluginName() 0 4 1
A getPluginComponent() 0 4 2
A getLang() 0 5 3
A locale_xhtml() 0 3 1
A localFN() 0 13 3
C setupLocale() 0 28 7
A getConf() 0 12 3
A loadConfig() 0 14 3
A readDefaultSettings() 0 11 2
A email() 0 7 4
B external_link() 0 14 7
A render_text() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like DokuWiki_PluginTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DokuWiki_PluginTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Do not inherit directly from this class, instead inherit from the specialized
5
 * ones in lib/plugin
6
 */
7
trait DokuWiki_PluginTrait {
8
9
    protected $localised = false;        // set to true by setupLocale() after loading language dependent strings
10
    protected $lang = array();           // array to hold language dependent strings, best accessed via ->getLang()
11
    protected $configloaded = false;     // set to true by loadConfig() after loading plugin configuration variables
12
    protected $conf = array();           // array to hold plugin settings, best accessed via ->getConf()
13
14
    /**
15
     * @see DokuWiki_PluginInterface::getInfo()
16
     */
17
    public function getInfo() {
18
        $parts = explode('_', get_class($this));
19
        $info = DOKU_PLUGIN . '/' . $parts[2] . '/plugin.info.txt';
20
        if(file_exists($info)) return confToHash($info);
21
22
        msg(
23
            'getInfo() not implemented in ' . get_class($this) . ' and ' . $info . ' not found.<br />' .
24
            'Verify you\'re running the latest version of the plugin. If the problem persists, send a ' .
25
            'bug report to the author of the ' . $parts[2] . ' plugin.', -1
26
        );
27
        return array(
28
            'date' => '0000-00-00',
29
            'name' => $parts[2] . ' plugin',
30
        );
31
    }
32
33
    /**
34
     * @see DokuWiki_PluginInterface::isSingleton()
35
     */
36
    public function isSingleton() {
37
        return true;
38
    }
39
40
    /**
41
     * @see DokuWiki_PluginInterface::loadHelper()
42
     */
43
    public function loadHelper($name, $msg = true) {
44
        $obj = plugin_load('helper', $name);
45
        if(is_null($obj) && $msg) msg("Helper plugin $name is not available or invalid.", -1);
46
        return $obj;
47
    }
48
49
    // region introspection methods
50
51
    /**
52
     * @see DokuWiki_PluginInterface::getPluginType()
53
     */
54
    public function getPluginType() {
55
        list($t) = explode('_', get_class($this), 2);
56
        return $t;
57
    }
58
59
    /**
60
     * @see DokuWiki_PluginInterface::getPluginName()
61
     */
62
    public function getPluginName() {
63
        list(/* $t */, /* $p */, $n) = explode('_', get_class($this), 4);
64
        return $n;
65
    }
66
67
    /**
68
     * @see DokuWiki_PluginInterface::getPluginComponent()
69
     */
70
    public function getPluginComponent() {
71
        list(/* $t */, /* $p */, /* $n */, $c) = explode('_', get_class($this), 4);
72
        return (isset($c) ? $c : '');
73
    }
74
75
    // endregion
76
    // region localization methods
77
78
    /**
79
     * @see DokuWiki_PluginInterface::getLang()
80
     */
81
    public function getLang($id) {
82
        if(!$this->localised) $this->setupLocale();
83
84
        return (isset($this->lang[$id]) ? $this->lang[$id] : '');
85
    }
86
87
    /**
88
     * @see DokuWiki_PluginInterface::locale_xhtml()
89
     */
90
    public function locale_xhtml($id) {
91
        return p_cached_output($this->localFN($id));
92
    }
93
94
    /**
95
     * @see DokuWiki_PluginInterface::localFN()
96
     */
97
    public function localFN($id, $ext = 'txt') {
98
        global $conf;
99
        $plugin = $this->getPluginName();
100
        $file = DOKU_CONF . 'plugin_lang/' . $plugin . '/' . $conf['lang'] . '/' . $id . '.' . $ext;
101
        if(!file_exists($file)) {
102
            $file = DOKU_PLUGIN . $plugin . '/lang/' . $conf['lang'] . '/' . $id . '.' . $ext;
103
            if(!file_exists($file)) {
104
                //fall back to english
105
                $file = DOKU_PLUGIN . $plugin . '/lang/en/' . $id . '.' . $ext;
106
            }
107
        }
108
        return $file;
109
    }
110
111
    /**
112
     * @see DokuWiki_PluginInterface::setupLocale()
113
     */
114
    public function setupLocale() {
115
        if($this->localised) return;
116
117
        global $conf, $config_cascade; // definitely don't invoke "global $lang"
118
        $path = DOKU_PLUGIN . $this->getPluginName() . '/lang/';
119
120
        $lang = array();
121
122
        // don't include once, in case several plugin components require the same language file
123
        @include($path . 'en/lang.php');
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
124
        foreach($config_cascade['lang']['plugin'] as $config_file) {
125
            if(file_exists($config_file . $this->getPluginName() . '/en/lang.php')) {
126
                include($config_file . $this->getPluginName() . '/en/lang.php');
127
            }
128
        }
129
130
        if($conf['lang'] != 'en') {
131
            @include($path . $conf['lang'] . '/lang.php');
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
132
            foreach($config_cascade['lang']['plugin'] as $config_file) {
133
                if(file_exists($config_file . $this->getPluginName() . '/' . $conf['lang'] . '/lang.php')) {
134
                    include($config_file . $this->getPluginName() . '/' . $conf['lang'] . '/lang.php');
135
                }
136
            }
137
        }
138
139
        $this->lang = $lang;
140
        $this->localised = true;
141
    }
142
143
    // endregion
144
    // region configuration methods
145
146
    /**
147
     * @see DokuWiki_PluginInterface::getConf()
148
     */
149
    public function getConf($setting, $notset = false) {
150
151
        if(!$this->configloaded) {
152
            $this->loadConfig();
153
        }
154
155
        if(isset($this->conf[$setting])) {
156
            return $this->conf[$setting];
157
        } else {
158
            return $notset;
159
        }
160
    }
161
162
    /**
163
     * @see DokuWiki_PluginInterface::loadConfig()
164
     */
165
    public function loadConfig() {
166
        global $conf;
167
168
        $defaults = $this->readDefaultSettings();
169
        $plugin = $this->getPluginName();
170
171
        foreach($defaults as $key => $value) {
172
            if(isset($conf['plugin'][$plugin][$key])) continue;
173
            $conf['plugin'][$plugin][$key] = $value;
174
        }
175
176
        $this->configloaded = true;
177
        $this->conf =& $conf['plugin'][$plugin];
178
    }
179
180
    /**
181
     * read the plugin's default configuration settings from conf/default.php
182
     * this function is automatically called through getConf()
183
     *
184
     * @return    array    setting => value
185
     */
186
    protected function readDefaultSettings() {
187
188
        $path = DOKU_PLUGIN . $this->getPluginName() . '/conf/';
189
        $conf = array();
190
191
        if(file_exists($path . 'default.php')) {
192
            include($path . 'default.php');
193
        }
194
195
        return $conf;
196
    }
197
198
    // endregion
199
    // region output methods
200
201
    /**
202
     * @see DokuWiki_PluginInterface::email()
203
     */
204
    public function email($email, $name = '', $class = '', $more = '') {
205
        if(!$email) return $name;
206
        $email = obfuscate($email);
207
        if(!$name) $name = $email;
208
        $class = "class='" . ($class ? $class : 'mail') . "'";
209
        return "<a href='mailto:$email' $class title='$email' $more>$name</a>";
210
    }
211
212
    /**
213
     * @see DokuWiki_PluginInterface::external_link()
214
     */
215
    public function external_link($link, $title = '', $class = '', $target = '', $more = '') {
216
        global $conf;
217
218
        $link = htmlentities($link);
219
        if(!$title) $title = $link;
220
        if(!$target) $target = $conf['target']['extern'];
221
        if($conf['relnofollow']) $more .= ' rel="nofollow"';
222
223
        if($class) $class = " class='$class'";
224
        if($target) $target = " target='$target'";
225
        if($more) $more = " " . trim($more);
226
227
        return "<a href='$link'$class$target$more>$title</a>";
228
    }
229
230
    /**
231
     * @see DokuWiki_PluginInterface::render_text()
232
     */
233
    public function render_text($text, $format = 'xhtml') {
234
        return p_render($format, p_get_instructions($text), $info);
235
    }
236
237
    // endregion
238
}
239