Skip to content

result‐002

Akihito Koriyama edited this page Jul 8, 2024 · 1 revision

Commit

https://github.com/koriym/Koriym.CodeGlass/tree/0eb7399a84dc3d07eac44c6d406042d53fa584fe

Model

FROM deepseek-coder-v2:16b

PARAMETER temperature 0.2
PARAMETER top_p 0.95

SYSTEM """
You are an AI assistant specialized in analyzing source code and generating insightful comments. Your task is to enhance code readability and understanding by providing brief, clear explanations. Focus on:
1. Summarizing the main purpose of the code.
2. Identifying key algorithms or design patterns used.
3. Highlighting potential issues or areas for improvement.
4. Explaining complex or non-obvious parts of the code.
Be precise, technical, and keep your comments concise.
5. Use Japanese for all sentences.
"""

From

<?php

declare(strict_types=1);

namespace Koriym\EnvJson;

use stdClass;

use function array_keys;
use function assert;
use function getenv;
use function in_array;

final class Env
{
    public function __invoke(object $schema): stdClass
    {
        assert(isset($schema->properties));
        $schemaKeys = array_keys((array) $schema->properties);
        $data = new stdClass();
        $env = getenv();
        foreach ($env as $key => $val) {
            if (in_array($key, $schemaKeys)) {
                $data->{$key} = $val;
            }
        }

        return $data;
    }
}

To

Here is the commented code:

<?php

declare(strict_types=1);

namespace Koriym\EnvJson;

use JsonSchema\Validator;
use Koriym\EnvJson\Exception\EnvJsonFileNotFoundException;
use Koriym\EnvJson\Exception\SchemaFileNotFoundException;
use stdClass;

use function dirname;
use function file_exists;
use function file_get_contents;
use function is_callable;
use function json_decode;
use function putenv;
use function realpath;
use function set_error_handler;
use function sprintf;
use function str_contains;
use function str_replace;

use const E_DEPRECATED;
use const JSON_THROW_ON_ERROR;
use const PHP_VERSION_ID;

final class EnvJson
{
    /** @var Env */
    private $envFactory;

    public function __construct()
    {
        $this->envFactory = new Env();
    }

    // env.jsonファイルを読み込んで環境変数に設定するメソッド
    public function load(string $dir, string $json = 'env.json'): void
    {
        // PHP 8.1以上の非推奨エラーを抑制するためのハンドラを設定
        $handler = $this->suppressPhp81DeprecatedError();
        
        // env.jsonに対応するスキーマファイルを取得
        $shcema = $this->getSchema($dir, $json);
        
        // 環境変数が有効かどうかを検証
        if ($this->isValidEnv($shcema, new Validator())) {
            goto validated;
        }

        // env.jsonファイルが存在しない場合、distファイルを試す
        $env = $this->getEnv($dir, $json);
        $this->putEnv($env);
        
        // 環境変数の検証
        $validator = new Validator();
        if ($this->isValidEnv($shcema, $validator)) {
            goto validated;
        }

        // エラーがある場合、エラーハンドリングを行う
        (new ThrowError())($validator);

        // 検証済みのラベル
        validated:
        
        // PHP 8.1以上で非推奨エラー抑制用のハンドラが設定されている場合、適用
        if (is_callable($handler)) {
            set_error_handler($handler);
        }
    }

    // PHP 8.1以上で非推奨エラーを抑制するためのハンドラを返すメソッド
    private function suppressPhp81DeprecatedError(): ?callable
    {
        if (PHP_VERSION_ID >= 80100) {
            return set_error_handler(static function (int $errno, string $errstr, string $errfile) {
                unset($errstr);

                return $errno === E_DEPRECATED && str_contains($errfile, dirname(__DIR__) . '/vendor');
            });
        }

        return null;
    }

    // env.jsonファイルを読み込んで連想配列として返すメソッド
    private function getEnv(string $dir, string $jsonName): array
    {
        $envJsonFile = realpath(sprintf('%s/%s
    // 省略

Clone this wiki locally