Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
],
"require": {
"php": "^7.2",
"ext-json": "*"
"ext-json": "*",
"rawr/t-regx": "^0.9.6"
},
"require-dev": {
"codeception/codeception": "^4.1",
Expand Down
75 changes: 72 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions src/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace TypistTech\Imposter;

use SplFileInfo;
use TRegx\CleanRegex\Match\Details\Match;

class Transformer implements TransformerInterface
{
Expand Down Expand Up @@ -155,12 +156,29 @@ private function prefixUseFunction(string $targetFile)
private function prefixUse(string $targetFile)
{
$pattern = sprintf(
'/%1$s\\s+(?!(const)|(function)|(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))/',
'%1$s\\s+(?!(const)|(function)|(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))',
'use',
$this->namespacePrefix
);
$replacement = sprintf('%1$s %2$s', 'use', $this->namespacePrefix);

$this->replace($pattern, $replacement, $targetFile);
$replacement = sprintf('%1$s %2$s', 'use', str_replace('\\\\', '\\', $this->namespacePrefix));

$content = $this->filesystem->get($targetFile);
$output = pattern($pattern)->replace($content)->all()
->callback(function (Match $m) use ($replacement, $content) {
// Find previous offset content and check if the last match of namespace or class is "class"
$offsetContent = substr($content, 0, $m->offset());
preg_match_all(
'/(namespace|class)[A-Za-z0-9\\\\ ]+.?{/sm',
$offsetContent,
$nsClass,
PREG_SET_ORDER
);
if (count($nsClass) > 0 && $nsClass[count($nsClass) - 1][1] === 'class') {
return $m->text();
}
return $replacement;
});

$this->filesystem->put($targetFile, $output);
}
}
13 changes: 13 additions & 0 deletions tests/_data/fake-vendor/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@
use \UnexpectedValueException;
use function OtherVendor\myFunc;
use const OtherVendor\MY_MAGIC_NUMBER;
use OtherVendor\MyTrait;
use OtherVendor\Package;

namespace OtherVendor\Package2 {
use OtherVendor\MyTrait;

class DummyClass2 {
use OtherVendor\MyTrait;
}
}

$closure = function () use ($aaa) {
// Just testing.
};

class DummyClass
{
use MyTrait;
use Package\OtherTrait;

public function useClosure()
{
array_map(function () use ($xxx) {
Expand Down
13 changes: 13 additions & 0 deletions tests/_data/fake-vendor/Expected.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@
use \UnexpectedValueException;
use function MyPlugin\Vendor\OtherVendor\myFunc;
use const MyPlugin\Vendor\OtherVendor\MY_MAGIC_NUMBER;
use MyPlugin\Vendor\OtherVendor\MyTrait;
use MyPlugin\Vendor\OtherVendor\Package;

namespace MyPlugin\Vendor\OtherVendor\Package2 {
use MyPlugin\Vendor\OtherVendor\MyTrait;

class DummyClass2 {
use OtherVendor\MyTrait;
}
}

$closure = function () use ($aaa) {
// Just testing.
};

class DummyClass
{
use MyTrait;
use Package\OtherTrait;

public function useClosure()
{
array_map(function () use ($xxx) {
Expand Down