Issues (54)

src/Highlighter/Highlighter.php (2 issues)

1
<?php declare(strict_types=1);
2
3
/**
4
 * @license     http://opensource.org/licenses/mit-license.php MIT
5
 * @link        https://github.com/nicoSWD
6
 * @author      Nicolas Oelgart <[email protected]>
7
 */
8
namespace nicoSWD\Rule\Highlighter;
9
10
use ArrayIterator;
11
use nicoSWD\Rule\Tokenizer\TokenizerInterface;
12
use nicoSWD\Rule\TokenStream\Token\TokenType;
13
14
final class Highlighter
15
{
16
    /** @var string[] */
17
    private array $styles = [
18
        TokenType::COMMENT => 'color: #948a8a; font-style: italic;',
19
        TokenType::LOGICAL => 'color: #c72d2d;',
20
        TokenType::OPERATOR => 'color: #000;',
21
        TokenType::PARENTHESIS => 'color: #000;',
22
        TokenType::SPACE => '',
23
        TokenType::UNKNOWN => '',
24
        TokenType::VALUE => 'color: #e36700; font-style: italic;',
25
        TokenType::VARIABLE => 'color: #007694; font-weight: 900;',
26
        TokenType::METHOD => 'color: #000',
27
        TokenType::SQUARE_BRACKET => '',
28
        TokenType::COMMA => '',
29
        TokenType::FUNCTION => '',
30
        TokenType::INT_VALUE => '',
31
    ];
32
33
    public function __construct(private TokenizerInterface $tokenizer)
0 ignored issues
show
The parameter $tokenizer is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

33
    public function __construct(/** @scrutinizer ignore-unused */ private TokenizerInterface $tokenizer)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
    }
36 4
37
    public function setStyle(int $group, string $style): void
38 4
    {
39 4
        if (!isset($this->styles[$group])) {
40
            throw new Exception\InvalidGroupException(
41 4
                'Invalid group'
42
            );
43 4
        }
44 2
45 2
        $this->styles[$group] = (string) $style;
46
    }
47
48
    public function highlightString(string $string): string
49 2
    {
50 2
        return $this->highlightTokens($this->tokenizer->tokenize($string));
0 ignored issues
show
Bug Best Practice introduced by
The property tokenizer does not exist on nicoSWD\Rule\Highlighter\Highlighter. Did you maybe forget to declare it?
Loading history...
51
    }
52 2
53
    public function highlightTokens(ArrayIterator $tokens): string
54 2
    {
55
        $string = '';
56
57 2
        foreach ($tokens as $token) {
58
            if ($style = $this->styles[$token->getType()]) {
59 2
                $value = htmlentities($token->getOriginalValue(), ENT_QUOTES, 'utf-8');
60
                $string .= '<span style="' . $style . '">' . $value . '</span>';
61 2
            } else {
62 2
                $string .= $token->getOriginalValue();
63 2
            }
64 2
        }
65
66 2
        return '<pre><code>' . $string . '</code></pre>';
67
    }
68
}
69