Allow PHP-8.2 and up Compatibility instead of just PHP-8.4

This commit is contained in:
johnnyq
2026-06-12 17:06:10 -04:00
parent 2204bd52f4
commit d3a93652f3
220 changed files with 7198 additions and 2635 deletions

View File

@@ -2,9 +2,12 @@
namespace Illuminate\Support\Traits;
use Carbon\CarbonInterval;
use Carbon\Unit;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
use stdClass;
@@ -269,7 +272,7 @@ trait InteractsWithData
*/
public function integer($key, $default = 0)
{
return intval($this->data($key, $default));
return (int) $this->data($key, $default);
}
/**
@@ -281,7 +284,21 @@ trait InteractsWithData
*/
public function float($key, $default = 0.0)
{
return floatval($this->data($key, $default));
return (float) $this->data($key, $default);
}
/**
* Retrieve data clamped between min and max values.
*
* @param string $key
* @param int|float $min
* @param int|float $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max, $default = 0)
{
return Number::clamp($this->data($key, $default), $min, $max);
}
/**
@@ -309,15 +326,40 @@ trait InteractsWithData
return Date::createFromFormat($format, $this->data($key), $tz);
}
/**
* Retrieve data from the instance as a CarbonInterval instance.
*
* @param string $key
* @param \Carbon\Unit|string|null $unit
* @return \Carbon\CarbonInterval|null
*/
public function interval($key, $unit = null)
{
if ($this->isNotFilled($key)) {
return null;
}
$value = $this->data($key);
if (is_null($unit)) {
return CarbonInterval::make($value);
}
$unit = $unit instanceof Unit ? $unit : Unit::fromName($unit);
return $unit->interval((float) $value);
}
/**
* Retrieve data from the instance as an enum.
*
* @template TEnum of \BackedEnum
* @template TDefault of TEnum|null
*
* @param string $key
* @param class-string<TEnum> $enumClass
* @param TEnum|null $default
* @return TEnum|null
* @param TDefault $default
* @return TEnum|TDefault
*/
public function enum($key, $enumClass, $default = null)
{
@@ -357,7 +399,7 @@ trait InteractsWithData
*/
protected function isBackedEnum($enumClass)
{
return enum_exists($enumClass) && method_exists($enumClass, 'tryFrom');
return is_a($enumClass, \BackedEnum::class, true);
}
/**

View File

@@ -1,95 +0,0 @@
<?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();
}
}