PHP 8.3.31
Preview: ReflectsClosures.php Size: 2.52 KB
/home/getspomw/royalsquad.us/vendor/laravel/framework/src/Illuminate/Support/Traits/ReflectsClosures.php

<?php

namespace Illuminate\Support\Traits;

use Closure;
use Illuminate\Support\Collection;
use Illuminate\Support\Reflector;
use ReflectionFunction;
use RuntimeException;

trait ReflectsClosures
{
    /**
     * Get the class name of the first parameter of the given Closure.
     *
     * @param  \Closure  $closure
     * @return string
     *
     * @throws \ReflectionException
     * @throws \RuntimeException
     */
    protected function firstClosureParameterType(Closure $closure)
    {
        $types = array_values($this->closureParameterTypes($closure));

        if (! $types) {
            throw new RuntimeException('The given Closure has no parameters.');
        }

        if ($types[0] === null) {
            throw new RuntimeException('The first parameter of the given Closure is missing a type hint.');
        }

        return $types[0];
    }

    /**
     * Get the class names of the first parameter of the given Closure, including union types.
     *
     * @param  \Closure  $closure
     * @return array
     *
     * @throws \ReflectionException
     * @throws \RuntimeException
     */
    protected function firstClosureParameterTypes(Closure $closure)
    {
        $reflection = new ReflectionFunction($closure);

        $types = (new Collection($reflection->getParameters()))->mapWithKeys(function ($parameter) {
            if ($parameter->isVariadic()) {
                return [$parameter->getName() => null];
            }

            return [$parameter->getName() => Reflector::getParameterClassNames($parameter)];
        })->filter()->values()->all();

        if (empty($types)) {
            throw new RuntimeException('The given Closure has no parameters.');
        }

        if (isset($types[0]) && empty($types[0])) {
            throw new RuntimeException('The first parameter of the given Closure is missing a type hint.');
        }

        return $types[0];
    }

    /**
     * Get the class names / types of the parameters of the given Closure.
     *
     * @param  \Closure  $closure
     * @return array
     *
     * @throws \ReflectionException
     */
    protected function closureParameterTypes(Closure $closure)
    {
        $reflection = new ReflectionFunction($closure);

        return (new Collection($reflection->getParameters()))->mapWithKeys(function ($parameter) {
            if ($parameter->isVariadic()) {
                return [$parameter->getName() => null];
            }

            return [$parameter->getName() => Reflector::getParameterClassName($parameter)];
        })->all();
    }
}

Directory Contents

Dirs: 0 × Files: 7

Name Size Perms Modified Actions
1.40 KB lrw-rw-rw- 2025-09-17 06:53:02
Edit Download
480 B lrw-rw-rw- 2025-09-17 06:53:02
Edit Download
1.83 KB lrw-rw-rw- 2025-09-17 06:53:02
Edit Download
9.65 KB lrw-rw-rw- 2025-09-17 06:53:02
Edit Download
629 B lrw-rw-rw- 2025-09-17 06:53:02
Edit Download
2.52 KB lrw-rw-rw- 2025-09-17 06:53:02
Edit Download
395 B lrw-rw-rw- 2025-09-17 06:53:02
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).