elFinderPluginNormalizer   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 87
Duplicated Lines 49.43 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 27
lcom 1
cbo 0
dl 43
loc 87
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B cmdPreprocess() 17 17 5
A onUpLoadPreSave() 12 12 3
A getOpts() 10 10 3
C normalize() 4 28 15

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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' => array(
16
 *				'Plugin.Normalizer.cmdPreprocess'
17
 *			),
18
 *			'upload.presave' => array(
19
 *				'Plugin.Normalizer.onUpLoadPreSave'
20
 *			)
21
 *		),
22
 *		// global configure (optional)
23
 *		'plugin' => array(
24
 *			'Normalizer' => array(
25
 *				'enable'    => true,
26
 *				'nfc'       => true,
27
 *				'nfkc'      => true,
28
 *				'lowercase' => false,
29
 * 				'convmap'   => array()
30
 *			)
31
 *		),
32
 *		// each volume configure (optional)
33
 *		'roots' => array(
34
 *			array(
35
 *				'driver' => 'LocalFileSystem',
36
 *				'path'   => '/path/to/files/',
37
 *				'URL'    => 'http://localhost/to/files/'
38
 *				'plugin' => array(
39
 *					'Normalizer' => array(
40
 *						'enable'    => true,
41
 *						'nfc'       => true,
42
 *						'nfkc'      => true,
43
 * 						'lowercase' => false,
44
 * 						'convmap'   => array()
45
 *					)
46
 *				)
47
 *			)
48
 *		)
49
 *	);
50
 *
51
 * @package elfinder
52
 * @author Naoki Sawada
53
 * @license New BSD
54
 */
55
class elFinderPluginNormalizer
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...
56
{
57
	private $opts = array();
58
	
59
	public function __construct($opts) {
60
		$defaults = array(
61
			'enable'    => true,  // For control by volume driver
62
			'nfc'       => true,  // Canonical Decomposition followed by Canonical Composition
63
			'nfkc'      => true,  // Compatibility Decomposition followed by Canonical
64
			'lowercase' => false, // Make chars lowercase
65
			'convmap'   => array()// 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...
66
		);
67
	
68
		$this->opts = array_merge($defaults, $opts);
69
	}
70
	
71 View Code Duplication
	public function cmdPreprocess($cmd, &$args, $elfinder, $volume) {
0 ignored issues
show
Unused Code introduced by
The parameter $cmd 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...
72
		$opts = $this->getOpts($volume);
73
		if (! $opts['enable']) {
74
			return false;
75
		}
76
		
77
		if (isset($args['name'])) {
78
			if (is_array($args['name'])) {
79
				foreach($args['name'] as $i => $name) {
80
					$args['name'][$i] = $this->normalize($name, $opts);
81
				}
82
			} else {
83
				$args['name'] = $this->normalize($args['name'], $opts);
84
			}
85
		}
86
		return true;
87
	}
88
	
89 View Code Duplication
	public function onUpLoadPreSave(&$path, &$name, $src, $elfinder, $volume) {
0 ignored issues
show
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...
90
		$opts = $this->getOpts($volume);
91
		if (! $opts['enable']) {
92
			return false;
93
		}
94
		
95
		if ($path) {
96
			$path = $this->normalize($path, $opts);
97
		}
98
		$name = $this->normalize($name, $opts);
99
		return true;
100
	}
101
	
102 View Code Duplication
	private function getOpts($volume) {
0 ignored issues
show
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...
103
		$opts = $this->opts;
104
		if (is_object($volume)) {
105
			$volOpts = $volume->getOptionsPlugin('Normalizer');
106
			if (is_array($volOpts)) {
107
				$opts = array_merge($this->opts, $volOpts);
108
			}
109
		}
110
		return $opts;
111
	}
112
	
113
	private function normalize($str, $opts) {
114
		if ($opts['nfc'] || $opts['nfkc']) {
115
			if (class_exists('Normalizer', false)) {
116 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...
117
					$str = Normalizer::normalize($str, Normalizer::FORM_C);
118 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...
119
					$str = Normalizer::normalize($str, Normalizer::FORM_KC);
120
			} else {
121
				if (! class_exists('I18N_UnicodeNormalizer', false)) {
122
					@ include_once 'I18N/UnicodeNormalizer.php';
0 ignored issues
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...
123
				}
124
				if (class_exists('I18N_UnicodeNormalizer', false)) {
125
					$normalizer = new I18N_UnicodeNormalizer();
126
					if ($opts['nfc'])
127
						$str = $normalizer->normalize($str, 'NFC');
128
					if ($opts['nfkc'])
129
						$str = $normalizer->normalize($str, 'NFKC');
130
				}
131
			}
132
		}
133
		if ($opts['lowercase']) {
134
			$str = strtolower($str);
135
		}
136
		if ($opts['convmap'] && is_array($opts['convmap'])) {
137
			$str = strtr($str, $opts['convmap']);
138
		}
139
		return $str;
140
	}
141
}
142