## Background / motivation

Currently a number of reflection classes have a method ->getNamespaceName().

The namespace comes in a format that is not straightforward to work with.
It requires the root namespace to be treated in a special way.

$namespace = $relfClass->getNamespaceName();
$shortname = $reflClass->getShortName();
$class = $reflClass->getName();

if ('' === $namespace) {
  assert($class === $namespace . $shortname);
  assert($class === $shortname);
  assert($class !== $namespace . '\\' . $shortname);
}
else {
  assert($class === $namespace . '\\' . $shortname;
  assert($class !== $namespace . $shortname);
}

Code that deals with namespaces therefore always needs a special case
for the root namespace.


## Proposal

I propose that reflection components that currently have a
->getNamespaceName() method get another method
->getTerminatedNamespace().
This comes with ending namespace separator, unless it is the root namespace.

namespace Acme\Animal;
class Cat {}

assert('Acme\Animal\\' === (new
\ReflectionClass(Cat::class))->getTerminatedNamespace());
assert('Acme\Animal' === (new
\ReflectionClass(Cat::class))->getNamespaceName());

assert('' === (new
\ReflectionClass(\stdClass::class))->getTerminatedNamespace());
assert('' === (new \ReflectionClass(\stdClass::class))->getNamespaceName());

foreach ([Cat::class, \stdClass::class] as $class) {
  $reflClass = new \ReflectionClass($class);
  assert($reflClass->getName() ===
$reflClass->getTerminatedNamespace() . $reflClass->getShortName());
}

This would allow all cases to be covered by the same logic.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to