Skip to content

Commit

Permalink
Fix incorrect classname replacing inside namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Aug 1, 2021
1 parent dfff4ba commit 03a148c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Prefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function replaceClassname($contents, $originalClassname, $classnamePrefix
# the remainder of the string.
namespace\s+[a-zA-Z0-9_\x7f-\xff\\\\]+; # Skip lines just declaring the namespace.
|
([^a-zA-Z0-9_\x7f-\xff\$])('. $searchClassname . ')([^a-zA-Z0-9_\x7f-\xff])
([^a-zA-Z0-9_\x7f-\xff\$\\\])('. $searchClassname . ')([^a-zA-Z0-9_\x7f-\xff\\\])
/x'; // # x: ignore whitespace in regex.

Expand Down
70 changes: 70 additions & 0 deletions tests/Unit/PrefixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1317,4 +1317,74 @@ class NA

$this->assertEquals($contents, $result);
}

/**
* class Normalizer_Test_Normalizer extends Normalizer_Test\Symfony\Polyfill\Intl\Normalizer_Test_Normalizer\Normalizer
*
* @throws \Exception
*/
public function testItDoesNotPrefixClassnameInsideInsideNamespaceName(): void
{

$contents = <<<'EOD'
class Normalizer extends Symfony\Polyfill\Intl\Normalizer\Foo
{
}
EOD;

$expected = <<<'EOD'
class Normalizer_Test_Normalizer extends Symfony\Polyfill\Intl\Normalizer\Foo
{
}
EOD;

$originalClassname = 'Normalizer';
$classnamePrefix = 'Normalizer_Test_';

$config = $this->createMock(StraussConfig::class);

$replacer = new Prefixer($config, __DIR__);

$result = $replacer->replaceClassname($contents, $originalClassname, $classnamePrefix);

$this->assertEquals($expected, $result);
}



/**
* class Normalizer_Test_Normalizer extends Normalizer_Test\Symfony\Polyfill\Intl\Normalizer_Test_Normalizer\Normalizer
*
* @throws \Exception
*/
public function testItDoesNotPrefixClassnameInsideEndNamespaceName(): void
{

$contents = <<<'EOD'
class Normalizer extends Symfony\Polyfill\Intl\Foo\Normalizer
{
}
EOD;

$expected = <<<'EOD'
class Normalizer_Test_Normalizer extends Symfony\Polyfill\Intl\Foo\Normalizer
{
}
EOD;

$originalClassname = 'Normalizer';
$classnamePrefix = 'Normalizer_Test_';

$config = $this->createMock(StraussConfig::class);

$replacer = new Prefixer($config, __DIR__);

$result = $replacer->replaceClassname($contents, $originalClassname, $classnamePrefix);

$this->assertEquals($expected, $result);
}
}

0 comments on commit 03a148c

Please sign in to comment.