Passed
Branch fuck-54 (e8694d)
by Kacper
02:57
created

TokenFactory::getOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2015, 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;
17
18
19
class TokenFactory implements TokenFactoryInterface
20
{
21
    const START = 0x1;
22
    const END   = 0x2;
23
24
    private $_class;
25
    private $_base;
26
    private $_rule;
27
    private $_offset;
28
    private $_type = 0x3;
29
30
    private $_cache = [];
31
32
33
    /**
34
     * TokenFactory constructor.
35
     *
36
     * @param $class
37
     */
38 61
    public function __construct($class) {
39 61
        $this->setClass($class);
40 60
    }
41
42 47
    public function create($params) {
43 47
        $params[0] = !empty($params[0]) ? $this->getName($params[0]) : $this->_base;
44 47
        if(empty($params['rule'])) {
45 42
            $params['rule'] = $this->_rule;
46 42
        }
47
48 47
        if(isset($params['pos'])) {
49 44
            $params['pos'] += $this->_offset;
50 44
        }
51
52 47
        $end = null;
53
54 47
        if (isset($params['length']) && ($this->_type & self::END)) {
55 37
            $end = $params;
56 37
            $end['pos'] += $params['length'];
57
58 37
            $params['end'] = new $this->_class($end);
59 37
        }
60
61
        /** @var Token $token */
62 47
        $token = new $this->_class($params);
63
64 47
        if($this->_type == 0x3) {
65 47
            return $token;
66
        }
67
68 2
        if($this->_type === self::START) {
69 2
            $token->setEnd(false);
70 2
            return $token;
71
        } else {
72 1
            $token->getEnd()->setStart(false);
73 1
            return $token->getEnd();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $token->getEnd(); of type Kadet\Highlighter\Parser\Token|null|false adds false to the return on line 73 which is incompatible with the return type declared by the interface Kadet\Highlighter\Parser...actoryInterface::create of type Kadet\Highlighter\Parser\Token. It seems like you forgot to handle an error condition.
Loading history...
74
        }
75
    }
76
77 23
    private function getName($name) {
78 23
        if (!isset($this->_cache[$name])) {
79 23
            $this->_cache[$name] = str_replace('$', $this->_base, $name);
80 23
        }
81
82 23
        return $this->_cache[$name];
83
    }
84
85
    /**
86
     * @return string
87
     */
88 1
    public function getBase()
89
    {
90 1
        return $this->_base;
91
    }
92
93
    /**
94
     * @param string $base
95
     */
96 13
    public function setBase($base)
97
    {
98 13
        $this->_cache = []; // invalidate cache
99 13
        $this->_base = $base;
100 13
    }
101
102
    /**
103
     * @return mixed
104
     */
105 1
    public function getRule()
106
    {
107 1
        return $this->_rule;
108
    }
109
110
    /**
111
     * @param mixed $rule
112
     */
113 58
    public function setRule($rule)
114
    {
115 58
        $this->_rule = $rule;
116 58
    }
117
118
    /**
119
     * @return mixed
120
     */
121 1
    public function getClass()
122
    {
123 1
        return $this->_class;
124
    }
125
126
    /**
127
     * @return int
128
     */
129
    public function getOffset()
130
    {
131
        return $this->_offset;
132
    }
133
134
    /**
135
     * @param int $offset
136
     */
137 13
    public function setOffset($offset)
138
    {
139 13
        $this->_offset = $offset;
140 13
    }
141
142
143
144
    /**
145
     * @param mixed $class
146
     *
147
     * @throws \InvalidArgumentException
148
     */
149 61
    public function setClass($class)
150
    {
151 61
        if(!is_a($class, 'Kadet\Highlighter\Parser\Token', true)) {
152 1
            throw new \InvalidArgumentException('$class must extend Kadet\Highlighter\Parser\Token');
153
        }
154
155 60
        $this->_class = $class;
156 60
    }
157
158 2
    public function setType($type)
159
    {
160 2
        $this->_type = $type;
161 2
    }
162
163
    public function getType()
164
    {
165
        return $this->_type;
166
    }
167
}