Completed
Push — master ( 6452b0...d576ea )
by Michael
05:58 queued 03:02
created

keyhighlighter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
// 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...
3
4
/**
5
 * This file contains the keyhighlighter class that highlight the chosen keyword in the current output buffer.
6
 *
7
 * @package keyhighlighter
8
 */
9
10
/**
11
 * keyhighlighter class
12
 *
13
 * This class highlight the chosen keywords in the current output buffer
14
 *
15
 * @package   keyhighlighter
16
 * @author    Setec Astronomy
17
 * @abstract  Highlight specific keywords.
18
 * @copyright 2004
19
 * @example   sample.php A sample code.
20
 * @link      http://setecastronomy.stufftoread.com
21
 */
22
class 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...
23
{
24
    /**
25
     * @access private
26
     */
27
    public $preg_keywords = '';
28
    /**
29
     * @access private
30
     */
31
    public $keywords = '';
32
    /**
33
     * @access private
34
     */
35
    public $singlewords = false;
36
    /**
37
     * @access private
38
     */
39
    public $replace_callback = null;
40
41
    public $content;
42
43
    /**
44
     * Main constructor
45
     *
46
     * This is the main constructor of keyhighlighter class. <br />
47
     * It's the only public method of the class.
48
     *
49
     * @param string   $keywords         the keywords you want to highlight
50
     * @param boolean  $singlewords      specify if it has to highlight also the single words.
51
     * @param callback $replace_callback a custom callback for keyword highlight.
52
     *                                   <code>
53
     *                                   <?php
54
     *                                   require ('keyhighlighter.class.php');
55
     *
56
     * function my_highlighter ($matches) {
57
     *    return '<span style="font-weight: bolder; color: #FF0000;">' . $matches[0] . '</span>';
58
     * }
59
     *
60
     * new keyhighlighter ('W3C', false, 'my_highlighter');
61
     * readfile ('http://www.w3c.org/');
62
     * ?>
63
     * </code>
64
     */
65
    // 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...
66
    public function __construct($keywords, $singlewords = false, $replace_callback = null)
67
    {
68
        $this->keywords         = $keywords;
69
        $this->singlewords      = $singlewords;
70
        $this->replace_callback = $replace_callback;
71
    }
72
73
    /**
74
     * @access private
75
     * @param $replace_matches
76
     * @return mixed
77
     */
78
    public function replace($replace_matches)
79
    {
80
        $patterns = array();
81
        if ($this->singlewords) {
82
            $keywords = explode(' ', $this->preg_keywords);
83
            foreach ($keywords as $keyword) {
84
                $patterns[] = '/(?' . '>' . $keyword . '+)/si';
85
            }
86
        } else {
87
            $patterns[] = '/(?' . '>' . $this->preg_keywords . '+)/si';
88
        }
89
90
        $result = $replace_matches[0];
91
92
        foreach ($patterns as $pattern) {
93
            if (!is_null($this->replace_callback)) {
94
                $result = preg_replace_callback($pattern, $this->replace_callback, $result);
95
            } else {
96
                $result = preg_replace($pattern, '<span class="highlightedkey">\\0</span>', $result);
97
            }
98
        }
99
100
        return $result;
101
    }
102
103
    /**
104
     * @access private
105
     * @param $buffer
106
     * @return mixed|string
107
     */
108
    public function highlight($buffer)
109
    {
110
        $buffer              = '>' . $buffer . '<';
111
        $this->preg_keywords = preg_replace('/[^\w ]/si', '', $this->keywords);
112
        $buffer              = preg_replace_callback("/(\>(((?" . ">[^><]+)|(?R))*)\<)/is", array(&$this, 'replace'), $buffer);
113
        $buffer              = xoops_substr($buffer, 1, -1);
114
115
        return $buffer;
116
    }
117
}
118