Completed
Push — master ( 39c39f...cd05eb )
by Richard
21:39 queued 10:29
created

MytsSyntaxhighlight::geshi()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 19.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (http://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 $ts
28
     * @param $source
29
     * @param $language
30
     *
31
     * @return bool|mixed|string
32
     */
33
    public function load($ts, $source, $language)
0 ignored issues
show
Unused Code introduced by
The parameter $language is not used and could be removed.

This check looks from 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__);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (loadConfig() instead of load()). Are you sure this is correct? If so, you might want to change this to $this->loadConfig().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
36
        if (empty($config['highlight'])) {
37
            return "<pre>{$source}</pre>";
38
        }
39
        $source = $ts->undoHtmlSpecialChars($source);
40
        $source = stripslashes($source);
41
        $source = MytsSyntaxhighlight::php($source);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
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