upgrade to phpstan lvl 8

This commit is contained in:
Fabio Ivona
2021-11-18 00:12:39 +01:00
parent f3f58c7f52
commit d4a8a3ec37
6 changed files with 54 additions and 7 deletions

View File

@ -4,7 +4,7 @@ includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
parameters: parameters:
level: 7 level: 8
paths: paths:
- src - src
parallel: parallel:

View File

@ -7,6 +7,7 @@ namespace Pest;
use Closure; use Closure;
use Pest\Exceptions\DatasetAlreadyExist; use Pest\Exceptions\DatasetAlreadyExist;
use Pest\Exceptions\DatasetDoesNotExist; use Pest\Exceptions\DatasetDoesNotExist;
use Pest\Exceptions\ShouldNotHappen;
use SebastianBergmann\Exporter\Exporter; use SebastianBergmann\Exporter\Exporter;
use function sprintf; use function sprintf;
use Traversable; use Traversable;
@ -61,7 +62,13 @@ final class Datasets
{ {
$dataset = self::$withs[$filename . '>>>' . $description]; $dataset = self::$withs[$filename . '>>>' . $description];
return self::resolve($description, $dataset); $dataset = self::resolve($description, $dataset);
if ($dataset === null) {
throw ShouldNotHappen::fromMessage('Could not resolve dataset.');
}
return $dataset;
} }
/** /**

View File

@ -142,6 +142,10 @@ final class TestCaseFactory
} }
$methodsCode = implode('', array_map(static function (TestCaseMethodFactory $method): string { $methodsCode = implode('', array_map(static function (TestCaseMethodFactory $method): string {
if ($method->description === null) {
throw ShouldNotHappen::fromMessage('The test description may not be empty.');
}
$methodName = Str::evaluable($method->description); $methodName = Str::evaluable($method->description);
$datasetsCode = ''; $datasetsCode = '';
@ -224,6 +228,10 @@ EOF;
} }
if (!$method->receivesArguments()) { if (!$method->receivesArguments()) {
if ($method->closure === null) {
throw ShouldNotHappen::fromMessage('The test closure may not be empty.');
}
$arguments = Reflection::getFunctionArguments($method->closure); $arguments = Reflection::getFunctionArguments($method->closure);
if (count($arguments) > 0) { if (count($arguments) > 0) {
@ -240,6 +248,10 @@ EOF;
public function getMethod(string $methodName): TestCaseMethodFactory public function getMethod(string $methodName): TestCaseMethodFactory
{ {
foreach ($this->methods as $method) { foreach ($this->methods as $method) {
if ($method->description === null) {
throw ShouldNotHappen::fromMessage('The test description may not be empty.');
}
if (Str::evaluable($method->description) === $methodName) { if (Str::evaluable($method->description) === $methodName) {
return $method; return $method;
} }

View File

@ -89,7 +89,7 @@ final class TestCaseMethodFactory
$testCase->chains->chain($this); $testCase->chains->chain($this);
$method->chains->chain($this); $method->chains->chain($this);
return call_user_func(Closure::bind($closure, $this, $this::class), ...func_get_args()); return \Pest\Support\Closure::safeBind($closure, $this, $this::class)(...func_get_args());
}; };
} }

View File

@ -22,8 +22,8 @@ final class ChainableClosure
throw ShouldNotHappen::fromMessage('$this not bound to chainable closure.'); throw ShouldNotHappen::fromMessage('$this not bound to chainable closure.');
} }
call_user_func_array(Closure::bind($closure, $this, $this::class), func_get_args()); \Pest\Support\Closure::safeBind($closure, $this, $this::class)(...func_get_args());
call_user_func_array(Closure::bind($next, $this, $this::class), func_get_args()); \Pest\Support\Closure::safeBind($next, $this, $this::class)(...func_get_args());
}; };
} }
@ -33,8 +33,8 @@ final class ChainableClosure
public static function fromStatic(Closure $closure, Closure $next): Closure public static function fromStatic(Closure $closure, Closure $next): Closure
{ {
return static function () use ($closure, $next): void { return static function () use ($closure, $next): void {
call_user_func_array(Closure::bind($closure, null, self::class), func_get_args()); \Pest\Support\Closure::safeBind($closure, null, self::class)(...func_get_args());
call_user_func_array(Closure::bind($next, null, self::class), func_get_args()); \Pest\Support\Closure::safeBind($next, null, self::class)(...func_get_args());
}; };
} }
} }

28
src/Support/Closure.php Normal file
View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
use Pest\Exceptions\ShouldNotHappen;
/**
* @internal
*/
final class Closure
{
public static function safeBind(\Closure|null $closure, ?object $newThis, object|string|null $newScope = 'static'): \Closure
{
if ($closure == null) {
throw ShouldNotHappen::fromMessage('Could not bind null closure.');
}
$closure = \Closure::bind($closure, $newThis, $newScope);
if ($closure == false) {
throw ShouldNotHappen::fromMessage('Could not bind closure.');
}
return $closure;
}
}