Completed
Branch 0.8-dev (8bed0e)
by Kacper
02:45
created

TerminatorToken   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 2
cbo 4
dl 0
loc 31
ccs 0
cts 17
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 7 2
A processStart() 0 4 1
A processEnd() 0 14 2
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Parser\Token;
17
18
19
use Kadet\Highlighter\Language\Language;
20
use Kadet\Highlighter\Parser\Context;
21
use Kadet\Highlighter\Parser\Result;
22
use Kadet\Highlighter\Parser\TokenIterator;
23
24
/**
25
 * Class TerminatorToken
26
 *
27
 * @package Kadet\Highlighter\Parser\Token
28
 *
29
 * @property array $closes
30
 */
31
class TerminatorToken extends MetaToken
32
{
33
    protected function validate(Context $context)
34
    {
35
        $this->setValid(
36
            $context->language === $this->rule->language &&
37
            $this->rule->validator->validate($context)
38
        );
39
    }
40
41
    protected function processStart(Context $context, Language $language, Result $result, TokenIterator $tokens)
42
    {
43
        return true; // That type of token makes no sense as start, just omit it.
44
    }
45
46
    protected function processEnd(Context $context, Language $language, Result $result, TokenIterator $tokens)
47
    {
48
        foreach(array_filter($context->stack, function ($name) {
49
            return in_array($name, $this->closes);
50
        }) as $hash => $name) {
51
            $end = new Token([$name, 'pos' => $this->pos]);
52
            $tokens[$hash]->setEnd($end);
53
            $result->append($end);
54
55
            unset($context->stack[$hash]);
56
        }
57
58
        return true;
59
    }
60
61
}
62