Skip to content

Commit db9dd53

Browse files
committed
Limit replacements to the beginning of the namespace string
Also: Rewrite the namespace regex to be more easily understandable fixes #67
1 parent 657d95e commit db9dd53

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/Replace/NamespaceReplacer.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ class NamespaceReplacer extends BaseReplacer
99

1010
public function replace($contents)
1111
{
12-
$searchNamespace = addslashes($this->autoloader->getSearchNamespace());
13-
$dependencyNamespace = addslashes($this->dep_namespace);
12+
$searchNamespace = preg_quote($this->autoloader->getSearchNamespace(), '/');
13+
$dependencyNamespace = preg_quote($this->dep_namespace, '/');
1414

15-
return preg_replace_callback(
16-
'/([^a-zA-Z0-9_\x7f-\xff])((?<!' . $dependencyNamespace . ')' . $searchNamespace . '[\\\|;])/U',
15+
return preg_replace_callback("
16+
/ # Start the pattern
17+
([^a-zA-Z0-9_\x7f-\xff]) # Match the non-class character before the namespace
18+
( # Start the namespace matcher
19+
(?<!$dependencyNamespace) # Does NOT start with the prefix
20+
(?<![a-zA-Z0-9_]\\\\) # Not a class-allowed character followed by a slash
21+
$searchNamespace # The namespace we're looking for
22+
[\\\|;] # Backslash, semicolon, or pipe
23+
) # End the namespace matcher
24+
/Ux",
1725
function ($matches) {
1826
return $matches[1] . $this->dep_namespace . $matches[2];
1927
},

0 commit comments

Comments
 (0)