Provides utilities for dealing with PHP name qualifications, such as resolving and localizing names.
Provides various utilities related to name qualification in PHP:
This project falls under the Serenata umbrella.
composer require "serenata/name-qualification-utilities"
<?php
use Serenata\Common\Position;
use Serenata\Common\FilePosition;
use Serenata\NameQualificationUtilities;
// NOTE: Implement a suitable provider in your code.
$fileNamespaceProvider = new class implements NameQualificationUtilities\FileNamespaceProviderInterface {
public function provide(string $file): array
{
return $arrayOfNamespaceObjectsWithImportsAttached;
}
};
$positionalNamespaceDeterminer = new NameQualificationUtilities\PositionalNamespaceDeterminer($fileNamespaceProvider);
$factory = new NameQualificationUtilities\PositionalNameResolverFactory($positionalNamespaceDeterminer);
// Create a name resolver that can resolve names using the imports and namespace that apply at the specified position.
$position = new Position($someLineNumber, $someCharacterIndexOnLine);
$filePosition = new FilePosition('MyPhpFile.php', $position);
$nameResolver = $factory->create($filePosition);
$result = $nameResolver->resolve('A', NameQualificationUtilities\NameKind::CLASSLIKE);
// $result will now contain the FQCN of the class, e.g. '\A', '\SomeNamespace\A', ...
Actual parsing of code to find available imports and namespaces is not provided, but you can use a tool such as php-parser for this. Instead, the available imports and namespaces have to be provided by the user of this library by implementing the interface FileNamespaceProviderInterface
.
It is necessary because PHP supports use statements or imports with the same name, as long as their "kind" is different. In other words, you can import a constant with the same name as a class:
<?php
use A\B;
use function A\B;
use const A\B;
NamespaceNameResolver
and StructureAwareNameResolver
?Some constant and function names can be ambiguous, for example:
<?php
namespace A;
$a = SOME_CONSTANT;
SOME_CONSTANT
can refer to \A\SOME_CONSTANT
or \SOME_CONSTANT
, PHP determines which one to use at runtime:
\A\SOME_CONSTANT
if it exists\SOME_CONSTANT
if it exists\A\SOME_CONSTANT
again and fail because neither existNamespaceNameResolver
does not know how to handle this situation and will bail out with an exception that the name is ambiguous.
StructureAwareNameResolver
will request additional information - from a provider you must inject into it - to determine which structures actually exist in order to successfully resolve the name.