Completed
Push — master ( 9ffe58...cd3227 )
by Carsten
03:21 queued 01:21
created

EmphStrongTrait::parseEmphStrong()   C

Complexity

Conditions 13
Paths 7

Size

Total Lines 50
Code Lines 23

Duplication

Lines 21
Ratio 42 %

Code Coverage

Tests 24
CRAP Score 13.0108

Importance

Changes 0
Metric Value
dl 21
loc 50
ccs 24
cts 25
cp 0.96
rs 5.3808
c 0
b 0
f 0
cc 13
eloc 23
nc 7
nop 1
crap 13.0108

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
 * @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
/**
11
 * Adds inline emphasizes and strong elements
12
 */
13
trait EmphStrongTrait
14
{
15
	/**
16
	 * Parses empathized and strong elements.
17
	 * @marker _
18
	 * @marker *
19
	 */
20 33
	protected function parseEmphStrong($text)
21
	{
22 33
		$marker = $text[0];
23
24 33
		if (!isset($text[1])) {
25 3
			return [['text', $text[0]], 1];
26
		}
27
28 33
		if ($marker == $text[1]) { // strong
29
			// work around a PHP bug that crashes with a segfault on too much regex backtrack
30
			// check whether the end marker exists in the text
31
			// https://github.com/erusev/parsedown/issues/443
32
			// https://bugs.php.net/bug.php?id=45735
33 27
			if (strpos($text, $marker . $marker, 2) === false) {
34 6
				return [['text', $text[0] . $text[1]], 2];
35
			}
36
37 21 View Code Duplication
			if ($marker == '*' && preg_match('/^[*]{2}((?:[^*]|[*][^*]*[*])+?)[*]{2}(?![*]{2})/s', $text, $matches) ||
38 21
				$marker == '_' && preg_match('/^__((?:[^_]|_[^_]*_)+?)__(?!__)/us', $text, $matches)) {
39
40
				return [
41
					[
42 21
						'strong',
43 21
						$this->parseInline($matches[1]),
44 21
					],
45 21
					strlen($matches[0])
46 21
				];
47
			}
48
		} else { // emph
49
			// work around a PHP bug that crashes with a segfault on too much regex backtrack
50
			// check whether the end marker exists in the text
51
			// https://github.com/erusev/parsedown/issues/443
52
			// https://bugs.php.net/bug.php?id=45735
53 21
			if (strpos($text, $marker, 1) === false) {
54 10
				return [['text', $text[0]], 1];
55
			}
56
57 16 View Code Duplication
			if ($marker == '*' && preg_match('/^[*]((?:[^*]|[*][*][^*]+?[*][*])+?)[*](?![*][^*])/s', $text, $matches) ||
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
58 16
				$marker == '_' && preg_match('/^_((?:[^_]|__[^_]*__)+?)_(?!_[^_])\b/us', $text, $matches)) {
59
				return [
60
					[
61 15
						'emph',
62 15
						$this->parseInline($matches[1]),
63 15
					],
64 15
					strlen($matches[0])
65 15
				];
66
			}
67
		}
68 5
		return [['text', $text[0]], 1];
69
	}
70
71 21
	protected function renderStrong($block)
72
	{
73 21
		return '<strong>' . $this->renderAbsy($block[1]) . '</strong>';
74
	}
75
76 15
	protected function renderEmph($block)
77
	{
78 15
		return '<em>' . $this->renderAbsy($block[1]) . '</em>';
79
	}
80
}
81