Completed
Branch 0.8-dev (5d9532)
by Kacper
02:49
created

LanguageToken::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 4
cts 6
cp 0.6667
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2.1481
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
use Kadet\Highlighter\Language\Language;
19
use Kadet\Highlighter\Parser\Context;
20
use Kadet\Highlighter\Parser\Result;
21
use Kadet\Highlighter\Parser\TokenIterator;
22
23
/**
24
 * Class LanguageToken
25
 *
26
 * @package Kadet\Highlighter\Parser\Token
27
 *
28
 * @property bool     $postProcess True if language is post processed.
29
 * @property Language $inject
30
 */
31
class LanguageToken extends Token
32
{
33 12
    public function __construct($name, $options = []) {
34 12
        parent::__construct($name, $options);
35 12
        if(isset($options['inject'])) {
36
            $this->inject = $options['inject'];
37
        }
38 12
    }
39
40 11
    public function getLanguage()
41 2
    {
42 11
        return $this->getStart() ? $this->getStart()->inject : $this->rule->language;
1 ignored issue
show
Documentation introduced by
The property inject does not exist on object<Kadet\Highlighter\Parser\Token\Token>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
    }
44
45 11
    protected function validate(Context $context)
46
    {
47 11
        $valid = false;
48
49 11
        if ($this->isStart()) {
50 2
            $lang = $this->rule->language;
51 2
            if ($lang === null && $this->inject !== $context->language) {
52 1
                $valid = true;
53 2
            } elseif ($context->language === $lang && $this->rule->validator->validate($context)) {
54 1
                $valid = true;
55 1
            }
56 2
        } else {
57 11
            $desired = $this->getLanguage();
58 11
            $valid   = $context->language === $desired && $this->rule->validator->validate($context);
59
        }
60 11
        $this->setValid($valid);
61 11
    }
62
    
63
64 2
    protected function processStart(Context $context, Language $language, Result $result, TokenIterator $tokens)
65
    {
66 2
        $result->merge($this->inject->parse($tokens));
67
68 2
        return true;
69
    }
70
71 11
    protected function processEnd(Context $context, Language $language, Result $result, TokenIterator $tokens)
72
    {
73 11
        $this->setStart($result->getStart());
74
75 11
        if ($this->_start->postProcess) {
76 1
            $source = substr($tokens->getSource(), $this->_start->pos, $this->_start->getLength());
77 1
            $tokens = $this->_start->inject->tokenize(
78 1
                $source, $result, $this->_start->pos, Language::EMBEDDED_BY_PARENT
79 1
            );
80 1
            $result->exchangeArray($this->_start->inject->parse($tokens)->getTokens());
81 1
        }
82
83
        # closing unclosed tokens
84 11
        foreach (array_reverse($context->stack, true) as $id => $name) {
85 1
            $end = new Token($name, ['pos' => $this->pos]);
86 1
            $tokens[$id]->setEnd($end);
87 1
            $result->append($end);
88 11
        }
89
90 11
        $result->append($this);
91 11
        return false;
92
    }
93
94
95
}
96