Skip to content

Commit 61390c8

Browse files
committed
matches
1 parent 9b2c6f5 commit 61390c8

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/ValueObject/Pattern/RegexPattern.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@
44

55
namespace Rector\Behastan\ValueObject\Pattern;
66

7-
use Entropy\Utils\Regex;
7+
use Entropy\Attributes\RelatedTest;
8+
use Rector\Behastan\Tests\ValueObject\Pattern\RegexPatternTest;
89

10+
#[RelatedTest(RegexPatternTest::class)]
911
final class RegexPattern extends AbstractPattern
1012
{
1113
public function isRegexPatternNeccessary(): bool
1214
{
13-
$matches = Regex::match($this->pattern, '~^/(?<body>(?:\\\\/|[^/])*)/$~');
14-
if ($matches === []) {
15+
// match <delim>body<delim> with same delimiter, no modifiers
16+
if (! preg_match('~^(?<d>.)(?<body>(?:\\\\.|(?!\k<d>).)*)\k<d>$~', $this->pattern, $matches)) {
1517
return true;
1618
}
1719

1820
$body = $matches['body'];
1921

2022
// ignore ^ at start and $ at end
23+
/** @var string $body */
2124
$body = preg_replace('~^\^~', '', $body);
25+
26+
/** @var string $body */
2227
$body = preg_replace('~(?<!\\\\)\$$~', '', $body);
2328

2429
// any unescaped regex meta char => needs regex

tests/ValueObject/Pattern/RegexPatternTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ public function test(): void
1919

2020
$regexPattern = new RegexPattern('/^I do this and that$/', 'someFilePath', 123, 'SomeClass', 'someMethod');
2121
$this->assertFalse($regexPattern->isRegexPatternNeccessary());
22+
23+
$regexPattern = new RegexPattern('#^I do this and that$#', 'someFilePath', 123, 'SomeClass', 'someMethod');
24+
$this->assertFalse($regexPattern->isRegexPatternNeccessary());
2225
}
2326
}

0 commit comments

Comments
 (0)