Skip to content

Commit 3eb36fa

Browse files
committed
Added util classes
1 parent 3477db3 commit 3eb36fa

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

src/Util/Finder.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Util;
4+
5+
use Okapi\Wildcards\Regex;
6+
7+
/**
8+
* # Finder
9+
*
10+
* The `Finder` class is used to match classes against a list of patterns.
11+
*/
12+
class Finder
13+
{
14+
/**
15+
* The classes that should be included in the search.
16+
*
17+
* @var string[] List with regex patterns
18+
*/
19+
private array $classesToMatch = [];
20+
21+
/**
22+
* Include a class or an array of classes in the search.
23+
* Can also be a wildcard pattern.
24+
*
25+
* @param class-string|class-string[] $path
26+
*
27+
* @return $this
28+
*/
29+
public function includeClass(string|array $path): self
30+
{
31+
$path = (array)$path;
32+
33+
// Convert wildcard patterns to regex
34+
foreach ($path as &$class) {
35+
$class = Regex::fromWildcard($class)->getRegex();
36+
}
37+
38+
$this->classesToMatch = array_merge(
39+
$this->classesToMatch,
40+
$path,
41+
);
42+
43+
return $this;
44+
}
45+
46+
/**
47+
* Check if a class exists in the search paths.
48+
*
49+
* @param class-string $class
50+
*
51+
* @return bool
52+
*/
53+
public function hasClass(string $class): bool
54+
{
55+
// Check if the class matches any of the patterns
56+
return array_reduce(
57+
$this->classesToMatch,
58+
function (bool $carry, string $pattern) use ($class): bool {
59+
return $carry || preg_match($pattern, $class) === 1;
60+
},
61+
false,
62+
);
63+
}
64+
}

src/Util/StringMutator.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Util;
4+
5+
/**
6+
* A utility class for mutating strings.
7+
*
8+
* This class is used to mutate strings in a way that preserves the positions of
9+
* edits. This is useful for transforming code, where edits to the code may
10+
* change the positions of other edits.
11+
*/
12+
class StringMutator
13+
{
14+
/**
15+
* The string to mutate.
16+
*
17+
* @var string
18+
*/
19+
private string $string;
20+
21+
/**
22+
* The edits to apply to the string.
23+
*
24+
* @var array{int, int, string}[] Start position, length, replacement
25+
*/
26+
public array $edits = [];
27+
28+
/**
29+
* StringMutator constructor.
30+
*
31+
* @param string $string
32+
*/
33+
public function __construct(string $string)
34+
{
35+
$this->string = $string;
36+
}
37+
38+
/**
39+
* Add an edit to the string.
40+
*
41+
* @param int $start
42+
* @param int $length
43+
* @param string $replacement
44+
*
45+
* @return $this
46+
*/
47+
public function edit(int $start, int $length, string $replacement): self
48+
{
49+
$this->edits[] = [$start, $length, $replacement];
50+
return $this;
51+
}
52+
53+
/**
54+
* Get the mutated string.
55+
*
56+
* @return string
57+
*/
58+
public function getMutatedString(): string
59+
{
60+
// Sort the edits by start position.
61+
usort($this->edits, function ($a, $b) {
62+
return $a[0] <=> $b[0];
63+
});
64+
65+
// Apply the edits.
66+
$result = $this->string;
67+
$offset = 0;
68+
foreach ($this->edits as $edit) {
69+
[$start, $length, $replacement] = $edit;
70+
$start += $offset;
71+
$result = substr_replace($result, $replacement, $start, $length);
72+
$offset += strlen($replacement) - $length;
73+
}
74+
return $result;
75+
}
76+
}

0 commit comments

Comments
 (0)