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 ( ed246f...a32662 )
by Tomasz
04:49
created

WordpressParser::createFromHandlers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Thunder\Shortcode\Parser;
3
4
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
5
use Thunder\Shortcode\Shortcode\ParsedShortcode;
6
use Thunder\Shortcode\Shortcode\Shortcode;
7
8
/**
9
 * IMPORTANT NOTE: USE THIS PARSER **ONLY** IF YOU WANT THE FULL COMPATIBILITY
10
 * WITH WORDPRESS AND REPORT BUGS **ONLY** IF ITS BEHAVIOR IS DIFFERENT.
11
 *
12
 * This is a direct port of WordPress' shortcode parser with code copied from
13
 * its latest release (4.3.1 at the moment) adjusted to conform to this library.
14
 * Main regex was copied from get_shortcode_regex(), changed to handle all
15
 * shortcode names and folded into single string ($shortcodeRegex property),
16
 * method parseParameters() is a copy of function shortcode_parse_atts(). Code
17
 * was only structurally refactored for better readability. Read the comment
18
 * at the bottom of ParserTest::provideShortcodes() to understand the
19
 * limitations of this parser.
20
 *
21
 * @see https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/shortcodes.php#L239
22
 * @see https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/shortcodes.php#L448
23
 *
24
 * @author Tomasz Kowalczyk <[email protected]>
25
 */
26
final class WordpressParser implements ParserInterface
27
{
28
    private static $shortcodeRegex = '/\\[(\\[?)(<NAMES>)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)/s';
29
    private static $argumentsRegex = '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
30
31
    private $names;
32
33 1
    public static function createFromHandlers(HandlerContainer $handlers)
34
    {
35 1
        return static::createFromNames($handlers->getNames());
36
    }
37
38 2
    public static function createFromNames(array $names)
39
    {
40 2
        foreach($names as $name) {
41 2
            if(false === is_string($name)) {
42 1
                throw new \InvalidArgumentException('Shortcode name must be a string!');
43
            }
44 2
        }
45
46 1
        $self = new self();
47 1
        $self->names = $names;
48
49 1
        return $self;
50
    }
51
52
    /**
53
     * @param string $text
54
     *
55
     * @return ParsedShortcode[]
56
     */
57 32
    public function parse($text)
58
    {
59 32
        $names = $this->names
60 32
            ? implode('|', array_map('preg_quote', $this->names))
61 32
            : '[a-zA-Z-]+';
62 32
        $regex = str_replace('<NAMES>', $names, static::$shortcodeRegex);
0 ignored issues
show
Comprehensibility introduced by
Since Thunder\Shortcode\Parser\WordpressParser is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
63 32
        preg_match_all($regex, $text, $matches, PREG_OFFSET_CAPTURE);
64
65 32
        $shortcodes = array();
66 32
        $count = count($matches[0]);
67 32
        for($i = 0; $i < $count; $i++) {
68 25
            $name = $matches[2][$i][0];
69 25
            $parameters = static::parseParameters($matches[3][$i][0]);
0 ignored issues
show
Comprehensibility introduced by
Since Thunder\Shortcode\Parser\WordpressParser is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
70 25
            $content = $matches[5][$i][0] ?: null;
71 25
            $match = $matches[0][$i][0];
72 25
            $offset = mb_strlen(substr($text, 0, $matches[0][$i][1]), 'utf-8');
73
74 25
            $shortcode = new Shortcode($name, $parameters, $content, null);
75 25
            $shortcodes[] = new ParsedShortcode($shortcode, $match, $offset);
76 25
        }
77
78 32
        return $shortcodes;
79
    }
80
81 25
    private static function parseParameters($text)
82
    {
83 25
        $text = preg_replace('/[\x{00a0}\x{200b}]+/u', ' ', $text);
84
85 25
        if(!preg_match_all(static::$argumentsRegex, $text, $matches, PREG_SET_ORDER)) {
0 ignored issues
show
Comprehensibility introduced by
Since Thunder\Shortcode\Parser\WordpressParser is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
86 13
            return ltrim($text) ? array(ltrim($text) => null) : array();
87
        }
88
89 14
        $parameters = array();
90 14
        foreach($matches as $match) {
91 14
            if(!empty($match[1])) {
92 11
                $parameters[strtolower($match[1])] = stripcslashes($match[2]);
93 14
            } elseif(!empty($match[3])) {
94 1
                $parameters[strtolower($match[3])] = stripcslashes($match[4]);
95 9
            } elseif(!empty($match[5])) {
96 5
                $parameters[strtolower($match[5])] = stripcslashes($match[6]);
97 9
            } elseif(isset($match[7]) && strlen($match[7])) {
98 1
                $parameters[stripcslashes($match[7])] = null;
99 5
            } elseif(isset($match[8])) {
100 4
                $parameters[stripcslashes($match[8])] = null;
101 4
            }
102 14
        }
103
104 14
        foreach($parameters as $key => $value) {
105 14
            if(false !== strpos($value, '<') && 1 !== preg_match('/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value)) {
106 1
                $parameters[$key] = '';
107 1
            }
108 14
        }
109
110 14
        return $parameters;
111
    }
112
}
113