Skip to content

Commit 68155c0

Browse files
nsbuckyKenrick Buchanan
andauthored
suggested fix for relativeNamespace function (#87)
Co-authored-by: Kenrick Buchanan <kenrick@specificperformance.com>
1 parent 49444de commit 68155c0

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/Blueprint.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ class Blueprint
1111
private $lexers = [];
1212
private $generators = [];
1313

14-
public static function relativeNamespace(string $fullyQualifiedClassName)
15-
{
16-
return ltrim(str_replace(config('blueprint.namespace'), '', $fullyQualifiedClassName), '\\');
17-
}
14+
public static function relativeNamespace(string $fullyQualifiedClassName)
15+
{
16+
$newClassName = preg_replace(
17+
'!^'.preg_quote(config('blueprint.namespace')).'!',
18+
'',
19+
$fullyQualifiedClassName,
20+
1
21+
);
22+
23+
return ltrim($newClassName,'\\');
24+
}
1825

1926
public function parse($content)
2027
{

tests/Feature/BlueprintTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,5 +274,27 @@ public function generate_uses_registered_generators_and_returns_generated_files(
274274
'updated' => ['one/existing.php', 'two/existing.php'],
275275
'deleted' => ['one/trashed.php', 'two/trashed.php'],
276276
], $this->subject->generate($tree));
277+
}
278+
279+
/**
280+
* @test
281+
*/
282+
public function relative_namespace_only_replace_first_occurrence_of_default_namespace()
283+
{
284+
$string = "App\Appointments";
285+
286+
$actual = Blueprint::relativeNamespace($string);
287+
288+
$this->assertEquals("Appointments", $actual);
289+
290+
config(['blueprint.namespace'=>'Foo']);
291+
292+
$string = "Foo\Appointments";
293+
294+
$actual = Blueprint::relativeNamespace($string);
295+
296+
$this->assertEquals("Appointments", $actual);
297+
298+
277299
}
278300
}

0 commit comments

Comments
 (0)