Code quality improvements

This commit is contained in:
Nuno Maduro
2022-09-16 11:27:17 +01:00
parent e9564febaf
commit 45011ebd14
42 changed files with 266 additions and 278 deletions

View File

@ -72,7 +72,7 @@ final class Arr
$results = [];
foreach ($array as $key => $value) {
if (is_array($value) && count($value) > 0) {
if (is_array($value) && $value !== []) {
$results = array_merge($results, static::dot($value, $prepend.$key.'.'));
} else {
$results[$prepend.$value] = $value;

View File

@ -15,7 +15,6 @@ final class Closure
/**
* Binds the given closure to the given "this".
*
* @return BaseClosure|never
*
* @throws ShouldNotHappen
*/

View File

@ -26,7 +26,7 @@ final class Container
public static function getInstance(): self
{
if (static::$instance === null) {
static::$instance = new static();
static::$instance = new self();
}
return static::$instance;
@ -49,10 +49,8 @@ final class Container
/**
* Adds the given instance to the container.
*
* @param mixed $instance
*/
public function add(string $id, $instance): void
public function add(string $id, mixed $instance): void
{
$this->instances[$id] = $instance;
}

View File

@ -153,7 +153,7 @@ final class Coverage
$shouldBeNewLine = true;
$eachLine = function (array $array, array $tests, int $line) use (&$shouldBeNewLine): array {
if (count($tests) > 0) {
if ($tests !== []) {
$shouldBeNewLine = true;
return $array;
@ -168,8 +168,8 @@ final class Coverage
$lastKey = count($array) - 1;
if (array_key_exists($lastKey, $array) && str_contains($array[$lastKey], '..')) {
[$from] = explode('..', $array[$lastKey]);
if (array_key_exists($lastKey, $array) && str_contains((string) $array[$lastKey], '..')) {
[$from] = explode('..', (string) $array[$lastKey]);
$array[$lastKey] = $line > $from ? sprintf('%s..%s', $from, $line) : sprintf('%s..%s', $line, $from);
return $array;

View File

@ -25,17 +25,12 @@ final class ExpectationPipeline
*/
private array $passables;
/**
* The expectation closure.
*/
private Closure $closure;
/**
* Creates a new instance of Expectation Pipeline.
*/
public function __construct(Closure $closure)
{
$this->closure = $closure;
public function __construct(
private readonly Closure $closure
) {
}
/**
@ -89,10 +84,6 @@ final class ExpectationPipeline
*/
public function carry(): Closure
{
return function ($stack, $pipe): Closure {
return function () use ($stack, $pipe) {
return $pipe($stack, ...$this->passables);
};
};
return fn ($stack, $pipe): Closure => fn () => $pipe($stack, ...$this->passables);
}
}

View File

@ -15,7 +15,7 @@ final class HigherOrderCallables
/**
* Creates a new Higher Order Callables instances.
*/
public function __construct(private object $target)
public function __construct(private readonly object $target)
{
// ..
}
@ -44,7 +44,7 @@ final class HigherOrderCallables
* @param callable|TValue $value
* @return Expectation<(callable(): mixed)|TValue>
*/
public function and(mixed $value)
public function and(mixed $value): Expectation
{
return $this->expect($value);
}

View File

@ -25,7 +25,7 @@ final class HigherOrderMessage
/**
* Creates a new higher order message.
*
* @param array<int, mixed> $arguments
* @param array<int, mixed>|null $arguments
*/
public function __construct(
public string $filename,
@ -97,7 +97,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()', $target::class, $methodName)));
return sprintf(self::UNDEFINED_METHOD, sprintf('%s::%s()', $target::class, $methodName));
}
return sprintf(self::UNDEFINED_METHOD, $methodName);

View File

@ -26,10 +26,8 @@ final class HigherOrderTapProxy
/**
* Dynamically sets properties on the target.
*
* @param mixed $value
*/
public function __set(string $property, $value): void
public function __set(string $property, mixed $value): void
{
$this->target->{$property} = $value; // @phpstan-ignore-line
}

View File

@ -9,10 +9,10 @@ use PHPUnit\Util\Filesystem;
abstract class Printer implements \PHPUnit\Util\Printer
{
/** @var resource|false */
/** @var resource|bool */
private $stream;
private bool $isPhpStream;
private readonly bool $isPhpStream;
private bool $isOpen;
@ -46,12 +46,14 @@ abstract class Printer implements \PHPUnit\Util\Printer
assert($this->isOpen);
assert($this->stream !== false);
// @phpstan-ignore-next-line
fwrite($this->stream, $buffer);
}
final public function flush(): void
{
if ($this->isOpen && $this->isPhpStream && $this->stream !== false) {
// @phpstan-ignore-next-line
fclose($this->stream);
$this->isOpen = false;

View File

@ -69,9 +69,9 @@ final class Reflection
{
$test = TestSuite::getInstance()->test;
return $test === null
? static::bindCallable($callable)
: Closure::fromCallable($callable)->bindTo($test)(...$test->providedData());
return $test instanceof \PHPUnit\Framework\TestCase
? Closure::fromCallable($callable)->bindTo($test)(...$test->providedData())
: static::bindCallable($callable);
}
/**
@ -119,9 +119,8 @@ final class Reflection
* @template TValue of object
*
* @param TValue $object
* @param mixed $value
*/
public static function setPropertyValue(object $object, string $property, $value): void
public static function setPropertyValue(object $object, string $property, mixed $value): void
{
/** @var ReflectionClass<TValue> $reflectionClass */
$reflectionClass = new ReflectionClass($object);
@ -153,8 +152,10 @@ final class Reflection
public static function getParameterClassName(ReflectionParameter $parameter): ?string
{
$type = $parameter->getType();
if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) {
if (! $type instanceof ReflectionNamedType) {
return null;
}
if ($type->isBuiltin()) {
return null;
}