|
4 | 4 |
|
5 | 5 | namespace Rector\Behastan\ValueObject; |
6 | 6 |
|
7 | | -use InvalidArgumentException; |
8 | 7 | use Rector\Behastan\ValueObject\Pattern\AbstractPattern; |
9 | 8 | use Rector\Behastan\ValueObject\Pattern\ExactPattern; |
10 | 9 | use Rector\Behastan\ValueObject\Pattern\RegexPattern; |
@@ -62,41 +61,17 @@ public function byType(string $type): array |
62 | 61 | return array_filter($this->patterns, fn (AbstractPattern $pattern): bool => $pattern instanceof $type); |
63 | 62 | } |
64 | 63 |
|
65 | | - public function regexPatternString(): string |
66 | | - { |
67 | | - $regexPatterns = $this->byType(RegexPattern::class); |
68 | | - |
69 | | - $regexPatternStrings = array_map( |
70 | | - fn (RegexPattern $regexPattern): string => $regexPattern->pattern, |
71 | | - $regexPatterns |
72 | | - ); |
73 | | - |
74 | | - return $this->combineRegexes($regexPatternStrings, '#'); |
75 | | - } |
76 | | - |
77 | 64 | /** |
78 | | - * @param string[] $regexes Like ['/foo/i', '~bar\d+~', '#baz#u'] |
| 65 | + * @return string[] |
79 | 66 | */ |
80 | | - private function combineRegexes(array $regexes, string $delimiter = '#'): string |
| 67 | + public function regexPatternsStrings(): array |
81 | 68 | { |
82 | | - $parts = []; |
83 | | - |
84 | | - foreach ($regexes as $regex) { |
85 | | - // Very common case: regex is given like "/pattern/flags" |
86 | | - // Parse: delimiter + pattern + delimiter + flags |
87 | | - if (! preg_match('~^(.)(.*)\\1([a-zA-Z]*)$~s', $regex, $m)) { |
88 | | - throw new InvalidArgumentException('Invalid regex: ' . $regex); |
89 | | - } |
90 | | - |
91 | | - $pattern = $m[2]; |
92 | | - $flags = $m[3]; |
| 69 | + $regexPatterns = $this->byType(RegexPattern::class); |
93 | 70 |
|
94 | | - // If you truly have mixed flags per-regex, you can't naively merge them. |
95 | | - // Best practice: normalize flags beforehand (same for all). |
96 | | - // We'll ignore per-regex flags here and let the caller decide final flags. |
97 | | - $parts[] = '(?:' . $pattern . ')'; |
98 | | - } |
| 71 | + $regexPatternStrings = array_map(function (RegexPattern $regexPattern): string { |
| 72 | + return $regexPattern->pattern; |
| 73 | + }, $regexPatterns); |
99 | 74 |
|
100 | | - return $delimiter . '(?:' . implode('|', $parts) . ')' . $delimiter; |
| 75 | + return array_values($regexPatternStrings); |
101 | 76 | } |
102 | 77 | } |
0 commit comments