Skip to content

Commit

Permalink
PHPStan 0.12 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
acrobat committed Dec 12, 2020
1 parent e190034 commit 944cc1b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ parameters:
paths:
- src
- tests
autoload_files:
- vendor/autoload.php
ignoreErrors:
5 changes: 4 additions & 1 deletion src/ContinuesVersionsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ private function computePossibleVersionsFromLastExisting(): void
$this->possibleVersions = $version->getNextVersionCandidates();
}

private function getLastArrayIndex(array $array)
/**
* @param array<int, mixed> $array
*/
private function getLastArrayIndex(array $array): int
{
end($array);

Expand Down
4 changes: 2 additions & 2 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class Version
/**
* Higher means more stable.
*
* @var int[]
* @var array<string,int>
*/
private static $stabilityIndexes = [
'alpha' => self::STABILITY_ALPHA,
Expand Down Expand Up @@ -81,7 +81,7 @@ private function __construct(int $major, int $minor, int $patch, int $stability,
$this->major,
$this->minor,
$this->patch,
strtoupper(array_search($this->stability, self::$stabilityIndexes, true)),
strtoupper(array_search($this->stability, self::$stabilityIndexes, true)), /** @phpstan-ignore-line */
$this->metaver
);
} else {
Expand Down
31 changes: 27 additions & 4 deletions tests/ContinuesVersionsValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

class ContinuesVersionsValidatorTest extends TestCase
{
/**
* @return array<int, string[]>
*/
public function provideInitialContinuesVersions(): iterable
{
yield ['0.1.0'];
Expand All @@ -31,7 +34,7 @@ public function provideInitialContinuesVersions(): iterable
* @test
* @dataProvider provideInitialContinuesVersions
*/
public function it_accepts_a_continues_version_with_no_pre_existing(string $new)
public function it_accepts_a_continues_version_with_no_pre_existing(string $new): void
{
$validator = new ContinuesVersionsValidator();

Expand All @@ -47,6 +50,9 @@ public function it_accepts_a_continues_version_with_no_pre_existing(string $new)
);
}

/**
* @return array<string, array<int, string[]|string>>
*/
public function provideContinuesVersions(): iterable
{
yield 'unstable #1' => ['0.3', ['0.2', '0.1'], ['0.2.1', '0.3', '1.0-BETA1', '1.0']];
Expand All @@ -63,15 +69,23 @@ public function provideContinuesVersions(): iterable
/**
* @test
* @dataProvider provideContinuesVersions
*
* @param array<int, string> $existing
* @param array<int, string> $possible
*/
public function it_accepts_a_continues_version(string $new, array $existing, array $possible)
public function it_accepts_a_continues_version(string $new, array $existing, array $possible): void
{
$validator = new ContinuesVersionsValidator(...$this->createVersions($existing));

self::assertTrue($validator->isContinues(Version::fromString($new)));
self::assertEquals($this->createVersions($possible), array_merge([], $validator->getPossibleVersions()));
}

/**
* @param array<int, string> $existing
*
* @return array<int, Version>
*/
private function createVersions(array $existing): array
{
return array_map(
Expand All @@ -82,6 +96,9 @@ function (string $version) {
);
}

/**
* @return array<int, string[]>
*/
public function provideNotInitialContinuesVersions(): iterable
{
yield ['0.2.0'];
Expand All @@ -95,7 +112,7 @@ public function provideNotInitialContinuesVersions(): iterable
* @test
* @dataProvider provideNotInitialContinuesVersions
*/
public function it_rejects_non_continues_version_with_no_pre_existing(string $new)
public function it_rejects_non_continues_version_with_no_pre_existing(string $new): void
{
$validator = new ContinuesVersionsValidator();

Expand All @@ -111,6 +128,9 @@ public function it_rejects_non_continues_version_with_no_pre_existing(string $ne
);
}

/**
* @return array<string, array<int, string[]|string>>
*/
public function provideNonContinuesVersions(): iterable
{
yield 'unstable #1' => ['0.5', ['0.2', '0.1'], ['0.2.1', '0.3', '1.0-BETA1', '1.0']];
Expand All @@ -127,8 +147,11 @@ public function provideNonContinuesVersions(): iterable
/**
* @test
* @dataProvider provideNonContinuesVersions
*
* @param array<int, string> $existing
* @param array<int, string> $possible
*/
public function it_rejects_non_continues_version(string $new, array $existing, array $possible)
public function it_rejects_non_continues_version(string $new, array $existing, array $possible): void
{
$validator = new ContinuesVersionsValidator(...$this->createVersions($existing));

Expand Down
31 changes: 21 additions & 10 deletions tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class VersionTest extends TestCase
{
/** @test */
public function it_creates_from_full_string()
public function it_creates_from_full_string(): void
{
$version = Version::fromString('1.0.0-beta-5');

Expand All @@ -33,7 +33,7 @@ public function it_creates_from_full_string()
}

/** @test */
public function it_creates_with_explicit_stable()
public function it_creates_with_explicit_stable(): void
{
$version = Version::fromString('1.0.0-stable');

Expand All @@ -47,7 +47,7 @@ public function it_creates_with_explicit_stable()
}

/** @test */
public function it_creates_without_patch()
public function it_creates_without_patch(): void
{
$version = Version::fromString('1.0');

Expand All @@ -60,6 +60,9 @@ public function it_creates_without_patch()
self::assertEquals('1.0.0', (string) $version);
}

/**
* @return array<string,string[]>
*/
public function provideValidFormats(): array
{
return [
Expand All @@ -77,15 +80,15 @@ public function provideValidFormats(): array
* @test
* @dataProvider provideValidFormats
*/
public function it_supports_various_formats(string $version, string $expectedOutput)
public function it_supports_various_formats(string $version, string $expectedOutput): void
{
$version = Version::fromString($version);

self::assertEquals($expectedOutput, (string) $version);
}

/** @test */
public function it_compares_two_versions_are_equal()
public function it_compares_two_versions_are_equal(): void
{
$version = Version::fromString('1.0.0-beta-5');
$version2 = Version::fromString('1.0.0-beta5');
Expand All @@ -96,7 +99,7 @@ public function it_compares_two_versions_are_equal()
}

/** @test */
public function it_fails_for_invalid_format()
public function it_fails_for_invalid_format(): void
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Unable to parse version "1.0.0-WAT"');
Expand All @@ -105,14 +108,17 @@ public function it_fails_for_invalid_format()
}

/** @test */
public function it_fails_with_stable_plus_metaver()
public function it_fails_with_stable_plus_metaver(): void
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Meta version of the stability flag cannot be set for stable.');

Version::fromString('1.0.0-stable-5');
}

/**
* @return array<string,array<int, array<int, string>|string>>>
*/
public function provideExpectedNextVersionCandidates(): array
{
return [
Expand All @@ -132,15 +138,20 @@ public function provideExpectedNextVersionCandidates(): array
/**
* @test
* @dataProvider provideExpectedNextVersionCandidates
*
* @param array<int, string> $expected
*/
public function it_provides_next_version_candidates($current, $expected)
public function it_provides_next_version_candidates(string $current, array $expected): void
{
$candidates = Version::fromString($current)->getNextVersionCandidates();
$expected = array_map([Version::class, 'fromString'], $expected);

self::assertEquals($expected, $candidates);
}

/**
* @return array<string,string[]>
*/
public function provideExpectedIncreasedVersion(): array
{
return [
Expand Down Expand Up @@ -199,13 +210,13 @@ public function provideExpectedIncreasedVersion(): array
* @test
* @dataProvider provideExpectedIncreasedVersion
*/
public function it_increases_to_next_version(string $current, string $expected, $stability)
public function it_increases_to_next_version(string $current, string $expected, string $stability): void
{
self::assertEquals(Version::fromString($expected), Version::fromString($current)->getNextIncreaseOf($stability));
}

/** @test */
public function it_cannot_increases_for_unsupported()
public function it_cannot_increases_for_unsupported(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown stability "next-stable", accepts "alpha", "beta", "rc", "stable", "major", "next", "minor", "patch".');
Expand Down

0 comments on commit 944cc1b

Please sign in to comment.