From 94585789dc24960c7f26d12889180ccaaf229cee Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 18 Nov 2021 23:39:37 +0000 Subject: [PATCH] refacto(phpstan-to-8): few adjustments --- phpstan.neon | 2 +- src/Bootstrappers/BootSubscribers.php | 4 +- src/Datasets.php | 49 ++++++++++++---------- src/Exceptions/ExpectationException.php | 21 ---------- src/Exceptions/InvalidExpectationValue.php | 23 ++++++++++ src/Expectation.php | 36 ++++++++-------- src/Factories/Annotations/Depends.php | 6 +-- src/Factories/Annotations/Groups.php | 6 +-- src/Factories/TestCaseFactory.php | 10 ++--- src/Factories/TestCaseMethodFactory.php | 2 +- src/Functions.php | 2 +- src/PendingCalls/UsesCall.php | 6 +-- src/Plugins/Coverage.php | 6 +-- src/Support/Arr.php | 4 +- src/Support/ChainableClosure.php | 8 ++-- src/Support/Closure.php | 12 +++++- src/Support/ExceptionTrace.php | 2 +- src/Support/HigherOrderCallables.php | 2 +- 18 files changed, 107 insertions(+), 94 deletions(-) delete mode 100644 src/Exceptions/ExpectationException.php create mode 100644 src/Exceptions/InvalidExpectationValue.php diff --git a/phpstan.neon b/phpstan.neon index ebd4bf30..d5dc4ef6 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,7 +4,7 @@ includes: - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon parameters: - level: 9 + level: max paths: - src diff --git a/src/Bootstrappers/BootSubscribers.php b/src/Bootstrappers/BootSubscribers.php index 42bdeaba..12486f50 100644 --- a/src/Bootstrappers/BootSubscribers.php +++ b/src/Bootstrappers/BootSubscribers.php @@ -15,7 +15,7 @@ final class BootSubscribers /** * The Kernel subscribers. * - * @var array + * @var array> */ private static array $subscribers = [ Subscribers\EnsureConfigurationIsValid::class, @@ -29,7 +29,7 @@ final class BootSubscribers { foreach (self::$subscribers as $subscriber) { Event\Facade::registerSubscriber( - new $subscriber() //@phpstan-ignore-line + new $subscriber() ); } } diff --git a/src/Datasets.php b/src/Datasets.php index 7f8013fe..248b1d38 100644 --- a/src/Datasets.php +++ b/src/Datasets.php @@ -34,7 +34,7 @@ final class Datasets /** * Sets the given. * - * @phpstan-param Closure|iterable $data + * @param Closure|iterable $data */ public static function set(string $name, Closure|iterable $data): void { @@ -46,9 +46,9 @@ final class Datasets } /** - * Sets the given. + * Sets the given "with". * - * @phpstan-param array|string> $with + * @param array|string> $with */ public static function with(string $filename, string $description, array $with): void { @@ -56,7 +56,9 @@ final class Datasets } /** - * @return Closure|iterable + * @return Closure|iterable|never + * + * @throws ShouldNotHappen */ public static function get(string $filename, string $description): Closure|iterable { @@ -65,7 +67,7 @@ final class Datasets $dataset = self::resolve($description, $dataset); if ($dataset === null) { - throw ShouldNotHappen::fromMessage('Could not resolve dataset.'); + throw ShouldNotHappen::fromMessage('Dataset [%s] not resolvable.'); } return $dataset; @@ -87,39 +89,40 @@ final class Datasets $dataset = self::processDatasets($dataset); - $datasetCombinations = self::getDataSetsCombinations($dataset); + $datasetCombinations = self::getDatasetsCombinations($dataset); - $dataSetDescriptions = []; - $dataSetValues = []; + $datasetDescriptions = []; + $datasetValues = []; foreach ($datasetCombinations as $datasetCombination) { $partialDescriptions = []; $values = []; - foreach ($datasetCombination as $dataset_data) { - $partialDescriptions[] = $dataset_data['label']; - //@phpstan-ignore-next-line - $values = array_merge($values, $dataset_data['values']); + foreach ($datasetCombination as $datasetCombinationElement) { + $partialDescriptions[] = $datasetCombinationElement['label']; + + // @phpstan-ignore-next-line + $values = array_merge($values, $datasetCombinationElement['values']); } - $dataSetDescriptions[] = $description . ' with ' . implode(' / ', $partialDescriptions); - $dataSetValues[] = $values; + $datasetDescriptions[] = $description . ' with ' . implode(' / ', $partialDescriptions); + $datasetValues[] = $values; } - foreach (array_count_values($dataSetDescriptions) as $descriptionToCheck => $count) { + foreach (array_count_values($datasetDescriptions) as $descriptionToCheck => $count) { if ($count > 1) { $index = 1; - foreach ($dataSetDescriptions as $i => $dataSetDescription) { - if ($dataSetDescription === $descriptionToCheck) { - $dataSetDescriptions[$i] .= sprintf(' #%d', $index++); + foreach ($datasetDescriptions as $i => $datasetDescription) { + if ($datasetDescription === $descriptionToCheck) { + $datasetDescriptions[$i] .= sprintf(' #%d', $index++); } } } } $namedData = []; - foreach ($dataSetDescriptions as $i => $dataSetDescription) { - $namedData[$dataSetDescription] = $dataSetValues[$i]; + foreach ($datasetDescriptions as $i => $datasetDescription) { + $namedData[$datasetDescription] = $datasetValues[$i]; } return $namedData; @@ -157,7 +160,7 @@ final class Datasets foreach ($datasets[$index] as $key => $values) { $values = is_array($values) ? $values : [$values]; $processedDataset[] = [ - 'label' => self::getDataSetDescription($key, $values), //@phpstan-ignore-line + 'label' => self::getDatasetDescription($key, $values), //@phpstan-ignore-line 'values' => $values, ]; } @@ -173,7 +176,7 @@ final class Datasets * * @return array>> */ - private static function getDataSetsCombinations(array $combinations): array + private static function getDatasetsCombinations(array $combinations): array { $result = [[]]; foreach ($combinations as $index => $values) { @@ -193,7 +196,7 @@ final class Datasets /** * @param array $data */ - private static function getDataSetDescription(int|string $key, array $data): string + private static function getDatasetDescription(int|string $key, array $data): string { $exporter = new Exporter(); diff --git a/src/Exceptions/ExpectationException.php b/src/Exceptions/ExpectationException.php deleted file mode 100644 index be68b40c..00000000 --- a/src/Exceptions/ExpectationException.php +++ /dev/null @@ -1,21 +0,0 @@ -value)) { - throw ExpectationException::invalidCurrentValueType('json', 'string'); + InvalidExpectationValue::expected('string'); } return $this->toBeJson()->and(json_decode($this->value, true)); @@ -360,13 +360,11 @@ 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); + // @phpstan-ignore-next-line + Assert::assertStringContainsString((string) $needle, $this->value); } else { if (!is_iterable($this->value)) { - throw ExpectationException::invalidCurrentValueType('toContain', 'iterable'); + InvalidExpectationValue::expected('iterable'); } Assert::assertContains($needle, $this->value); } @@ -383,7 +381,7 @@ final class Expectation public function toStartWith(string $expected): Expectation { if (!is_string($this->value)) { - throw ExpectationException::invalidCurrentValueType('toStartWith', 'string'); + InvalidExpectationValue::expected('string'); } Assert::assertStringStartsWith($expected, $this->value); @@ -399,7 +397,7 @@ final class Expectation public function toEndWith(string $expected): Expectation { if (!is_string($this->value)) { - throw ExpectationException::invalidCurrentValueType('toEndWith', 'string'); + InvalidExpectationValue::expected('string'); } Assert::assertStringEndsWith($expected, $this->value); @@ -443,7 +441,7 @@ final class Expectation public function toHaveCount(int $count): Expectation { if (!is_countable($this->value) && !is_iterable($this->value)) { - throw ExpectationException::invalidCurrentValueType('toHaveCount', 'string'); + InvalidExpectationValue::expected('string'); } Assert::assertCount($count, $this->value); @@ -743,7 +741,7 @@ final class Expectation public function toBeDirectory(): Expectation { if (!is_string($this->value)) { - throw ExpectationException::invalidCurrentValueType('toBeDirectory', 'string'); + InvalidExpectationValue::expected('string'); } Assert::assertDirectoryExists($this->value); @@ -757,7 +755,7 @@ final class Expectation public function toBeReadableDirectory(): Expectation { if (!is_string($this->value)) { - throw ExpectationException::invalidCurrentValueType('toBeReadableDirectory', 'string'); + InvalidExpectationValue::expected('string'); } Assert::assertDirectoryIsReadable($this->value); @@ -771,7 +769,7 @@ final class Expectation public function toBeWritableDirectory(): Expectation { if (!is_string($this->value)) { - throw ExpectationException::invalidCurrentValueType('toBeWritableDirectory', 'string'); + InvalidExpectationValue::expected('string'); } Assert::assertDirectoryIsWritable($this->value); @@ -785,7 +783,7 @@ final class Expectation public function toBeFile(): Expectation { if (!is_string($this->value)) { - throw ExpectationException::invalidCurrentValueType('toBeFile', 'string'); + InvalidExpectationValue::expected('string'); } Assert::assertFileExists($this->value); @@ -799,8 +797,9 @@ final class Expectation public function toBeReadableFile(): Expectation { if (!is_string($this->value)) { - throw ExpectationException::invalidCurrentValueType('toBeReadableFile', 'string'); + InvalidExpectationValue::expected('string'); } + Assert::assertFileIsReadable($this->value); return $this; @@ -812,7 +811,7 @@ final class Expectation public function toBeWritableFile(): Expectation { if (!is_string($this->value)) { - throw ExpectationException::invalidCurrentValueType('toBeWritableFile', 'string'); + InvalidExpectationValue::expected('string'); } Assert::assertFileIsWritable($this->value); @@ -859,8 +858,9 @@ final class Expectation { foreach ((array) $object as $property => $value) { if (!is_object($this->value) && !is_string($this->value)) { - throw ExpectationException::invalidCurrentValueType('toMatchObject', 'object|string'); + InvalidExpectationValue::expected('object|string'); } + Assert::assertTrue(property_exists($this->value, $property)); /* @phpstan-ignore-next-line */ @@ -885,7 +885,7 @@ final class Expectation public function toMatch(string $expression): Expectation { if (!is_string($this->value)) { - throw ExpectationException::invalidCurrentValueType('toMatch', 'string'); + InvalidExpectationValue::expected('string'); } Assert::assertMatchesRegularExpression($expression, $this->value); diff --git a/src/Factories/Annotations/Depends.php b/src/Factories/Annotations/Depends.php index 66c4838c..f41076d9 100644 --- a/src/Factories/Annotations/Depends.php +++ b/src/Factories/Annotations/Depends.php @@ -15,11 +15,11 @@ final class Depends /** * Adds annotations regarding the "depends" feature. * - * @param array $annotations + * @param array $annotations * - * @return array + * @return array */ - public function add(TestCaseMethodFactory $method, array $annotations): array + public function __invoke(TestCaseMethodFactory $method, array $annotations): array { foreach ($method->depends as $depend) { $depend = Str::evaluable($depend); diff --git a/src/Factories/Annotations/Groups.php b/src/Factories/Annotations/Groups.php index ab7749b7..0876ff5c 100644 --- a/src/Factories/Annotations/Groups.php +++ b/src/Factories/Annotations/Groups.php @@ -14,11 +14,11 @@ final class Groups /** * Adds annotations regarding the "groups" feature. * - * @param array $annotations + * @param array $annotations * - * @return array + * @return array */ - public function add(TestCaseMethodFactory $method, array $annotations): array + public function __invoke(TestCaseMethodFactory $method, array $annotations): array { foreach ($method->groups as $group) { $annotations[] = "@group $group"; diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index 517a586c..f6a61ba2 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -73,9 +73,9 @@ final class TestCaseFactory { $methodsUsingOnly = $this->methodsUsingOnly(); - $methods = array_filter($this->methods, function ($method) use ($methodsUsingOnly) { + $methods = array_values(array_filter($this->methods, function ($method) use ($methodsUsingOnly) { return count($methodsUsingOnly) === 0 || in_array($method, $methodsUsingOnly, true); - }); + })); if (count($methods) > 0) { $this->evaluate($this->filename, $methods); @@ -99,7 +99,7 @@ final class TestCaseFactory /** * Creates a Test Case class using a runtime evaluate. * - * @param array $methods + * @param array $methods */ public function evaluate(string $filename, array $methods): string { @@ -152,8 +152,8 @@ final class TestCaseFactory $annotations = ['@test']; foreach (self::$annotations as $annotation) { - //@phpstan-ignore-next-line - $annotations = (new $annotation())->add($method, $annotations); + /** @phpstan-ignore-next-line */ + $annotations = (new $annotation())->__invoke($method, $annotations); } if (count($method->datasets) > 0) { diff --git a/src/Factories/TestCaseMethodFactory.php b/src/Factories/TestCaseMethodFactory.php index e2ece273..540f2677 100644 --- a/src/Factories/TestCaseMethodFactory.php +++ b/src/Factories/TestCaseMethodFactory.php @@ -89,7 +89,7 @@ final class TestCaseMethodFactory $testCase->chains->chain($this); $method->chains->chain($this); - return \Pest\Support\Closure::safeBind($closure, $this, $this::class)(...func_get_args()); + return \Pest\Support\Closure::bind($closure, $this, $this::class)(...func_get_args()); }; } diff --git a/src/Functions.php b/src/Functions.php index 0f72cee7..fe1dd39a 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -77,7 +77,7 @@ if (!function_exists('uses')) { { $filename = Backtrace::file(); - return new UsesCall($filename, $classAndTraits); + return new UsesCall($filename, array_values($classAndTraits)); } } diff --git a/src/PendingCalls/UsesCall.php b/src/PendingCalls/UsesCall.php index 457f8699..cb3fdc8c 100644 --- a/src/PendingCalls/UsesCall.php +++ b/src/PendingCalls/UsesCall.php @@ -36,14 +36,14 @@ final class UsesCall /** * Holds the groups of the uses. * - * @var array + * @var array */ private array $groups = []; /** * Creates a new Pending Call. * - * @param array $classAndTraits + * @param array $classAndTraits */ public function __construct( private string $filename, @@ -89,7 +89,7 @@ final class UsesCall */ public function group(string ...$groups): UsesCall { - $this->groups = $groups; + $this->groups = array_values($groups); return $this; } diff --git a/src/Plugins/Coverage.php b/src/Plugins/Coverage.php index f2ee5457..9cf45f3b 100644 --- a/src/Plugins/Coverage.php +++ b/src/Plugins/Coverage.php @@ -80,10 +80,10 @@ final class Coverage implements AddsOutput, HandlesArguments } if ($input->getOption(self::MIN_OPTION) !== null) { - /** @var int|float $min_option */ - $min_option = $input->getOption(self::MIN_OPTION); + /** @var int|float $minOption */ + $minOption = $input->getOption(self::MIN_OPTION); - $this->coverageMin = (float) $min_option; + $this->coverageMin = (float) $minOption; } return $originals; diff --git a/src/Support/Arr.php b/src/Support/Arr.php index 531e0d19..922110e1 100644 --- a/src/Support/Arr.php +++ b/src/Support/Arr.php @@ -12,7 +12,7 @@ final class Arr /** * Checks if the given array has the given key. * - * @param array $array + * @param array $array */ public static function has(array $array, string|int $key): bool { @@ -36,7 +36,7 @@ final class Arr /** * Gets the given key value. * - * @param array $array + * @param array $array */ public static function get(array $array, string|int $key, mixed $default = null): mixed { diff --git a/src/Support/ChainableClosure.php b/src/Support/ChainableClosure.php index b3406d86..5c92e6fd 100644 --- a/src/Support/ChainableClosure.php +++ b/src/Support/ChainableClosure.php @@ -22,8 +22,8 @@ final class ChainableClosure throw ShouldNotHappen::fromMessage('$this not bound to chainable closure.'); } - \Pest\Support\Closure::safeBind($closure, $this, $this::class)(...func_get_args()); - \Pest\Support\Closure::safeBind($next, $this, $this::class)(...func_get_args()); + \Pest\Support\Closure::bind($closure, $this, $this::class)(...func_get_args()); + \Pest\Support\Closure::bind($next, $this, $this::class)(...func_get_args()); }; } @@ -33,8 +33,8 @@ final class ChainableClosure public static function fromStatic(Closure $closure, Closure $next): Closure { return static function () use ($closure, $next): void { - \Pest\Support\Closure::safeBind($closure, null, self::class)(...func_get_args()); - \Pest\Support\Closure::safeBind($next, null, self::class)(...func_get_args()); + \Pest\Support\Closure::bind($closure, null, self::class)(...func_get_args()); + \Pest\Support\Closure::bind($next, null, self::class)(...func_get_args()); }; } } diff --git a/src/Support/Closure.php b/src/Support/Closure.php index ece77d57..decb9b71 100644 --- a/src/Support/Closure.php +++ b/src/Support/Closure.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Pest\Support; +use Closure as BaseClosure; use Pest\Exceptions\ShouldNotHappen; /** @@ -11,13 +12,20 @@ use Pest\Exceptions\ShouldNotHappen; */ final class Closure { - public static function safeBind(\Closure|null $closure, ?object $newThis, object|string|null $newScope = 'static'): \Closure + /** + * Binds the given closure to the given "this". + * + * @return BaseClosure|never + * + * @throws ShouldNotHappen + */ + public static function bind(BaseClosure|null $closure, ?object $newThis, object|string|null $newScope = 'static'): BaseClosure { if ($closure == null) { throw ShouldNotHappen::fromMessage('Could not bind null closure.'); } - $closure = \Closure::bind($closure, $newThis, $newScope); + $closure = BaseClosure::bind($closure, $newThis, $newScope); if ($closure == false) { throw ShouldNotHappen::fromMessage('Could not bind closure.'); diff --git a/src/Support/ExceptionTrace.php b/src/Support/ExceptionTrace.php index 17fc2e40..2e6d53f4 100644 --- a/src/Support/ExceptionTrace.php +++ b/src/Support/ExceptionTrace.php @@ -51,7 +51,7 @@ final class ExceptionTrace $property = new ReflectionProperty($t, 'serializableTrace'); $property->setAccessible(true); - /** @var array> $trace */ + /** @var array> $trace */ $trace = $property->getValue($t); $cleanedTrace = []; diff --git a/src/Support/HigherOrderCallables.php b/src/Support/HigherOrderCallables.php index be02b86d..c6533929 100644 --- a/src/Support/HigherOrderCallables.php +++ b/src/Support/HigherOrderCallables.php @@ -25,7 +25,7 @@ final class HigherOrderCallables * * Create a new expectation. Callable values will be executed prior to returning the new expectation. * - * @param (callable():TValue)|TValue $value + * @param (Closure():TValue)|TValue $value * * @return Expectation */