17
17
use Chevere \Regex \Interfaces \RegexInterface ;
18
18
use InvalidArgumentException ;
19
19
use LogicException ;
20
- use Safe \Exceptions \PcreException ;
21
- use Throwable ;
22
20
use function Chevere \Message \message ;
23
- use function Safe \preg_match ;
24
- use function Safe \preg_match_all ;
25
21
26
22
final class Regex implements RegexInterface
27
23
{
@@ -35,7 +31,7 @@ final class Regex implements RegexInterface
35
31
public function __construct (
36
32
private string $ pattern
37
33
) {
38
- $ this ->assertRegex ();
34
+ $ this ->assertPattern ();
39
35
$ delimiter = $ this ->pattern [0 ];
40
36
$ this ->noDelimiters = trim ($ this ->pattern , $ delimiter );
41
37
$ this ->noDelimitersNoAnchors = strval (
@@ -60,22 +56,19 @@ public function noDelimitersNoAnchors(): string
60
56
61
57
public function match (string $ value ): array
62
58
{
63
- try {
64
- $ match = preg_match ($ this ->pattern , $ value , $ matches );
59
+ $ match = @preg_match ($ this ->pattern , $ value , $ matches );
60
+ if (is_int ($ match )) {
61
+ return $ match === 1 ? $ matches : [];
65
62
}
66
63
// @codeCoverageIgnoreStart
67
- catch (PcreException $ e ) {
68
- throw new LogicException (
69
- (string ) message (
70
- 'Error `%function%` %message% ' ,
71
- function: 'preg_match ' ,
72
- message: $ e ->getMessage ()
73
- )
74
- );
75
- }
64
+ throw new LogicException (
65
+ (string ) message (
66
+ 'Error `%function%` %error% ' ,
67
+ function: 'preg_match ' ,
68
+ error: static ::ERRORS [preg_last_error ()],
69
+ )
70
+ );
76
71
// @codeCoverageIgnoreEnd
77
-
78
- return $ match === 1 ? $ matches : [];
79
72
}
80
73
81
74
public function assertMatch (string $ value ): void
@@ -96,22 +89,19 @@ public function assertMatch(string $value): void
96
89
97
90
public function matchAll (string $ value ): array
98
91
{
99
- try {
100
- $ match = preg_match_all ($ this ->pattern , $ value , $ matches );
92
+ $ match = @preg_match_all ($ this ->pattern , $ value , $ matches );
93
+ if (is_int ($ match )) {
94
+ return $ match === 1 ? $ matches : [];
101
95
}
102
96
// @codeCoverageIgnoreStart
103
- catch (PcreException $ e ) {
104
- throw new LogicException (
105
- (string ) message (
106
- 'Error `%function%` %message% ' ,
107
- function: 'preg_match_all ' ,
108
- message: $ e ->getMessage ()
109
- )
110
- );
111
- }
97
+ throw new LogicException (
98
+ (string ) message (
99
+ 'Error `%function%` %error% ' ,
100
+ function: 'preg_match ' ,
101
+ error: static ::ERRORS [preg_last_error ()],
102
+ )
103
+ );
112
104
// @codeCoverageIgnoreEnd
113
-
114
- return $ match === 1 ? $ matches : [];
115
105
}
116
106
117
107
public function assertMatchAll (string $ value ): void
@@ -130,19 +120,18 @@ public function assertMatchAll(string $value): void
130
120
);
131
121
}
132
122
133
- private function assertRegex (): void
123
+ private function assertPattern (): void
134
124
{
135
- try {
136
- preg_match ($ this ->pattern , '' );
137
- } catch (Throwable $ e ) {
138
- throw new InvalidArgumentException (
139
- previous: $ e ,
140
- message: (string ) message (
141
- 'Invalid regex string `%regex%` provided: %error% ' ,
142
- regex: $ this ->pattern ,
143
- error: static ::ERRORS [preg_last_error ()],
144
- )
145
- );
125
+ if (@preg_match ($ this ->pattern , '' ) !== false ) {
126
+ return ;
146
127
}
128
+
129
+ throw new InvalidArgumentException (
130
+ (string ) message (
131
+ 'Invalid regex pattern `%pattern%` provided: %error% ' ,
132
+ pattern: $ this ->pattern ,
133
+ error: static ::ERRORS [preg_last_error ()],
134
+ )
135
+ );
147
136
}
148
137
}
0 commit comments