elFinderPluginNormalizer::normalize()   F
last analyzed

Complexity

Conditions 18
Paths 270

Size

Total Lines 43
Code Lines 27

Duplication

Lines 6
Ratio 13.95 %

Importance

Changes 0
Metric Value
cc 18
eloc 27
nc 270
nop 2
dl 6
loc 43
rs 3.6714
c 0
b 0
f 0

How to fix   Complexity   

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
 * elFinder Plugin Normalizer.
4
 *
5
 * UTF-8 Normalizer of file-name and file-path etc.
6
 * nfc(NFC): Canonical Decomposition followed by Canonical Composition
7
 * nfkc(NFKC): Compatibility Decomposition followed by Canonical
8
 *
9
 * This plugin require Class "Normalizer" (PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
10
 * or PEAR package "I18N_UnicodeNormalizer"
11
 *
12
 * ex. binding, configure on connector options
13
 *	$opts = array(
14
 *		'bind' => array(
15
 *			'upload.pre mkdir.pre mkfile.pre rename.pre archive.pre ls.pre' => array(
16
 *				'Plugin.Normalizer.cmdPreprocess'
17
 *			),
18
 *			'ls' => array(
19
 *				'Plugin.Normalizer.cmdPostprocess'
20
 *			),
21
 *			'upload.presave' => array(
22
 *				'Plugin.Normalizer.onUpLoadPreSave'
23
 *			)
24
 *		),
25
 *		// global configure (optional)
26
 *		'plugin' => array(
27
 *			'Normalizer' => array(
28
 *				'enable'    => true,
29
 *				'nfc'       => true,
30
 *				'nfkc'      => true,
31
 *				'umlauts'   => false,
32
 *				'lowercase' => false,
33
 * 				'convmap'   => array()
34
 *			)
35
 *		),
36
 *		// each volume configure (optional)
37
 *		'roots' => array(
38
 *			array(
39
 *				'driver' => 'LocalFileSystem',
40
 *				'path'   => '/path/to/files/',
41
 *				'URL'    => 'http://localhost/to/files/'
42
 *				'plugin' => array(
43
 *					'Normalizer' => array(
44
 *						'enable'    => true,
45
 *						'nfc'       => true,
46
 *						'nfkc'      => true,
47
 *						'umlauts'   => false,
48
 * 						'lowercase' => false,
49
 * 						'convmap'   => array()
50
 *					)
51
 *				)
52
 *			)
53
 *		)
54
 *	);
55
 *
56
 * @author Naoki Sawada
57
 * @license New BSD
58
 */
59
class elFinderPluginNormalizer extends elFinderPlugin
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
60
{
61
    private $replaced = [];
62
    private $keyMap = [
63
        'ls' => 'intersect',
64
        'upload' => 'renames',
65
    ];
66
67
    public function __construct($opts)
68
    {
69
        $defaults = [
70
            'enable' => true,  // For control by volume driver
71
            'nfc' => true,  // Canonical Decomposition followed by Canonical Composition
72
            'nfkc' => true,  // Compatibility Decomposition followed by Canonical
73
            'umlauts' => false, // Convert umlauts with their closest 7 bit ascii equivalent
74
            'lowercase' => false, // Make chars lowercase
75
            'convmap' => [], // Convert map ('FROM' => 'TO') array
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
        ];
77
78
        $this->opts = array_merge($defaults, $opts);
79
    }
80
81 View Code Duplication
    public function cmdPreprocess($cmd, &$args, $elfinder, $volume)
0 ignored issues
show
Unused Code introduced by
The parameter $elfinder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $opts = $this->getCurrentOpts($volume);
84
        if (! $opts['enable']) {
85
            return false;
86
        }
87
        $this->replaced[$cmd] = [];
88
        $key = (isset($this->keyMap[$cmd])) ? $this->keyMap[$cmd] : 'name';
89
90
        if (isset($args[$key])) {
91
            if (is_array($args[$key])) {
92
                foreach ($args[$key] as $i => $name) {
93
                    $this->replaced[$cmd][$name] = $args[$key][$i] = $this->normalize($name, $opts);
94
                }
95
            } else {
96
                $name = $args[$key];
97
                $this->replaced[$cmd][$name] = $args[$key] = $this->normalize($name, $opts);
98
            }
99
        }
100
101
        return true;
102
    }
103
104 View Code Duplication
    public function cmdPostprocess($cmd, &$result, $args, $elfinder)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $elfinder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        if ($cmd === 'ls') {
107
            if (! empty($result['list']) && ! empty($this->replaced['ls'])) {
108
                foreach ($result['list'] as $hash => $name) {
109
                    if ($keys = array_keys($this->replaced['ls'], $name)) {
110
                        if (count($keys) === 1) {
111
                            $result['list'][$hash] = $keys[0];
112
                        } else {
113
                            $result['list'][$hash] = $keys;
114
                        }
115
                    }
116
                }
117
            }
118
        }
119
    }
120
121
    // NOTE: $thash is directory hash so it unneed to process at here
122 View Code Duplication
    public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume)
0 ignored issues
show
Unused Code introduced by
The parameter $thash is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $src is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $elfinder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        $opts = $this->getCurrentOpts($volume);
125
        if (! $opts['enable']) {
126
            return false;
127
        }
128
129
        $name = $this->normalize($name, $opts);
130
131
        return true;
132
    }
133
134
    private function normalize($str, $opts)
135
    {
136
        if ($opts['nfc'] || $opts['nfkc']) {
137
            if (class_exists('Normalizer', false)) {
138 View Code Duplication
                if ($opts['nfc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_C)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
                    $str = Normalizer::normalize($str, Normalizer::FORM_C);
140
                }
141 View Code Duplication
                if ($opts['nfkc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_KC)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
                    $str = Normalizer::normalize($str, Normalizer::FORM_KC);
143
                }
144
            } else {
145
                if (! class_exists('I18N_UnicodeNormalizer', false)) {
146
                    include_once 'I18N/UnicodeNormalizer.php';
147
                }
148
                if (class_exists('I18N_UnicodeNormalizer', false)) {
149
                    $normalizer = new I18N_UnicodeNormalizer();
150
                    if ($opts['nfc']) {
151
                        $str = $normalizer->normalize($str, 'NFC');
152
                    }
153
                    if ($opts['nfkc']) {
154
                        $str = $normalizer->normalize($str, 'NFKC');
155
                    }
156
                }
157
            }
158
        }
159
        if ($opts['umlauts']) {
160
            if (strpos($str = htmlentities($str, ENT_QUOTES, 'UTF-8'), '&') !== false) {
161
                $str = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i', '$1', $str), ENT_QUOTES, 'utf-8');
162
            }
163
        }
164
        if ($opts['convmap'] && is_array($opts['convmap'])) {
165
            $str = strtr($str, $opts['convmap']);
166
        }
167
        if ($opts['lowercase']) {
168
            if (function_exists('mb_strtolower')) {
169
                $str = mb_strtolower($str, 'UTF-8');
170
            } else {
171
                $str = strtolower($str);
172
            }
173
        }
174
175
        return $str;
176
    }
177
}
178