PcreRegex   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 201
Duplicated Lines 26.87 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 54
loc 201
ccs 62
cts 62
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 9 9 1
A get() 9 9 1
A getAll() 9 9 1
B replace() 0 27 6
A split() 9 9 1
A grep() 9 9 1
A filter() 9 9 1
A setUp() 0 7 1
A tearDown() 0 4 1
A handleError() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Gobie\Regex\Wrappers\Pcre;
4
5
/**
6
 * Wrapper around PCRE library.
7
 *
8
 * It is meant to be replaceable for PCRE functions.
9
 *
10
 * Usage:
11
 * <code>
12
 * if ($matches = PcreRegex::getAll($pattern, $subject)) {
13
 *   // do stuff here with $matches
14
 * }
15
 * </code>
16
 *
17
 * @link http://php.net/pcre
18
 */
19
class PcreRegex
20
{
21
22
    /**
23
     * Regular expression match and return if pattern matches given subject.
24
     *
25
     * @param string $pattern Pattern
26
     * @param string $subject Subject
27
     * @param int    $offset  Offset
28
     * @return bool True if pattern matches given subject, false otherwise
29
     * @throws PcreRegexException When compilation or runtime error occurs
30
     * @link http://php.net/function.preg-match.php
31
     */
32 15 View Code Duplication
    public static function match($pattern, $subject, $offset = 0)
0 ignored issues
show
Coding Style introduced by
function match() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34 15
        static::setUp($pattern);
35 15
        $res = \preg_match($pattern, $subject, $matches, 0, $offset);
36 10
        static::tearDown();
37 10
        static::handleError($pattern);
38
39 7
        return (bool) $res;
40
    }
41
42
    /**
43
     * Regular expression match and return first match.
44
     *
45
     * @param string $pattern Pattern
46
     * @param string $subject Subject
47
     * @param int    $flags   Flags
48
     * @param int    $offset  Offset
49
     * @return string[] Array with first match that matches given subject, empty array otherwise
50
     * @throws PcreRegexException When compilation or runtime error occurs
51
     * @link http://php.net/function.preg-match.php
52
     */
53 13 View Code Duplication
    public static function get($pattern, $subject, $flags = 0, $offset = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55 13
        static::setUp($pattern);
56 13
        \preg_match($pattern, $subject, $matches, $flags, $offset);
57 8
        static::tearDown();
58 8
        static::handleError($pattern);
59
60 6
        return $matches;
61
    }
62
63
    /**
64
     * Global regular expression match and return all matches.
65
     *
66
     * @param string $pattern Pattern
67
     * @param string $subject Subject
68
     * @param int    $flags   Flags
69
     * @param int    $offset  Offset
70
     * @return string[][] Array of matches that match given subject, empty array otherwise
71
     * @throws PcreRegexException When compilation or runtime error occurs
72
     * @link http://php.net/function.preg-match-all.php
73
     */
74 15 View Code Duplication
    public static function getAll($pattern, $subject, $flags = \PREG_PATTERN_ORDER, $offset = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76 15
        static::setUp($pattern);
77 15
        \preg_match_all($pattern, $subject, $matches, $flags, $offset);
78 10
        static::tearDown();
79 10
        static::handleError($pattern);
80
81 8
        return \array_filter($matches);
82
    }
83
84
    /**
85
     * Regular expression replace and return replaced.
86
     *
87
     * @param string|string[]          $pattern     Pattern or array of patterns
88
     * @param callable|string|string[] $replacement Replacement (string or callable) or array of string replacements
89
     * @param string|string[]          $subject     Subject or array of subjects
90
     * @param int                      $limit       Limit of replacements
91
     * @return string|string[] Replaced subject or array of subjects
92
     * @throws PcreRegexException When compilation or runtime error occurs
93
     * @link http://php.net/function.preg-replace.php
94
     * @link http://php.net/function.preg-replace-callback.php
95
     */
96 31
    public static function replace($pattern, $replacement, $subject, $limit = -1)
97
    {
98 31
        static::setUp($pattern);
99
100 31
        if ((\is_object($replacement) || \is_array($replacement)) && \is_callable($replacement)) {
101 12
            foreach ((array) $pattern as $patternPart) {
102 12
                \preg_match($patternPart, '');
103
            }
104
105 7
            static::tearDown();
106
107 7
            $res = \preg_replace_callback($pattern, $replacement, $subject, $limit);
108
109 6
            if ($res === null) {
110 2
                static::handleError($pattern);
111
            }
112
113 4
            return $res;
114
        }
115
116 19
        $res = \preg_replace($pattern, $replacement, $subject, $limit);
117
118 12
        static::tearDown();
119 12
        static::handleError($pattern);
120
121 10
        return $res;
122
    }
123
124
    /**
125
     * Regular expression split and return all parts.
126
     *
127
     * @param string $pattern Pattern
128
     * @param string $subject Subject
129
     * @param int    $limit   Limit
130
     * @param int    $flags   Flags defaults to PREG_SPLIT_DELIM_CAPTURE
131
     * @return string[] Array of split parts, array with original string otherwise
132
     * @throws PcreRegexException When compilation or runtime error occurs
133
     * @link http://php.net/function.preg-split.php
134
     */
135 13 View Code Duplication
    public static function split($pattern, $subject, $limit = -1, $flags = \PREG_SPLIT_DELIM_CAPTURE)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137 13
        static::setUp($pattern);
138 13
        $res = \preg_split($pattern, $subject, $limit, $flags);
139 8
        static::tearDown();
140 8
        static::handleError($pattern);
141
142 6
        return $res;
143
    }
144
145
    /**
146
     * Regular expression grep and return matching items.
147
     *
148
     * @param string          $pattern Pattern
149
     * @param string|string[] $subject Subject or array of subjects
150
     * @param int             $flags   Flags
151
     * @return string[] Array with items that matches given pattern, empty array otherwise
152
     * @throws PcreRegexException When compilation or runtime error occurs
153
     * @link http://php.net/function.preg-grep.php
154
     */
155 11 View Code Duplication
    public static function grep($pattern, $subject, $flags = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157 11
        static::setUp($pattern);
158 11
        $res = \preg_grep($pattern, (array) $subject, $flags);
159 6
        static::tearDown();
160 6
        static::handleError($pattern);
161
162 4
        return $res;
163
    }
164
165
    /**
166
     * Regular expression filter and return only replaced.
167
     *
168
     * @param string|string[] $pattern     Pattern or array of patterns
169
     * @param string|string[] $replacement Replacement or array of replacements
170
     * @param string|string[] $subject     Subject or array of subjects
171
     * @param int             $limit       Limit of replacements
172
     * @return string|string[] Replaced subject or array of subjects
173
     * @throws PcreRegexException When compilation or runtime error occurs
174
     * @link http://php.net/function.preg-filter.php
175
     */
176 12 View Code Duplication
    public static function filter($pattern, $replacement, $subject, $limit = -1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178 12
        static::setUp($pattern);
179 12
        $res = \preg_filter($pattern, $replacement, $subject, $limit);
180 5
        static::tearDown();
181 5
        static::handleError($pattern);
182
183 3
        return $res;
184
    }
185
186
    /**
187
     * Prepare error handler for catching compilation errors.
188
     *
189
     * @param string|string[] $pattern Pattern or array of patterns
190
     */
191
    protected static function setUp($pattern)
192
    {
193 110
        \set_error_handler(function ($errno, $errstr) use ($pattern) {
194 45
            \restore_error_handler();
195 45
            throw new PcreRegexException($errstr, null, $pattern);
196 110
        });
197 110
    }
198
199
    /**
200
     * Clean up after setUp().
201
     */
202 66
    protected static function tearDown()
203
    {
204 66
        \restore_error_handler();
205 66
    }
206
207
    /**
208
     * Handle runtime errors in PCRE.
209
     *
210
     * @param string|string[] $pattern Pattern or array of patterns
211
     * @throws PcreRegexException When runtime error occurs
212
     */
213 61
    protected static function handleError($pattern)
214
    {
215 61
        if ($error = \preg_last_error()) {
216 17
            throw new PcreRegexException(null, $error, $pattern);
217
        }
218 44
    }
219
}
220