upgrade to phpstan lvl 9

This commit is contained in:
Fabio Ivona
2021-11-18 23:27:37 +01:00
parent 9dd40e4610
commit 7ea6d8a35d
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 - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
parameters: parameters:
level: 8 level: 9
paths: paths:
- src - src

View File

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

View File

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

View File

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

View File

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