CodeBlock::__invoke()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 9.0168
c 0
b 0
f 0
cc 5
nc 4
nop 2
1
<?php
2
3
namespace Faulancer\View\Helper;
4
5
use Faulancer\View\AbstractViewHelper;
6
7
/**
8
 * Class CodeBlock | CodeBlock.php
9
 *
10
 * @package Faulancer\View\Helper
11
 * @author  Florian Knapp <[email protected]>
12
 */
13
class CodeBlock extends AbstractViewHelper
14
{
15
16
    /**
17
     * @param                $filename
18
     * @param int            $actualLine
19
     * @return string
20
     * @codeCoverageIgnore
21
     */
22
    public function __invoke($filename, $actualLine = 0)
23
    {
24
25
        $lineNr = 0;
26
        $lines  = '';
27
        $file   = fopen($filename, 'r');
28
29
        $tabChar = '&nbsp;&nbsp;&nbsp;&nbsp;';
30
        $regularChar = '&nbsp;';
31
32
        while ($line = fgets($file)) {
33
34
            $lineNr++;
35
36
            if ($lineNr >= $actualLine - 8 && $lineNr <= $actualLine + 4) {
37
38
                $l = str_replace("\t", $tabChar, $line);
39
                $l = str_replace(" ", $regularChar, $l);
40
                $l = str_replace(['<', '>'], ['&lsaquo;', '&rsaquo;'], $l);
41
42
                $data = '<code>' . $l . '</code>';
43
44
                if ($lineNr === $actualLine) {
45
                    $lines .= '<span class="line highlight"><span class="line-nr">' . $lineNr . '</span>' . $data . '</span>';
46
                } else {
47
                    $lines .= '<span class="line"><span class="line-nr">' . $lineNr . '</span>' . $data . '</span>';
48
                }
49
50
            }
51
52
        }
53
54
        fclose($file);
55
56
        return $lines;
57
58
    }
59
60
}