Profile   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 18
c 1
b 1
f 0
dl 0
loc 39
rs 10
ccs 0
cts 6
cp 0
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate() 0 29 10
1
<?php
2
3
/**
4
 * Semantic representation of profile URLs
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 Beta 1
11
 *
12
 */
13
14
namespace ElkArte\UrlGenerator\Semantic;
15
16
/**
17
 * Class Profile
18
 *
19
 * @package ElkArte\UrlGenerator\Semantic
20
 */
21
class Profile extends Standard
22
{
23
	/**
24
	 * {@inheritDoc}
25
	 */
26
	protected $_types = ['profile'];
27
28
	/**
29
	 * {@inheritDoc}
30
	 */
31
    public function generate($params)
32
	{
33
		// Detect sprintf/substitution tokens so they survive for later sprintf()
34
		$name = $params['name'] ?? '';
35
		$u = $params['u'] ?? 0;
36
37
		$isNameToken = is_string($name) && $name !== '' && $name[0] === '%' && preg_match('~^%\d\$s$~m', $name) === 1;
38
		$isUidToken = is_string($u) && $u !== '' && $u[0] === '%' && preg_match('~^%\d\$d$~m', $u) === 1;
39
40
		if ($isNameToken)
41
		{
42
			$slug = $name; // keep token as-is (do NOT encode)
43
		}
44
		else
45
		{
46
			// Safely build a slug from the display name; guard against null
47
			$name = (string) $name;
48
			$name = trim($name);
49
			$slug = $name === '' ? 'member' : preg_replace('~\s+~u', '-', $name);
50
			$slug = trim($slug, '-');
51
			$slug = rawurlencode($slug);
52
		}
53
54
		$uid = $isUidToken ? $u : (int) $u;
55
56
		$url = 'p/' . $slug . '-' . $uid;
57
		unset($params['name'], $params['u'], $params['action']);
58
59
		return $url . $this->generateQuery($params);
60
	}
61
}
62