Skip to content

Commit

Permalink
Merge pull request #1428 from staabm/patch-1
Browse files Browse the repository at this point in the history
Improve `getNamespacedName()` return type
  • Loading branch information
Ocramius committed May 31, 2024
2 parents 4609cd4 + c91de93 commit 5caf819
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 7 deletions.
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
"conflict": {
"thecodingmachine/safe": "<1.1.3"
},
"scripts": {
"cs": "tools/vendor/bin/phpcs",
"csfix": "tools/vendor/bin/phpcbf",
"psalm": "tools/vendor/bin/psalm",
"psalm-baseline": "tools/vendor/bin/psalm --set-baseline=psalm-baseline.xml"
},
"suggest": {
"composer/composer": "Required to use the ComposerSourceLocator"
},
Expand Down
10 changes: 8 additions & 2 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ class ReflectionClass implements Reflection
*/
private array|null $cachedParentClasses = null;

/** @internal */
/**
* @internal
*
* @param non-empty-string|null $namespace
*/
protected function __construct(
private Reflector $reflector,
ClassNode|InterfaceNode|TraitNode|EnumNode $node,
Expand Down Expand Up @@ -253,7 +257,7 @@ public static function createFromInstance(object $instance): self
* @internal
*
* @param ClassNode|InterfaceNode|TraitNode|EnumNode $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
* @param string|null $namespace optional - if omitted, we assume it is global namespaced class
* @param non-empty-string|null $namespace optional - if omitted, we assume it is global namespaced class
*/
public static function createFromNode(
Reflector $reflector,
Expand Down Expand Up @@ -329,6 +333,8 @@ public function getParentClassName(): string|null
/**
* Get the "namespace" name of the class (e.g. for A\B\Foo, this will
* return "A\B").
*
* @return non-empty-string|null
*/
public function getNamespaceName(): string|null
{
Expand Down
7 changes: 6 additions & 1 deletion src/Reflection/ReflectionConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ReflectionConstant implements Reflection
/** @psalm-allow-private-mutation */
private CompiledValue|null $compiledValue = null;

/** @param non-empty-string|null $namespace */
private function __construct(
private Reflector $reflector,
Node\Stmt\Const_|Node\Expr\FuncCall $node,
Expand Down Expand Up @@ -108,7 +109,8 @@ public static function createFromName(string $constantName): self
*
* @internal
*
* @param Node\Stmt\Const_|Node\Expr\FuncCall $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
* @param Node\Stmt\Const_|Node\Expr\FuncCall $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
* @param non-empty-string|null $namespace
*/
public static function createFromNode(
Reflector $reflector,
Expand All @@ -126,6 +128,7 @@ public static function createFromNode(
return self::createFromDefineFunctionCall($reflector, $node, $locatedSource);
}

/** @param non-empty-string|null $namespace */
private static function createFromConstKeyword(
Reflector $reflector,
Node\Stmt\Const_ $node,
Expand Down Expand Up @@ -182,6 +185,8 @@ public function getName(): string
/**
* Get the "namespace" name of the constant (e.g. for A\B\FOO, this will
* return "A\B").
*
* @return non-empty-string|null
*/
public function getNamespaceName(): string|null
{
Expand Down
9 changes: 7 additions & 2 deletions src/Reflection/ReflectionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class ReflectionEnum extends ReflectionClass
/** @var array<non-empty-string, ReflectionEnumCase> */
private array $cases;

/** @phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found */
/**
* @param non-empty-string|null $namespace
*
* @phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found
*/
private function __construct(
private Reflector $reflector,
EnumNode $node,
Expand All @@ -43,7 +47,8 @@ private function __construct(
/**
* @internal
*
* @param EnumNode $node
* @param EnumNode $node
* @param non-empty-string|null $namespace
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Reflection/ReflectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ReflectionFunction implements Reflection

private bool $isStatic;

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

/** @internal */
/**
* @internal
*
* @param non-empty-string|null $namespace
*/
public static function createFromNode(
Reflector $reflector,
Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node,
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/ReflectionFunctionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ public function getName(): string
/**
* Get the "namespace" name of the function (e.g. for A\B\foo, this will
* return "A\B").
*
* @return non-empty-string|null
*/
public function getNamespaceName(): string|null
{
Expand Down
6 changes: 5 additions & 1 deletion src/Reflection/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ class ReflectionMethod
/** @var int-mask-of<ReflectionMethodAdapter::IS_*> */
private int $modifiers;

/** @param non-empty-string|null $aliasName */
/**
* @param non-empty-string|null $aliasName
* @param non-empty-string|null $namespace
*/
private function __construct(
private Reflector $reflector,
MethodNode|Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node,
Expand All @@ -59,6 +62,7 @@ private function __construct(
* @internal
*
* @param non-empty-string|null $aliasName
* @param non-empty-string|null $namespace
*/
public static function createFromNode(
Reflector $reflector,
Expand Down
5 changes: 5 additions & 0 deletions src/SourceLocator/Ast/Strategy/NodeToReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Roave\BetterReflection\SourceLocator\Ast\Strategy;

use LogicException;
use PhpParser\Node;
use Roave\BetterReflection\Reflection\ReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionConstant;
Expand All @@ -28,6 +29,10 @@ public function __invoke(
): ReflectionClass|ReflectionConstant|ReflectionFunction {
$namespaceName = $namespace?->name?->name;

if ($namespaceName === '') {
throw new LogicException('Namespace name should never be empty');
}

if ($node instanceof Node\Stmt\Enum_) {
return ReflectionEnum::createFromNode(
$reflector,
Expand Down

0 comments on commit 5caf819

Please sign in to comment.