Parser   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 3
dl 0
loc 74
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parseFile() 0 8 2
A parseBlock() 0 9 1
C parse() 0 52 9
1
<?php
2
3
namespace Thruster\Component\Tokenizer;
4
5
use Thruster\Component\Tokenizer\Token\NewLineToken;
6
use Thruster\Component\Tokenizer\Token\WhitespaceToken;
7
use Thruster\Component\Tokenizer\Token\ConstantEncapsedStringToken;
8
use Thruster\Component\Tokenizer\Exception\FileNotFoundException;
9
10
/**
11
 * Class Parser
12
 *
13
 * @package Thruster\Component\Tokenizer
14
 * @author  Aurimas Niekis <[email protected]>
15
 */
16
class Parser
17
{
18
    public function parseFile(string $file): array
19
    {
20
        if (false === file_exists($file)) {
21
            throw new FileNotFoundException($file);
22
        }
23
24
        return $this->parse(file_get_contents($file));
25
    }
26
27
    public function parseBlock(string $blockCode): array
28
    {
29
        $tokens = $this->parse('<?php ' . $blockCode);
30
31
        array_shift($tokens);
32
        array_shift($tokens);
33
34
        return $tokens;
35
    }
36
37
    public function parse(string $sourceCode): array
38
    {
39
        $tokens = [];
40
41
        $line = 0;
42
        foreach (token_get_all($sourceCode) as $data) {
43
            /** @var Token $token */
44
            if (false === is_array($data)) {
45
                $tokenClass = TokenInterface::CHARACTER_TO_CLASS_MAP[$data];
46
47
                $tokens[] = new $tokenClass($data, $line);
48
49
                continue;
50
            }
51
52
            $tokenClass = TokenInterface::TOKEN_TO_CLASS_MAP[$data[0]];
53
            $content = $data[1];
54
            $line = $data[2];
55
56
            if ($tokenClass === ConstantEncapsedStringToken::class || 0 === strpos($content, '/*')) {
57
                $tokens[] = new $tokenClass($content, $line);
58
59
                continue;
60
            }
61
62
            $splitContent = preg_split(
63
                '#(\r\n|\n| +)#',
64
                $content,
65
                -1,
66
               PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
67
            );
68
69
            foreach ($splitContent as $splitPart) {
70
                if ("\r\n" === $splitPart || "\n" === $splitPart) {
71
                    $tokens[] = new NewLineToken($splitPart, $line);
72
                    $line++;
73
74
                    continue;
75
                }
76
77
                if (0 === strpos($splitPart, ' ')) {
78
                    $tokens[] = new WhitespaceToken($splitPart, $line);
79
80
                    continue;
81
                }
82
83
                $tokens[] = new $tokenClass($splitPart, $line);
84
            }
85
        }
86
87
        return $tokens;
88
    }
89
}