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

TokenFactory::create()   F

Complexity

Conditions 15
Paths 1472

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 15

Importance

Changes 9
Bugs 1 Features 0
Metric Value
c 9
b 1
f 0
dl 0
loc 53
ccs 36
cts 36
cp 1
rs 3.2569
cc 15
eloc 30
nc 1472
nop 2
crap 15

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
17
18
use Kadet\Highlighter\Parser\Token\Token;
19
20
/**
21
 * Factory used to handle various token creation.
22
 *
23
 * @package Kadet\Highlighter\Parser
24
 */
25
class TokenFactory implements TokenFactoryInterface
26
{
27
    private $_class;
28
    private $_base;
29
    private $_rule;
30
    private $_offset;
31
    private $_type = 0x3;
32
33
    private $_cache = [];
34
35
    /**
36
     * Constructor
37
     *
38
     * @param string $class {@see }
39
     */
40 62
    public function __construct($class)
41
    {
42 62
        $this->setClass($class);
43 61
    }
44
45
    /**
46
     * @param       $name
47
     * @param array $params
48
     *
49
     * @return false|Token|null
50
     */
51 46
    public function create($name, $params = [ ])
52
    {
53 46
        $name = $name !== null ? $this->getName($name) : $this->_base;
54
55 46
        if (!isset($params['rule'])) {
56 42
            $params['rule'] = $this->_rule;
57 42
        }
58
59 46
        if (isset($params['pos'])) {
60 43
            $params['pos'] += $this->_offset;
61 43
        }
62
63 46
        $class = isset($params['class']) ? $params['class'] : $this->_class;
64 46
        $end   = isset($params['end'])   ? $params['end']   : null;
0 ignored issues
show
Bug Compatibility introduced by
The expression isset($params['end']) ? $params['end'] : null; of type integer|null adds the type integer to the return on line 97 which is incompatible with the return type declared by the interface Kadet\Highlighter\Parser...actoryInterface::create of type false|Kadet\Highlighter\Parser\Token\Token|null.
Loading history...
65 46
        $start = isset($params['start']) ? $params['start'] : null;
0 ignored issues
show
Bug Compatibility introduced by
The expression isset($params['start']) ...params['start'] : null; of type integer|null adds the type integer to the return on line 77 which is incompatible with the return type declared by the interface Kadet\Highlighter\Parser...actoryInterface::create of type false|Kadet\Highlighter\Parser\Token\Token|null.
Loading history...
Bug Compatibility introduced by
The expression isset($params['start']) ...params['start'] : null; of type integer|null adds the type integer to the return on line 102 which is incompatible with the return type declared by the interface Kadet\Highlighter\Parser...actoryInterface::create of type false|Kadet\Highlighter\Parser\Token\Token|null.
Loading history...
66
67
        // we don't want to pass that into token
68 46
        unset($params['class'], $params['end'], $params['start']);
69
70 46
        if($this->_type & Token::START) {
71 46
            if($start === null) {
72 46
                $start = new $class($name, $params);
73 46
            }
74
75 46
            if($this->_type === Token::START) {
76 2
                $start->setEnd(false);
77 2
                return $start;
78
            }
79 46
        }
80
81 46
        if($this->_type & Token::END) {
82 46
            if (isset($params['length'])) {
83 38
                $length = $params['length'];
84 38
                unset($params['length']);
85
86 38
                $end = $params;
87 38
                $end['pos'] += $length;
88
89
                /** @var Token $end */
90 38
                $end = new $class($name, $end);
91 46
            } elseif($end === null) {
92 8
                $end = new $class($name, $params);
93 8
            }
94
95 46
            if($this->_type === Token::END || $start === null) {
96 1
                $end->setStart(false);
97 1
                return $end;
98
            }
99 46
        }
100
101 46
        $start->setEnd($end);
102 46
        return $start;
103
    }
104
105 19
    private function getName($name)
106
    {
107 19
        if (!isset($this->_cache[$name])) {
108 19
            $this->_cache[$name] = str_replace('$', $this->_base, $name);
109 19
        }
110
111 19
        return $this->_cache[$name];
112
    }
113
114
    /**
115
     * @param string $base
116
     */
117 17
    public function setBase($base)
118
    {
119 17
        $this->_cache = []; // invalidate cache
120 17
        $this->_base  = $base;
121 17
    }
122
123
    /**
124
     * @param mixed $rule
125
     */
126 59
    public function setRule($rule)
127
    {
128 59
        $this->_rule = $rule;
129 59
    }
130
131
    /**
132
     * @param int $offset
133
     */
134 13
    public function setOffset($offset)
135
    {
136 13
        $this->_offset = $offset;
137 13
    }
138
139
    /**
140
     * @param mixed $class
141
     *
142
     * @throws \InvalidArgumentException
143
     */
144 62
    public function setClass($class)
145
    {
146 62
        if (!is_a($class, 'Kadet\Highlighter\Parser\Token\Token', true)) {
147 1
            throw new \InvalidArgumentException('$class must extend Kadet\Highlighter\Parser\Token\Token');
148
        }
149
150 61
        $this->_class = $class;
151 61
    }
152
153 2
    public function setType($type)
154
    {
155 2
        $this->_type = $type;
156 2
    }
157
}
158