Language   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 3
dl 0
loc 95
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
tokens() 0 1 ?
colors() 0 1 ?
A matchExtension() 0 6 1
A lex() 0 4 1
A highlight() 0 8 2
A oneOf() 0 4 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Console\Language;
11
12
use Railt\Io\Readable;
13
use Railt\Lexer\Factory;
14
use Railt\Lexer\LexerInterface;
15
use Railt\Lexer\Result\Eoi;
16
17
/**
18
 * Class Language
19
 */
20
abstract class Language implements LanguageInterface
21
{
22
    /**
23
     * @var string[]
24
     */
25
    public const DEFAULT_COLORS = [
26
        'T_COMMENT'  => 'fg=cyan',
27
        'T_KEYWORD'  => 'fg=magenta',
28
        'T_STRING'   => 'fg=green',
29
        'T_DIGIT'    => 'fg=green',
30
        'T_VARIABLE' => 'fg=yellow',
31
    ];
32
33
    /**
34
     * @var string[]
35
     */
36
    public const DEFAULT_TOKENS = [
37
        'T_WHITESPACE'    => '\s+',
38
    ];
39
40
    /**
41
     * @var LexerInterface
42
     */
43
    private $lexer;
44
45
    /**
46
     * @var array
47
     */
48
    private $colors;
49
50
    /**
51
     * Language constructor.
52
     * @throws \InvalidArgumentException
53
     * @throws \Railt\Lexer\Exception\BadLexemeException
54
     */
55
    public function __construct()
56
    {
57
        $this->lexer  = Factory::create(\array_merge(self::DEFAULT_TOKENS, $this->tokens()), [Eoi::T_NAME]);
58
        $this->colors = \array_merge(self::DEFAULT_COLORS, $this->colors());
59
    }
60
61
    /**
62
     * @return array|string[]
63
     */
64
    abstract public function tokens(): array;
65
66
    /**
67
     * @return array|string[]
68
     */
69
    abstract public function colors(): array;
70
71
    /**
72
     * @param Readable $file
73
     * @param array $extensions
74
     * @return bool
75
     */
76
    protected function matchExtension(Readable $file, array $extensions): bool
77
    {
78
        $ext = \mb_strtolower(\pathinfo($file->getPathname(), \PATHINFO_EXTENSION));
79
80
        return \in_array($ext, $extensions, true);
81
    }
82
83
    /**
84
     * @param Readable $file
85
     * @return iterable
86
     */
87
    public function lex(Readable $file): iterable
88
    {
89
        return $this->lexer->lex($file);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->lexer->lex($file); of type Traversable|Railt\Lexer\TokenInterface[] adds the type Traversable to the return on line 89 which is incompatible with the return type declared by the interface Railt\Console\Language\LanguageInterface::lex of type Railt\Console\Language\i...\Lexer\TokenInterface[].
Loading history...
90
    }
91
92
    /**
93
     * @param string $token
94
     * @param string $content
95
     * @return string
96
     */
97
    public function highlight(string $token, string $content): string
98
    {
99
        $color   = $this->colors[$token] ?? null;
100
        $content = \str_replace('\\', "\u{0000}", $content);
101
        $content = $color === null ? $content : \sprintf('<%s>%s</>', $color, $content);
102
103
        return $content;
104
    }
105
106
    /**
107
     * @param array $tokens
108
     * @return string
109
     */
110
    protected function oneOf(array $tokens): string
111
    {
112
        return \sprintf('(%s)\b', \implode('|', $tokens));
113
    }
114
}
115