Completed
Pull Request — master (#12)
by
unknown
01:54
created

lx_keyhighlighter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0
1
<?php
2
/**
3
 * This file contains the keyhighlighter class that highlight the chosen keyword in the current output buffer.
4
 *
5
 * @package keyhighlighter
6
 */
7
8
/**
9
 * keyhighlighter class
10
 *
11
 * This class highlight the chosen keywords in the current output buffer
12
 *
13
 * @package   keyhighlighter
14
 * @author    Setec Astronomy
15
 * @abstract  Highlight specific keywords.
16
 * @example   sample.php A sample code.
17
 * @link      http://setecastronomy.stufftoread.com
18
 */
19
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
20
21
class lx_keyhighlighter
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
22
{
23
    /**
24
     * @access private
25
     */
26
    public $preg_keywords = '';
27
    /**
28
     * @access private
29
     */
30
    public $keywords = '';
31
    /**
32
     * @access private
33
     */
34
    public $singlewords = false;
35
    /**
36
     * @access private
37
     */
38
    public $replace_callback = null;
39
40
    public $content;
41
42
    /**
43
     * Main constructor
44
     *
45
     * This is the main constructor of keyhighlighter class. <br>
46
     * It's the only public method of the class.
47
     * @param string   $keywords         the keywords you want to highlight
48
     * @param boolean  $singlewords      specify if it has to highlight also the single words.
49
     * @param callback $replace_callback a custom callback for keyword highlight.
50
     *                                   <code>
51
     *                                   <?php
52
     *                                   require ('keyhighlighter.class.php');
53
     *
54
     * function my_highlighter ($matches) {
55
     *    return '<span style="font-weight: bolder; color: #FF0000;">' . $matches[0] . '</span>';
56
     * }
57
     *
58
     * new keyhighlighter ('W3C', false, 'my_highlighter');
59
     * readfile ('http://www.w3c.org/');
60
     * ?>
61
     * </code>
62
     */
63
    // public function __construct ()
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
    public function __construct($keywords, $singlewords = false, $replace_callback = null)
65
    {
66
        $this->keywords         = $keywords;
67
        $this->singlewords      = $singlewords;
68
        $this->replace_callback = $replace_callback;
69
        //ob_start ([
70
                    $this,
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ','
Loading history...
71
                    'highlight'
72
                    ]);
73
    }
74
75
    /**
76
     * @access private
77
     * @param $replace_matches
78
     * @return mixed
79
     */
80
    public function replace($replace_matches)
81
    {
82
        $patterns = [];
83
        if ($this->singlewords) {
84
            $keywords = explode(' ', $this->preg_keywords);
85
            foreach ($keywords as $keyword) {
86
                $patterns[] = '/(?' . '>' . $keyword . '+)/si';
87
            }
88
        } else {
89
            $patterns[] = '/(?' . '>' . $this->preg_keywords . '+)/si';
90
        }
91
92
        $result = $replace_matches[0];
93
94
        foreach ($patterns as $pattern) {
95
            if (null !== $this->replace_callback) {
96
                $result = preg_replace_callback($pattern, $this->replace_callback, $result);
97
            } else {
98
                $result = preg_replace($pattern, '<span class="highlightedkey">\\0</span>', $result);
99
            }
100
        }
101
102
        return $result;
103
    }
104
105
    /**
106
     * @access private
107
     * @param $buffer
108
     * @return mixed|string
109
     */
110
    public function highlight($buffer)
111
    {
112
        $buffer              = '>' . $buffer . '<';
113
        $this->preg_keywords = preg_replace('/[^\w ]/si', '', $this->keywords);
114
        $buffer              = preg_replace_callback("/(\>(((?" . ">[^><]+)|(?R))*)\<)/is", array(&$this, 'replace'), $buffer);
115
        $buffer              = xoops_substr($buffer, 1, -1, $trimmarker = '');
116
117
        return $buffer;
118
    }
119
}
120