Skip to content

Commit 5caf819

Browse files
authored
Merge pull request #1428 from staabm/patch-1
Improve `getNamespacedName()` return type
2 parents 4609cd4 + c91de93 commit 5caf819

8 files changed

+45
-7
lines changed

composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
"conflict": {
5050
"thecodingmachine/safe": "<1.1.3"
5151
},
52+
"scripts": {
53+
"cs": "tools/vendor/bin/phpcs",
54+
"csfix": "tools/vendor/bin/phpcbf",
55+
"psalm": "tools/vendor/bin/psalm",
56+
"psalm-baseline": "tools/vendor/bin/psalm --set-baseline=psalm-baseline.xml"
57+
},
5258
"suggest": {
5359
"composer/composer": "Required to use the ComposerSourceLocator"
5460
},

src/Reflection/ReflectionClass.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ class ReflectionClass implements Reflection
146146
*/
147147
private array|null $cachedParentClasses = null;
148148

149-
/** @internal */
149+
/**
150+
* @internal
151+
*
152+
* @param non-empty-string|null $namespace
153+
*/
150154
protected function __construct(
151155
private Reflector $reflector,
152156
ClassNode|InterfaceNode|TraitNode|EnumNode $node,
@@ -253,7 +257,7 @@ public static function createFromInstance(object $instance): self
253257
* @internal
254258
*
255259
* @param ClassNode|InterfaceNode|TraitNode|EnumNode $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
256-
* @param string|null $namespace optional - if omitted, we assume it is global namespaced class
260+
* @param non-empty-string|null $namespace optional - if omitted, we assume it is global namespaced class
257261
*/
258262
public static function createFromNode(
259263
Reflector $reflector,
@@ -329,6 +333,8 @@ public function getParentClassName(): string|null
329333
/**
330334
* Get the "namespace" name of the class (e.g. for A\B\Foo, this will
331335
* return "A\B").
336+
*
337+
* @return non-empty-string|null
332338
*/
333339
public function getNamespaceName(): string|null
334340
{

src/Reflection/ReflectionConstant.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ReflectionConstant implements Reflection
6161
/** @psalm-allow-private-mutation */
6262
private CompiledValue|null $compiledValue = null;
6363

64+
/** @param non-empty-string|null $namespace */
6465
private function __construct(
6566
private Reflector $reflector,
6667
Node\Stmt\Const_|Node\Expr\FuncCall $node,
@@ -108,7 +109,8 @@ public static function createFromName(string $constantName): self
108109
*
109110
* @internal
110111
*
111-
* @param Node\Stmt\Const_|Node\Expr\FuncCall $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
112+
* @param Node\Stmt\Const_|Node\Expr\FuncCall $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
113+
* @param non-empty-string|null $namespace
112114
*/
113115
public static function createFromNode(
114116
Reflector $reflector,
@@ -126,6 +128,7 @@ public static function createFromNode(
126128
return self::createFromDefineFunctionCall($reflector, $node, $locatedSource);
127129
}
128130

131+
/** @param non-empty-string|null $namespace */
129132
private static function createFromConstKeyword(
130133
Reflector $reflector,
131134
Node\Stmt\Const_ $node,
@@ -182,6 +185,8 @@ public function getName(): string
182185
/**
183186
* Get the "namespace" name of the constant (e.g. for A\B\FOO, this will
184187
* return "A\B").
188+
*
189+
* @return non-empty-string|null
185190
*/
186191
public function getNamespaceName(): string|null
187192
{

src/Reflection/ReflectionEnum.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ class ReflectionEnum extends ReflectionClass
2727
/** @var array<non-empty-string, ReflectionEnumCase> */
2828
private array $cases;
2929

30-
/** @phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found */
30+
/**
31+
* @param non-empty-string|null $namespace
32+
*
33+
* @phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found
34+
*/
3135
private function __construct(
3236
private Reflector $reflector,
3337
EnumNode $node,
@@ -43,7 +47,8 @@ private function __construct(
4347
/**
4448
* @internal
4549
*
46-
* @param EnumNode $node
50+
* @param EnumNode $node
51+
* @param non-empty-string|null $namespace
4752
*
4853
* @psalm-suppress MoreSpecificImplementedParamType
4954
*/

src/Reflection/ReflectionFunction.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ReflectionFunction implements Reflection
2929

3030
private bool $isStatic;
3131

32+
/** @param non-empty-string|null $namespace */
3233
private function __construct(
3334
private Reflector $reflector,
3435
Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node,
@@ -76,7 +77,11 @@ public function __toString(): string
7677
return ReflectionFunctionStringCast::toString($this);
7778
}
7879

79-
/** @internal */
80+
/**
81+
* @internal
82+
*
83+
* @param non-empty-string|null $namespace
84+
*/
8085
public static function createFromNode(
8186
Reflector $reflector,
8287
Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node,

src/Reflection/ReflectionFunctionAbstract.php

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ public function getName(): string
174174
/**
175175
* Get the "namespace" name of the function (e.g. for A\B\foo, this will
176176
* return "A\B").
177+
*
178+
* @return non-empty-string|null
177179
*/
178180
public function getNamespaceName(): string|null
179181
{

src/Reflection/ReflectionMethod.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ class ReflectionMethod
3333
/** @var int-mask-of<ReflectionMethodAdapter::IS_*> */
3434
private int $modifiers;
3535

36-
/** @param non-empty-string|null $aliasName */
36+
/**
37+
* @param non-empty-string|null $aliasName
38+
* @param non-empty-string|null $namespace
39+
*/
3740
private function __construct(
3841
private Reflector $reflector,
3942
MethodNode|Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node,
@@ -59,6 +62,7 @@ private function __construct(
5962
* @internal
6063
*
6164
* @param non-empty-string|null $aliasName
65+
* @param non-empty-string|null $namespace
6266
*/
6367
public static function createFromNode(
6468
Reflector $reflector,

src/SourceLocator/Ast/Strategy/NodeToReflection.php

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Roave\BetterReflection\SourceLocator\Ast\Strategy;
66

7+
use LogicException;
78
use PhpParser\Node;
89
use Roave\BetterReflection\Reflection\ReflectionClass;
910
use Roave\BetterReflection\Reflection\ReflectionConstant;
@@ -28,6 +29,10 @@ public function __invoke(
2829
): ReflectionClass|ReflectionConstant|ReflectionFunction {
2930
$namespaceName = $namespace?->name?->name;
3031

32+
if ($namespaceName === '') {
33+
throw new LogicException('Namespace name should never be empty');
34+
}
35+
3136
if ($node instanceof Node\Stmt\Enum_) {
3237
return ReflectionEnum::createFromNode(
3338
$reflector,

0 commit comments

Comments
 (0)