Skip to content

Commit

Permalink
Use positive lookahead to allow phrases
Browse files Browse the repository at this point in the history
Instead of looking for characters: \ | ; and space, it now looks for phrases ";", "\", "|" and " as"

Fix for #124
  • Loading branch information
BrianHenryIE committed Feb 18, 2021
1 parent 3b1243c commit 26e4f9d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Replace/NamespaceReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ public function replace($contents, $file = null)
( # Start the namespace matcher
(?<!$dependencyNamespace) # Does NOT start with the prefix
(?<![a-zA-Z0-9_]\\\\) # Not a class-allowed character followed by a slash
(?<!class\s) # Not a class declaration.
$searchNamespace # The namespace we're looking for
[\\\|;\s] # Backslash, pipe, semicolon, or space
(?<!class\s) # Not a class declaration
$searchNamespace # The namespace were looking for
(?=;|\\\|\\|\sas) # Followed by one of
# semicolon when it is the class namespace itself
# backslash when it is part of a longer namespace or namespaced class
# pipe
# space followed by the as keyword
) # End the namespace matcher
/Ux",
/Ux", # U non-greedy matching by default, x ignore whitespace in regex.
function ($matches) {
return $matches[1] . $this->dep_namespace . $matches[2];
},
Expand Down
41 changes: 41 additions & 0 deletions tests/replacers/NamespaceReplacerIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/replacers/NamespaceReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 26e4f9d

Please sign in to comment.