<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2019 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Leevel\Console;

use DirectoryIterator;
use InvalidArgumentException;
use Leevel\Console\Argument;
use Leevel\Console\Command;
use Leevel\Filesystem\Fso;
use Leevel\Leevel\Utils\ClassParser;
use Leevel\Leevel\Utils\Doc as UtilsDoc;

/**
 * 解析单元测试用例为 Markdown 文档.
 *
 * @author <NAME> <<EMAIL>>
 *
 * @since 2019.02.28
 *
 * @version 1.0
 * @codeCoverageIgnore
 */
class Doc extends Command
{
    /**
     * 命令名字.
     *
     * @var string
     */
    protected $name = 'make:doc';

    /**
     * 命令行描述.
     *
     * @var string
     */
    protected $description = 'Markdown generation based on test cases.';

    /**
     * 响应命令.
     */
    public function handle(): void
    {
        $this->includeBootstrapFile();

        if (!($files = $this->parseFile())) {
            throw new InvalidArgumentException('Files was not found.');
        }

        $this->utilsDoc = new UtilsDoc($this->outputDir());
        $this->classParser = new ClassParser();

        $succeedCount = 0;

        foreach ($files as $file) {
            if (true === $this->convertMarkdown($file)) {
                $succeedCount++;
            }
        }

        $this->info(sprintf('A total of <comment>%d</comment> files generate succeed.', $succeedCount));
    }

    /**
     * 生成 markdown 文件.
     *
     * @param string $file
     *
     * @return bool
     */
    protected function convertMarkdown(string $file): bool
    {
        $className = $this->classParser->handle($file);

        if (!class_exists($className)) {
            return false;
        }

        $result = $this->utilsDoc->handleAndSave($className);

        if (true === $result) {
            $this->line(sprintf('Class <info>%s</info> was generate succeed.', $className));
        }

        return $result;
    }

    /**
     * 分析测试用例文件.
     *
     * @return array
     */
    protected function parseFile(): array
    {
        $fileOrDir = dirname($this->testsDir()).'/'.$this->path();

        $result = [];

        if (is_file($fileOrDir)) {
            $result[] = $fileOrDir;
        } elseif (is_dir($fileOrDir)) {
            Fso::listDirectory($fileOrDir, true, function (DirectoryIterator $file) use (&$result) {
                if ($file->isFile()) {
                    $result[] = $file->getPathname();
                }
            });
        }

        return $result;
    }

    /**
     * 载入测试用例启动文件.
     */
    protected function includeBootstrapFile(): void<commit_msg>refactor: opimization the doc console<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2019 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Leevel\Console;

use DirectoryIterator;
use InvalidArgumentException;
use Leevel\Console\Argument;
use Leevel\Console\Command;
use Leevel\Filesystem\Fso;
use Leevel\Leevel\Utils\ClassParser;
use Leevel\Leevel\Utils\Doc as UtilsDoc;

/**
 * 解析单元测试用例为 Markdown 文档.
 *
 * @author <NAME> <<EMAIL>>
 *
 * @since 2019.02.28
 *
 * @version 1.0
 * @codeCoverageIgnore
 */
class Doc extends Command
{
    /**
     * 命令名字.
     *
     * @var string
     */
    protected $name = 'make:doc';

    /**
     * 命令行描述.
     *
     * @var string
     */
    protected $description = 'Markdown generation based on test cases.';

    /**
     * 响应命令.
     */
    public function handle(): void
    {
        $this->includeBootstrapFile();

        if (!($files = $this->parseFiles())) {
            throw new InvalidArgumentException('Files was not found.');
        }

        $this->utilsDoc = new UtilsDoc($this->outputDir());
        $this->classParser = new ClassParser();

        $succeedCount = 0;

        foreach ($files as $file) {
            if (true === $this->convertMarkdown($file)) {
                $succeedCount++;
            }
        }

        $this->info(sprintf('A total of <comment>%d</comment> files generate succeed.', $succeedCount));
    }

    /**
     * 生成 markdown 文件.
     *
     * @param string $file
     *
     * @return bool
     */
    protected function convertMarkdown(string $file): bool
    {
        $className = $this->classParser->handle($file);

        if (!class_exists($className)) {
            return false;
        }

        $result = $this->utilsDoc->handleAndSave($className);

        if (true === $result) {
            $this->line(sprintf('Class <info>%s</info> was generate succeed.', $className));
        }

        return $result;
    }

    /**
     * 分析测试用例文件.
     *
     * @return array
     */
    protected function parseFiles(): array
    {
        $fileOrDir = dirname($this->testsDir()).'/'.$this->path();

        $result = [];

        if (is_file($fileOrDir)) {
            $result[] = $fileOrDir;
        } elseif (is_dir($fileOrDir)) {
            Fso::listDirectory($fileOrDir, true, function (DirectoryIterator $file) use (&$result) {
                if ($file->isFile()) {
                    $result[] = $file->getPathname();
                }
            });
        }

        return $result;
    }

    /**
     * 载入测试用例启动文件.
     */
    protected function includeBootstrapFile(): void
{'fraction_non_alphanumeric': 0.1566208619612742, 'fraction_numerical': 0.006246096189881324, 'mean_word_length': 1.8104431768319438, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5088198', 'n_tokens_mistral': 2083, 'n_tokens_neox': 2021, 'n_words': 407}