Issues (9)

src/PcreExtension.php (4 issues)

1
<?php
2
3
namespace Jasny\Twig;
4
5
use Twig\Error\RuntimeError;
6
use Twig\Extension\AbstractExtension;
7
use Twig\TwigFilter;
8
9
/**
10
 * Expose the PCRE functions to Twig.
11
 *
12
 * @see http://php.net/manual/en/book.pcre.php
13
 */
14
class PcreExtension extends AbstractExtension
15
{
16
    /**
17
     * Class constructor
18
     */
19 27
    public function __construct()
20
    {
21 27
        if (!extension_loaded('pcre')) {
22
            throw new \Exception("The Twig PCRE extension requires the PCRE extension."); // @codeCoverageIgnore
23
        }
24 27
    }
25
26
    /**
27
     * Return extension name
28
     *
29
     * @return string
30
     */
31 1
    public function getName()
32
    {
33 1
        return 'jasny/pcre';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 26
    public function getFilters()
40
    {
41
        return [
42 26
            new TwigFilter('preg_quote', [$this, 'quote']),
43 26
            new TwigFilter('preg_match', [$this, 'match']),
44 26
            new TwigFilter('preg_get', [$this, 'get']),
45 26
            new TwigFilter('preg_get_all', [$this, 'getAll']),
46 26
            new TwigFilter('preg_grep', [$this, 'grep']),
47 26
            new TwigFilter('preg_replace', [$this, 'replace']),
48 26
            new TwigFilter('preg_filter', [$this, 'filter']),
49 26
            new TwigFilter('preg_split', [$this, 'split']),
50
        ];
51
    }
52
53
54
    /**
55
     * Check that the regex doesn't use the eval modifier
56
     *
57
     * @param string $pattern
58
     * @throws \LogicException
59
     */
60 8
    protected function assertNoEval($pattern)
61
    {
62 8
        $pos = strrpos($pattern, $pattern[0]);
63 8
        $modifiers = substr($pattern, $pos + 1);
64
65 8
        if (strpos($modifiers, 'e') !== false) {
66 2
            throw new RuntimeError("Using the eval modifier for regular expressions is not allowed");
67
        }
68 6
    }
69
70
71
    /**
72
     * Quote regular expression characters.
73
     *
74
     * @param string $value
75
     * @param string $delimiter
76
     * @return string
77
     */
78 3
    public function quote($value, $delimiter = '/')
79
    {
80 3
        if (!isset($value)) {
81 1
            return null;
82
        }
83
84 2
        return preg_quote($value, $delimiter);
85
    }
86
87
    /**
88
     * Perform a regular expression match.
89
     *
90
     * @param string $value
91
     * @param string $pattern
92
     * @return boolean
93
     */
94 4
    public function match($value, $pattern)
95
    {
96 4
        if (!isset($value)) {
97 1
            return null;
98
        }
99
100 3
        return preg_match($pattern, $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match($pattern, $value) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
101
    }
102
103
    /**
104
     * Perform a regular expression match and return a matched group.
105
     *
106
     * @param string $value
107
     * @param string $pattern
108
     * @return string
109
     */
110 3
    public function get($value, $pattern, $group = 0)
111
    {
112 3
        if (!isset($value)) {
113 1
            return null;
114
        }
115
116 2
        return preg_match($pattern, $value, $matches) && isset($matches[$group]) ? $matches[$group] : null;
117
    }
118
119
    /**
120
     * Perform a regular expression match and return the group for all matches.
121
     *
122
     * @param string $value
123
     * @param string $pattern
124
     * @return array
125
     */
126 3
    public function getAll($value, $pattern, $group = 0)
127
    {
128 3
        if (!isset($value)) {
129 1
            return null;
130
        }
131
132 2
        return preg_match_all($pattern, $value, $matches, PREG_PATTERN_ORDER) && isset($matches[$group])
133 2
            ? $matches[$group] : [];
134
    }
135
136
    /**
137
     * Perform a regular expression match and return an array of entries that match the pattern
138
     *
139
     * @param array  $values
140
     * @param string $pattern
141
     * @param string $flags    Optional 'invert' to return entries that do not match the given pattern.
142
     * @return array
143
     */
144 3
    public function grep($values, $pattern, $flags = '')
145
    {
146 3
        if (!isset($values)) {
147 1
            return null;
148
        }
149
150 2
        if (is_string($flags)) {
0 ignored issues
show
The condition is_string($flags) is always true.
Loading history...
151 2
            $flags = $flags === 'invert' ? PREG_GREP_INVERT : 0;
152
        }
153
154 2
        return preg_grep($pattern, $values, $flags);
155
    }
156
157
    /**
158
     * Perform a regular expression search and replace.
159
     *
160
     * @param string $value
161
     * @param string $pattern
162
     * @param string $replacement
163
     * @param int    $limit
164
     * @return string
165
     */
166 5
    public function replace($value, $pattern, $replacement = '', $limit = -1)
167
    {
168 5
        $this->assertNoEval($pattern);
169
170 4
        if (!isset($value)) {
171 1
            return null;
172
        }
173
174 3
        return preg_replace($pattern, $replacement, $value, $limit);
175
    }
176
177
    /**
178
     * Perform a regular expression search and replace, returning only matched subjects.
179
     *
180
     * @param string $value
181
     * @param string $pattern
182
     * @param string $replacement
183
     * @param int    $limit
184
     * @return string
185
     */
186 3
    public function filter($value, $pattern, $replacement = '', $limit = -1)
187
    {
188 3
        $this->assertNoEval($pattern);
189
190 2
        if (!isset($value)) {
191 1
            return null;
192
        }
193
194 1
        return preg_filter($pattern, $replacement, $value, $limit);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_filter($patt...cement, $value, $limit) also could return the type string[] which is incompatible with the documented return type string.
Loading history...
195
    }
196
197
    /**
198
     * Split text into an array using a regular expression.
199
     *
200
     * @param string $value
201
     * @param string $pattern
202
     * @return array
203
     */
204 2
    public function split($value, $pattern)
205
    {
206 2
        if (!isset($value)) {
207 1
            return null;
208
        }
209
210 1
        return preg_split($pattern, $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_split($pattern, $value) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
211
    }
212
}
213