forked from CuyZ/Valinor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle namespace for Closure without class scope
- Loading branch information
Showing
6 changed files
with
117 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Utility\Reflection; | ||
|
||
use PhpToken; | ||
|
||
/** @internal */ | ||
final class NamespaceFinder | ||
{ | ||
public function findNamespace(string $content): ?string | ||
{ | ||
$tokens = PhpToken::tokenize($content); | ||
$tokensCount = count($tokens); | ||
/* @infection-ignore-all Unneeded because of the nature of namespace-related token */ | ||
$pointer = $tokensCount - 1; | ||
|
||
while (! $tokens[$pointer]->is(T_NAMESPACE)) { | ||
/* @infection-ignore-all Unneeded because of the nature of namespace-related token */ | ||
if ($pointer === 0) { | ||
return null; | ||
} | ||
|
||
$pointer--; | ||
} | ||
|
||
while (! $tokens[$pointer]->is([T_NAME_QUALIFIED, T_STRING])) { | ||
$pointer++; | ||
} | ||
|
||
return (string)$tokens[$pointer]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Functional\Utility\Reflection; | ||
|
||
use CuyZ\Valinor\Utility\Reflection\PhpParser; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionFunction; | ||
|
||
final class PhpParserTest extends TestCase | ||
{ | ||
public function test_can_parse_namespace_for_closure_with_one_level_namespace(): void | ||
{ | ||
$function = require_once 'closure-with-one-level-namespace.php'; | ||
|
||
$reflection = new ReflectionFunction($function); | ||
|
||
$namespace = PhpParser::parseNamespace($reflection); | ||
|
||
self::assertSame('OneLevelNamespace', $namespace); | ||
} | ||
|
||
public function test_can_parse_namespace_for_closure_with_qualified_namespace(): void | ||
{ | ||
$function = require_once 'closure-with-qualified-namespace.php'; | ||
|
||
$reflection = new ReflectionFunction($function); | ||
|
||
$namespace = PhpParser::parseNamespace($reflection); | ||
|
||
self::assertSame('Root\QualifiedNamespace', $namespace); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/Functional/Utility/Reflection/closure-with-one-level-namespace.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace OneLevelNamespace; | ||
|
||
return function () {}; |
5 changes: 5 additions & 0 deletions
5
tests/Functional/Utility/Reflection/closure-with-qualified-namespace.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace Root\QualifiedNamespace; | ||
|
||
return function () {}; |