Completed
Push — master ( 98a062...441031 )
by Ryan
10:10
created

Purifier   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 16.13 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 10
loc 62
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
B purify() 10 33 5

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 namespace Anomaly\Streams\Platform\Support;
2
3
/**
4
 * Class Purifier
5
 *
6
 * @link   http://pyrocms.com/
7
 * @author PyroCMS, Inc. <[email protected]>
8
 * @author Ryan Thompson <[email protected]>
9
 */
10
class Purifier extends \HTMLPurifier
11
{
12
13
    /**
14
     * Create a new Purifier instance.
15
     *
16
     * @param null $config
17
     */
18
    public function __construct($config = null)
19
    {
20
        parent::__construct($config);
21
22
        $cache = app_storage_path('support/purifier');
23
24
        if (!is_dir($cache)) {
25
            mkdir($cache, 0777, true);
26
        }
27
28
        $this->config->set('Cache.SerializerPath', $cache);
29
    }
30
31
    /**
32
     * Return purified HTML.
33
     *
34
     * @param string $html
35
     * @param null   $config
36
     * @return string
37
     */
38
    public function purify($html, $config = null)
39
    {
40
        /**
41
         * Replace <pre> and <code> blocks
42
         * that are complete with placeholders.
43
         */
44
        preg_match_all("/\<pre\>(.+?)\<\/pre\>/s", $html, $pres, PREG_PATTERN_ORDER);
45
        preg_match_all("/\<code\>(.+?)\<\/code\>/s", $html, $codes, PREG_PATTERN_ORDER);
46
47
        $html = preg_replace("/\<pre\>(.+?)\<\/pre\>/s", "PRE_PLACEHOLDER", $html);
48
        $html = preg_replace("/\<code\>(.+?)\<\/code\>/s", "CODE_PLACEHOLDER", $html);
49
50
        // Purify!
51
        $html = parent::purify($html, $config);
52
53
        /**
54
         * Replace the placeholders with the
55
         * complete <pre> and <code> blocks.
56
         */
57 View Code Duplication
        if (isset($pres[0])) {
0 ignored issues
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
            foreach ($pres[0] as $pre) {
59
                $html = preg_replace('/PRE_PLACEHOLDER/', $pre, $html, 1);
60
            }
61
        }
62
63 View Code Duplication
        if (isset($codes[0])) {
0 ignored issues
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...
64
            foreach ($codes[0] as $code) {
65
                $html = preg_replace('/CODE_PLACEHOLDER/', $code, $html, 1);
66
            }
67
        }
68
69
        return $html;
70
    }
71
}
72