22 private $scalarTypesProvider;
27 private $namespaces = [];
47 if (substr(
$type, 0, 1) !== self::NS_SEPARATOR
48 && !in_array(
$type, $this->scalarTypesProvider->getTypes())
52 $unqualifiedName =
$name[0];
53 $isQualifiedName = count(
$name) > 1 ? true :
false;
54 if (isset($availableNamespaces[$unqualifiedName])) {
55 $namespace = $availableNamespaces[$unqualifiedName];
56 if ($isQualifiedName) {
58 return $namespace . self::NS_SEPARATOR . implode(self::NS_SEPARATOR,
$name);
62 return self::NS_SEPARATOR . $availableNamespaces[0] . self::NS_SEPARATOR .
$type;
77 $fileContent = implode(
'', $fileContent);
79 $cacheKey = sha1($fileContent);
81 if (isset($this->namespaces[$cacheKey])) {
82 return $this->namespaces[$cacheKey];
85 $fileContent = token_get_all($fileContent);
86 $classStart = array_search(
'{', $fileContent);
87 $fileContent = array_slice($fileContent, 0, $classStart);
89 foreach ($fileContent as $position =>
$token) {
91 $import = array_slice($fileContent, $position);
92 $importEnd = array_search(
';', $import);
93 $import = array_slice($import, 0, $importEnd);
96 foreach ($import as
$item) {
101 $imports[$importsCount][] =
$item;
103 foreach ($imports as $import) {
104 $import = array_filter($import,
function (
$token) {
105 $whitelist = [T_NS_SEPARATOR, T_STRING, T_AS];
106 if (isset(
$token[0]) && in_array(
$token[0], $whitelist)) {
111 $import = array_map(
function (
$element) {
114 $import = array_values($import);
115 if ($import[0] === self::NS_SEPARATOR) {
116 array_shift($import);
119 if (in_array(
'as', $import)) {
120 $importName = array_splice($import, -1)[0];
123 $useStatement = implode(
'', $import);
125 $output[$importName] = self::NS_SEPARATOR . $useStatement;
127 $key = explode(self::NS_SEPARATOR, $useStatement);
129 $output[$key] = self::NS_SEPARATOR . $useStatement;
134 $this->namespaces[$cacheKey] =
$output;
135 return $this->namespaces[$cacheKey];
resolveNamespace($type, array $availableNamespaces)
__construct(ScalarTypesProvider $scalarTypesProvider=null)
getImportedNamespaces(array $fileContent)
if(!isset($_GET['name'])) $name