#!/usr/bin/env php
<?php
// ? create-project installation : require installation
$vendor_dir = is_dir(dirname(dirname((__FILE__))).'/vendor') ? dirname(dirname((__FILE__))).'/vendor' : dirname(dirname(dirname(dirname(__FILE__))));
require $vendor_dir.'/autoload.php';

use wapmorgan\PhpCodeAnalyzer\PhpCodeAnalyzer;

$version = file_exists(__DIR__.'/version.txt') ? trim(file_get_contents(__DIR__.'/version.txt')) : PhpCodeAnalyzer::VERSION;
$doc = <<<DOC
PhpCodeAnalyzer $version
Usage:
    phpca [-v] [-q] [--output=<path>] [--no-report] [--no-progress] [--since-version=<version>] FILES...
    phpca [-v] [-q] [--output=<path>] --extension=<ext> FILES...
    phpca -h
    phpca --version

Options:
  -h --help                 Show this text
  -v --verbose              Show more debug text
     --version              Show version.
  -q --quiet                Don't print any messages
  --output=<path>           Path where to generate XML report
  --extension=<ext>         Look for usage a specific extension
  --no-report               Turn off summary report
  --no-progress             Turn off progress
  --since-version=<version> Only include extensions not included since version

DOC;

$args = Docopt::handle($doc, ['version' => $version]);

if (isset($args['--verbose']) && $args['--verbose'])
    $_ENV['verbose'] = true;
else
    $_ENV['verbose'] = false;

if (isset($args['--no-progress']) && $args['--no-progress'])
    $_ENV['--no-progress'] = true;
else
    $_ENV['--no-progress'] = false;

$_ENV['quiet'] = (isset($args['--quiet']) && $args['--quiet']);

if (isset($args['--output']))
    $_ENV['output'] = $args['--output'];
else
    $_ENV['output'] = null;

$analyzer = new PhpCodeAnalyzer();

if (isset($args['--since-version']) && $args['--since-version'])
    $analyzer->setSinceVersion($args['--since-version']);

if (isset($args['--extension']))
    $analyzer->loadOneExtensionData($args['--extension']);
else
    $analyzer->loadData();


if (!empty($args['FILES'])) {
    $analyzer->printXmlStart();
    foreach ($args['FILES'] as $file) {
        if (is_dir($file)) {
            $analyzer->analyzeDir($file);
        } else {
            $analyzer->analyzeFile($file);
        }
    }
    if ((!isset($args['--no-report']) || !$args['--no-report']) && !isset($args['--extension'])) {
        $analyzer->printUsedExtensions();
    }
    $analyzer->printXmlEnd();
}
