Skip to content

Commit

Permalink
Prefix instanceof ...
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Dec 12, 2023
1 parent 6a913be commit 67f471a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Prefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public function replaceNamespace(string $contents, string $originalNamespace, st
|implements\s+
|extends\s+ # when the class being extended is namespaced inline
|return\s+
|instanceof\s+ # when checking the class type of an object in a conditional
|\(\s* # inside a function declaration as the first parameters type
|,\s* # inside a function declaration as a subsequent parameter type
|\.\s* # as part of a concatenated string
Expand Down
63 changes: 63 additions & 0 deletions tests/Issues/StraussIssue83Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* instanceof not prefixed properly.
*
* @see https://github.com/BrianHenryIE/strauss/issues/83
*/

namespace BrianHenryIE\Strauss\Tests\Issues;

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

/**
* @package BrianHenryIE\Strauss\Tests\Issues
* @coversNothing
*/
class StraussIssue83Test extends \BrianHenryIE\Strauss\Tests\Integration\Util\IntegrationTestCase
{
public function test_namespace_keyword_on_opening_line()
{
self::markTestSkipped('Slow test.');

$composerJsonString = <<<'EOD'
{
"name": "issue/83",
"require": {
"aws/aws-sdk-php": "3.293.8"
},
"extra": {
"strauss": {
"namespace_prefix": "Company\\Project\\",
"exclude_from_copy": {
"file_patterns": [
"/^((?!aws\\/aws-sdk-php).)*$/"
]
}
}
}
}
EOD;

chdir($this->testsWorkingDir);

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

exec('composer install');

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

$strauss = new Compose();

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

self::assertEquals(0, $result);

$php_string = file_get_contents($this->testsWorkingDir . '/vendor-prefixed/aws/aws-sdk-php/src/ClientResolver.php');

self::assertStringNotContainsString('$value instanceof \Aws\EndpointV2\EndpointProviderV2', $php_string);
self::assertStringContainsString('$value instanceof \Company\Project\Aws\EndpointV2\EndpointProviderV2', $php_string);
}
}
53 changes: 52 additions & 1 deletion tests/Unit/PrefixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Composer\Composer;
use Composer\Config;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputInterface;

/**
* Class ReplacerTest
Expand Down Expand Up @@ -43,7 +44,9 @@ protected function aaasetUp(): void
$composer = new Composer();
$composer->setConfig($composerConfig);

$this->config = new StraussConfig($composer);
$input = $this->createMock(InputInterface::class);

$this->config = new StraussConfig($composer, $input);
}

public function testNamespaceReplacer()
Expand Down Expand Up @@ -1784,4 +1787,52 @@ public function get_google_client() {

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

/**
* @see https://github.com/BrianHenryIE/strauss/issues/83
* @see vendor-prefixed/aws/aws-sdk-php/src/ClientResolver.php:955
*/
public function testPrefixesFullNamespaceInInstanceOf(): void
{
$contents = <<<'EOD'
<?php
namespace Aws;
class ClientResolver
public static function _apply_user_agent($inputUserAgent, array &$args, HandlerList $list)
{
if (($args['endpoint_discovery'] instanceof \Aws\EndpointDiscovery\Configuration
&& $args['endpoint_discovery']->isEnabled())
) {
}
}
}
EOD;

$expected = <<<'EOD'
<?php
namespace Company\Project\Aws;
class ClientResolver
public static function _apply_user_agent($inputUserAgent, array &$args, HandlerList $list)
{
if (($args['endpoint_discovery'] instanceof \Company\Project\Aws\EndpointDiscovery\Configuration
&& $args['endpoint_discovery']->isEnabled())
) {
}
}
}
EOD;

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

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

$result = $replacer->replaceNamespace($contents, 'Aws\\EndpointDiscovery', 'Company\\Project\\Aws\\EndpointDiscovery');
$result = $replacer->replaceNamespace($result, 'Aws', 'Company\\Project\\Aws');

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

0 comments on commit 67f471a

Please sign in to comment.