Passed
Branch php-cs-fixer (b9836a)
by Fabio
15:02
created

parseDSN()   F

Complexity

Conditions 21
Paths 3463

Size

Total Lines 113
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 70
nc 3463
nop 1
dl 0
loc 113
rs 2
c 0
b 0
f 0

How to fix   Long Method    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
/**
4
 * I18N Utility file.
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the BSD License.
8
 *
9
 * Copyright(c) 2004 by Wei Zhuo. All rights reserved.
10
 *
11
 * To contact the author write to <weizhuo[at]gmail[dot]com>
12
 * The latest version of PRADO can be obtained from:
13
 * {@link http://prado.sourceforge.net/}
14
 *
15
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
16
 * @package Prado\I18N\core
17
 */
18
19
20
namespace Prado\I18N\core;
21
22
/**
23
 * For a given DSN (database connection string), return some information
0 ignored issues
show
Bug introduced by
The type Prado\I18N\core\DSN was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
 * about the DSN. This function comes from PEAR's DB package.
25
 *
26
 * LICENSE: This source file is subject to version 3.0 of the PHP license
27
 * that is available through the world-wide-web at the following URI:
28
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
29
 * the PHP License and are unable to obtain it through the web, please
30
 * send a note to [email protected] so we can mail you a copy immediately.
31
 *
32
 * @param string DSN format, similar to PEAR's DB
33
 * @return array DSN information.
34
 * @author     Stig Bakken <[email protected]>
35
 * @author     Tomas V.V.Cox <[email protected]>
36
 * @author     Daniel Convissor <[email protected]>
37
 * @copyright  1997-2005 The PHP Group
38
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
39
 * @link       http://pear.php.net/package/DB
40
 */
41
	function parseDSN($dsn)
42
	{
43
		if (is_array($dsn)) {
44
			return $dsn;
45
		}
46
47
		$parsed = [
48
			'phptype' => false,
49
			'dbsyntax' => false,
50
			'username' => false,
51
			'password' => false,
52
			'protocol' => false,
53
			'hostspec' => false,
54
			'port' => false,
55
			'socket' => false,
56
			'database' => false
57
		];
58
59
		// Find phptype and dbsyntax
60
		if (($pos = strpos($dsn, '://')) !== false) {
0 ignored issues
show
introduced by
The condition $pos = strpos($dsn, '://') !== false can never be false.
Loading history...
61
			$str = substr($dsn, 0, $pos);
62
			$dsn = substr($dsn, $pos + 3);
63
		} else {
64
			$str = $dsn;
65
			$dsn = null;
66
		}
67
68
		// Get phptype and dbsyntax
69
		// $str => phptype(dbsyntax)
70
		if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
71
			$parsed['phptype'] = $arr[1];
72
			$parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
73
		} else {
74
			$parsed['phptype'] = $str;
75
			$parsed['dbsyntax'] = $str;
76
		}
77
78
		if (empty($dsn)) {
79
			return $parsed;
80
		}
81
82
		// Get (if found): username and password
83
		// $dsn => username:password@protocol+hostspec/database
84
		if (($at = strrpos($dsn, '@')) !== false) {
0 ignored issues
show
introduced by
The condition $at = strrpos($dsn, '@') !== false can never be false.
Loading history...
85
			$str = substr($dsn, 0, $at);
86
			$dsn = substr($dsn, $at + 1);
87
			if (($pos = strpos($str, ':')) !== false) {
0 ignored issues
show
introduced by
The condition $pos = strpos($str, ':') !== false can never be false.
Loading history...
88
				$parsed['username'] = rawurldecode(substr($str, 0, $pos));
89
				$parsed['password'] = rawurldecode(substr($str, $pos + 1));
90
			} else {
91
				$parsed['username'] = rawurldecode($str);
92
			}
93
		}
94
95
		// Find protocol and hostspec
96
97
		// $dsn => proto(proto_opts)/database
98
		if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
99
			$proto = $match[1];
100
			$proto_opts = (!empty($match[2])) ? $match[2] : false;
101
			$dsn = $match[3];
102
103
		// $dsn => protocol+hostspec/database (old format)
104
		} else {
105
			if (strpos($dsn, '+') !== false) {
106
				list($proto, $dsn) = explode('+', $dsn, 2);
107
			}
108
			if (strpos($dsn, '/') !== false) {
109
				list($proto_opts, $dsn) = explode('/', $dsn, 2);
110
			} else {
111
				$proto_opts = $dsn;
112
				$dsn = null;
113
			}
114
		}
115
116
		// process the different protocol options
117
		$parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
118
		$proto_opts = rawurldecode($proto_opts);
119
		if ($parsed['protocol'] == 'tcp') {
120
			if (strpos($proto_opts, ':') !== false) {
121
				list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts);
122
			} else {
123
				$parsed['hostspec'] = $proto_opts;
124
			}
125
		} elseif ($parsed['protocol'] == 'unix') {
126
			$parsed['socket'] = $proto_opts;
127
		}
128
129
		// Get dabase if any
130
		// $dsn => database
131
		if (!empty($dsn)) {
0 ignored issues
show
introduced by
The condition ! empty($dsn) can never be false.
Loading history...
132
			// /database
133
			if (($pos = strpos($dsn, '?')) === false) {
0 ignored issues
show
introduced by
The condition $pos = strpos($dsn, '?') === false can never be true.
Loading history...
134
				$parsed['database'] = $dsn;
135
			// /database?param1=value1&param2=value2
136
			} else {
137
				$parsed['database'] = substr($dsn, 0, $pos);
138
				$dsn = substr($dsn, $pos + 1);
139
				if (strpos($dsn, '&') !== false) {
140
					$opts = explode('&', $dsn);
141
				} else { // database?param1=value1
142
					$opts = [$dsn];
143
				}
144
				foreach ($opts as $opt) {
145
					list($key, $value) = explode('=', $opt);
146
					if (!isset($parsed[$key])) { // don't allow params overwrite
147
						$parsed[$key] = rawurldecode($value);
148
					}
149
				}
150
			}
151
		}
152
153
		return $parsed;
154
	}
155
	
156
   
157
	/**
158
	 * Convert strings to UTF-8 via iconv. NB, the result may not by UTF-8
159
	 * if the conversion failed.
160
	 * @param string string to convert to UTF-8
161
	 * @return string UTF-8 encoded string, original string if iconv failed.
162
	 */
163
	function I18N_toUTF8($string, $from)
164
	{
165
		if($from != 'UTF-8')
166
		{
167
			$s = iconv($from, 'UTF-8', $string); //to UTF-8
168
			return $s !== false ? $s : $string; //it could return false
0 ignored issues
show
introduced by
The condition $s !== false can never be false.
Loading history...
169
		}
170
		return $string;
171
	}
172
173
	/**
174
	 * Convert UTF-8 strings to a different encoding. NB. The result
175
	 * may not have been encoded if iconv fails.
176
	 * @param string the UTF-8 string for conversion
0 ignored issues
show
Bug introduced by
The type Prado\I18N\core\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
177
	 * @return string encoded string.
178
	 */
179
	function I18N_toEncoding($string, $to)
180
	{
181
		if($to != 'UTF-8')
182
		{
183
			$s = iconv('UTF-8', $to, $string);
184
			return $s !== false ? $s : $string;
0 ignored issues
show
introduced by
The condition $s !== false can never be false.
Loading history...
185
		}
186
		return $string;
187
	}
188
	
189