Skip to content

Commit 80a7cac

Browse files
committed
fix deps
1 parent f026584 commit 80a7cac

File tree

3 files changed

+37
-45
lines changed

3 files changed

+37
-45
lines changed

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
],
1414
"require": {
1515
"php": "^8.1",
16-
"chevere/message": "^0.1.0",
17-
"thecodingmachine/safe": "^2.5"
16+
"chevere/message": "^1.0.0"
1817
},
1918
"require-dev": {
2019
"phpstan/phpstan": "^1.9",

phpstan.neon

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src

src/Regex.php

+32-43
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
use Chevere\Regex\Interfaces\RegexInterface;
1818
use InvalidArgumentException;
1919
use LogicException;
20-
use Safe\Exceptions\PcreException;
21-
use Throwable;
2220
use function Chevere\Message\message;
23-
use function Safe\preg_match;
24-
use function Safe\preg_match_all;
2521

2622
final class Regex implements RegexInterface
2723
{
@@ -35,7 +31,7 @@ final class Regex implements RegexInterface
3531
public function __construct(
3632
private string $pattern
3733
) {
38-
$this->assertRegex();
34+
$this->assertPattern();
3935
$delimiter = $this->pattern[0];
4036
$this->noDelimiters = trim($this->pattern, $delimiter);
4137
$this->noDelimitersNoAnchors = strval(
@@ -60,22 +56,19 @@ public function noDelimitersNoAnchors(): string
6056

6157
public function match(string $value): array
6258
{
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 : [];
6562
}
6663
// @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+
);
7671
// @codeCoverageIgnoreEnd
77-
78-
return $match === 1 ? $matches : [];
7972
}
8073

8174
public function assertMatch(string $value): void
@@ -96,22 +89,19 @@ public function assertMatch(string $value): void
9689

9790
public function matchAll(string $value): array
9891
{
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 : [];
10195
}
10296
// @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+
);
112104
// @codeCoverageIgnoreEnd
113-
114-
return $match === 1 ? $matches : [];
115105
}
116106

117107
public function assertMatchAll(string $value): void
@@ -130,19 +120,18 @@ public function assertMatchAll(string $value): void
130120
);
131121
}
132122

133-
private function assertRegex(): void
123+
private function assertPattern(): void
134124
{
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;
146127
}
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+
);
147136
}
148137
}

0 commit comments

Comments
 (0)