ListingsRule::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Kaloa\Renderer\Xml\Rule;
4
5
use DOMElement;
6
use Kaloa\Renderer\Xml\Rule\AbstractRule;
7
8
/**
9
 *
10
 */
11
final class ListingsRule extends AbstractRule
12
{
13
    /**
14
     *
15
     * @var int
16
     */
17
    private $listingCount;
18
19
    /**
20
     *
21
     * @var array
22
     */
23
    private $positionCache;
24
25
    /**
26
     *
27
     */
28 1
    public function init()
29
    {
30 1
        $this->listingCount = 0;
31 1
        $this->positionCache = array();
32 1
    }
33
34
    /**
35
     *
36
     */
37 1
    public function render()
38
    {
39 1
        foreach ($this->runXpathQuery('//listing') as $node) {
40
            /* @var $node DOMElement */
41 1
            $parent = $node->parentNode;
42
43
44 1
            $fragment = $this->getDocument()->createDocumentFragment();
45
46 1
            $language = (string) $node->getAttribute('language');
47 1
            $caption  = (string) $node->getAttribute('caption');
48
49 1
            $file = (string) $node->getAttribute('file');
50 1
            $from = $node->getAttribute('from');
51 1
            $length = $node->getAttribute('length');
52
53 1
            if ($from === '') {
54 1
                $from = null;
55 1
            }
56
57 1
            if ($length === '') {
58 1
                $length = null;
59 1
            }
60
61 1
            $source = '';
0 ignored issues
show
Unused Code introduced by
$source is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
62
63 1
            if ('' === $file) {
64 1
                $source = $node->nodeValue;
65 1
            } else {
66
                $resourceBasePath = $this->renderer
0 ignored issues
show
Bug introduced by
The property renderer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
                        ->getConfig()->getResourceBasePath();
68
                $file = $resourceBasePath . '/' . $file;
69
70
                $lines = file($file, FILE_IGNORE_NEW_LINES);
71
                if ($from === null && $length === null) {
72
                    $from = 1;
73
                    $length = count($lines);
74
                } elseif ($from !== null && $length === null) {
75
                    $length = count($lines) - $from + 1;
76
                } elseif ($from === null && $length !== null) {
77
                    if (isset($this->positionCache[$file])) {
78
                        $from = $this->positionCache[$file];
79
                    } else {
80
                        $from = 1;
81
                    }
82
                }
83
84
                $source = implode("\n", array_splice($lines, $from - 1, $length));
85
86
                $this->positionCache[$file] = $from + $length;
87
            }
88
89
            // Smart trim code
90 1
            $source = preg_replace('/(?:\s*\n)?(.*)$/s', '$1', $source);
91 1
            $source = rtrim($source);
92
93 1
            if ($language === 'xsl' || $language === 'xslt') {
94
                $language = 'xml';
95
            }
96
97 1
            if ($language !== '') {
98 1
                $source = $this->shHighlight($source, $language);
99 1
            } else {
100 1
                $source = '<pre><span class="preformatted">' . $this->escape($source) . '</span></pre>';
101
            }
102
103 1
            $s = '<div>';
104
105 1
            if ($caption !== '') {
106 1
                $this->listingCount++;
107 1
                $s .= '<p class="caption">Listing ' . $this->listingCount . ': ' . $caption . '</p>';
108 1
            }
109
110 1
            $s .= $source;
111
112 1
            $s .= '</div>';
113
114 1
            $fragment->appendXML($s);
115
116 1
            $parent->replaceChild($fragment, $node);
117 1
        }
118 1
    }
119
120
    /**
121
     *
122
     * @param string $source
123
     * @param string $language
124
     * @return string
125
     */
126 1
    private function shHighlight($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...
127
    {
128
        /*$this->geshi->set_language($language);
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% 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...
129
        $this->geshi->set_source($source);
130
131
        $this->geshi->highlight_lines_extra(array(-1));*/
132
133
        //$source = $this->sh->highlight($source, $language);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
134
135 1
        $source = '<pre>' . htmlspecialchars($source, ENT_QUOTES, 'UTF-8') . '</pre>';
136
137
        #$source = $this->geshi->parse_code();
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
138
139
        /** @todo Otherwise, DOMDocument would warn about unknown 'nbsp' entities */
140
        //$source = str_replace('&nbsp;', ' ', $source);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
141
142
        //$source = preg_replace('/^(<pre[^>]*>)(.*)(<\/pre>)$/ms', '$1<code>$2</code>$3', $source);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
143
144
145
        // Post process Geshi output
146
        // Get stuff between <pre>...</pre>
147
148 1
        $preTop = preg_replace('/(<pre[^>]*>).*/s', '\\1', $source);
149 1
        $code = preg_replace('/<pre[^>]*>(.*)<\/pre>/s', '\\1', $source);
150
151 1
        $code = explode("\n", $code);
152 1
        $count = count($code);
153 1
        $parsedCode = '';
154
155 1
        for ($i = 0; $i < $count; $i++) {
156
            /** @todo mermshaus Downstream hack */
157 1
            if (true) {
0 ignored issues
show
Bug introduced by
Avoid IF statements that are always true or false
Loading history...
158 1
                $c = 0;
159 1
                if ($code[$i] === '&nbsp;') {
160
                    // Empty line
161
                    $c = 6;
0 ignored issues
show
Unused Code introduced by
$c is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
162
                } else {
163 1
                    while (substr($code[$i], $c, 1) === ' ') {
164 1
                        $c++;
165 1
                    }
166
                }
167
168
                /*$code[$i] = substr($code[$i], 0, $c)
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
169
                        . '<span style="display: block; background: #eee;" id="' . $geshi->overall_id . '-' . $i . '">'
170
                        . substr($code[$i], $c)
171
                        . '</span>';*/
172
173 1
                $class = '';
0 ignored issues
show
Unused Code introduced by
$class is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
174 1
                if (in_array($i, array(5, 9, 12))) {
175 1
                    $class = 'line hl';
176 1
                } else {
177 1
                    $class = 'line';
178
                }
179
180 1
                $code[$i] = '<span class="' . $class . '" id="hic-svnt-dracones">'
181 1
                        . $code[$i]
182 1
                        . '</span>';
183
184
                /*$code[$i] = '<span class="'.$class.'" id="' . $geshi->overall_id . '-' . $i . '">'
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
185
                        . $code[$i]
186
                        . '</span>';*/
187 1
            }
188
189
            //$code[$i] = preg_replace_callback('/>(.*?)</s', function ($matches) { return '>'
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% 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...
190
            //. str_replace(' ', '&nbsp;<wbr/>', $matches[1]) . '<'; }, $code[$i]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
191
192 1
            $parsedCode .= $code[$i] . "\n";
193 1
        }
194
195 1
        $parsedCode = $preTop . '<code>' . rtrim($parsedCode) . '</code></pre>';
196
197 1
        return $parsedCode;
198
    }
199
}
200