Returns the migration namespace and class name.
@param string $fileContents Specifies the file contents.
@return array|null Returns an array with keys 'class', 'namespace'.
Returns null if the parsing fails.
777080
public function extractMigrationInfoFromSource($fileContents)
    {
        $stream = new PhpSourceStream($fileContents);

        $result = [];

        while ($stream->forward()) {
            $tokenCode = $stream->getCurrentCode();

            if ($tokenCode == T_NAMESPACE) {
                $namespace = $this->extractNamespace($stream);
                if ($namespace === null) {
                    return null;
                }

                $result['namespace'] = $namespace;
            }

            if ($tokenCode == T_CLASS) {
                $className = $this->extractClassName($stream);
                if ($className === null) {
                    return null;
                }

                $result['class'] = $className;
            }
        }

        if (!$result) {
            return null;
        }

        return $result;
    }
341299
public function findClass($path)
    {
        $namespace = null;
        $tokens = token_get_all(file_get_contents($path));
        foreach ($tokens as $key => $token) {
            if ($this->tokenIsNamespace($token)) {
                $namespace = $this->getNamespace($key + 2, $tokens);
            } elseif ($this->tokenIsClassOrInterface($token)) {
                return ltrim($namespace.'\\'.$this->getClass($key + 2, $tokens), '\\');
            }
        }
    }
418543
public static function createFromInline($fileContents)
    {
        // '//# sourceMappingURL=data:application/json;charset:utf-8;base64,
        // '//# sourceMappingURL=data:application/json;base64,'
        $pos = strrpos($fileContents, "\n//# sourceMappingURL=");
        $isCss = false;
        if ($pos === false) {
            $pos = strrpos($fileContents, "\n/*# sourceMappingURL=");
            if ($pos === false) {
                throw new Exception("No sourceMappingURL found");
            }
            $isCss = true;
        }
        $url = substr($fileContents, $pos + 22);
        $url = rtrim($url);
        if ($isCss) {
            if (substr($url, -2) != '*/') {
                throw new Exception("sourceMappingURL isn't wrapped with closing */");
            }
            $url = substr($url, 0, -2); //remove "*/"
            $url = rtrim($url);
        }

        if (substr($url, 0, 29) == 'data:application/json;base64,') {
            $map = substr($url, 29);
        } else if (substr($url, 0, 29 + 14) == 'data:application/json;charset:utf-8;base64,') {
            $map = substr($url, 29 + 14);
        } else if (substr($url, 0, 29 + 14) == 'data:application/json;charset=utf-8;base64,') {
            $map = substr($url, 29 + 14);
        } else {
            throw new Exception("Unsupported sourceMappingURL");
        }
        $map = base64_decode($map);
        $map = json_decode($map);
        $fileContents = substr($fileContents, 0, $pos);
        $ret = new self($map, $fileContents);
        $ret->setMimeType($isCss ? 'text/css' : 'text/javascript');
        return $ret;
    }
762680
public function prepare($fileContent)
    {
        $replacings = [
            '{name}',
            '{namespace}',
            '{testNamespace}',
            '{table}',
            '{getters}',
            '{interfaceGetters}',
            '{fillable}',
            '{repository}',
            '{directory}',
            '{eloquentTest}',
            '{repositoryTest}',
            '{repositoryTestData}',
        ];

        $replacements = [
            $this->getProvidedName(),
            $this->getAppNamespace(),
            $this->getTestsNamespace(),
            $this->getTable(),
            $this->getSettersGetters($this->fields),
            $this->getSettersGetters($this->fields, true),
            $this->getFillable($this->fields),
            $this->getRepositoryPayloads($this->fields),
            $this->getDirectory(),
            $this->getEloquentTests($this->fields),
            $this->getRepositoryTests($this->fields),
            $this->getRepositoryDataTests($this->fields),
        ];

        return str_replace($replacings, $replacements, $fileContent);
    }
537303
protected function getClassNameWithNamespace(File $phpCsFile): ?string
    {
        try {
            $lastToken = TokenHelper::getLastTokenPointer($phpCsFile);
        } catch (EmptyFileException $e) {
            return null;
        }

        if (!NamespaceHelper::findCurrentNamespaceName($phpCsFile, $lastToken)) {
            return null;
        }

        return ClassHelper::getFullyQualifiedName(
            $phpCsFile,
            $phpCsFile->findPrevious(TokenHelper::$typeKeywordTokenCodes, $lastToken)
        );
    }
90470
public function getNamespace($ns) {
    $namespaces = $this
      ->getNamespaces()
      ->filter(function(NamespaceNode $node) use ($ns) {
        return $node->getName()->getPath() === $ns;
      });

    return $namespaces->isEmpty() ? NULL : $namespaces[0];
  }
415487
public function getAllElementClassContentClassnames()
    {
        $classnames = array_filter(
            $this->getAllClassContentClassnames(),
            function ($classname) {
                return false !== strpos(
                    $classname,
                    AbstractClassContent::CLASSCONTENT_BASE_NAMESPACE.'Element\\'
                );
            }
        );

        $classnames[] = AbstractClassContent::CLASSCONTENT_BASE_NAMESPACE.'ContentSet';

        return $classnames;
    }
1356801
public function getAllClassContentClassnames()
    {
        if (null === $this->contentClassnames) {
            $cacheId = md5('all_classcontents_classnames_'.$this->app->getContext().'_'.$this->app->getEnvironment());
            if (!$this->app->isDebugMode() && false !== $value = $this->cache->load($cacheId)) {
                $this->contentClassnames = json_decode($value, true);
            } else {
                $this->contentClassnames = [AbstractClassContent::CLASSCONTENT_BASE_NAMESPACE . 'ContentSet'];
                foreach ($this->app->getClassContentDir() as $directory) {
                    $this->contentClassnames = array_merge(
                            $this->contentClassnames,
                            CategoryManager::getClassContentClassnamesFromDir($directory)
                    );
                }

                $this->cache->save($cacheId, json_encode($this->contentClassnames));
            }
        }

        return $this->contentClassnames;
    }
603240
protected function getClassName(File $phpCsFile): string
    {
        $namespace = $this->getClassNameWithNamespace($phpCsFile);

        if ($namespace) {
            return trim($namespace, '\\');
        }

        $fileName = $phpCsFile->getFilename();
        $fileNameParts = explode(DIRECTORY_SEPARATOR, $fileName);
        $directoryPosition = array_search('src', array_values($fileNameParts));
        if (!$directoryPosition) {
            $directoryPosition = array_search('tests', array_values($fileNameParts)) + 1;
        }
        $classNameParts = array_slice($fileNameParts, $directoryPosition + 1);
        $className = implode('\\', $classNameParts);
        $className = str_replace('.php', '', $className);

        return $className;
    }
326576
public function getVarDefinitions($fromContents = null) {
		if(is_null($fromContents)) {
			$fromContents = $this->_contents;
		}

		return self::getTemplateVarDefinitions($fromContents);
	}
804437
private function addToImportTable($alias, $functionOrConst, $namespaceNameParts, $contents, & $namespaceImportTable, & $functionImportTable, & $constImportTable):array
    {
        if ($alias !== null) {
            if ($functionOrConst === null) {
                // namespaces are case-insensitive
//                $alias = \strtolower($alias);
                $namespaceImportTable[$alias] = ResolvedName::buildName($namespaceNameParts, $contents);
                return array($namespaceImportTable, $functionImportTable, $constImportTable);
            } elseif ($functionOrConst->kind === TokenKind::FunctionKeyword) {
                // functions are case-insensitive
//                $alias = \strtolower($alias);
                $functionImportTable[$alias] = ResolvedName::buildName($namespaceNameParts, $contents);
                return array($namespaceImportTable, $functionImportTable, $constImportTable);
            } elseif ($functionOrConst->kind === TokenKind::ConstKeyword) {
                // constants are case-sensitive
                $constImportTable[$alias] = ResolvedName::buildName($namespaceNameParts, $contents);
                return array($namespaceImportTable, $functionImportTable, $constImportTable);
            }
            return array($namespaceImportTable, $functionImportTable, $constImportTable);
        }
        return array($namespaceImportTable, $functionImportTable, $constImportTable);
    }
1085740
private function getResourceFilesInSource(string $sourceFileContent): array
  {
    $resource_files = [];
    foreach ($this->getResourcesInfo() as $file_info)
    {
      if (strpos($sourceFileContent, $file_info['path_name_in_sources_with_hash'])!==false)
      {
        $resource_files[] = $file_info;
      }
    }

    return $resource_files;
  }
1007175
private function findFileNamespaces()
    {
        $namespaces = array();

        // namespaces can be only top-level nodes, so we can scan them directly
        foreach ($this->topLevelNodes as $topLevelNode) {
            if ($topLevelNode instanceof Namespace_) {
                $namespaceName = $topLevelNode->name ? $topLevelNode->name->toString() : '';

                $namespaces[$namespaceName] = new ReflectionFileNamespace(
                    $this->fileName,
                    $namespaceName,
                    $topLevelNode
                );
            }
        }

        return $namespaces;
    }
546765
public static function getNamespace($classPath)
    {
        $src = file_get_contents($classPath);

        if (preg_match('/namespace\s+(.+?);/', $src, $m)) {
            return $m[1];
        }

        return null;
    }
1326943
static public function findClass($name, $type=null, $namespaces=null)
	{
	    $name = StringHelper::toClassName($name);
	    if (!$namespaces && strpos($name, '\\') !== false && class_exists($name)) {
	        return $name;
	    }
	    
	    $class_names = [];
	    if (!empty($type)) {
	        $type = StringHelper::toCamelCase($type);
	        if ($pos = strpos($name, '\\')) {
	            $class_names[] = $type . '\\' . $name . $type;
	            $class_names[] = $type . '\\' . $name;
	            
	            $name = substr($name, 0, $pos) . '\\' . $type . '\\' . substr($name, $pos+1);
	        } else {
	            $name = $type . '\\' . $name;
	        }
	        $class_names[] = $name;
	        $class_names[] = $name . $type;
		} else {
		    $class_names[] = $name;
		}
		
		if ($namespaces) {
            $namespaces = (array)static::formatNamespace($namespaces);
		} else {
            //$namespaces = array_unique(array_merge((array)$namespaces, static::getNamespaces()));
		    $namespaces = static::getNamespaces();
		}
		
		foreach ($namespaces as $nss) {
		    foreach ($class_names as $class) { 
		        if (class_exists($nss . $class)) {
		            return $nss . $class;
		        }
		    }
		}
		
		return null;
	}
1035944
private function getMigrationFileContents($class_name)
    {
        $namespace = $this->getNamespace();

        $contents = [
            '<?php',
            '',
        ];

        if ($header_comment = $this->getHeaderComment()) {
            $contents[] = '/*';
            $contents = array_merge($contents, array_map(function ($line) {
                if ($line) {
                    return ' * ' . $line;
                } else {
                    return ' *';
                }
            }, explode("\n", $header_comment)));
            $contents[] = ' */';
            $contents[] = '';
        }

        if ($namespace) {
            $contents[] = 'namespace ' . $namespace . ';';
            $contents[] = '';
        }

        $contents[] = 'use ActiveCollab\DatabaseMigrations\Migration\Migration;';
        $contents[] = '';

        if ($namespace) {
            $contents[] = '/**';
            $contents[] = ' * @package ' . $namespace;
            $contents[] = ' */';
        }

        $contents[] = 'class ' . $class_name . ' extends Migration';
        $contents[] = '{';
        $contents[] = '    /**';
        $contents[] = '     * {@inheritdoc}';
        $contents[] = '     */';
        $contents[] = '    public function up()';
        $contents[] = '    {';
        $contents[] = '    }';
        $contents[] = '}';
        $contents[] = '';

        return implode("\n", $contents);
    }
1310822
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
    {
        if ($this->fq_classlike_name === 'static') {
            return 'static::' . $this->const_name;
        }

        if ($this->fq_classlike_name === $this_class) {
            return 'self::' . $this->const_name;
        }

        if ($namespace && stripos($this->fq_classlike_name, $namespace . '\\') === 0) {
            return preg_replace(
                '/^' . preg_quote($namespace . '\\') . '/i',
                '',
                $this->fq_classlike_name
            ) . '::' . $this->const_name;
        }

        if (!$namespace && stripos($this->fq_classlike_name, '\\') === false) {
            return $this->fq_classlike_name . '::' . $this->const_name;
        }

        if (isset($aliased_classes[strtolower($this->fq_classlike_name)])) {
            return $aliased_classes[strtolower($this->fq_classlike_name)] . '::' . $this->const_name;
        }

        return '\\' . $this->fq_classlike_name . '::' . $this->const_name;
    }