Skip to content

Commit

Permalink
Close coenjacobs#36 Close coenjacobs#44 Static in array and ternary
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Jul 8, 2022
1 parent 81bdb99 commit ebb1a86
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Prefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ public function replaceNamespace($contents, $originalNamespace, $replacement)
|\|\s*
|!\s* # negating the result of a static call
|=>\s* # as the value in an associative array
|\[\s* # In a square array
|\?\s* # In a ternary operator
|:\s* # In a ternary operator
)
(
{$searchNamespace} # followed by the namespace to replace
Expand Down
62 changes: 62 additions & 0 deletions tests/Issues/StraussIssue44Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @see https://github.com/BrianHenryIE/strauss/issues/44
*/

namespace BrianHenryIE\Strauss\Tests\Issues;

use BrianHenryIE\Strauss\Console\Commands\Compose;
use Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

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

/**
* Unprefixed static function call in ternary operation.
*
* @author BrianHenryIE
*/
public function testStaticIsNotPrefixed()
{

$composerJsonString = <<<'EOD'
{
"name": "brianhenryie/strauss-issue-44",
"require": {
"guzzlehttp/guzzle": "7.4.5"
},
"extra": {
"strauss": {
"namespace_prefix": "Strauss\\Issue44\\",
"classmap_prefix": "Strauss_Issue44_"
}
}
}
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);

$php_string = file_get_contents($this->testsWorkingDir . 'strauss/guzzlehttp/guzzle/src/BodySummarizer.php');

$this->assertStringNotContainsString('? \GuzzleHttp\Psr7\Message::bodySummary($message)', $php_string);

$this->assertStringContainsString('? \Strauss\Issue44\GuzzleHttp\Psr7\Message::bodySummary($message)', $php_string);
}
}
101 changes: 101 additions & 0 deletions tests/Unit/PrefixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1421,4 +1421,105 @@ class Normalizer

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


/**
*
* @see https://github.com/BrianHenryIE/strauss/issues/36
*
*/
public function testItReplacesStaticInsideSquareArray(): void
{

$contents = <<<'EOD'
namespace ST;
class StraussTestPackage {
public function __construct() {
$arr = array();
$arr[ ( new \ST\StraussTestPackage2() )->test() ] = true;
$arr[ \ST\StraussTestPackage2::test2() ] = true;
}
}
EOD;

$expected = <<<'EOD'
namespace StraussTest\ST;
class StraussTestPackage {
public function __construct() {
$arr = array();
$arr[ ( new \StraussTest\ST\StraussTestPackage2() )->test() ] = true;
$arr[ \StraussTest\ST\StraussTestPackage2::test2() ] = true;
}
}
EOD;

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

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

$result = $replacer->replaceNamespace($contents, 'ST', 'StraussTest\\ST');

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

/**
*
* @see https://github.com/BrianHenryIE/strauss/issues/44
*
*/
public function testItReplacesStaticInsideMultilineTernary(): void
{

$contents = <<<'EOD'
namespace GuzzleHttp;
use Psr\Http\Message\MessageInterface;
final class BodySummarizer implements BodySummarizerInterface
{
/**
* Returns a summarized message body.
*/
public function summarize(MessageInterface $message): ?string
{
return $this->truncateAt === null
? \GuzzleHttp\Psr7\Message::bodySummary($message)
: \GuzzleHttp\Psr7\Message::bodySummary($message, $this->truncateAt);
}
}
EOD;

$expected = <<<'EOD'
namespace StraussTest\GuzzleHttp;
use Psr\Http\Message\MessageInterface;
final class BodySummarizer implements BodySummarizerInterface
{
/**
* Returns a summarized message body.
*/
public function summarize(MessageInterface $message): ?string
{
return $this->truncateAt === null
? \StraussTest\GuzzleHttp\Psr7\Message::bodySummary($message)
: \StraussTest\GuzzleHttp\Psr7\Message::bodySummary($message, $this->truncateAt);
}
}
EOD;

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

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

$result = $replacer->replaceNamespace($contents, 'GuzzleHttp', 'StraussTest\\GuzzleHttp');

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


}

0 comments on commit ebb1a86

Please sign in to comment.