refactor: PHP 8 features

This commit is contained in:
Nuno Maduro
2021-10-24 18:29:59 +01:00
parent e8c2fe6e35
commit 2b687a7269
43 changed files with 283 additions and 635 deletions

View File

@ -15,9 +15,8 @@ final class Arr
{
/**
* @param array<mixed> $array
* @param string|int $key
*/
public static function has(array $array, $key): bool
public static function has(array $array, string|int $key): bool
{
$key = (string) $key;
@ -38,12 +37,11 @@ final class Arr
/**
* @param array<mixed> $array
* @param string|int $key
* @param null $default
*
* @return array|mixed|null
*/
public static function get(array $array, $key, $default = null)
public static function get(array $array, string|int $key, $default = null)
{
$key = (string) $key;
@ -51,7 +49,7 @@ final class Arr
return $array[$key];
}
if (strpos($key, '.') === false) {
if (!str_contains($key, '.')) {
return $array[$key] ?? $default;
}

View File

@ -26,7 +26,6 @@ final class Backtrace
$current = null;
foreach (debug_backtrace(self::BACKTRACE_OPTIONS) as $trace) {
if (Str::endsWith($trace[self::FILE], 'overrides/Runner/TestSuiteLoader.php')) {
break;
}

View File

@ -18,9 +18,9 @@ final class ChainableClosure
{
return function () use ($closure, $next): void {
/* @phpstan-ignore-next-line */
call_user_func_array(Closure::bind($closure, $this, get_class($this)), func_get_args());
call_user_func_array(Closure::bind($closure, $this, $this::class), func_get_args());
/* @phpstan-ignore-next-line */
call_user_func_array(Closure::bind($next, $this, get_class($this)), func_get_args());
call_user_func_array(Closure::bind($next, $this, $this::class), func_get_args());
};
}

View File

@ -13,15 +13,12 @@ use ReflectionParameter;
*/
final class Container
{
/**
* @var self
*/
private static $instance;
private static ?Container $instance = null;
/**
* @var array<string, mixed>
*/
private $instances = [];
private array $instances = [];
/**
* Gets a new or already existing container.

View File

@ -162,7 +162,7 @@ final class Coverage
$lastKey = count($array) - 1;
if (array_key_exists($lastKey, $array) && strpos($array[$lastKey], '..') !== false) {
if (array_key_exists($lastKey, $array) && str_contains($array[$lastKey], '..')) {
[$from] = explode('..', $array[$lastKey]);
$array[$lastKey] = $line > $from ? sprintf('%s..%s', $from, $line) : sprintf('%s..%s', $line, $from);

View File

@ -8,19 +8,13 @@ use Closure;
final class Extendable
{
/**
* The extendable class.
*
* @var string
*/
private $extendableClass;
/**
* Creates a new extendable instance.
*/
public function __construct(string $extendableClass)
{
$this->extendableClass = $extendableClass;
public function __construct(
private string $extendableClass
) {
// ..
}
/**

View File

@ -6,8 +6,6 @@ namespace Pest\Support;
use Closure;
use Pest\Expectation;
use Pest\PendingObjects\TestCall;
use PHPUnit\Framework\TestCase;
/**
* @internal
@ -15,13 +13,11 @@ use PHPUnit\Framework\TestCase;
final class HigherOrderCallables
{
/**
* @var object
* Creates a new Higher Order Callables instances.
*/
private $target;
public function __construct(object $target)
public function __construct(private object $target)
{
$this->target = $target;
// ..
}
/**
@ -33,7 +29,7 @@ final class HigherOrderCallables
*
* @return Expectation<TValue>
*/
public function expect($value)
public function expect(mixed $value): Expectation
{
return new Expectation($value instanceof Closure ? Reflection::bindCallableWithData($value) : $value);
}
@ -47,17 +43,15 @@ final class HigherOrderCallables
*
* @return Expectation<TValue>
*/
public function and($value)
public function and(mixed $value)
{
return $this->expect($value);
}
/**
* Tap into the test case to perform an action and return the test case.
*
* @return TestCall|TestCase|object
*/
public function tap(callable $callable)
public function tap(callable $callable): object
{
Reflection::bindCallableWithData($callable);

View File

@ -15,68 +15,31 @@ final class HigherOrderMessage
{
public const UNDEFINED_METHOD = 'Method %s does not exist';
/**
* The filename where the function was originally called.
*
* @readonly
*
* @var string
*/
public $filename;
/**
* The line where the function was originally called.
*
* @readonly
*
* @var int
*/
public $line;
/**
* The method or property name to access.
*
* @readonly
*
* @var string
*/
public $name;
/**
* The arguments.
*
* @var array<int, mixed>|null
*
* @readonly
*/
public $arguments;
/**
* An optional condition that will determine if the message will be executed.
*
* @var callable(): bool|null
* @var (callable(): bool)|null
*/
public $condition = null;
public $condition;
/**
* Creates a new higher order message.
*
* @param array<int, mixed>|null $arguments
*/
public function __construct(string $filename, int $line, string $methodName, $arguments)
{
$this->filename = $filename;
$this->line = $line;
$this->name = $methodName;
$this->arguments = $arguments;
public function __construct(
public string $filename,
public int $line,
public string $name,
public ?array $arguments
) {
// ..
}
/**
* Re-throws the given `$throwable` with the good line and filename.
*
* @return mixed
*/
public function call(object $target)
public function call(object $target): mixed
{
/* @phpstan-ignore-next-line */
if (is_callable($this->condition) && call_user_func(Closure::bind($this->condition, $target)) === false) {
@ -91,7 +54,8 @@ final class HigherOrderMessage
try {
return is_array($this->arguments)
? Reflection::call($target, $this->name, $this->arguments)
: $target->{$this->name}; /* @phpstan-ignore-line */
: $target->{$this->name};
/* @phpstan-ignore-line */
} catch (Throwable $throwable) {
Reflection::setPropertyValue($throwable, 'file', $this->filename);
Reflection::setPropertyValue($throwable, 'line', $this->line);
@ -122,10 +86,8 @@ final class HigherOrderMessage
/**
* Determines whether or not there exists a higher order callable with the message name.
*
* @return bool
*/
private function hasHigherOrderCallable()
private function hasHigherOrderCallable(): bool
{
return in_array($this->name, get_class_methods(HigherOrderCallables::class), true);
}
@ -133,7 +95,7 @@ final class HigherOrderMessage
private static function getUndefinedMethodMessage(object $target, string $methodName): string
{
if (\PHP_MAJOR_VERSION >= 8) {
return sprintf(sprintf(self::UNDEFINED_METHOD, sprintf('%s::%s()', get_class($target), $methodName)));
return sprintf(sprintf(self::UNDEFINED_METHOD, sprintf('%s::%s()', $target::class, $methodName)));
}
return sprintf(self::UNDEFINED_METHOD, $methodName);

View File

@ -12,7 +12,7 @@ final class HigherOrderMessageCollection
/**
* @var array<int, HigherOrderMessage>
*/
private $messages = [];
private array $messages = [];
/**
* Adds a new higher order message to the collection.
@ -63,9 +63,7 @@ final class HigherOrderMessageCollection
{
return array_reduce(
$this->messages,
static function (int $total, HigherOrderMessage $message) use ($name): int {
return $total + (int) ($name === $message->name);
},
static fn (int $total, HigherOrderMessage $message): int => $total + (int) ($name === $message->name),
0,
);
}

View File

@ -15,19 +15,13 @@ final class HigherOrderTapProxy
{
private const UNDEFINED_PROPERTY = 'Undefined property: P\\';
/**
* The target being tapped.
*
* @var TestCase
*/
public $target;
/**
* Create a new tap proxy instance.
*/
public function __construct(TestCase $target)
{
$this->target = $target;
public function __construct(
public TestCase $target
) {
// ..
}
/**

View File

@ -193,9 +193,7 @@ final class Reflection
}
$arguments[$parameter->getName()] = implode('|', array_map(
static function (ReflectionNamedType $type): string {
return $type->getName();
},
static fn (ReflectionNamedType $type): string => $type->getName(),
($types instanceof ReflectionNamedType)
? [$types] // NOTE: normalize as list of to handle unions
: $types->getTypes(),

View File

@ -33,7 +33,7 @@ final class Str
*/
public static function startsWith(string $target, string $search): bool
{
return substr($target, 0, strlen($search)) === $search;
return str_starts_with($target, $search);
}
/**