diff --git a/phpstan.neon b/phpstan.neon index 362ba447..ebd4bf30 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,7 +4,7 @@ includes: - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon parameters: - level: 8 + level: 9 paths: - src diff --git a/src/Datasets.php b/src/Datasets.php index c91de5e7..7f8013fe 100644 --- a/src/Datasets.php +++ b/src/Datasets.php @@ -98,7 +98,8 @@ final class Datasets foreach ($datasetCombination as $dataset_data) { $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); @@ -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> $combinations * - * @return array> + * @return array>> */ private static function getDataSetsCombinations(array $combinations): array { @@ -184,6 +186,7 @@ final class Datasets $result = $tmp; } + //@phpstan-ignore-next-line return $result; } diff --git a/src/Exceptions/ExpectationException.php b/src/Exceptions/ExpectationException.php index ca14ce76..be68b40c 100644 --- a/src/Exceptions/ExpectationException.php +++ b/src/Exceptions/ExpectationException.php @@ -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)); + } } diff --git a/src/Expectation.php b/src/Expectation.php index dada9447..5ee04b0b 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -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)); } diff --git a/src/Support/Container.php b/src/Support/Container.php index 57b82f54..1f6b45cd 100644 --- a/src/Support/Container.php +++ b/src/Support/Container.php @@ -37,7 +37,7 @@ final class Container * * @param class-string $id * - * @return object + * @return mixed */ public function get(string $id) { diff --git a/src/Support/HigherOrderMessageCollection.php b/src/Support/HigherOrderMessageCollection.php index cb69a3a9..54bef82a 100644 --- a/src/Support/HigherOrderMessageCollection.php +++ b/src/Support/HigherOrderMessageCollection.php @@ -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; } }