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

Topic::generate()   D

Complexity

Conditions 10
Paths 384

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 10
nc 384
nop 1
dl 0
loc 18
ccs 0
cts 6
cp 0
crap 110
rs 4.5333
c 1
b 1
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
 * Semantic representation of topic 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 dev
11
 *
12
 */
13
14
namespace ElkArte\UrlGenerator\Semantic;
15
16
/**
17
 * Class Topic
18
 *
19
 * @package ElkArte\UrlGenerator\Semantic
20
 */
21
class Topic extends Standard
22
{
23
	/** {@inheritDoc} */
24
	protected $_types = ['topic'];
25
26
	/**
27
	 * {@inheritDoc}
28
	 */
29
	public function generate($params)
30
	{
31
		$subject = isset($params['subject']) && $params['subject'] !== null ? (string) $params['subject'] : '';
32
33
		// Simple, safe slug: trim, collapse whitespace to '-', fallback to 'topic' if empty
34
		$subject = trim($subject);
35
		$slug = $subject === '' ? 'topic' : preg_replace('~\s+~u', '-', $subject);
36
		$slug = trim($slug, '-');
37
38
		$topic = isset($params['topic']) ? (int) $params['topic'] : 0;
39
		$has_start = isset($params['start']) && $params['start'] !== '' && $params['start'] !== null;
40
		$start = $has_start ? $params['start'] : null;
41
42
		// Semantic pagination format is dot-appended after the id (e.g., t/slug-id.10)
43
		$url = 't/' . rawurlencode($slug) . '-' . $topic . ($has_start && $start !== 0 ? '.' . $start : '');
44
		unset($params['subject'], $params['topic'], $params['start']);
45
46
		return $url . $this->generateQuery($params);
47
	}
48
}
49