GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 9135f5...5265cb )
by Tomasz
02:37
created

RegexParser   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.56%

Importance

Changes 16
Bugs 6 Features 6
Metric Value
wmc 20
c 16
b 6
f 6
lcom 1
cbo 5
dl 0
loc 84
ccs 40
cts 41
cp 0.9756
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A parse() 0 13 2
A parseValue() 0 4 2
A extractValue() 0 6 2
A isDelimitedValue() 0 5 2
A parseParameters() 0 13 3
B parseSingle() 0 16 7
1
<?php
2
namespace Thunder\Shortcode\Parser;
3
4
use Thunder\Shortcode\Shortcode\ParsedShortcode;
5
use Thunder\Shortcode\Shortcode\Shortcode;
6
use Thunder\Shortcode\Syntax\Syntax;
7
use Thunder\Shortcode\Syntax\SyntaxInterface;
8
use Thunder\Shortcode\Utility\RegexBuilderUtility;
9
10
/**
11
 * @author Tomasz Kowalczyk <[email protected]>
12
 */
13
final class RegexParser implements ParserInterface
14
{
15
    /** @var SyntaxInterface */
16
    private $syntax;
17
    private $shortcodeRegex;
18
    private $singleShortcodeRegex;
19
    private $parametersRegex;
20
21 60
    public function __construct(SyntaxInterface $syntax = null)
22
    {
23 60
        $this->syntax = $syntax ?: new Syntax();
24 60
        $this->shortcodeRegex = RegexBuilderUtility::buildShortcodeRegex($this->syntax);
25 60
        $this->singleShortcodeRegex = RegexBuilderUtility::buildSingleShortcodeRegex($this->syntax);
26 60
        $this->parametersRegex = RegexBuilderUtility::buildParametersRegex($this->syntax);
27 60
    }
28
29
    /**
30
     * @param string $text
31
     *
32
     * @return ParsedShortcode[]
33
     */
34 101
    public function parse($text)
35
    {
36 101
        preg_match_all($this->shortcodeRegex, $text, $matches, PREG_OFFSET_CAPTURE);
37
38
        // loop instead of array_map to pass the arguments explicitly
39 101
        $shortcodes = array();
40 101
        foreach($matches[0] as $match) {
41 92
            $offset = mb_strlen(substr($text, 0, $match[1]), 'utf-8');
42 92
            $shortcodes[] = $this->parseSingle($match[0], $offset);
43 101
        }
44
45 101
        return array_filter($shortcodes);
46
    }
47
48 92
    private function parseSingle($text, $offset)
49
    {
50 92
        preg_match($this->singleShortcodeRegex, $text, $matches, PREG_OFFSET_CAPTURE);
51
52 92
        $name = $matches['name'][0];
53 92
        if(!preg_match('~^'.RegexBuilderUtility::buildNameRegex().'$~us', $name)) {
54
            return null;
55
        }
56 92
        $parameters = isset($matches['parameters'][0]) ? $this->parseParameters($matches['parameters'][0]) : array();
57 92
        $bbCode = isset($matches['bbCode'][0]) && $matches['bbCode'][1] !== -1
58 92
            ? $this->extractValue($matches['bbCode'][0])
59 92
            : null;
60 92
        $content = isset($matches['content'][0]) && $matches['content'][1] !== -1 ? $matches['content'][0] : null;
61
62 92
        return new ParsedShortcode(new Shortcode($name, $parameters, $content, $bbCode), $text, $offset);
63
    }
64
65 92
    private function parseParameters($text)
66
    {
67 92
        preg_match_all($this->parametersRegex, $text, $argsMatches);
68
69
        // loop because PHP 5.3 can't handle $this properly and I want separate methods
70 92
        $return = array();
71 92
        foreach ($argsMatches[1] as $item) {
72 35
            $parts = explode($this->syntax->getParameterValueSeparator(), $item, 2);
73 35
            $return[trim($parts[0])] = $this->parseValue(isset($parts[1]) ? $parts[1] : null);
74 92
        }
75
76 92
        return $return;
77
    }
78
79 35
    private function parseValue($value)
80
    {
81 35
        return null === $value ? null : $this->extractValue(trim($value));
82
    }
83
84 39
    private function extractValue($value)
85
    {
86 39
        $length = strlen($this->syntax->getParameterValueDelimiter());
87
88 39
        return $this->isDelimitedValue($value) ? substr($value, $length, -1 * $length) : $value;
89
    }
90
91 39
    private function isDelimitedValue($value)
92
    {
93 39
        return preg_match('/^'.$this->syntax->getParameterValueDelimiter().'/us', $value)
94 39
            && preg_match('/'.$this->syntax->getParameterValueDelimiter().'$/us', $value);
95
    }
96
}
97