upgrade to phpstan lvl 9

This commit is contained in:
Fabio Ivona
2021-11-18 23:27:37 +01:00
parent 9258dcc988
commit 0b5cea6df1
6 changed files with 70 additions and 11 deletions

View File

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

View File

@ -98,6 +98,7 @@ final class Datasets
foreach ($datasetCombination as $dataset_data) {
$partialDescriptions[] = $dataset_data['label'];
//@phpstan-ignore-next-line
$values = array_merge($values, $dataset_data['values']);
}
@ -152,10 +153,11 @@ final class Datasets
$datasets[$index] = iterator_to_array($datasets[$index]);
}
//@phpstan-ignore-next-line
foreach ($datasets[$index] as $key => $values) {
$values = is_array($values) ? $values : [$values];
$processedDataset[] = [
'label' => self::getDataSetDescription($key, $values),
'label' => self::getDataSetDescription($key, $values), //@phpstan-ignore-line
'values' => $values,
];
}
@ -169,7 +171,7 @@ final class Datasets
/**
* @param array<array<mixed>> $combinations
*
* @return array<array<mixed>>
* @return array<array<array<mixed>>>
*/
private static function getDataSetsCombinations(array $combinations): array
{
@ -184,6 +186,7 @@ final class Datasets
$result = $tmp;
}
//@phpstan-ignore-next-line
return $result;
}

View File

@ -9,8 +9,13 @@ namespace Pest\Exceptions;
*/
final class ExpectationException extends \Exception
{
public static function invalidValue(string $expectationName, string $valueRequired): ExpectationException
public static function invalidCurrentValueType(string $expectationName, string $valueRequired): ExpectationException
{
return new ExpectationException(sprintf('%s expectation requires a %s value.', $expectationName, $valueRequired));
}
public static function invalidExpectedValueType(string $expectationName, string $valueRequired): ExpectationException
{
return new ExpectationException(sprintf('%s expectation requires a %s as expected value.', $expectationName, $valueRequired));
}
}

View File

@ -70,7 +70,7 @@ final class Expectation
public function json(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidValue('json', 'string');
throw ExpectationException::invalidCurrentValueType('json', 'string');
}
return $this->toBeJson()->and(json_decode($this->value, true));
@ -360,8 +360,14 @@ final class Expectation
{
foreach ($needles as $needle) {
if (is_string($this->value)) {
if (!is_string($needle)) {
throw ExpectationException::invalidExpectedValueType('toContain', 'string');
}
Assert::assertStringContainsString($needle, $this->value);
} else {
if (!is_iterable($this->value)) {
throw ExpectationException::invalidCurrentValueType('toContain', 'iterable');
}
Assert::assertContains($needle, $this->value);
}
}
@ -376,6 +382,10 @@ final class Expectation
*/
public function toStartWith(string $expected): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toStartWith', 'string');
}
Assert::assertStringStartsWith($expected, $this->value);
return $this;
@ -388,6 +398,10 @@ final class Expectation
*/
public function toEndWith(string $expected): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toEndWith', 'string');
}
Assert::assertStringEndsWith($expected, $this->value);
return $this;
@ -428,6 +442,10 @@ final class Expectation
*/
public function toHaveCount(int $count): Expectation
{
if (!is_countable($this->value) && !is_iterable($this->value)) {
throw ExpectationException::invalidCurrentValueType('toHaveCount', 'string');
}
Assert::assertCount($count, $this->value);
return $this;
@ -440,6 +458,7 @@ final class Expectation
{
$this->toBeObject();
//@phpstan-ignore-next-line
Assert::assertTrue(property_exists($this->value, $name));
if (func_num_args() > 1) {
@ -651,6 +670,8 @@ final class Expectation
public function toBeJson(): Expectation
{
Assert::assertIsString($this->value);
//@phpstan-ignore-next-line
Assert::assertJson($this->value);
return $this;
@ -721,6 +742,10 @@ final class Expectation
*/
public function toBeDirectory(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeDirectory', 'string');
}
Assert::assertDirectoryExists($this->value);
return $this;
@ -731,6 +756,10 @@ final class Expectation
*/
public function toBeReadableDirectory(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeReadableDirectory', 'string');
}
Assert::assertDirectoryIsReadable($this->value);
return $this;
@ -741,6 +770,10 @@ final class Expectation
*/
public function toBeWritableDirectory(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeWritableDirectory', 'string');
}
Assert::assertDirectoryIsWritable($this->value);
return $this;
@ -751,6 +784,10 @@ final class Expectation
*/
public function toBeFile(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeFile', 'string');
}
Assert::assertFileExists($this->value);
return $this;
@ -761,6 +798,9 @@ final class Expectation
*/
public function toBeReadableFile(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeReadableFile', 'string');
}
Assert::assertFileIsReadable($this->value);
return $this;
@ -771,6 +811,9 @@ final class Expectation
*/
public function toBeWritableFile(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeWritableFile', 'string');
}
Assert::assertFileIsWritable($this->value);
return $this;
@ -815,6 +858,9 @@ final class Expectation
public function toMatchObject(iterable|object $object): Expectation
{
foreach ((array) $object as $property => $value) {
if (!is_object($this->value) && !is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toMatchObject', 'object|string');
}
Assert::assertTrue(property_exists($this->value, $property));
/* @phpstan-ignore-next-line */
@ -838,6 +884,9 @@ final class Expectation
*/
public function toMatch(string $expression): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toMatch', 'string');
}
Assert::assertMatchesRegularExpression($expression, $this->value);
return $this;
@ -897,10 +946,10 @@ final class Expectation
}
if (!class_exists($exception)) {
throw new ExpectationFailedException("Exception with message \"{$exception}\" not thrown.");
throw new ExpectationFailedException("Exception with message \"$exception\" not thrown.");
}
throw new ExpectationFailedException("Exception \"{$exception}\" not thrown.");
throw new ExpectationFailedException("Exception \"$exception\" not thrown.");
}
/**
@ -925,7 +974,7 @@ final class Expectation
*/
public function __call(string $method, array $parameters)
{
if (!static::hasExtend($method)) {
if (!Expectation::hasExtend($method)) {
/* @phpstan-ignore-next-line */
return new HigherOrderExpectation($this, $this->value->$method(...$parameters));
}
@ -939,7 +988,8 @@ final class Expectation
*/
public function __get(string $name): Expectation|OppositeExpectation|Each|HigherOrderExpectation
{
if (!method_exists($this, $name) && !static::hasExtend($name)) {
if (!method_exists($this, $name) && !Expectation::hasExtend($name)) {
//@phpstan-ignore-next-line
return new HigherOrderExpectation($this, $this->retrieve($name, $this->value));
}

View File

@ -37,7 +37,7 @@ final class Container
*
* @param class-string $id
*
* @return object
* @return mixed
*/
public function get(string $id)
{

View File

@ -40,6 +40,7 @@ final class HigherOrderMessageCollection
public function chain(object $target): void
{
foreach ($this->messages as $message) {
//@phpstan-ignore-next-line
$target = $message->call($target) ?? $target;
}
}