Completed
Pull Request — master (#1184)
by Alexey
12:56
created

Token   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 251
rs 10
wmc 16

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setKept() 0 6 1
A getOffset() 0 3 1
A isKept() 0 3 1
A setOffset() 0 6 1
A getUnificationIndex() 0 3 1
A getTokenName() 0 3 1
A setValue() 0 6 1
A getAST() 0 16 3
A getRepresentation() 0 3 1
A setNamespace() 0 6 1
A getNamespace() 0 3 1
A setRepresentation() 0 6 1
A __construct() 0 14 1
A getValue() 0 3 1
1
<?php
2
/**
3
 * Hoa
4
 *
5
 *
6
 * @license
7
 *
8
 * BSD 3-Clause License
9
 *
10
 * Copyright © 2007-2017, Hoa community. All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions are met:
14
 *
15
 * 1. Redistributions of source code must retain the above copyright notice, this
16
 *    list of conditions and the following disclaimer.
17
 *
18
 * 2. Redistributions in binary form must reproduce the above copyright notice,
19
 *    this list of conditions and the following disclaimer in the documentation
20
 *    and/or other materials provided with the distribution.
21
 *
22
 * 3. Neither the name of the copyright holder nor the names of its
23
 *    contributors may be used to endorse or promote products derived from
24
 *    this software without specific prior written permission.
25
 *
26
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 */
38
39
namespace JMS\Serializer\Type\Compiler\Llk\Rule;
40
41
use JMS\Serializer\Type\Compiler;
42
use Hoa\File;
0 ignored issues
show
Bug introduced by
The type Hoa\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
44
/**
45
 * Class \JMS\Serializer\Type\Compiler\Llk\Rule\Token.
46
 *
47
 * The token rule.
48
 *
49
 * @copyright  Copyright © 2007-2017 Hoa community
50
 * @license    New BSD License
51
 */
52
final class Token extends Rule
0 ignored issues
show
Deprecated Code introduced by
The class JMS\Serializer\Type\Compiler\Llk\Rule\Rule has been deprecated: alias ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

52
final class Token extends /** @scrutinizer ignore-deprecated */ Rule
Loading history...
53
{
54
    /**
55
     * LL(k) compiler of hoa://Library/Regex/Grammar.pp.
56
     *
57
     * @var \JMS\Serializer\Type\Compiler\Llk\Parser|null
58
     */
59
    protected static $_regexCompiler = null;
60
61
    /**
62
     * Token name.
63
     *
64
     * @var string|null
65
     */
66
    protected $_tokenName            = null;
67
68
    /**
69
     * Namespace.
70
     *
71
     * @var string|null
72
     */
73
    protected $_namespace            = null;
74
75
    /**
76
     * Token representation.
77
     *
78
     * @var string|null
79
     */
80
    protected $_regex                = null;
81
82
    /**
83
     * AST of the regex.
84
     *
85
     * @var \JMS\Serializer\Type\Compiler\Llk\TreeNode
86
     */
87
    protected $_ast;
88
89
    /**
90
     * Token value.
91
     *
92
     * @var string|null
93
     */
94
    protected $_value                = null;
95
96
    /**
97
     * Whether the token is kept or not in the AST.
98
     *
99
     * @var bool
100
     */
101
    protected $_kept                 = false;
102
103
    /**
104
     * Unification index.
105
     *
106
     * @var int
107
     */
108
    protected $_unification          = -1;
109
110
    /**
111
     * Token offset.
112
     *
113
     * @var int
114
     */
115
    protected $_offset               = 0;
116
117
    /**
118
     * Constructor.
119
     *
120
     * @param   string|int  $name           Name.
121
     * @param   string  $tokenName      Token name.
122
     * @param   string|null  $nodeId         Node ID.
123
     * @param   int     $unification    Unification index.
124
     * @param   bool    $kept           Whether the token is kept or not in the AST.
125
     */
126
    public function __construct(
127
        $name,
128
        $tokenName,
129
        $nodeId,
130
        $unification,
131
        $kept = false
132
    ) {
133
        parent::__construct($name, null, $nodeId);
134
135
        $this->_tokenName   = $tokenName;
136
        $this->_unification = $unification;
137
        $this->setKept($kept);
138
139
        return;
140
    }
141
142
    /**
143
     * Get token name.
144
     *
145
     * @return string|null
146
     */
147
    public function getTokenName()
148
    {
149
        return $this->_tokenName;
150
    }
151
152
    /**
153
     * Set token namespace.
154
     *
155
     * @param   string  $namespace    Namespace.
156
     * @return  string
157
     */
158
    public function setNamespace($namespace)
159
    {
160
        $old              = $this->_namespace;
161
        $this->_namespace = $namespace;
162
163
        return $old;
164
    }
165
166
    /**
167
     * Get token namespace.
168
     *
169
     * @return  string
170
     */
171
    public function getNamespace()
172
    {
173
        return $this->_namespace;
174
    }
175
176
    /**
177
     * Set representation.
178
     *
179
     * @param   string  $regex    Representation.
180
     * @return  string|null
181
     */
182
    public function setRepresentation(string $regex)
183
    {
184
        $old          = $this->_regex;
185
        $this->_regex = $regex;
186
187
        return $old;
188
    }
189
190
    /**
191
     * Get token representation.
192
     *
193
     * @return  string|null
194
     */
195
    public function getRepresentation()
196
    {
197
        return $this->_regex;
198
    }
199
200
    /**
201
     * Get AST of the token representation.
202
     *
203
     * @return  \JMS\Serializer\Type\Compiler\Llk\TreeNode
204
     */
205
    public function getAST()
206
    {
207
        if (null === static::$_regexCompiler) {
208
            $stream = new File\Read('hoa://Library/Regex/Grammar.pp');
0 ignored issues
show
Bug introduced by
The type Hoa\File\Read was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
209
            $stream->rewind();
210
211
            static::$_regexCompiler = Compiler\Llk::load($stream);
212
        }
213
214
        if (null === $this->_ast) {
215
            $this->_ast = static::$_regexCompiler->parse(
216
                (string) $this->getRepresentation()
217
            );
218
        }
219
220
        return $this->_ast;
221
    }
222
223
    /**
224
     * Set token value.
225
     *
226
     * @param   string  $value    Value.
227
     * @return  string
228
     */
229
    public function setValue(string $value)
230
    {
231
        $old          = $this->_value;
232
        $this->_value = $value;
233
234
        return $old;
235
    }
236
237
    /**
238
     * Get token value.
239
     *
240
     * @return  string
241
     */
242
    public function getValue(): string
243
    {
244
        return $this->_value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_value could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
245
    }
246
247
    /**
248
     * Set token offset.
249
     *
250
     * @param   int  $offset    Offset.
251
     * @return  int
252
     */
253
    public function setOffset($offset)
254
    {
255
        $old           = $this->_offset;
256
        $this->_offset = $offset;
257
258
        return $old;
259
    }
260
261
    /**
262
     * Get token offset.
263
     *
264
     * @return int
265
     */
266
    public function getOffset()
267
    {
268
        return $this->_offset;
269
    }
270
271
    /**
272
     * Set whether the token is kept or not in the AST.
273
     *
274
     * @param   bool  $kept    Kept.
275
     * @return  bool
276
     */
277
    public function setKept($kept)
278
    {
279
        $old         = $this->_kept;
280
        $this->_kept = $kept;
281
282
        return $old;
283
    }
284
285
    /**
286
     * Check whether the token is kept in the AST or not.
287
     *
288
     * @return  bool
289
     */
290
    public function isKept()
291
    {
292
        return $this->_kept;
293
    }
294
295
    /**
296
     * Get unification index.
297
     *
298
     * @return  int
299
     */
300
    public function getUnificationIndex()
301
    {
302
        return $this->_unification;
303
    }
304
}
305