Skip to content

Commit a814daa

Browse files
committed
fix: resolve namespace section and traits correctly (fixes #111)
1 parent c7df087 commit a814daa

File tree

5 files changed

+122
-8
lines changed

5 files changed

+122
-8
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
],
2626
"require": {
2727
"php": "^7.2",
28-
"ext-json": "*"
28+
"ext-json": "*",
29+
"rawr/t-regx": "^0.9.6"
2930
},
3031
"require-dev": {
3132
"codeception/codeception": "^4.1",

composer.lock

+72-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Transformer.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace TypistTech\Imposter;
66

77
use SplFileInfo;
8+
use TRegx\CleanRegex\Match\Details\Match;
89

910
class Transformer implements TransformerInterface
1011
{
@@ -155,12 +156,29 @@ private function prefixUseFunction(string $targetFile)
155156
private function prefixUse(string $targetFile)
156157
{
157158
$pattern = sprintf(
158-
'/%1$s\\s+(?!(const)|(function)|(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))/',
159+
'%1$s\\s+(?!(const)|(function)|(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))',
159160
'use',
160161
$this->namespacePrefix
161162
);
162-
$replacement = sprintf('%1$s %2$s', 'use', $this->namespacePrefix);
163-
164-
$this->replace($pattern, $replacement, $targetFile);
163+
$replacement = sprintf('%1$s %2$s', 'use', str_replace('\\\\', '\\', $this->namespacePrefix));
164+
165+
$content = $this->filesystem->get($targetFile);
166+
$output = pattern($pattern)->replace($content)->all()
167+
->callback(function (Match $m) use ($replacement, $content) {
168+
// Find previous offset content and check if the last match of namespace or class is "class"
169+
$offsetContent = substr($content, 0, $m->offset());
170+
preg_match_all(
171+
'/(namespace|class)[A-Za-z0-9\\\\ ]+.?{/sm',
172+
$offsetContent,
173+
$nsClass,
174+
PREG_SET_ORDER
175+
);
176+
if (count($nsClass) > 0 && $nsClass[count($nsClass) - 1][1] === 'class') {
177+
return $m->text();
178+
}
179+
return $replacement;
180+
});
181+
182+
$this->filesystem->put($targetFile, $output);
165183
}
166184
}

tests/_data/fake-vendor/Dummy.php

+13
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@
1313
use \UnexpectedValueException;
1414
use function OtherVendor\myFunc;
1515
use const OtherVendor\MY_MAGIC_NUMBER;
16+
use OtherVendor\MyTrait;
17+
use OtherVendor\Package;
18+
19+
namespace OtherVendor\Package2 {
20+
use OtherVendor\MyTrait;
21+
22+
class DummyClass2 {
23+
use OtherVendor\MyTrait;
24+
}
25+
}
1626

1727
$closure = function () use ($aaa) {
1828
// Just testing.
1929
};
2030

2131
class DummyClass
2232
{
33+
use MyTrait;
34+
use Package\OtherTrait;
35+
2336
public function useClosure()
2437
{
2538
array_map(function () use ($xxx) {

tests/_data/fake-vendor/Expected.php

+13
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@
1313
use \UnexpectedValueException;
1414
use function MyPlugin\Vendor\OtherVendor\myFunc;
1515
use const MyPlugin\Vendor\OtherVendor\MY_MAGIC_NUMBER;
16+
use MyPlugin\Vendor\OtherVendor\MyTrait;
17+
use MyPlugin\Vendor\OtherVendor\Package;
18+
19+
namespace MyPlugin\Vendor\OtherVendor\Package2 {
20+
use MyPlugin\Vendor\OtherVendor\MyTrait;
21+
22+
class DummyClass2 {
23+
use OtherVendor\MyTrait;
24+
}
25+
}
1626

1727
$closure = function () use ($aaa) {
1828
// Just testing.
1929
};
2030

2131
class DummyClass
2232
{
33+
use MyTrait;
34+
use Package\OtherTrait;
35+
2336
public function useClosure()
2437
{
2538
array_map(function () use ($xxx) {

0 commit comments

Comments
 (0)