Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a space after the namespace we're searching for #84

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Replace/NamespaceReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@

class NamespaceReplacer extends BaseReplacer
{
/** @var string */
/**
* The prefix to add to existing namespaces.
*
* @var string "My\Mozart\Prefix".
*/
public $dep_namespace = '';

public function replace($contents)
/**
* @param string $contents The text to make replacements in.
* @param null $file Only used in ClassmapReplacer (for recording which files were changed).
*
* @return string The updated text.
*/
public function replace($contents, $file = null)
{
$searchNamespace = preg_quote($this->autoloader->getSearchNamespace(), '/');
$dependencyNamespace = preg_quote($this->dep_namespace, '/');
Expand All @@ -20,7 +30,7 @@ public function replace($contents)
(?<!$dependencyNamespace) # Does NOT start with the prefix
(?<![a-zA-Z0-9_]\\\\) # Not a class-allowed character followed by a slash
$searchNamespace # The namespace we're looking for
[\\\|;] # Backslash, semicolon, or pipe
[\\\|;\s] # Backslash, pipe, semicolon, or space
) # End the namespace matcher
/Ux",
function ($matches) {
Expand Down
19 changes: 19 additions & 0 deletions tests/replacers/NamespaceReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,23 @@ public function it_doesnt_double_replace_namespaces_that_also_exist_inside_anoth
// Then, we test that chickenReplacer(eggReplacer()) yields the expected result.
$this->assertEquals($expected, $chickenReplacer->replace($eggReplacer->replace($contents)));
}

/**
* @see https://github.com/coenjacobs/mozart/issues/75
*
* @test
*/
public function it_replaces_namespace_use_as_declarations(): void
{

$namespace = 'Symfony\Polyfill\Mbstring';
$prefix = "MBViews\\Dependencies\\";

$replacer = self::createReplacer($namespace, $prefix);

$contents = "use Symfony\Polyfill\Mbstring as p;";
$expected = "use MBViews\Dependencies\Symfony\Polyfill\Mbstring as p;";

$this->assertEquals($expected, $replacer->replace($contents));
}
}