Skip to content

Commit ebb1a86

Browse files
committed
Close coenjacobs#36 Close coenjacobs#44 Static in array and ternary
1 parent 81bdb99 commit ebb1a86

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

src/Prefixer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ public function replaceNamespace($contents, $originalNamespace, $replacement)
145145
|\|\s*
146146
|!\s* # negating the result of a static call
147147
|=>\s* # as the value in an associative array
148+
|\[\s* # In a square array
149+
|\?\s* # In a ternary operator
150+
|:\s* # In a ternary operator
148151
)
149152
(
150153
{$searchNamespace} # followed by the namespace to replace
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* @see https://github.com/BrianHenryIE/strauss/issues/44
4+
*/
5+
6+
namespace BrianHenryIE\Strauss\Tests\Issues;
7+
8+
use BrianHenryIE\Strauss\Console\Commands\Compose;
9+
use Exception;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
/**
14+
* @package BrianHenryIE\Strauss\Tests\Issues
15+
* @coversNothing
16+
*/
17+
class StraussIssue44Test extends \BrianHenryIE\Strauss\Tests\Integration\Util\IntegrationTestCase
18+
{
19+
20+
/**
21+
* Unprefixed static function call in ternary operation.
22+
*
23+
* @author BrianHenryIE
24+
*/
25+
public function testStaticIsNotPrefixed()
26+
{
27+
28+
$composerJsonString = <<<'EOD'
29+
{
30+
"name": "brianhenryie/strauss-issue-44",
31+
"require": {
32+
"guzzlehttp/guzzle": "7.4.5"
33+
},
34+
"extra": {
35+
"strauss": {
36+
"namespace_prefix": "Strauss\\Issue44\\",
37+
"classmap_prefix": "Strauss_Issue44_"
38+
}
39+
}
40+
}
41+
EOD;
42+
43+
file_put_contents($this->testsWorkingDir . 'composer.json', $composerJsonString);
44+
45+
chdir($this->testsWorkingDir);
46+
47+
exec('composer install');
48+
49+
$inputInterfaceMock = $this->createMock(InputInterface::class);
50+
$outputInterfaceMock = $this->createMock(OutputInterface::class);
51+
52+
$strauss = new Compose();
53+
54+
$result = $strauss->run($inputInterfaceMock, $outputInterfaceMock);
55+
56+
$php_string = file_get_contents($this->testsWorkingDir . 'strauss/guzzlehttp/guzzle/src/BodySummarizer.php');
57+
58+
$this->assertStringNotContainsString('? \GuzzleHttp\Psr7\Message::bodySummary($message)', $php_string);
59+
60+
$this->assertStringContainsString('? \Strauss\Issue44\GuzzleHttp\Psr7\Message::bodySummary($message)', $php_string);
61+
}
62+
}

tests/Unit/PrefixerTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,4 +1421,105 @@ class Normalizer
14211421

14221422
$this->assertEquals($expected, $result);
14231423
}
1424+
1425+
1426+
/**
1427+
*
1428+
* @see https://github.com/BrianHenryIE/strauss/issues/36
1429+
*
1430+
*/
1431+
public function testItReplacesStaticInsideSquareArray(): void
1432+
{
1433+
1434+
$contents = <<<'EOD'
1435+
namespace ST;
1436+
class StraussTestPackage {
1437+
public function __construct() {
1438+
$arr = array();
1439+
1440+
$arr[ ( new \ST\StraussTestPackage2() )->test() ] = true;
1441+
1442+
$arr[ \ST\StraussTestPackage2::test2() ] = true;
1443+
}
1444+
}
1445+
EOD;
1446+
1447+
$expected = <<<'EOD'
1448+
namespace StraussTest\ST;
1449+
class StraussTestPackage {
1450+
public function __construct() {
1451+
$arr = array();
1452+
1453+
$arr[ ( new \StraussTest\ST\StraussTestPackage2() )->test() ] = true;
1454+
1455+
$arr[ \StraussTest\ST\StraussTestPackage2::test2() ] = true;
1456+
}
1457+
}
1458+
EOD;
1459+
1460+
$config = $this->createMock(StraussConfig::class);
1461+
1462+
$replacer = new Prefixer($config, __DIR__);
1463+
1464+
$result = $replacer->replaceNamespace($contents, 'ST', 'StraussTest\\ST');
1465+
1466+
$this->assertEquals($expected, $result);
1467+
}
1468+
1469+
/**
1470+
*
1471+
* @see https://github.com/BrianHenryIE/strauss/issues/44
1472+
*
1473+
*/
1474+
public function testItReplacesStaticInsideMultilineTernary(): void
1475+
{
1476+
1477+
$contents = <<<'EOD'
1478+
namespace GuzzleHttp;
1479+
1480+
use Psr\Http\Message\MessageInterface;
1481+
1482+
final class BodySummarizer implements BodySummarizerInterface
1483+
{
1484+
/**
1485+
* Returns a summarized message body.
1486+
*/
1487+
public function summarize(MessageInterface $message): ?string
1488+
{
1489+
return $this->truncateAt === null
1490+
? \GuzzleHttp\Psr7\Message::bodySummary($message)
1491+
: \GuzzleHttp\Psr7\Message::bodySummary($message, $this->truncateAt);
1492+
}
1493+
}
1494+
EOD;
1495+
1496+
$expected = <<<'EOD'
1497+
namespace StraussTest\GuzzleHttp;
1498+
1499+
use Psr\Http\Message\MessageInterface;
1500+
1501+
final class BodySummarizer implements BodySummarizerInterface
1502+
{
1503+
/**
1504+
* Returns a summarized message body.
1505+
*/
1506+
public function summarize(MessageInterface $message): ?string
1507+
{
1508+
return $this->truncateAt === null
1509+
? \StraussTest\GuzzleHttp\Psr7\Message::bodySummary($message)
1510+
: \StraussTest\GuzzleHttp\Psr7\Message::bodySummary($message, $this->truncateAt);
1511+
}
1512+
}
1513+
EOD;
1514+
1515+
$config = $this->createMock(StraussConfig::class);
1516+
1517+
$replacer = new Prefixer($config, __DIR__);
1518+
1519+
$result = $replacer->replaceNamespace($contents, 'GuzzleHttp', 'StraussTest\\GuzzleHttp');
1520+
1521+
$this->assertEquals($expected, $result);
1522+
}
1523+
1524+
14241525
}

0 commit comments

Comments
 (0)