Passed
Push — master ( 203740...2c4c0a )
by Kacper
04:22
created

TokenFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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
 * Factory used to handle various token creation.
20
 *
21
 * @package Kadet\Highlighter\Parser
22
 */
23
class TokenFactory implements TokenFactoryInterface
24
{
25
    private $_class;
26
    private $_base;
27
    private $_rule;
28
    private $_offset;
29
    private $_type = 0x3;
30
31
    private $_cache = [];
32
33
    /**
34
     * Constructor
35
     *
36
     * @param string $class {@see }
37
     */
38 63
    public function __construct($class) {
39 63
        $this->setClass($class);
40 62
    }
41
42
    /**
43
     * @param $params
44
     *
45
     * @return Token|null|false
46
     */
47 49
    public function create($params) {
48 49
        $params[0] = !empty($params[0]) ? $this->getName($params[0]) : $this->_base;
49 49
        if(empty($params['rule'])) {
50 43
            $params['rule'] = $this->_rule;
51 43
        }
52
53 49
        if(isset($params['pos'])) {
54 46
            $params['pos'] += $this->_offset;
55 46
        }
56
57 49
        $end = null;
58
59 49
        if (isset($params['length']) && ($this->_type & Token::END)) {
60 39
            $end = $params;
61 39
            $end['pos'] += $params['length'];
62
63 39
            $params['end'] = new $this->_class($end);
64 39
        }
65
66
        /** @var Token $token */
67 49
        $token = new $this->_class($params);
68
69 49
        if($this->_type == 0x3) {
70 49
            return $token;
71
        }
72
73 2
        if($this->_type === Token::START) {
74 2
            $token->setEnd(false);
75 2
            return $token;
76
        } else {
77 1
            $token->getEnd()->setStart(false);
78 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 78 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...
79 1
        }
80
    }
81
82 25
    private function getName($name) {
83 25
        if (!isset($this->_cache[$name])) {
84 25
            $this->_cache[$name] = str_replace('$', $this->_base, $name);
85 25
        }
86
87 25
        return $this->_cache[$name];
88
    }
89
90
    /**
91
     * @param string $base
92
     */
93 13
    public function setBase($base)
94
    {
95 13
        $this->_cache = []; // invalidate cache
96 13
        $this->_base = $base;
97 13
    }
98
99
    /**
100
     * @param mixed $rule
101
     */
102 61
    public function setRule($rule)
103
    {
104 61
        $this->_rule = $rule;
105 61
    }
106
107
    /**
108
     * @param int $offset
109
     */
110 13
    public function setOffset($offset)
111
    {
112 13
        $this->_offset = $offset;
113 13
    }
114
115
    /**
116
     * @param mixed $class
117
     *
118
     * @throws \InvalidArgumentException
119
     */
120 63
    public function setClass($class)
121
    {
122 63
        if(!is_a($class, 'Kadet\Highlighter\Parser\Token', true)) {
123 1
            throw new \InvalidArgumentException('$class must extend Kadet\Highlighter\Parser\Token');
124
        }
125
126 62
        $this->_class = $class;
127 62
    }
128
129 2
    public function setType($type)
130
    {
131 2
        $this->_type = $type;
132
    }
133
}