Passed
Push — development ( e7eb97...e615e2 )
by Spuds
01:30 queued 23s
created

Standard::generateQuery()   C

Complexity

Conditions 13
Paths 17

Size

Total Lines 45
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 19
nc 17
nop 1
dl 0
loc 45
ccs 9
cts 9
cp 1
crap 13
rs 6.6166
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
/**
4
 * Standard representation of any URL that doesn't have a custom builder
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 dev
11
 *
12
 */
13
14
namespace ElkArte\UrlGenerator\Standard;
15
16
use ElkArte\UrlGenerator\AbstractUrlGenerator;
17
18
/**
19
 * Class Standard
20
 *
21
 * @package ElkArte\UrlGenerator\Standard
22
 */
23
class Standard extends AbstractUrlGenerator
24
{
25
	/** {@inheritDoc} */
26
	protected $_types = ['standard'];
27
28
	/**
29
	 * {@inheritDoc}
30
	 */
31
	public function generate($params)
32
	{
33 39
		return $this->generateQuery($params);
34
	}
35 39
36
	/**
37
	 * {@inheritDoc}
38
	 */
39
	protected function generateQuery($params): string
40
	{
41 41
		if (!is_array($params))
42
		{
43 41
			return '';
44 41
		}
45
46 33
		$args = [];
47
		foreach ($params as $k => $v)
48 8
		{
49
			if (is_int($k))
50 2
			{
51
				if ($v === '')
52 6
				{
53
					continue;
54
				}
55
56 33
				$args[$k] = $v;
57
				continue;
58
			}
59
60 41
			// A substitution token like $1 $2{stuff}, should be left alone
61
			if (is_string($v) && $v !== '')
62
			{
63
				// A sprintf token (%1$d %2$s etc.) should be left alone, as should
64
				// a substitution token like $1 $2{stuff}
65
				if (($v[0] === '$' && preg_match('~^\$\d({.*})?$~m', $v) !== 0)
66
					|| ($v[0] === '%' && preg_match('~^%\d\$[ds]$~m', $v) !== 0))
67
				{
68
					$args[$k] = $k . '=' . $v;
69
					continue;
70
				}
71
			}
72
73
			if ($v === null)
74
			{
75
				continue;
76
			}
77
78
			$args[$k] = $k . '=' . urlencode($v);
79
		}
80
81
		$args = $this->getHash($args);
82
83
		return (!empty($args) ? $this->_separator : '') . implode($this->_separator, $args);
84
	}
85
}
86