Skip to content

Commit

Permalink
bump phpstan level to 5 (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored May 30, 2024
1 parent e9173fa commit 56c1cd5
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
parameters:
phpVersion: 80100
level: 4
level: 5
paths:
- src
- tests
Expand Down
24 changes: 24 additions & 0 deletions src/Collection/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,30 @@ public function dropWhile(callable $predicate)
return self::fromPointer($map);
}

/**
* @param callable(Tuple2<K, V>):bool $predicate
*
* @return Map<K,V>
*/
public function dropUntil(callable $predicate)
{
return parent::dropUntil($predicate);
}

/**
* @param callable(V): void $action
*
* @return Map<K,V>
*/
public function peek(callable $action)
{
if (!$this->isEmpty()) {
$action($this->get()->get());
}

return $this;
}

/**
* Take n next entries of map.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Collection/GenericListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function testListContainsAll(): void
self::assertFalse(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll([1, 2, 3, 4])));
self::assertFalse(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll([1, 2, 4])));
self::assertFalse(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll([1, 4])));
self::assertFalse(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll(['a'])));
self::assertFalse(GenericList::ofAll([1, 2, 3])->containsAll(GenericList::ofAll([0])));
}

public function testFlatMap(): void
Expand Down
22 changes: 10 additions & 12 deletions tests/Collection/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ public function testEmptyMapPeek(): void
public function testMapPeek(): void
{
$counter = 0;
Map::fromArray(['a' => 1])->peek(function (Option $head) use (&$counter) {$counter = $head->get(); });
Map::fromArray(['a' => 1])->peek(function (int $head) use (&$counter) {$counter = $head; });
self::assertEquals(1, $counter);
}

public function testMapTake(): void
{
$map = Map::fromArray(['a' => 'apple', 'b' => 'banana', '42' => 'pear', 'd' => 'orange']);
$map = Map::fromArray(['a' => 'apple', 'b' => 'banana', 'a42' => 'pear', 'd' => 'orange']);

self::assertTrue(Map::fromArray(['a' => 'apple', 'b' => 'banana'])->equals($map->take(2)));
self::assertTrue(Map::fromArray(['a' => 'apple'])->equals($map->take(1)));
Expand All @@ -109,9 +109,9 @@ public function testMapTake(): void

public function testMapDrop(): void
{
$map = Map::fromArray(['a' => 'apple', 'b' => 'banana', '42' => 'pear', 'd' => 'orange']);
$map = Map::fromArray(['a' => 'apple', 'b' => 'banana', 'a42' => 'pear', 'd' => 'orange']);

self::assertTrue(Map::fromArray(['42' => 'pear', 'd' => 'orange'])->equals($map->drop(2)));
self::assertTrue(Map::fromArray(['a42' => 'pear', 'd' => 'orange'])->equals($map->drop(2)));
self::assertTrue(Map::fromArray(['d' => 'orange'])->equals($map->drop(3)));
self::assertTrue($map->equals($map->drop(0)));
self::assertSame($map, $map->drop(-1));
Expand All @@ -120,14 +120,14 @@ public function testMapDrop(): void

public function testMapFilter(): void
{
$map = Map::fromArray(['a' => 'apple', 'b' => 'banana', '42' => 'pear', 'd' => 'orange']);
$map = Map::fromArray(['a' => 'apple', 'b' => 'banana', 'a42' => 'pear', 'd' => 'orange']);

self::assertTrue(Map::fromArray(['a' => 'apple'])->equals(
$map->filter(function ($entry) {return $entry[1] === 'apple'; })
));

self::assertTrue(Map::fromArray(['42' => 'pear'])->equals(
$map->filter(function ($entry) {return is_numeric($entry[0]); })
self::assertTrue(Map::fromArray(['a42' => 'pear'])->equals(
$map->filter(function ($entry) {return $entry[0] === 'a42'; })
));

self::assertNotSame($map, $map->filter(function () {return true; }));
Expand Down Expand Up @@ -190,8 +190,8 @@ public function testMapContainsValue(): void
$map = Map::fromArray(['a' => 'b', 'c' => 'd', 'e' => Option::of('munus')]);

self::assertTrue($map->containsValue('d'));
self::assertFalse($map->containsValue('a'));
self::assertTrue($map->containsValue('munus'));
self::assertFalse($map->containsValue('a')); // @phpstan-ignore-line phpstan is too smart and knows what exactly is in map
self::assertTrue($map->containsValue('munus')); // @phpstan-ignore-line phpstan is too smart and knows what exactly is in map
}

public function testMapValues(): void
Expand Down Expand Up @@ -231,9 +231,7 @@ public function testMapDropUntil(): void
self::assertTrue(Map::fromArray(['c' => 3, 'd' => 4])->equals($map->dropUntil(function (Tuple $node): bool {
return $node[0] === 'c';
})));
self::assertTrue(Map::empty()->equals($map->dropUntil(function (Tuple $node): bool {
return false;
})));
self::assertTrue(Map::empty()->equals($map->dropUntil(fn () => false)));
}

public function testMapMerge(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Collection/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testSetCanHoldObjects(): void
$set = Set::ofAll([Set::of(1, 2, 3), Set::of(4, 5, 6)]);

self::assertEquals(2, $set->length());
self::assertFalse($set->contains(new \stdClass()));
self::assertFalse($set->contains(Set::empty()));
self::assertTrue($set->contains(Set::of(1, 2, 3)));
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Collection/Stream/CollectorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ public function testToSetCollector(): void
public function testToMapWithDefaultValueMapperCollector(): void
{
$map = Stream::from(1)->take(3)->collect(Collectors::toMap(
fn (int $value): string => (string) $value
fn (int $value): string => 'a'.$value
));

self::assertTrue(Map::fromArray(['1' => 1, '2' => 2, '3' => 3])->equals($map));
self::assertTrue(Map::fromArray(['a1' => 1, 'a2' => 2, 'a3' => 3])->equals($map));
}

public function testToMapCollector(): void
{
$map = Stream::from(1)->take(3)->collect(Collectors::toMap(
fn (int $value): string => (string) $value,
fn (int $value): string => 'a'.$value,
fn (int $value): int => $value * 2,
));

self::assertTrue(Map::fromArray(['1' => 2, '2' => 4, '3' => 6])->equals($map));
self::assertTrue(Map::fromArray(['a1' => 2, 'a2' => 4, 'a3' => 6])->equals($map));
}

public function testSummingCollector(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Collection/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function testStreamContainsAll(): void
self::assertFalse(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll([1, 2, 3, 4])));
self::assertFalse(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll([1, 2, 4])));
self::assertFalse(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll([1, 4])));
self::assertFalse(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll(['a'])));
self::assertFalse(Stream::ofAll([1, 2, 3])->containsAll(Stream::ofAll([0])));
}

public function testStreamMapSingle(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Control/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testGetOrSomething(): void
/** @var Option<string> $option */
$option = Option::of(null);
self::assertNull($option->getOrNull());
self::assertTrue($option->getOrElse(true));
self::assertSame('string', $option->getOrElse('string'));
}

public function testMap(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Control/TryToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public function testMapFailure(): void

public function testMapWithMapperFailure(): void
{
$try = TryTo::run('time');
$try = TryTo::run(fn () => 0);

self::assertInstanceOf(\TypeError::class, $try->map('strtoupper')->getCause());
self::assertInstanceOf(\DivisionByZeroError::class, $try->map(fn (int $x) => 1 / $x)->getCause());
}

public function testMapWitMapperSuccess(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/LazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function testMap(): void
{
$lazy = Lazy::of(function () {return 4; });

self::assertInstanceOf(Lazy::class, $lazy->map('range'));
self::assertEquals(2, $lazy->map('sqrt')->get());
self::assertSame(5, $lazy->map(fn (int $x) => $x + 1)->get());
self::assertEquals(2, $lazy->map('sqrt')->get()); // sqrt returns float
}

public function testCollect(): void
Expand Down

0 comments on commit 56c1cd5

Please sign in to comment.