Completed
Push — master ( 82eb41...4f6b0a )
by Alexander
03:49
created

GlobSubLexer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 2
dl 0
loc 32
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenAt() 0 15 4
A decodeGlob() 0 10 1
1
<?php
2
namespace Xiag\Rql\Parser\SubLexer;
3
4
use Xiag\Rql\Parser\Token;
5
use Xiag\Rql\Parser\SubLexerInterface;
6
use Xiag\Rql\Parser\Glob;
7
8
class GlobSubLexer implements SubLexerInterface
9
{
10
    /**
11
     * @inheritdoc
12
     */
13 84
    public function getTokenAt($code, $cursor)
14
    {
15 84
        if (!preg_match('/([a-z0-9\*\?]|\%[0-9a-f]{2})+/Ai', $code, $matches, null, $cursor)) {
16 14
            return null;
17 84
        } elseif (strpos($matches[0], '?') === false && strpos($matches[0], '*') === false) {
18 84
            return null;
19
        }
20
21 22
        return new Token(
22 22
            Token::T_GLOB,
23 22
            $this->decodeGlob($matches[0]),
24 22
            $cursor,
25 22
            $cursor + strlen($matches[0])
26 22
        );
27
    }
28
29 22
    private function decodeGlob($glob)
30
    {
31 22
        return preg_replace_callback(
32 22
            '/[^\*\?]+/i',
33 22
            function ($encoded) {
34 13
                return Glob::encode(rawurldecode($encoded[0]));
35 22
            },
36
            $glob
37 22
        );
38
    }
39
}
40