Skip to content

Commit

Permalink
Reflection*::getNamespaceName() returns null when no namespace - name…
Browse files Browse the repository at this point in the history
…space as empty string is implemented only in adapters
  • Loading branch information
kukulich committed Sep 22, 2022
1 parent 0e3d2f7 commit 126c9d4
Show file tree
Hide file tree
Showing 22 changed files with 42 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/NodeCompiler/CompileNodeToValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function __invoke(Node $node, CompilerContext $context): CompiledValue
}

if ($node instanceof Node\Scalar\MagicConst\Namespace_) {
return $context->getNamespace();
return $context->getNamespace() ?? '';
}

if ($node instanceof Node\Scalar\MagicConst\Method) {
Expand Down Expand Up @@ -170,7 +170,7 @@ private function getEnumPropertyValue(Node\Expr\PropertyFetch $node, CompilerCon
private function resolveConstantName(Node\Expr\ConstFetch $constNode, CompilerContext $context): string
{
$constantName = $constNode->name->toString();
$namespace = $context->getNamespace();
$namespace = $context->getNamespace() ?? '';

if ($constNode->name->isUnqualified()) {
$namespacedConstantName = sprintf('%s\\%s', $namespace, $constantName);
Expand Down
4 changes: 2 additions & 2 deletions src/NodeCompiler/CompilerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public function getFileName(): string|null
return $this->getClass()?->getFileName() ?? $this->getFunction()?->getFileName();
}

public function getNamespace(): string
public function getNamespace(): string|null
{
if ($this->contextReflection instanceof ReflectionConstant) {
return $this->contextReflection->getNamespaceName();
}

return $this->getClass()?->getNamespaceName() ?? $this->getFunction()?->getNamespaceName() ?? '';
return $this->getClass()?->getNamespaceName() ?? $this->getFunction()?->getNamespaceName();
}

public function getClass(): ReflectionClass|null
Expand Down
2 changes: 1 addition & 1 deletion src/NodeCompiler/Exception/UnableToCompileNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static function compilerContextToContextDescription(CompilerContext $fet
}

$namespace = $fetchContext->getNamespace();
if ($namespace !== '') {
if ($namespace !== null) {
return sprintf('namespace %s', $namespace);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public function inNamespace(): bool

public function getNamespaceName(): string
{
return $this->betterReflectionClass->getNamespaceName();
return $this->betterReflectionClass->getNamespaceName() ?? '';
}

public function getShortName(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public function inNamespace(): bool

public function getNamespaceName(): string
{
return $this->betterReflectionEnum->getNamespaceName();
return $this->betterReflectionEnum->getNamespaceName() ?? '';
}

public function getShortName(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function getName(): string

public function getNamespaceName(): string
{
return $this->betterReflectionFunction->getNamespaceName();
return $this->betterReflectionFunction->getNamespaceName() ?? '';
}

public function getNumberOfParameters(): int
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function getName(): string

public function getNamespaceName(): string
{
return $this->betterReflectionMethod->getNamespaceName();
return $this->betterReflectionMethod->getNamespaceName() ?? '';
}

public function getNumberOfParameters(): int
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public function inNamespace(): bool

public function getNamespaceName(): string
{
return $this->betterReflectionObject->getNamespaceName();
return $this->betterReflectionObject->getNamespaceName() ?? '';
}

public function getShortName(): string
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ public function getName(): string
* Get the "namespace" name of the class (e.g. for A\B\Foo, this will
* return "A\B").
*/
public function getNamespaceName(): string
public function getNamespaceName(): string|null
{
return $this->namespace ?? '';
return $this->namespace;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/ReflectionConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ public function getName(): string
* Get the "namespace" name of the constant (e.g. for A\B\FOO, this will
* return "A\B").
*/
public function getNamespaceName(): string
public function getNamespaceName(): string|null
{
return $this->namespace ?? '';
return $this->namespace;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Reflection/ReflectionFunctionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,22 @@ abstract public function getShortName(): string;
*/
public function getName(): string
{
if (! $this->inNamespace()) {
$namespace = $this->getNamespaceName();

if ($namespace === null) {
return $this->getShortName();
}

return $this->getNamespaceName() . '\\' . $this->getShortName();
return $namespace . '\\' . $this->getShortName();
}

/**
* Get the "namespace" name of the function (e.g. for A\B\foo, this will
* return "A\B").
*/
public function getNamespaceName(): string
public function getNamespaceName(): string|null
{
return $this->namespace ?? '';
return $this->namespace;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ public function inNamespace(): bool
return false;
}

public function getNamespaceName(): string
public function getNamespaceName(): string|null
{
return '';
return null;
}

public function isClosure(): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function getName(): string
return $this->reflectionClass->getName();
}

public function getNamespaceName(): string
public function getNamespaceName(): string|null
{
return $this->reflectionClass->getNamespaceName();
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/NodeCompiler/CompilerContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,6 @@ public function testCreatingContextWithoutNamespace(): void

$context = new CompilerContext($reflector, $constant);

self::assertSame('', $context->getNamespace());
self::assertNull($context->getNamespace());
}
}
4 changes: 2 additions & 2 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function testClassNameMethodsWithoutNamespace(): void

self::assertFalse($classInfo->inNamespace());
self::assertSame('ClassWithNoNamespace', $classInfo->getName());
self::assertSame('', $classInfo->getNamespaceName());
self::assertNull($classInfo->getNamespaceName());
self::assertSame('ClassWithNoNamespace', $classInfo->getShortName());
}

Expand All @@ -165,7 +165,7 @@ public function testClassNameMethodsWithExplicitGlobalNamespace(): void

self::assertFalse($classInfo->inNamespace());
self::assertSame('ClassWithExplicitGlobalNamespace', $classInfo->getName());
self::assertSame('', $classInfo->getNamespaceName());
self::assertNull($classInfo->getNamespaceName());
self::assertSame('ClassWithExplicitGlobalNamespace', $classInfo->getShortName());
}

Expand Down
6 changes: 3 additions & 3 deletions test/unit/Reflection/ReflectionConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testNameMethodsWithNoNamespaceByConst(): void

self::assertFalse($reflection->inNamespace());
self::assertSame('FOO', $reflection->getName());
self::assertSame('', $reflection->getNamespaceName());
self::assertNull($reflection->getNamespaceName());
self::assertSame('FOO', $reflection->getShortName());
}

Expand All @@ -62,7 +62,7 @@ public function testNameMethodsWithNoNamespaceByDefine(): void

self::assertFalse($reflection->inNamespace());
self::assertSame('FOO', $reflection->getName());
self::assertSame('', $reflection->getNamespaceName());
self::assertNull($reflection->getNamespaceName());
self::assertSame('FOO', $reflection->getShortName());
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public function testNameMethodsInExplicitGlobalNamespace(): void

self::assertFalse($reflection->inNamespace());
self::assertSame('FOO', $reflection->getName());
self::assertSame('', $reflection->getNamespaceName());
self::assertNull($reflection->getNamespaceName());
self::assertSame('FOO', $reflection->getShortName());
}

Expand Down
4 changes: 2 additions & 2 deletions test/unit/Reflection/ReflectionFunctionAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testNameMethodsWithoutNamespace(): void

self::assertSame('foo', $functionInfo->getName());
self::assertFalse($functionInfo->inNamespace());
self::assertSame('', $functionInfo->getNamespaceName());
self::assertNull($functionInfo->getNamespaceName());
self::assertSame('foo', $functionInfo->getShortName());
}

Expand All @@ -85,7 +85,7 @@ public function testNameMethodsInRootNamespace(): void

self::assertSame('foo', $functionInfo->getName());
self::assertFalse($functionInfo->inNamespace());
self::assertSame('', $functionInfo->getNamespaceName());
self::assertNull($functionInfo->getNamespaceName());
self::assertSame('foo', $functionInfo->getShortName());
}

Expand Down
4 changes: 2 additions & 2 deletions test/unit/Reflection/ReflectionFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testNameMethodsWithNoNamespace(): void

self::assertFalse($function->inNamespace());
self::assertSame('foo', $function->getName());
self::assertSame('', $function->getNamespaceName());
self::assertNull($function->getNamespaceName());
self::assertSame('foo', $function->getShortName());
}

Expand All @@ -71,7 +71,7 @@ public function testNameMethodsInExplicitGlobalNamespace(): void

self::assertFalse($function->inNamespace());
self::assertSame('foo', $function->getName());
self::assertSame('', $function->getNamespaceName());
self::assertNull($function->getNamespaceName());
self::assertSame('foo', $function->getShortName());
}

Expand Down
4 changes: 2 additions & 2 deletions test/unit/Reflection/ReflectionMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function testMethodNameWithNamespace(): void

self::assertFalse($methodInfo->inNamespace());
self::assertSame('someMethod', $methodInfo->getName());
self::assertSame('', $methodInfo->getNamespaceName());
self::assertNull($methodInfo->getNamespaceName());
self::assertSame('someMethod', $methodInfo->getShortName());
}

Expand All @@ -251,7 +251,7 @@ public function testMethodNameWithTraitAlias(): void

self::assertFalse($methodInfo->inNamespace());
self::assertSame('b_renamed', $methodInfo->getName());
self::assertSame('', $methodInfo->getNamespaceName());
self::assertNull($methodInfo->getNamespaceName());
self::assertSame('b_renamed', $methodInfo->getShortName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function testInternalConstants(string $constantName, mixed $constantValue
self::assertSame($constantName, $constantReflection->getName());
self::assertSame($constantName, $constantReflection->getShortName());

self::assertNotNull($constantReflection->getNamespaceName());
self::assertNull($constantReflection->getNamespaceName());
self::assertFalse($constantReflection->inNamespace());
self::assertTrue($constantReflection->isInternal());
self::assertFalse($constantReflection->isUserDefined());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public function testInternalConstants(string $constantName, mixed $constantValue
self::assertSame($constantName, $constantReflection->getName());
self::assertSame($constantName, $constantReflection->getShortName());

self::assertNotNull($constantReflection->getNamespaceName());
self::assertNull($constantReflection->getNamespaceName());
self::assertFalse($constantReflection->inNamespace());
self::assertTrue($constantReflection->isInternal());
self::assertFalse($constantReflection->isUserDefined());
Expand Down
10 changes: 5 additions & 5 deletions test/unit/SourceLocator/Type/ClosureSourceLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function setUp(): void
$this->reflector = $this->createMock(Reflector::class);
}

/** @return list<array{0: Closure, 1: string, 2: string, 3: int, 4: int}> */
/** @return list<array{0: Closure, 1: string|null, 2: string, 3: int, 4: int}> */
public function closuresProvider(): array
{
$fileWithClosureInNamespace = FileHelper::normalizeWindowsPath(realpath(__DIR__ . '/../../Fixture/ClosureInNamespace.php'));
Expand All @@ -49,14 +49,14 @@ public function closuresProvider(): array

return [
[require $fileWithClosureInNamespace, 'Roave\BetterReflectionTest\Fixture', $fileWithClosureInNamespace, 5, 8],
[require $fileWithClosureNoNamespace, '', $fileWithClosureNoNamespace, 3, 6],
[require $fileWithClosureNoNamespace, null, $fileWithClosureNoNamespace, 3, 6],
[require $fileWithArrowFunctionInNamespace, 'Roave\BetterReflectionTest\Fixture', $fileWithArrowFunctionInNamespace, 5, 5],
[require $fileWithArrowFunctionNoNamespace, '', $fileWithArrowFunctionNoNamespace, 3, 3],
[require $fileWithArrowFunctionNoNamespace, null, $fileWithArrowFunctionNoNamespace, 3, 3],
];
}

/** @dataProvider closuresProvider */
public function testLocateIdentifier(Closure $closure, string $namespace, string $file, int $startLine, int $endLine): void
public function testLocateIdentifier(Closure $closure, string|null $namespace, string $file, int $startLine, int $endLine): void
{
$locator = new ClosureSourceLocator($closure, $this->parser);

Expand Down Expand Up @@ -97,7 +97,7 @@ public function testEvaledClosureThrowsInvalidFileLocation(): void
}

/** @dataProvider closuresProvider */
public function testLocateIdentifiersByType(Closure $closure, string $namespace, string $file, int $startLine, int $endLine): void
public function testLocateIdentifiersByType(Closure $closure, string|null $namespace, string $file, int $startLine, int $endLine): void
{
/** @var list<ReflectionFunction> $reflections */
$reflections = (new ClosureSourceLocator($closure, $this->parser))->locateIdentifiersByType(
Expand Down

0 comments on commit 126c9d4

Please sign in to comment.