Passed
Push — master ( 82fa70...46a136 )
by Christian
01:59
created

PhpFileFormatter::replaceSpecialCharacters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scheb\Tombstone\Analyzer\Report\Html\Renderer;
4
5
/*
6
 * Based on code from PHP_CodeCoverage
7
 * https://github.com/sebastianbergmann/php-code-coverage
8
 *
9
 * Copyright (c) 2009-2015, Sebastian Bergmann <[email protected]>.
10
 * All rights reserved.
11
 */
12
class PhpFileFormatter
13
{
14
    private const COLOR_HTML = 'html';
15
    private const COLOR_COMMENT = 'comment';
16
    private const COLOR_KEYWORD = 'keyword';
17
    private const COLOR_STRING = 'string';
18
    private const COLOR_DEFAULT = 'default';
19
20 1
    public static function loadFile(string $file): array
21
    {
22 1
        $buffer = file_get_contents($file);
23 1
        $tokens = token_get_all($buffer);
24 1
        $result = [''];
25 1
        $i = 0;
26 1
        $fileEndsWithNewLine = "\n" == substr($buffer, -1);
27 1
        unset($buffer);
28
29 1
        foreach ($tokens as $token) {
30
            // Handle plain string tokens
31 1
            if (is_string($token)) {
32 1
                $result[$i] .= self::formatValue($token, self::COLOR_KEYWORD);
33 1
                continue;
34
            }
35
36 1
            [$tokenType, $value] = $token;
37
38
            // Handle new lines
39 1
            if ("\n" === $value) {
40 1
                $result[++$i] = '';
41 1
                continue;
42
            }
43
44 1
            $value = self::replaceSpecialCharacters($value);
45 1
            $lines = explode("\n", $value);
46 1
            foreach ($lines as $lineNumber => $lineValue) {
47 1
                $lineValue = trim($lineValue);
48
49 1
                if ('' !== $lineValue) {
50 1
                    $result[$i] .= self::formatValue($lineValue, self::getColor($tokenType));
51
                }
52
53 1
                if (isset($lines[$lineNumber + 1])) {
54 1
                    $result[++$i] = '';
55
                }
56
            }
57
        }
58
59 1
        if ($fileEndsWithNewLine) {
60 1
            unset($result[count($result) - 1]);
61
        }
62
63 1
        return $result;
64
    }
65
66 1
    private static function replaceSpecialCharacters(string $value): string
67
    {
68 1
        return str_replace(
69 1
            ["\t", ' '],
70 1
            ['&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;'],
71 1
            htmlspecialchars($value, ENT_COMPAT)
72
        );
73
    }
74
75 1
    private static function formatValue(string $value, string $color): string
76
    {
77 1
        return sprintf(
78 1
            '<span class="%s">%s</span>',
79 1
            $color,
80 1
            $value
81
        );
82
    }
83
84 1
    private static function getColor(int $token): string
85
    {
86
        switch ($token) {
87 1
            case T_INLINE_HTML:
88 1
                return self::COLOR_HTML;
89 1
            case T_COMMENT:
90 1
            case T_DOC_COMMENT:
91 1
                return self::COLOR_COMMENT;
92 1
            case T_ABSTRACT:
93 1
            case T_ARRAY:
94 1
            case T_AS:
95 1
            case T_BREAK:
96 1
            case T_CALLABLE:
97 1
            case T_CASE:
98 1
            case T_CATCH:
99 1
            case T_CLASS:
100 1
            case T_CLONE:
101 1
            case T_CONTINUE:
102 1
            case T_DEFAULT:
103 1
            case T_ECHO:
104 1
            case T_ELSE:
105 1
            case T_ELSEIF:
106 1
            case T_EMPTY:
107 1
            case T_ENDDECLARE:
108 1
            case T_ENDFOR:
109 1
            case T_ENDFOREACH:
110 1
            case T_ENDIF:
111 1
            case T_ENDSWITCH:
112 1
            case T_ENDWHILE:
113 1
            case T_EXIT:
114 1
            case T_EXTENDS:
115 1
            case T_FINAL:
116 1
            case T_FINALLY:
117 1
            case T_FOREACH:
118 1
            case T_FUNCTION:
119 1
            case T_GLOBAL:
120 1
            case T_IF:
121 1
            case T_IMPLEMENTS:
122 1
            case T_INCLUDE:
123 1
            case T_INCLUDE_ONCE:
124 1
            case T_INSTANCEOF:
125 1
            case T_INSTEADOF:
126 1
            case T_INTERFACE:
127 1
            case T_ISSET:
128 1
            case T_LOGICAL_AND:
129 1
            case T_LOGICAL_OR:
130 1
            case T_LOGICAL_XOR:
131 1
            case T_NAMESPACE:
132 1
            case T_NEW:
133 1
            case T_PRIVATE:
134 1
            case T_PROTECTED:
135 1
            case T_PUBLIC:
136 1
            case T_REQUIRE:
137 1
            case T_REQUIRE_ONCE:
138 1
            case T_RETURN:
139 1
            case T_STATIC:
140 1
            case T_THROW:
141 1
            case T_TRAIT:
142 1
            case T_TRY:
143 1
            case T_UNSET:
144 1
            case T_USE:
145 1
            case T_VAR:
146 1
            case T_WHILE:
147 1
            case T_YIELD:
148 1
                return self::COLOR_KEYWORD;
149 1
            case T_CONSTANT_ENCAPSED_STRING:
150 1
                return self::COLOR_STRING;
151
            default:
152 1
                return self::COLOR_DEFAULT;
153
        }
154
    }
155
}
156