|
5 | 5 | namespace TypistTech\Imposter; |
6 | 6 |
|
7 | 7 | use SplFileInfo; |
| 8 | +use TRegx\CleanRegex\Match\Details\Match; |
8 | 9 |
|
9 | 10 | class Transformer implements TransformerInterface |
10 | 11 | { |
@@ -155,12 +156,23 @@ private function prefixUseFunction(string $targetFile) |
155 | 156 | private function prefixUse(string $targetFile) |
156 | 157 | { |
157 | 158 | $pattern = sprintf( |
158 | | - '/%1$s\\s+(?!(const)|(function)|(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))/', |
| 159 | + '%1$s\\s+(?!(const)|(function)|(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))', |
159 | 160 | 'use', |
160 | 161 | $this->namespacePrefix |
161 | 162 | ); |
162 | | - $replacement = sprintf('%1$s %2$s', 'use', $this->namespacePrefix); |
| 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()->callback(function (Match $m) use ($replacement, $content) { |
| 167 | + // Find previous offset content and check if the last match of namespace or class is "class" |
| 168 | + $offsetContent = substr($content, 0, $m->offset()); |
| 169 | + preg_match_all('/(namespace|class)[A-Za-z0-9\\\\ ]+.?{/sm', $offsetContent, $namespaceClassMatches, PREG_SET_ORDER); |
| 170 | + if (count($namespaceClassMatches) > 0 && $namespaceClassMatches[count($namespaceClassMatches) - 1][1] === 'class') { |
| 171 | + return $m->text(); |
| 172 | + } |
| 173 | + return $replacement; |
| 174 | + }); |
163 | 175 |
|
164 | | - $this->replace($pattern, $replacement, $targetFile); |
| 176 | + $this->filesystem->put($targetFile, $output); |
165 | 177 | } |
166 | 178 | } |
0 commit comments