Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"phpunit/phpunit": "^9.4.2",
"slevomat/coding-standard": "^6.4.1",
"squizlabs/php_codesniffer": "^3.5.0",
"bonami/collections": "^0.3"
"bonami/collections": "dev-jm-phpstan-1.0"
},
"config": {
"bin-dir": "bin"
Expand Down
6 changes: 5 additions & 1 deletion extension.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
services:
-
class: Bonami\Collection\Phpstan\ArrayListWithoutNullsReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: Bonami\Collection\Phpstan\LateStaticBindingStaticMethodReturnTypeExtension
factory: Bonami\Collection\Phpstan\LateStaticBindingStaticMethodReturnTypeExtension::forMethods('Bonami\Collection\ArrayList', ['fromEmpty', 'of', 'fill', 'fromIterable'])
Expand All @@ -21,7 +25,7 @@ services:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: Bonami\Collection\Phpstan\LateStaticBindingStaticMethodReturnTypeExtension
factory: Bonami\Collection\Phpstan\LateStaticBindingStaticMethodReturnTypeExtension::forMethods('Bonami\Collection\LazyList', ['fill', 'fromEmpty', 'fromArray', 'fromTraversable', 'fromIterable', 'of'])
factory: Bonami\Collection\Phpstan\LateStaticBindingStaticMethodReturnTypeExtension::forMethods('Bonami\Collection\LazyList', ['fill', 'fromEmpty', 'fromArray', 'fromIterable', 'of'])
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
-
Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
includes:
- extension.neon
parameters:
inferPrivatePropertyTypeFromConstructor: true
reportUnmatchedIgnoredErrors: true
level: 8
level: 9
paths:
- src/
- tests/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Bonami\Collection\Phpstan;

use Bonami\Collection\ArrayList;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

class ArrayListWithoutNullsReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return ArrayList::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'withoutNulls';
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type {
$type = $scope->getType($methodCall->var);

$declaringClassReflection = $methodReflection->getDeclaringClass();
$referencedClasses = $type->getReferencedClasses();

if (in_array(ArrayList::class, $referencedClasses, true)) {
$types = $declaringClassReflection->typeMapToList(
$declaringClassReflection->getTemplateTypeMap()->resolveToBounds()
);

return new GenericObjectType(
$declaringClassReflection->getName(),
array_map(static function (Type $type) {
return TypeCombinator::removeNull($type);
}, $types)
);
}

return $type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

class GroupByMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
/** @var string */
private $class;

public function __construct(string $class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class LateStaticBindingMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
/** @var string */
private $class;

/** @var array<string, int> */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

class LateStaticBindingStaticMethodReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
{
/** @var string */
private $class;

/** @var array<string, int> */
Expand Down
8 changes: 6 additions & 2 deletions tests/Bonami/Collection/Phpstan/ArrayListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,15 @@ public function testSliceReturnType(): void

public function testWithoutNullsReturnType(): void
{
$genericList = ArrayList::fromIterable([new Foo()])->withoutNulls();
/** @var iterable<int, Foo|null> $iterable */
$iterable = [new Foo(), null];
/** @var ArrayList<Foo|null> $list */
$list = ArrayList::fromIterable($iterable);
$genericList = $list->withoutNulls();
$this->requireArrayListOfFoo($genericList->withoutNulls());
self::assertInstanceOf(ArrayList::class, $genericList);

$concreteList = FooArrayList::fromIterable([new Foo()])->withoutNulls();
$concreteList = FooArrayList::fromIterable($iterable)->withoutNulls();
$this->requireFooList($concreteList->withoutNulls());
self::assertInstanceOf(FooArrayList::class, $concreteList);
}
Expand Down
11 changes: 0 additions & 11 deletions tests/Bonami/Collection/Phpstan/LazyListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,6 @@ public function testFromArrayReturnType(): void
self::assertInstanceOf(FooLazyList::class, $concreteList);
}

public function testFromTraversableReturnType(): void
{
$genericList = LazyList::fromTraversable(new ArrayIterator([new Foo()]));
$this->requireLazyListOfFoo($genericList);
self::assertInstanceOf(LazyList::class, $genericList);

$concreteList = FooLazyList::fromTraversable(new ArrayIterator([new Foo()]));
$this->requireFooList($concreteList);
self::assertInstanceOf(FooLazyList::class, $concreteList);
}

public function testFromIterableReturnType(): void
{
$genericList = LazyList::fromIterable([new Foo()]);
Expand Down