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

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\TokenIterator;

require_once dirname(__DIR__).'/vendor/autoload.php';

$sources = dirname(__DIR__).'/src';
$factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();

$parser = new PHPStan\PhpDocParser\Parser\PhpDocParser(new \PHPStan\PhpDocParser\Parser\TypeParser(), new \PHPStan\PhpDocParser\Parser\ConstExprParser());

$readme = [];

/** @var SplFileInfo $file */
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sources, RecursiveDirectoryIterator::SKIP_DOTS)) as $file) {
    $parts = explode('/', $file->getPath());
    $package = end($parts);

    if ('internal' === $package) {
        continue;
    }

    $function = $file->getBasename('.php');

    if ('bootstrap' === $function || 'Lodash' === $function) {
        continue;
    }

    if (!\is_callable("_\\$function")) {
        continue;
    }

    $reflection = new \ReflectionFunction("_\\$function");

    $docblock = $parser->parse(new TokenIterator((new Lexer())->tokenize($reflection->getDocComment())));

    $category = (string) current($docblock->getTagsByName('@category'))->value;

    $readme[$category][$function] = '';

    $readme[$category][$function] .= "### $function\n\n";

    foreach (array_filter($docblock->children, function (PhpDocChildNode $child): bool {
        return $child instanceof PhpDocTextNode;
    }) as $text) {
        $readme[$category][$function] .= (string) $text.PHP_EOL;
    }

    $readme[$category][$function] .= "**Arguments:**\n\n";

    foreach ($docblock->getTagsByName('@param') as $param) {
        $readme[$category][$function] .= (string) $param.PHP_EOL.PHP_EOL;
    }

    $readme[$category][$function] .= PHP_EOL.PHP_EOL;

    $readme[$category][$function] .= "**Return:**\n\n";

    $readme[$category][$function] .= (string) current($docblock->getTagsByName('@return'));

    $readme[$category][$function] .= PHP_EOL.PHP_EOL;

    if (\preg_match('#<code>((.|\n)*?)</code>#', $readme[$category][$function], $matches)) {

        $readme[$category][$function] = str_replace($matches[0], '', $readme[$category][$function]);

        $example = $matches[1];

        $readme[$category][$function] .= "Example:\n```php\n<?php\n use function _\\$function;\n";
        $readme[$category][$function] .= "$example\n```\n";
    }
}

ksort($readme, SORT_ASC);

$content = <<<README
# Lodash-PHP

Lodash-PHP is a port of the [Lodash JS library](https://lodash.com/) to PHP. It is a set of easy to use utility functions for everyday PHP projects.

Lodash-PHP tries to mimick lodash.js as close as possible

# Installation

Install Lodash-PHP through composer:

```bash
$ composer require lodash-php/lodash-php
```

# Usage

Each method in Lodash-PHP is a separate function that can be imported and used on it's own.

```php
<?php

use function _\\each;

each([1, 2, 3], function (int \$item) {
    var_dump(\$item);
});
```

Lodash-PHP also comes with a global `_` class that can be used globally.

```php
<?php

_::each([1, 2, 3], function (int \$item) {
    var_dump(\$item);
});
```

# Methods

README;

foreach ($readme as $category => $functions) {
    $content .= "- [$category](#".strtolower($category).")\n";
}

$content .= PHP_EOL;

foreach ($readme as $category => $functions) {
    $content .= "## $category\n\n";

    ksort($functions, SORT_ASC);

    foreach ($functions as $function) {
        $content .= $function;
    }
}

file_put_contents(dirname(__DIR__).'/README.md', $content);
