Filter::bbcode2html()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 66
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 66
ccs 52
cts 52
cp 1
rs 9.3191
cc 2
eloc 51
nc 2
nop 1
crap 2

How to fix   Long Method   

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
namespace Anax\Comments;
4
5
/**
6
 * Convert bbcode to HTML
7
 */
8
class Filter
9
{
10 3
    public function bbcode2html($bbtext)
11
    {
12
13
        $bbtags = array(
14 3
            '[heading1]' => '<h1>','[/heading1]' => '</h1>',
15 3
            '[heading2]' => '<h2>','[/heading2]' => '</h2>',
16 3
            '[heading3]' => '<h3>','[/heading3]' => '</h3>',
17 3
            '[heading4]' => '<h4>','[/heading4]' => '</h4>',
18 3
            '[h1]' => '<h1>','[/h1]' => '</h1>',
19 3
            '[h2]' => '<h2>','[/h2]' => '</h2>',
20 3
            '[h3]' => '<h3>','[/h3]' => '</h3>',
21 3
            '[h4]' => '<h4>','[/h4]' => '</h4>',
22
23 3
            '[paragraph]' => '<p>','[/paragraph]' => '</p>',
24 3
            '[para]' => '<p>','[/para]' => '</p>',
25 3
            '[p]' => '<p>','[/p]' => '</p>',
26 3
            '[left]' => '<p style="text-align:left;">','[/left]' => '</p>',
27 3
            '[right]' => '<p style="text-align:right;">','[/right]' => '</p>',
28 3
            '[center]' => '<p style="text-align:center;">','[/center]' => '</p>',
29 3
            '[justify]' => '<p style="text-align:justify;">','[/justify]' => '</p>',
30
31 3
            '[bold]' => '<span style="font-weight:bold;">','[/bold]' => '</span>',
32 3
            '[italic]' => '<span style="font-weight:bold;">','[/italic]' => '</span>',
33 3
            '[underline]' => '<span style="text-decoration:underline;">','[/underline]' => '</span>',
34 3
            '[b]' => '<span style="font-weight:bold;">','[/b]' => '</span>',
35 3
            '[i]' => '<span style="font-style:italic;">','[/i]' => '</span>',
36 3
            '[u]' => '<span style="text-decoration:underline;">','[/u]' => '</span>',
37 3
            '[break]' => '<br>',
38 3
            '[br]' => '<br>',
39 3
            '[newline]' => '<br>',
40 3
            '[nl]' => '<br>',
41
42 3
            '[unordered_list]' => '<ul>','[/unordered_list]' => '</ul>',
43 3
            '[list]' => '<ul>','[/list]' => '</ul>',
44 3
            '[ul]' => '<ul>','[/ul]' => '</ul>',
45
46 3
            '[ordered_list]' => '<ol>','[/ordered_list]' => '</ol>',
47 3
            '[ol]' => '<ol>','[/ol]' => '</ol>',
48 3
            '[list_item]' => '<li>','[/list_item]' => '</li>',
49 3
            '[li]' => '<li>','[/li]' => '</li>',
50
51 3
            '[*]' => '<li>','[/*]' => '</li>',
52 3
            '[code]' => '<code>','[/code]' => '</code>',
53 3
            '[preformatted]' => '<pre>','[/preformatted]' => '</pre>',
54 3
            '[pre]' => '<pre>','[/pre]' => '</pre>',
55 3
        );
56
57 3
        $bbtext = str_ireplace(array_keys($bbtags), array_values($bbtags), $bbtext);
58
59
        $bbextended = array(
60 3
            "/\[url](.*?)\[\/url]/i" => "<a href=\"http://$1\" title=\"$1\">$1</a>",
61 3
            "/\[url=(.*?)\](.*?)\[\/url\]/i" => "<a href=\"$1\" title=\"$1\">$2</a>",
62 3
            "/\[email=(.*?)\](.*?)\[\/email\]/i" => "<a href=\"mailto:$1\">$2</a>",
63 3
            "/\[mail=(.*?)\](.*?)\[\/mail\]/i" => "<a href=\"mailto:$1\">$2</a>",
64 3
            "/\[img\]([^[]*)\[\/img\]/i" => "<img src=\"$1\" alt=\" \" />",
65 3
            "/\[image\]([^[]*)\[\/image\]/i" => "<img src=\"$1\" alt=\" \" />",
66 3
            "/\[image_left\]([^[]*)\[\/image_left\]/i" => "<img src=\"$1\" alt=\" \" class=\"img_left\" />",
67 3
            "/\[image_right\]([^[]*)\[\/image_right\]/i" => "<img src=\"$1\" alt=\" \" class=\"img_right\" />",
68 3
        );
69
70 3
        foreach ($bbextended as $match => $replacement) {
71 3
            $bbtext = preg_replace($match, $replacement, $bbtext);
72 3
        }
73
74 3
        return $bbtext;
75
    }
76
}
77