From 1a42be4cf64fd4e49b7a24944168de7c6461d041 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sun, 22 Nov 2020 18:52:11 -0800 Subject: [PATCH] Allow a space after the namespace we're searching for #75 --- src/Replace/NamespaceReplacer.php | 16 +++++++++++++--- tests/replacers/NamespaceReplacerTest.php | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Replace/NamespaceReplacer.php b/src/Replace/NamespaceReplacer.php index be7715f9..ddda03cd 100644 --- a/src/Replace/NamespaceReplacer.php +++ b/src/Replace/NamespaceReplacer.php @@ -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, '/'); @@ -20,7 +30,7 @@ public function replace($contents) (?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)); + } }