Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Sep 18, 2021
1 parent 2636ca1 commit 67605ba
Show file tree
Hide file tree
Showing 4 changed files with 27,544 additions and 6 deletions.
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
<file>./src</file>
<file>./tests</file>
<exclude-pattern>/tests/reports/</exclude-pattern>
<exclude-pattern>/tests/Issues/data</exclude-pattern>
</ruleset>
25 changes: 19 additions & 6 deletions src/Prefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,17 @@ public function replaceClassname($contents, $originalClassname, $classnamePrefix
{
$searchClassname = preg_quote($originalClassname, '/');

// This could be more specific if we could enumerate all preceeding and proceeding words ("new", "("...).
// This could be more specific if we could enumerate all preceding and proceeding words ("new", "("...).
$pattern = '
/ # Start the pattern
namespace\s+([a-zA-Z0-9_\x7f-\xff\\\\]+).*?{.*?(namespace|\z)
namespace\s+[a-zA-Z0-9_\x7f-\xff\\\\]+\s*{(.*?)(namespace|\z)
# Look for a preceeding namespace declaration, up until a
# potential second namespace declaration.
| # if found, match that much before continuing the search on
# the remainder of the string.
namespace\s+[a-zA-Z0-9_\x7f-\xff\\\\]+; # Skip lines just declaring the namespace.
namespace\s+[a-zA-Z0-9_\x7f-\xff\\\\]+\s*;(.*) # 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\\\]) # outside a namespace the class will not be prefixed with a slash
/xs'; // # x: ignore whitespace in regex. s dot matches newline

Expand All @@ -232,9 +232,22 @@ public function replaceClassname($contents, $originalClassname, $classnamePrefix
);

return $updated;
} else {
$newContents = '';
foreach ($matches as $index => $captured) {
if (0 === $index) {
continue;
}

if ($captured == $originalClassname) {
$newContents .= $classnamePrefix;
}

$newContents .= $captured;
}
return $newContents;
}

return $matches[1] . $matches[2] . $matches[3] . $classnamePrefix . $originalClassname . $matches[5];
// return $matches[1] . $matches[2] . $matches[3] . $classnamePrefix . $originalClassname . $matches[5];
};

$result = preg_replace_callback($pattern, $replacingFunction, $contents);
Expand Down
89 changes: 89 additions & 0 deletions tests/Issues/StraussIssue33Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
*
*/

namespace BrianHenryIE\Strauss\Tests\Issues;

use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
use BrianHenryIE\Strauss\Console\Commands\Compose;
use BrianHenryIE\Strauss\Prefixer;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @package BrianHenryIE\Strauss\Tests\Issues
* @coversNothing
*/
class StraussIssue33Test extends \BrianHenryIE\Strauss\Tests\Integration\Util\IntegrationTestCase
{

/**
*/
public function test_backtrack_limit_exhausted()
{

$composerJsonString = <<<'EOD'
{
"name": "brianhenryie/strauss-backtrack-limit-exhausted",
"minimum-stability": "dev",
"require": {
"afragen/wp-dependency-installer": "^3.1",
"mpdf/mpdf": "*"
},
"extra": {
"strauss": {
"namespace_prefix": "BrianHenryIE\\Strauss_Backtrack_Limit_Exhausted\\",
"target_directory": "/strauss/",
"classmap_prefix": "BH_Strauss_Backtrack_Limit_Exhausted_"
}
}
}

EOD;

file_put_contents($this->testsWorkingDir . 'composer.json', $composerJsonString);

chdir($this->testsWorkingDir);

exec('composer install');

$inputInterfaceMock = $this->createMock(InputInterface::class);
$outputInterfaceMock = $this->createMock(OutputInterface::class);

$strauss = new Compose();

$result = $strauss->run($inputInterfaceMock, $outputInterfaceMock);

$this->assertNotEquals(1, $result);
}



/**
*
*/
public function test_unit_backtrack_limit_exhausted()
{

$contents = file_get_contents(__DIR__.'/data/Mpdf.php');

$originalClassname = 'WP_Dependency_Installer';

$classnamePrefix = 'BH_Strauss_Backtrack_Limit_Exhausted_';

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

$exception = null;

$prefixer = new Prefixer($config, $this->testsWorkingDir);

try {
$prefixer->replaceClassname($contents, $originalClassname, $classnamePrefix);
} catch (\Exception $e) {
$exception = $e;
}

$this->assertNull($exception);
}
}
Loading

0 comments on commit 67605ba

Please sign in to comment.