#!/usr/bin/env php
<?php declare(strict_types=1);

use PHPStan\Command\AnalyseCommand;
use PHPStan\Command\DumpDependenciesCommand;

gc_disable(); // performance boost

define('__PHPSTAN_RUNNING__', true);

$autoloaderInWorkingDirectory = getcwd() . '/vendor/autoload.php';
if (is_file($autoloaderInWorkingDirectory)) {
	require_once $autoloaderInWorkingDirectory;
}

$autoloadProjectAutoloaderFile = function (string $file): void {
	$path = dirname(__DIR__) . $file;
	if (!extension_loaded('phar')) {
		if (is_file($path)) {
			require_once $path;
		}
	} else {
		$pharPath = \Phar::running(false);
		if ($pharPath === '') {
			if (is_file($path)) {
				require_once $path;
			}
		} else {
			$path = dirname($pharPath) . $file;
        	if (is_file($path)) {
        		require_once $path;
        	}
		}
	}
};

$autoloadProjectAutoloaderFile('/../../autoload.php');

$devOrPharAutoloadFile = __DIR__ . '/../vendor/autoload.php';
if (is_file($devOrPharAutoloadFile)) {
	require_once $devOrPharAutoloadFile;
}

$version = 'Version unknown';
try {
	$version = \Jean85\PrettyVersions::getVersion('phpstan/phpstan')->getPrettyVersion();
} catch (\OutOfBoundsException $e) {

}

$application = new \Symfony\Component\Console\Application(
	'PHPStan - PHP Static Analysis Tool',
	$version
);
$application->add(new AnalyseCommand());
$application->add(new DumpDependenciesCommand());
$application->run();
