From 26e4f9d763b35932c3249c277eed608dc8cb4006 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 17 Feb 2021 16:36:44 -0800 Subject: [PATCH] Use positive lookahead to allow phrases Instead of looking for characters: \ | ; and space, it now looks for phrases ";", "\", "|" and " as" Fix for #124 --- src/Replace/NamespaceReplacer.php | 12 ++++-- .../NamespaceReplacerIntegrationTest.php | 41 +++++++++++++++++++ tests/replacers/NamespaceReplacerTest.php | 16 ++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/Replace/NamespaceReplacer.php b/src/Replace/NamespaceReplacer.php index 711ec086..7b418895 100644 --- a/src/Replace/NamespaceReplacer.php +++ b/src/Replace/NamespaceReplacer.php @@ -29,11 +29,15 @@ public function replace($contents, $file = null) ( # Start the namespace matcher (?dep_namespace . $matches[2]; }, diff --git a/tests/replacers/NamespaceReplacerIntegrationTest.php b/tests/replacers/NamespaceReplacerIntegrationTest.php index 80bc04a3..c2e7094f 100644 --- a/tests/replacers/NamespaceReplacerIntegrationTest.php +++ b/tests/replacers/NamespaceReplacerIntegrationTest.php @@ -103,6 +103,47 @@ public function test_it_does_not_make_classname_replacement_inside_namespaced_fi } + + /** + * Another Mpdf problem, presumably down to the classname matching the namespace. + * + * Typed function properties whose class type (name) match the namespace being replaced are + * unintentionally prefixed, causing PHP to crash. + * + * Occurring at dev-master#3b1243ca8505fa6436569800dc34269178930f39 + * + * @see https://github.com/coenjacobs/mozart/issues/124 + */ + public function test_it_does_not_prefix_function_argument_types_whose_classname_matches_the_namespace() + { + + $composer = $this->composer; + + $composer->require["mpdf/mpdf"] = "8.0.10"; + + file_put_contents($this->testsWorkingDir . '/composer.json', json_encode($composer)); + + chdir($this->testsWorkingDir); + + exec('composer update'); + + $inputInterfaceMock = $this->createMock(InputInterface::class); + $outputInterfaceMock = $this->createMock(OutputInterface::class); + + $mozartCompose = new Compose(); + + $mozartCompose->run($inputInterfaceMock, $outputInterfaceMock); + + $mpdf_php = file_get_contents($this->testsWorkingDir .'/dep_directory/Mpdf/Conversion/DecToOther.php'); + + // Confirm problem is gone. + $this->assertStringNotContainsString('public function __construct(Mozart\Mpdf $mpdf)', $mpdf_php); + + // Confirm solution is correct. + $this->assertStringContainsString('public function __construct(Mpdf $mpdf)', $mpdf_php); + } + + /** * Delete $this->testsWorkingDir after each test. * diff --git a/tests/replacers/NamespaceReplacerTest.php b/tests/replacers/NamespaceReplacerTest.php index d317378a..d5f89da0 100644 --- a/tests/replacers/NamespaceReplacerTest.php +++ b/tests/replacers/NamespaceReplacerTest.php @@ -123,4 +123,20 @@ public function it_replaces_namespace_use_as_declarations(): void $this->assertEquals($expected, $replacer->replace($contents)); } + +/** + * @test + */ +public function it_doesnt_prefix_function_types_that_happen_to_match_the_namespace() { + + $namespace = 'Mpdf'; + $prefix = "Mozart\\"; + + $replacer = self::createReplacer($namespace, $prefix); + + $contents = 'public function getServices( Mpdf $mpdf, LoggerInterface $logger, $config, )'; + $expected = 'public function getServices( Mpdf $mpdf, LoggerInterface $logger, $config, )'; + + $this->assertEquals($expected, $replacer->replace($contents)); +} }