Maslosoft Addendum API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
<?php
/**
* This software package is licensed under AGPL, Commercial license.
*
* @package maslosoft/addendum
* @licence AGPL, Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com> (Meta container, further improvements, bugfixes)
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
* @link https://maslosoft.com/addendum/ - maslosoft addendum
* @link https://code.google.com/p/addendum/ - original addendum project
*/
namespace Maslosoft\Addendum\Utilities;
use Maslosoft\Addendum\Builder\DocComment;
use Maslosoft\Addendum\Utilities\NameNormalizer;
use ReflectionMethod;
use ReflectionProperty;
use Reflector;
/**
* UseResolver
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class UseResolver
{
/**
* Resolve class alias
* @param Reflector $reflection
* @param string $className
* @return string
*/
public static function resolve(Reflector $reflection, $className)
{
if ($reflection instanceof ReflectionProperty || $reflection instanceof ReflectionMethod)
{
$reflection = $reflection->getDeclaringClass();
}
$docs = (new DocComment())->forClass($reflection);
$use = $docs['use'];
$ns = $docs['namespace'];
$aliases = $docs['useAliases'];
// Resolve to itself with keywords
if ($className === 'self' || $className === 'static')
{
$fqn = $ns . '\\' . $docs['className'];
return $fqn;
}
// This is for same namespaced class as current class
if (strpos($className, '\\') === false)
{
$aliases[$ns . '\\' . $className] = $className;
}
if (in_array($className, $use))
{
return $className;
}
foreach ($use as $useClause)
{
$patternClass = preg_quote($className);
$pattern = "~\\\\$patternClass$~";
if (preg_match($pattern, $useClause))
{
return $useClause;
}
}
foreach ($aliases as $useClause => $alias)
{
if ($className == $alias)
{
NameNormalizer::normalize($useClause, false);
return $useClause;
}
}
return $className;
}
}
API documentation generated by ApiGen