<commit_before><?php

declare(strict_types=1);

namespace BEAR\Sunday\Provide\Router;

use BEAR\Sunday\Annotation\DefaultSchemeHost;
use BEAR\Sunday\Exception\BadRequestJsonException;
use BEAR\Sunday\Extension\Router\RouterInterface;
use BEAR\Sunday\Extension\Router\RouterMatch;
use function is_array;

final class WebRouter implements RouterInterface
{
    /**
     * @var string
     */
    private $schemeHost;

    /**
     * @DefaultSchemeHost
     */
    public function __construct(string $schemeHost)
    {
        $this->schemeHost = $schemeHost;
    }

    /**
     * {@inheritdoc}
     */
    public function match(array $globals, array $server)
    {
        $method = \strtolower($server['REQUEST_METHOD']);
        $match = new RouterMatch;
        $match->method = $method;
        $match->path = $this->schemeHost . \parse_url($server['REQUEST_URI'], PHP_URL_PATH);
        $match->query = ($method === 'get') ? $globals['_GET'] : $this->getUnsafeQuery($method, $globals, $server);

        return $match;
    }

    /**
     * {@inheritdoc}
     */
    public function generate($name, $data)
    {
        return false;
    }

    /**
     * Return request query by media-type
     *
     * @param array{CONTENT_TYPE?: string, HTTP_CONTENT_TYPE?: string, HTTP_RAW_POST_DATA?: string} $server
     * @param array<string, mixed>                                                                  $globals
     *
     * @return array<string, mixed> $globals
     */
    private function getUnsafeQuery(string $method, array $globals, array $server) : array
    {
        if ($method === 'post' && is_array($globals['_POST'])) {
            return $globals['_POST'];
        }
        $contentType = $server['CONTENT_TYPE'] ?? ($server['HTTP_CONTENT_TYPE']) ?? '';
        $isFormUrlEncoded = strpos($contentType, 'application/x-www-form-urlencoded') !== false;
        $rawBody = $server['HTTP_RAW_POST_DATA'] ?? rtrim((string) file_get_contents('php://input'));
        if ($isFormUrlEncoded) {
            \parse_str(rtrim($rawBody), $put);

            return $put;
        }
        $isApplicationJson = strpos($contentType, 'application/json') !== false;
        if (! $isApplicationJson) {
            return [];
        }
        /** @var array<string, mixed> $content */
        $content = json_decode($rawBody, true);
        $error = json_last_error();
        if ($error !== JSON_ERROR_NONE) {
            throw new BadRequestJsonException(json_last_error_msg());
        }

        return $content;
    }
}
<commit_msg>fix MixedReturnTypeCoercion<commit_after><?php

declare(strict_types=1);

namespace BEAR\Sunday\Provide\Router;

use BEAR\Sunday\Annotation\DefaultSchemeHost;
use BEAR\Sunday\Exception\BadRequestJsonException;
use BEAR\Sunday\Extension\Router\RouterInterface;
use BEAR\Sunday\Extension\Router\RouterMatch;
use function is_array;

final class WebRouter implements RouterInterface
{
    /**
     * @var string
     */
    private $schemeHost;

    /**
     * @DefaultSchemeHost
     */
    public function __construct(string $schemeHost)
    {
        $this->schemeHost = $schemeHost;
    }

    /**
     * {@inheritdoc}
     */
    public function match(array $globals, array $server)
    {
        $method = \strtolower($server['REQUEST_METHOD']);
        $match = new RouterMatch;
        $match->method = $method;
        $match->path = $this->schemeHost . \parse_url($server['REQUEST_URI'], PHP_URL_PATH);
        $match->query = ($method === 'get') ? $globals['_GET'] : $this->getUnsafeQuery($method, $globals, $server);

        return $match;
    }

    /**
     * {@inheritdoc}
     */
    public function generate($name, $data)
    {
        return false;
    }

    /**
     * Return request query by media-type
     *
     * @param array{CONTENT_TYPE?: string, HTTP_CONTENT_TYPE?: string, HTTP_RAW_POST_DATA?: string} $server
     * @param array<string, mixed>                                                                  $globals
     *
     * @return array<string, mixed> $globals
     */
    private function getUnsafeQuery(string $method, array $globals, array $server) : array
    {
        if ($method === 'post' && is_array($globals['_POST'])) {
            /** @var array<string, mixed> $globals['_POST'] */
            return $globals['_POST'];
        }
        $contentType = $server['CONTENT_TYPE'] ?? ($server['HTTP_CONTENT_TYPE']) ?? '';
        $isFormUrlEncoded = strpos($contentType, 'application/x-www-form-urlencoded') !== false;
        $rawBody = $server['HTTP_RAW_POST_DATA'] ?? rtrim((string) file_get_contents('php://input'));
        if ($isFormUrlEncoded) {
            \parse_str(rtrim($rawBody), $put);

            /** @var array<string, mixed> $put */
            return $put;
        }
        $isApplicationJson = strpos($contentType, 'application/json') !== false;
        if (! $isApplicationJson) {
            return [];
        }
        /** @var array<string, mixed> $content */
        $content = json_decode($rawBody, true);
        $error = json_last_error();
        if ($error !== JSON_ERROR_NONE) {
            throw new BadRequestJsonException(json_last_error_msg());
        }

        return $content;
    }
}

{'fraction_non_alphanumeric': 0.14496644295302014, 'fraction_numerical': 0.0003835091083413231, 'mean_word_length': 2.4634794156706508, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2248098', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1527, 'n_words': 293}