Factory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 59
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 5
A isLookahead() 0 4 1
A isMultistate() 0 4 1
1
<?php
2
/**
3
 * This file is part of Lexer package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Lexer;
11
12
use Railt\Lexer\Driver\NativeRegex;
13
use Railt\Lexer\Driver\ParleLexer;
14
15
/**
16
 * Class Lexer
17
 */
18
class Factory
19
{
20
    /**
21
     * The factory should return any implementation supporting the PCRE lookahead syntax.
22
     *
23
     * @var int
24
     */
25
    public const LOOKAHEAD = 0x02;
26
27
    /**
28
     * The factory should return any multistate implementation.
29
     *
30
     * @var int
31
     */
32
    public const MULTISTATE = 0x04;
33
34
    /**
35
     * @param array $tokens
36
     * @param array $skip
37
     * @param int $flags
38
     * @return LexerInterface|SimpleLexerInterface|MultistateLexerInterface
39
     * @throws Exception\BadLexemeException
40
     * @throws \InvalidArgumentException
41
     */
42
    public static function create(array $tokens, array $skip = [], int $flags = self::LOOKAHEAD): LexerInterface
43
    {
44
        switch (true) {
45
            case self::isMultistate($flags):
46
                $error = \vsprintf('Multistate %slexer does not implemented yet', [
47
                    self::isLookahead($flags) ? 'lookahead ' : '',
48
                ]);
49
                throw new \InvalidArgumentException($error);
50
51
            case ! self::isLookahead($flags) && \class_exists(\Parle\Lexer::class, false):
52
                return new ParleLexer($tokens, $skip);
53
54
            default:
55
                return new NativeRegex($tokens, $skip);
56
        }
57
    }
58
59
    /**
60
     * @param int $flags
61
     * @return bool
62
     */
63
    private static function isLookahead(int $flags): bool
64
    {
65
        return (bool)($flags & self::LOOKAHEAD);
66
    }
67
68
    /**
69
     * @param int $flags
70
     * @return bool
71
     */
72
    private static function isMultistate(int $flags): bool
73
    {
74
        return (bool)($flags & self::MULTISTATE);
75
    }
76
}
77