#!/usr/bin/env php
<?php

/*
 * This file is part of the phpstan-magento package.
 *
 * (c) bitExpert AG
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

// this CLI script will lint all the .neon files in the repository

$path = realpath(__DIR__ . '/../');
$it = new RecursiveDirectoryIterator($path);
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::LEAVES_ONLY);
$it = new RegexIterator($it, '~\.neon$~');

$success = true;
foreach ($it as $file) {
    /** @var SplFileInfo $file */
    if (strpos($file->getRealPath(), '/vendor/') !== false) {
        continue;
    }

    try {
        $neon = Nette\Neon\Neon::decodeFile($file->getRealPath());
        array_walk_recursive($neon, function($value, $key) use($success) {
            if (($key === 'class') && !class_exists($value)) {
                throw new \RuntimeException(sprintf('Class "%s" does not exist', $value));
            }
        });
    } catch (Nette\Neon\Exception $e) {
        $success = false;
        $relPath = str_replace($path . DIRECTORY_SEPARATOR, '', $file->getRealPath());
        echo sprintf('Failed parsing file "%s"', $relPath)."\n";
    } catch (\RuntimeException $e) {
        $success = false;
        echo $e->getMessage()."\n";
    }
}

exit($success ? 0 : 1);
