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 ( cd702d...f7c46f )
by Freek
01:32
created

Renderer::getMaximumLineLength()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Outline;
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
        $maximumLineLength = $this->getMaximumLineLength($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, $maximumLineLength, $lineNumber);
32
        }
33
34
        return implode(PHP_EOL, $rendered);
35
    }
36
37
    protected function renderLine(?array $line, int $maximumLineLength, string $lineNumber): string
38
    {
39
        if (!$line) {
40
            return "<div>{$lineNumber}: </div>";
41
        }
42
43
        $renderedLine = array_map(function ($characterValue) use ($maximumLineLength) {
44
            $class = 'code';
45
46
            if ($characterValue < 0) {
47
                $class .= ' indent';
48
            }
49
50
            $color = $this->getColor($characterValue, $maximumLineLength);
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 getMaximumLineLength(array $lines): int
59
    {
60
        $maximumLineLenghts = [];
61
62
        foreach ($lines as $line) {
63
            if (!$line) {
64
                continue;
65
            }
66
67
            $maximumLineLenghts[] = max($line);
68
        }
69
70
        return max($maximumLineLenghts);
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