EmphStrongTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 68.29 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 56
loc 82
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parseStrong() 27 27 2
A parseEmph() 29 29 2
A renderStrong() 0 4 1
A renderEmph() 0 4 1
parseInline() 0 1 ?
renderAbsy() 0 1 ?

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) 2015 Nobuo Kihara
4
 * @license https://github.com/softark/creole/blob/master/LICENSE
5
 * @link https://github.com/softark/creole#readme
6
 */
7
8
namespace softark\creole\inline;
9
10
/**
11
 * Adds inline emphasizes and strong elements
12
 */
13
trait EmphStrongTrait
14
{
15
    /**
16
     * Parses strong element: ** ... **
17
     * @marker **
18
     */
19 7 View Code Duplication
    protected function parseStrong($text)
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...
20
    {
21
        $pattern = <<< REGEXP
22 7
/^\*\*(
23
  (.*?{{{.*?}}}.*?)+?|  # including inline code span
24
  (?!.*{{{.*?}}}).*?    # without inline code span
25
)\*\*/sx
26
REGEXP;
27 7
        if (preg_match($pattern, $text, $matches)) {
28
            return [
29
                [
30 7
                    'strong',
31 7
                    $this->parseInline($matches[1])
32
                ],
33 7
                strlen($matches[0])
34
            ];
35
        } else {
36
            // no ending ** ... should be treated as strong
37
            return [
38
                [
39 2
                    'strong',
40 2
                    $this->parseInline(substr($text, 2))
41
                ],
42 2
                strlen($text)
43
            ];
44
        }
45
    }
46
47
    /**
48
     * Parses strong element: // ... //
49
     * @marker //
50
     */
51 8 View Code Duplication
    protected function parseEmph($text)
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...
52
    {
53
        // must take care of code elements, 'http://', 'https://', and 'ftp://'
54
        $pattern = <<< REGEXP
55 8
/^\/\/(
56
  (.*?{{{.*?}}}.*?)+?|  # including inline code span
57
  ((?!.*{{{.*?}}}).*?)  # without inline code span
58
)(?<!http:|(?<=h)ttps:|(?<=f)tp:)\/\/(?!\/)/sx
59
REGEXP;
60
61 8
        if (preg_match($pattern, $text, $matches)) {
62
            return [
63
                [
64 8
                    'emph',
65 8
                    $this->parseInline($matches[1])
66
                ],
67 8
                strlen($matches[0])
68
            ];
69
        } else {
70
            // no ending // ... should be treated as em
71
            return [
72
                [
73 2
                    'emph',
74 2
                    $this->parseInline(substr($text, 2))
75
                ],
76 2
                strlen($text)
77
            ];
78
        }
79
    }
80
81 7
    protected function renderStrong($block)
82
    {
83 7
        return '<strong>' . $this->renderAbsy($block[1]) . '</strong>';
84
    }
85
86 8
    protected function renderEmph($block)
87
    {
88 8
        return '<em>' . $this->renderAbsy($block[1]) . '</em>';
89
    }
90
91
    abstract protected function parseInline($text);
92
93
    abstract protected function renderAbsy($absy);
94
}
95