GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 5aebb5...c93486 )
by Brent
02:35
created

Renderer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 34
dl 0
loc 84
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRendered() 0 5 1
A __construct() 0 3 1
A renderLines() 0 13 2
A getRgbHex() 0 7 1
A renderLine() 0 19 3
A getColor() 0 7 1
A getMaximumCharacterPositionDensity() 0 13 3
1
<?php
2
3
namespace Spatie\CodeOutline;
4
5
class Renderer
6
{
7
    /** @var array */
8
    protected $lines;
9
10
    public function __construct(array $lines)
11
    {
12
        $this->lines = $lines;
13
    }
14
15
    public function getRendered(): string
16
    {
17
        $lines = $this->renderLines();
18
19
        return str_replace('{{ outline }}', $lines, file_get_contents(__DIR__.'/index.twig'));
20
    }
21
22
    protected function renderLines()
23
    {
24
        $rendered = [];
25
26
        $maximumCharacterPositionDensity = $this->getMaximumCharacterPositionDensity($this->lines);
27
28
        foreach ($this->lines as $lineNumber => $line) {
29
            $lineNumber = str_pad($lineNumber, 3, '0', STR_PAD_LEFT);
30
31
            $rendered[] = $this->renderLine($line, $maximumCharacterPositionDensity, $lineNumber);
32
        }
33
34
        return implode(PHP_EOL, $rendered);
35
    }
36
37
    protected function renderLine(?array $line, int $maximumCharacterPositionDensity, string $lineNumber): string
38
    {
39
        if (!$line) {
40
            return "<div>{$lineNumber}: </div>";
41
        }
42
43
        $renderedLine = array_map(function ($characterValue) use ($maximumCharacterPositionDensity) {
44
            $class = 'code';
45
46
            if ($characterValue < 0) {
47
                $class .= ' indent';
48
            }
49
50
            $color = $this->getColor($characterValue, $maximumCharacterPositionDensity);
51
52
            return "<span class=\"{$class}\" style=\"background-color:{$color}\">&nbsp;</span>";
53
        }, $line);
54
55
        return "<div>{$lineNumber}: ".implode('', $renderedLine).'</div>';
56
    }
57
58
    protected function getMaximumCharacterPositionDensity(array $lines): int
59
    {
60
        $maximumDensityForLine = [];
61
62
        foreach ($lines as $line) {
63
            if (!$line) {
64
                continue;
65
            }
66
67
            $maximumDensityForLine[] = max($line);
68
        }
69
70
        return max($maximumDensityForLine);
71
    }
72
73
    protected function getColor(int $value, int $max): string
74
    {
75
        $modifier = 1 - ($value / $max);
76
77
        $gray = (245 * $modifier);
78
79
        return $this->getRgbHex($gray, $gray, $gray);
80
    }
81
82
    protected function getRgbHex(int $red, int $green, int $blue)
83
    {
84
        $colors = array_map(function ($color) {
85
            return str_pad(dechex($color), 2, '0', STR_PAD_LEFT);
86
        }, [$red, $green, $blue]);
87
88
        return '#'.implode('', $colors);
89
    }
90
}
91