Skip to content

Commit

Permalink
refacto(phpstan-to-8): few adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuno Maduro authored and fabio-ivona committed Nov 26, 2021
1 parent 0b5cea6 commit 86dca12
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 94 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon

parameters:
level: 9
level: max
paths:
- src

Expand Down
4 changes: 2 additions & 2 deletions src/Bootstrappers/BootSubscribers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class BootSubscribers
/**
* The Kernel subscribers.
*
* @var array<int, class-string>
* @var array<int, class-string<\PHPUnit\Event\Subscriber>>
*/
private static array $subscribers = [
Subscribers\EnsureConfigurationIsValid::class,
Expand All @@ -29,7 +29,7 @@ public function __invoke(): void
{
foreach (self::$subscribers as $subscriber) {
Event\Facade::registerSubscriber(
new $subscriber() //@phpstan-ignore-line
new $subscriber()
);
}
}
Expand Down
49 changes: 26 additions & 23 deletions src/Datasets.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class Datasets
/**
* Sets the given.
*
* @phpstan-param Closure|iterable<int|string, mixed> $data
* @param Closure|iterable<int|string, mixed> $data
*/
public static function set(string $name, Closure|iterable $data): void
{
Expand All @@ -46,17 +46,19 @@ public static function set(string $name, Closure|iterable $data): void
}

/**
* Sets the given.
* Sets the given "with".
*
* @phpstan-param array<Closure|iterable<int|string, mixed>|string> $with
* @param array<Closure|iterable<int|string, mixed>|string> $with
*/
public static function with(string $filename, string $description, array $with): void
{
self::$withs[$filename . '>>>' . $description] = $with;
}

/**
* @return Closure|iterable<int|string, mixed>
* @return Closure|iterable<int|string, mixed>|never
*
* @throws ShouldNotHappen
*/
public static function get(string $filename, string $description): Closure|iterable
{
Expand All @@ -65,7 +67,7 @@ public static function get(string $filename, string $description): Closure|itera
$dataset = self::resolve($description, $dataset);

if ($dataset === null) {
throw ShouldNotHappen::fromMessage('Could not resolve dataset.');
throw ShouldNotHappen::fromMessage('Dataset [%s] not resolvable.');
}

return $dataset;
Expand All @@ -87,39 +89,40 @@ public static function resolve(string $description, array $dataset): array|null

$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;
Expand Down Expand Up @@ -157,7 +160,7 @@ private static function processDatasets(array $datasets): array
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,
];
}
Expand All @@ -173,7 +176,7 @@ private static function processDatasets(array $datasets): array
*
* @return array<array<array<mixed>>>
*/
private static function getDataSetsCombinations(array $combinations): array
private static function getDatasetsCombinations(array $combinations): array
{
$result = [[]];
foreach ($combinations as $index => $values) {
Expand All @@ -193,7 +196,7 @@ private static function getDataSetsCombinations(array $combinations): array
/**
* @param array<int, mixed> $data
*/
private static function getDataSetDescription(int|string $key, array $data): string
private static function getDatasetDescription(int|string $key, array $data): string
{
$exporter = new Exporter();

Expand Down
21 changes: 0 additions & 21 deletions src/Exceptions/ExpectationException.php

This file was deleted.

23 changes: 23 additions & 0 deletions src/Exceptions/InvalidExpectationValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Pest\Exceptions;

use InvalidArgumentException;

/**
* @internal
*/
final class InvalidExpectationValue extends InvalidArgumentException
{
/**
* @return never
*
* @throws self
*/
public static function expected(string $type): void
{
throw new self(sprintf('Invalid expectation value type. Expected [%s].', $type));
}
}
36 changes: 18 additions & 18 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use InvalidArgumentException;
use Pest\Concerns\Extendable;
use Pest\Concerns\RetrievesValues;
use Pest\Exceptions\ExpectationException;
use Pest\Exceptions\InvalidExpectationValue;
use Pest\Support\Arr;
use Pest\Support\NullClosure;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -70,7 +70,7 @@ public function and(mixed $value): Expectation
public function json(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('json', 'string');
InvalidExpectationValue::expected('string');
}

return $this->toBeJson()->and(json_decode($this->value, true));
Expand Down Expand Up @@ -360,13 +360,11 @@ public function toContain(mixed ...$needles): 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);
}
Expand All @@ -383,7 +381,7 @@ public function toContain(mixed ...$needles): 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);
Expand All @@ -399,7 +397,7 @@ public function toStartWith(string $expected): 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);
Expand Down Expand Up @@ -443,7 +441,7 @@ public function toHaveLength(int $number): 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);
Expand Down Expand Up @@ -743,7 +741,7 @@ public function toHaveKeys(array $keys): Expectation
public function toBeDirectory(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeDirectory', 'string');
InvalidExpectationValue::expected('string');
}

Assert::assertDirectoryExists($this->value);
Expand All @@ -757,7 +755,7 @@ public function toBeDirectory(): Expectation
public function toBeReadableDirectory(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeReadableDirectory', 'string');
InvalidExpectationValue::expected('string');
}

Assert::assertDirectoryIsReadable($this->value);
Expand All @@ -771,7 +769,7 @@ public function toBeReadableDirectory(): Expectation
public function toBeWritableDirectory(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeWritableDirectory', 'string');
InvalidExpectationValue::expected('string');
}

Assert::assertDirectoryIsWritable($this->value);
Expand All @@ -785,7 +783,7 @@ public function toBeWritableDirectory(): Expectation
public function toBeFile(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeFile', 'string');
InvalidExpectationValue::expected('string');
}

Assert::assertFileExists($this->value);
Expand All @@ -799,8 +797,9 @@ public function toBeFile(): Expectation
public function toBeReadableFile(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeReadableFile', 'string');
InvalidExpectationValue::expected('string');
}

Assert::assertFileIsReadable($this->value);

return $this;
Expand All @@ -812,7 +811,7 @@ public function toBeReadableFile(): Expectation
public function toBeWritableFile(): Expectation
{
if (!is_string($this->value)) {
throw ExpectationException::invalidCurrentValueType('toBeWritableFile', 'string');
InvalidExpectationValue::expected('string');
}
Assert::assertFileIsWritable($this->value);

Expand Down Expand Up @@ -859,8 +858,9 @@ 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');
InvalidExpectationValue::expected('object|string');
}

Assert::assertTrue(property_exists($this->value, $property));

/* @phpstan-ignore-next-line */
Expand All @@ -885,7 +885,7 @@ public function toMatchObject(iterable|object $object): 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);

Expand Down
6 changes: 3 additions & 3 deletions src/Factories/Annotations/Depends.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ final class Depends
/**
* Adds annotations regarding the "depends" feature.
*
* @param array<string> $annotations
* @param array<int, string> $annotations
*
* @return array<string>
* @return array<int, string>
*/
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);
Expand Down
6 changes: 3 additions & 3 deletions src/Factories/Annotations/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ final class Groups
/**
* Adds annotations regarding the "groups" feature.
*
* @param array<string> $annotations
* @param array<int, string> $annotations
*
* @return array<string>
* @return array<int, string>
*/
public function add(TestCaseMethodFactory $method, array $annotations): array
public function __invoke(TestCaseMethodFactory $method, array $annotations): array
{
foreach ($method->groups as $group) {
$annotations[] = "@group $group";
Expand Down
10 changes: 5 additions & 5 deletions src/Factories/TestCaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public function make(): void
{
$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);
Expand All @@ -99,7 +99,7 @@ public function methodsUsingOnly(): array
/**
* Creates a Test Case class using a runtime evaluate.
*
* @param array<TestCaseMethodFactory> $methods
* @param array<int, TestCaseMethodFactory> $methods
*/
public function evaluate(string $filename, array $methods): string
{
Expand Down Expand Up @@ -152,8 +152,8 @@ public function evaluate(string $filename, array $methods): string
$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) {
Expand Down
Loading

0 comments on commit 86dca12

Please sign in to comment.