MytsSyntaxhighlight::php()   F
last analyzed

Complexity

Conditions 11
Paths 512

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 43
rs 3.8277
c 0
b 0
f 0
cc 11
nc 512
nop 1

How to fix   Complexity   

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
 * TextSanitizer extension
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2025 XOOPS Project (https://xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             class
15
 * @subpackage          textsanitizer
16
 * @since               2.3.0
17
 * @author              Taiwen Jiang <[email protected]>
18
 */
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
/**
22
 * Class MytsSyntaxhighlight
23
 */
24
class MytsSyntaxhighlight extends MyTextSanitizerExtension
25
{
26
    /**
27
     * @param MyTextSanitizer $myts
28
     * @param $source
29
     * @param $language
30
     *
31
     * @return bool|mixed|string
32
     */
33
    public function load($myts, $source, $language)
0 ignored issues
show
Unused Code introduced by
The parameter $language is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

33
    public function load($myts, $source, /** @scrutinizer ignore-unused */ $language)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
        $config = parent::loadConfig(__DIR__);
36
        if (empty($config['highlight'])) {
37
            return "<pre>{$source}</pre>";
38
        }
39
        $source = $myts->undoHtmlSpecialChars($source);
40
        $source = stripslashes($source);
41
        $source = $this->php($source);
42
43
        return $source;
44
    }
45
46
    /**
47
     * @param $text
48
     *
49
     * @return mixed|string
50
     */
51
    public function php($text)
52
    {
53
        $text          = trim($text);
54
        $addedtag_open = 0;
55
        if (!strpos($text, '<?php') && (substr($text, 0, 5) !== '<?php')) {
56
            $text          = '<?php ' . $text;
57
            $addedtag_open = 1;
58
        }
59
        $addedtag_close = 0;
60
        if (!strpos($text, '?>')) {
61
            $text .= '?>';
62
            $addedtag_close = 1;
63
        }
64
        $oldlevel = error_reporting(0);
65
66
        //There is a bug in the highlight function(php < 5.3) that it doesn't render
67
        //backslashes properly like in \s. So here we replace any backslashes
68
        $text = str_replace("\\", 'XxxX', $text);
69
70
        $buffer = highlight_string($text, true); // Require PHP 4.20+
71
72
        //Placing backspaces back again
73
        $buffer = str_replace('XxxX', "\\", $buffer);
74
75
        error_reporting($oldlevel);
76
        $pos_open = $pos_close = 0;
77
        if ($addedtag_open) {
78
            $pos_open = strpos($buffer, '&lt;?php&nbsp;');
79
        }
80
        if ($addedtag_close) {
81
            $pos_close = strrpos($buffer, '?&gt;');
82
        }
83
84
        $str_open  = $addedtag_open ? substr($buffer, 0, $pos_open) : '';
85
        $str_close = $pos_close ? substr($buffer, $pos_close + 5) : '';
86
87
        $length_open  = $addedtag_open ? $pos_open + 14 : 0;
88
        $length_text  = $pos_close ? $pos_close - $length_open : 0;
89
        $str_internal = $length_text ? substr($buffer, $length_open, $length_text) : substr($buffer, $length_open);
90
91
        $buffer = $str_open . $str_internal . $str_close;
92
93
        return $buffer;
94
    }
95
}
96