Basic   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 165
Duplicated Lines 5.45 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 1
dl 9
loc 165
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 8 2
A nobb() 0 3 1
A noparse() 0 5 1
A c() 0 4 1
A color() 0 4 1
A font() 0 3 1
A size() 0 3 1
A br() 0 3 1
A hr() 0 3 1
A clear() 0 3 1
A img() 9 9 2
B url() 0 13 7
B list() 0 30 8

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
 * Class Basic
4
 *
5
 * @filesource   Basic.php
6
 * @created      26.04.2018
7
 * @package      chillerlan\BBCode\Output\HTML
8
 * @author       smiley <[email protected]>
9
 * @copyright    2018 smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\BBCode\Output\HTML;
14
15
class Basic extends HTMLModuleAbstract{
16
17
	/**
18
	 * @var array
19
	 */
20
	protected $tags = [
21
		'noparse', 'nobb', 'color', 'font', 'size','br', 'hr', 'clear','img', 'url', 'c', 'list',
22
		'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
23
		'strong', 'sub', 'sup', 'del', 'small', 'em', 's', 'b', 'u', 'i', 'tt',
24
	];
25
26
	/**
27
	 * @var array
28
	 */
29
	protected $singletags = ['br', 'hr', 'clear'];
30
31
	/**
32
	 * @var array
33
	 */
34
	protected $noparse = ['noparse', 'nobb', 'c'];
35
36
	/**
37
	 * @return string
38
	 */
39
	protected function transform():string{
40
41
		if(empty($this->content)){
42
			return '';
43
		}
44
45
		return '<'.$this->tag.' class="bb-text '.$this->tag.'">'.$this->content.'</'.$this->tag.'>';
46
	}
47
48
	/**
49
	 * @return string
50
	 */
51
	protected function nobb():string{
52
		return $this->noparse();
53
	}
54
55
	/**
56
	 * @return string
57
	 */
58
	protected function noparse():string{
59
		$this->clearPseudoClosingTags();
60
61
		return '<pre class="bb-noparse">'.$this->content.'</pre>'; // $this->match
62
	}
63
64
	/**
65
	 * @return string
66
	 */
67
	protected function c(){ // @todo
68
		$this->clearPseudoClosingTags();
69
		return '<code class="bb-inline-code" style="display: inline">'.$this->content.'</code>';
70
	}
71
72
	/**
73
	 * @return string
74
	 */
75
	protected function color(){
76
		// @todo: preg_match('/^#([a-f\d]{3}){1,2}$/i', $value)
77
		return '<span class="bb-text color" style="color: '.'; background-color: '.';">'.$this->content.'</span>';
78
	}
79
80
	/**
81
	 * @return string
82
	 */
83
	protected function font(){ // @todo: restrict fonts via classname
84
		return '<span class="bb-text font comic-sans">'.$this->content.'</span>';
85
	}
86
87
	/**
88
	 * @return string
89
	 */
90
	protected function size(){ // @todo: restrict sizes via css
91
		return '<span class="bb-text size extra-tiny">'.$this->content.'</span>';
92
	}
93
94
	/**
95
	 * @return string
96
	 */
97
	public function br():string{ // @todo: adjust padding-bottom
98
		return '<br class="bb-br"/>';
99
	}
100
101
	/**
102
	 * @return string
103
	 */
104
	public function hr():string{ // @todo line style
105
		return '<hr class="bb-hr"/>';
106
	}
107
108
	/**
109
	 * @return string
110
	 */
111
	protected function clear(){
112
		return '<br class="bb-clear '.$this->bbtagIn(['both', 'left', 'right'], 'both').'"/>';
113
	}
114
115
	/**
116
	 * @return string
117
	 */
118 View Code Duplication
	protected function img():string{
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...
119
		$url = filter_var($this->content, FILTER_VALIDATE_URL);// @todo
120
121
		if(!$url){
122
			return '';
123
		}
124
125
		return '<img src="'.$url.'" class="bb-img" alt />'; // @todo: alt, title
126
	}
127
128
	/**
129
	 * @return string
130
	 */
131
	protected function url():string{
132
133
		if(empty($this->content)){
134
			return '';
135
		}
136
137
		$url    = filter_var($this->bbtag() ?? $this->content, FILTER_VALIDATE_URL);// @todo
138
		$host   = parse_url($url, PHP_URL_HOST);
139
		$target = (!empty($host) && (isset($_SERVER['SERVER_NAME']) && $host === $_SERVER['SERVER_NAME'])) || empty($host) ? 'self' : 'blank';
140
141
		return '<a class="bb-url '.$target.'" '.($url ? ' href="'.$url.'" target="_'.$target.'"' : '').'>'.$this->content.'</a>'; // .$this->getTitle()
142
143
	}
144
145
	/**
146
	 * @return string
147
	 */
148
	protected function list():string{
149
150
		if(empty($this->content)){
151
			return '';
152
		}
153
154
		$ol    = ['0', '1', 'a', 'A', 'i', 'I'];
155
		$ul    = ['c', 'd', 's'];
156
		$types = [
157
			'0' => 'decimal-leading-zero',
158
			'1' => 'decimal',
159
			'a' => 'lower-alpha',
160
			'A' => 'upper-alpha',
161
			'i' => 'lower-roman',
162
			'I' => 'upper-roman',
163
			'c' => 'circle',
164
			's' => 'square',
165
			'd' => 'disc',
166
		];
167
168
		$start    = $this->bbtag();
169
		$list_tag = (count($this->attributes) === 0 || $this->attributeIn('type', $ul) ? 'ul' : 'ol');
170
171
		return '<'.$list_tag.' class="bb-list '.$this->attributeKeyIn('type', $types, 'disc').'" '
172
		       .(is_numeric($start) && $this->attributeIn('type', $ol) ? ' start="'.ceil($start).'"' : '')
173
		       .($this->getAttribute('reversed') && $this->attributeIn('type', $ol) ? ' reversed="true"' : '')
174
		       .'>'
175
		       .'<li>'.implode(array_slice(explode('[*]', $this->content), true), '</li><li>').'</li>' // nasty
176
		       .'</'.$list_tag.'>';
177
	}
178
179
}
180