-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPatternCollection.php
More file actions
102 lines (84 loc) · 2.73 KB
/
PatternCollection.php
File metadata and controls
102 lines (84 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace Rector\Behastan\ValueObject;
use InvalidArgumentException;
use Rector\Behastan\ValueObject\Pattern\AbstractPattern;
use Rector\Behastan\ValueObject\Pattern\ExactPattern;
use Rector\Behastan\ValueObject\Pattern\RegexPattern;
final readonly class PatternCollection
{
/**
* @param AbstractPattern[] $patterns
*/
public function __construct(
private array $patterns
) {
}
/**
* @param class-string<AbstractPattern> $type
*/
public function countByType(string $type): int
{
$patternsByType = $this->byType($type);
return count($patternsByType);
}
public function count(): int
{
return count($this->patterns);
}
/**
* @return AbstractPattern[]
*/
public function all(): array
{
return $this->patterns;
}
/**
* @return string[]
*/
public function exactPatternStrings(): array
{
$exactPatterns = $this->byType(ExactPattern::class);
return array_map(fn (ExactPattern $exactPattern): string => $exactPattern->pattern, $exactPatterns);
}
/**
* @template TPattern as AbstractPattern
*
* @param class-string<TPattern> $type
* @return TPattern[]
*/
public function byType(string $type): array
{
return array_filter($this->patterns, fn (AbstractPattern $pattern): bool => $pattern instanceof $type);
}
public function regexPatternString(): string
{
$regexPatterns = $this->byType(RegexPattern::class);
$regexPatternStrings = array_map(
fn (RegexPattern $regexPattern): string => $regexPattern->pattern,
$regexPatterns
);
return $this->combineRegexes($regexPatternStrings, '#');
}
/**
* @param string[] $regexes Like ['/foo/i', '~bar\d+~', '#baz#u']
*/
private function combineRegexes(array $regexes, string $delimiter = '#'): string
{
$parts = [];
foreach ($regexes as $regex) {
// Very common case: regex is given like "/pattern/flags"
// Parse: delimiter + pattern + delimiter + flags
if (! preg_match('~^(.)(.*)\\1([a-zA-Z]*)$~s', $regex, $m)) {
throw new InvalidArgumentException('Invalid regex: ' . $regex);
}
$pattern = $m[2];
$flags = $m[3];
// If you truly have mixed flags per-regex, you can't naively merge them.
// Best practice: normalize flags beforehand (same for all).
// We'll ignore per-regex flags here and let the caller decide final flags.
$parts[] = '(?:' . $pattern . ')';
}
return $delimiter . '(?:' . implode('|', $parts) . ')' . $delimiter;
}
}