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 ( 79a219...884dd9 )
by Tomasz
01:20
created

WordpressParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
use Thunder\Shortcode\Utility\RegexBuilderUtility;
8
9
/**
10
 * IMPORTANT NOTE: USE THIS PARSER **ONLY** IF YOU WANT THE FULL COMPATIBILITY
11
 * WITH WORDPRESS AND REPORT BUGS **ONLY** IF ITS BEHAVIOR IS DIFFERENT.
12
 *
13
 * This is a direct port of WordPress' shortcode parser with code copied from
14
 * its latest release (4.3.1 at the moment) adjusted to conform to this library.
15
 * Main regex was copied from get_shortcode_regex(), changed to handle all
16
 * shortcode names and folded into single string ($shortcodeRegex property),
17
 * method parseParameters() is a copy of function shortcode_parse_atts(). Code
18
 * was only structurally refactored for better readability. Read the comment
19
 * at the bottom of ParserTest::provideShortcodes() to understand the
20
 * limitations of this parser.
21
 *
22
 * @see https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/shortcodes.php#L239
23
 * @see https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/shortcodes.php#L448
24
 *
25
 * @author Tomasz Kowalczyk <[email protected]>
26
 */
27
final class WordpressParser implements ParserInterface
28
{
29
    /** @var string */
30
    private static $shortcodeRegex = '/\\[(\\[?)(<NAMES>)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)/s';
31
    /** @var string */
32
    private static $argumentsRegex = '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
33
34
    /** @var string[] */
35
    private $names = array();
36
37 2
    public function __construct()
38
    {
39 2
    }
40
41
    /** @return self */
42 1
    public static function createFromHandlers(HandlerContainer $handlers)
43
    {
44 1
        return static::createFromNames($handlers->getNames());
45
    }
46
47
    /**
48
     * @param string[] $names
49
     *
50
     * @return self
51
     */
52 2
    public static function createFromNames(array $names)
53
    {
54 2
        foreach($names as $name) {
55
            /** @psalm-suppress DocblockTypeContradiction, RedundantConditionGivenDocblockType */
56 2
            if(false === is_string($name)) {
57 1
                throw new \InvalidArgumentException('Shortcode name must be a string!');
58
            }
59 2
        }
60
61 1
        $self = new self();
62 1
        $self->names = $names;
63
64 1
        return $self;
65
    }
66
67
    /**
68
     * @param string $text
69
     *
70
     * @return ParsedShortcode[]
71
     */
72 39
    public function parse($text)
73
    {
74 39
        $names = $this->names
75 39
            ? implode('|', array_map('preg_quote', $this->names))
76 39
            : RegexBuilderUtility::buildNameRegex();
77 39
        $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...
78 39
        preg_match_all($regex, $text, $matches, PREG_OFFSET_CAPTURE);
79
80 39
        $shortcodes = array();
81 39
        $count = count($matches[0]);
82 39
        for($i = 0; $i < $count; $i++) {
83 33
            $name = $matches[2][$i][0];
84 33
            $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...
85 33
            $content = $matches[5][$i][1] !== -1 ? $matches[5][$i][0] : null;
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $matches[5][$i][1] (string) and -1 (integer) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
86 33
            $match = $matches[0][$i][0];
87 33
            $offset = mb_strlen(substr($text, 0, $matches[0][$i][1]), 'utf-8');
88
89 33
            $shortcode = new Shortcode($name, $parameters, $content, null);
90 33
            $shortcodes[] = new ParsedShortcode($shortcode, $match, $offset);
91 33
        }
92
93 39
        return $shortcodes;
94
    }
95
96
    /**
97
     * @param string $text
98
     *
99
     * @psalm-return array<string,string|null>
100
     */
101 33
    private static function parseParameters($text)
102
    {
103 33
        $text = preg_replace('/[\x{00a0}\x{200b}]+/u', ' ', $text);
104
105 33
        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...
106 19
            return ltrim($text) ? array(ltrim($text) => null) : array();
107
        }
108
109 17
        $parameters = array();
110 17
        foreach($matches as $match) {
111 17
            if(!empty($match[1])) {
112 11
                $parameters[strtolower($match[1])] = stripcslashes($match[2]);
113 17
            } elseif(!empty($match[3])) {
114 1
                $parameters[strtolower($match[3])] = stripcslashes($match[4]);
115 12
            } elseif(!empty($match[5])) {
116 8
                $parameters[strtolower($match[5])] = stripcslashes($match[6]);
117 12
            } elseif(isset($match[7]) && $match[7] !== '') {
118 1
                $parameters[stripcslashes($match[7])] = null;
119 6
            } elseif(isset($match[8])) {
120 6
                $parameters[stripcslashes($match[8])] = null;
121 6
            }
122 17
        }
123
124 17
        foreach($parameters as $key => $value) {
125
            // NOTE: the `?: ''` fallback is the only change from the way WordPress parses shortcodes to satisfy Psalm's PossiblyNullArgument
126 17
            $value = $value ?: '';
127 17
            if(false !== strpos($value, '<') && 1 !== preg_match('/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value)) {
128 1
                $parameters[$key] = '';
129 1
            }
130 17
        }
131
132 17
        return $parameters;
133
    }
134
}
135