<commit_before><?php

namespace Psalm\Internal\Provider;

use PhpParser;
use Psalm\Context;
use Psalm\CodeLocation;
use Psalm\Type;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use \Psalm\Plugin\Hook\FunctionReturnTypeProviderInterface;

class FunctionReturnTypeProvider
{
    /**
     * @var array<
     *   string,
     *   \Closure(
     *     StatementsAnalyzer,
     *     string,
     *     array<PhpParser\Node\Arg>,
     *     Context,
     *     CodeLocation
     *   ) : Type\Union
     * >
     */
    private static $handlers = [];

    public function __construct()
    {
        self::$handlers = [];

        $this->register(ReturnTypeProvider\ArrayColumnReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayFilterReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayMapReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayMergeReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayPointerAdjustmentReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayPopReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayRandReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayReduceReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArraySliceReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\FilterVarReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\IteratorToArrayReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ParseUrlReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\RangeReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\StrReplaceReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\VersionCompareReturnTypeProvider::class);
    }

    /**
     * @param  class-string<FunctionReturnTypeProviderInterface> $class
     * @return void
     */
    public function register(string $class)
    {
        $callable = \Closure::fromCallable([$class, 'get']);

        foreach ($class::getFunctionIds() as $function_id) {
            self::$handlers[$function_id] = $callable;
        }
    }

    public function has(string $function_id) : bool
    {
        return isset(self::$handlers[strtolower($function_id)]);
    }

    /**
     * @param  array<PhpParser\Node\Arg>  $call_args
     */
    public function getReturnType(
        StatementsAnalyzer $statements_analyzer,
        string $function_id,
        array $call_args,
        Context $context,
        CodeLocation $code_location
    ) : Type\Union {<commit_msg>Add PHP 7.0-compatible closure creation<commit_after><?php

namespace Psalm\Internal\Provider;

use PhpParser;
use Psalm\Context;
use Psalm\CodeLocation;
use Psalm\Type;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use \Psalm\Plugin\Hook\FunctionReturnTypeProviderInterface;

class FunctionReturnTypeProvider
{
    /**
     * @var array<
     *   string,
     *   \Closure(
     *     StatementsAnalyzer,
     *     string,
     *     array<PhpParser\Node\Arg>,
     *     Context,
     *     CodeLocation
     *   ) : Type\Union
     * >
     */
    private static $handlers = [];

    public function __construct()
    {
        self::$handlers = [];

        $this->register(ReturnTypeProvider\ArrayColumnReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayFilterReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayMapReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayMergeReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayPointerAdjustmentReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayPopReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayRandReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArrayReduceReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ArraySliceReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\FilterVarReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\IteratorToArrayReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\ParseUrlReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\RangeReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\StrReplaceReturnTypeProvider::class);
        $this->register(ReturnTypeProvider\VersionCompareReturnTypeProvider::class);
    }

    /**
     * @param  class-string<FunctionReturnTypeProviderInterface> $class
     * @return void
     */
    public function register(string $class)
    {
        if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
            $callable = \Closure::fromCallable([$class, 'get']);
        } else {
            $callable = (new \ReflectionClass($class))->getMethod('get')->getClosure(new $class);
        }

        foreach ($class::getFunctionIds() as $function_id) {
            self::$handlers[$function_id] = $callable;
        }
    }

    public function has(string $function_id) : bool
    {
        return isset(self::$handlers[strtolower($function_id)]);
    }

    /**
     * @param  array<PhpParser\Node\Arg>  $call_args
     */
    public function getReturnType(
        StatementsAnalyzer $statements_analyzer,
        string $function_id,
        array $call_args,
        Context $context,
        CodeLocation $code_location
    ) : Type\Union {
{'fraction_non_alphanumeric': 0.11682157827592492, 'fraction_numerical': 0.0010934937124111536, 'mean_word_length': 3.4873262469337694, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '3433210', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1503, 'n_words': 195}