UrlLinkTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 22.86 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 8
loc 35
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseUrl() 0 18 3
A renderAutoUrl() 8 8 2

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
 * @copyright Copyright (c) 2014 Carsten Brandt
4
 * @license https://github.com/cebe/markdown/blob/master/LICENSE
5
 * @link https://github.com/cebe/markdown#readme
6
 */
7
8
namespace cebe\markdown\inline;
9
10
// work around https://github.com/facebook/hhvm/issues/1120
11 1
defined('ENT_HTML401') || define('ENT_HTML401', 0);
12
13
/**
14
 * Adds auto linking for URLs
15
 */
16
trait UrlLinkTrait
17
{
18
	/**
19
	 * Parses urls and adds auto linking feature.
20
	 * @marker http
21
	 * @marker ftp
22
	 */
23 5
	protected function parseUrl($markdown)
24
	{
25
		$pattern = <<<REGEXP
26 5
			/(?(R) # in case of recursion match parentheses
27
				 \(((?>[^\s()]+)|(?R))*\)
28
			|      # else match a link with title
29
				^(https?|ftp):\/\/(([^\s<>()]+)|(?R))+(?<![\.,:;\'"!\?\s])
30
			)/x
31
REGEXP;
32
33 5
		if (!in_array('parseLink', $this->context) && preg_match($pattern, $markdown, $matches)) {
34
			return [
35 4
				['autoUrl', $matches[0]],
36 4
				strlen($matches[0])
37
			];
38
		}
39 2
		return [['text', substr($markdown, 0, 4)], 4];
40
	}
41
42 4 View Code Duplication
	protected function renderAutoUrl($block)
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...
43
	{
44 4
		$href = htmlspecialchars($block[1], ENT_COMPAT | ENT_HTML401, 'UTF-8');
45 4
		$decodedUrl = urldecode($block[1]);
46 4
		$secureUrlText = preg_match('//u', $decodedUrl) ? $decodedUrl : $block[1];
47 4
		$text = htmlspecialchars($secureUrlText, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
48 4
		return "<a href=\"$href\">$text</a>";
49
	}
50
}
51