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

use EdmondsCommerce\PHPQA\Helper;

$files = [
    __DIR__.'/../../../autoload.php',
    __DIR__.'/../vendor/autoload.php',
];

$autoloadFileFound = false;
foreach ($files as $file) {
    if (file_exists($file)) {
        require $file;
        $autoloadFileFound = true;
        break;
    }
}

if (!$autoloadFileFound) {
    echo 'You need to set up the project dependencies using the following commands:'.PHP_EOL.
         'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
         'php composer.phar install'.PHP_EOL;
    die(1);
}

$ignoredRegexPatterns = array_slice($argv, 1);

try {
    $errors = (new \EdmondsCommerce\PHPQA\Psr4Validator(
        $ignoredRegexPatterns,
        Helper::getProjectRootDirectory(),
        Helper::getComposerJsonDecoded()
    ))->main();
    if ([] !== $errors) {
        echo "\nErrors found:\n"
             .\var_export($errors, true);
        throw new \RuntimeException(
            'Errors validating PSR4'
        );
    }
} catch (\Exception $e) {
    echo "\n".$e->getMessage()."\n";
    exit(1);
}
echo "\nNo errors found\n";
