-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
CollectionHelper.php
92 lines (71 loc) · 3.02 KB
/
CollectionHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace NunoMaduro\Larastan\Support;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Enumerable;
use Iterator;
use IteratorAggregate;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use Traversable;
final class CollectionHelper
{
public function determineGenericCollectionTypeFromType(Type $type): ?GenericObjectType
{
if ($type instanceof TypeWithClassName) {
if ((new ObjectType(Enumerable::class))->isSuperTypeOf($type)->yes()) {
return $this->getTypeFromEloquentCollection($type);
}
if (
(new ObjectType(Traversable::class))->isSuperTypeOf($type)->yes() ||
(new ObjectType(IteratorAggregate::class))->isSuperTypeOf($type)->yes() ||
(new ObjectType(Iterator::class))->isSuperTypeOf($type)->yes()
) {
return $this->getTypeFromIterator($type);
}
}
if (! $type->isArray()->yes()) {
return new GenericObjectType(Collection::class, [$type->toArray()->getIterableKeyType(), $type->toArray()->getIterableValueType()]);
}
if ($type->isIterableAtLeastOnce()->no()) {
return new GenericObjectType(Collection::class, [new BenevolentUnionType([new IntegerType(), new StringType()]), new MixedType()]);
}
return null;
}
private function getTypeFromEloquentCollection(TypeWithClassName $valueType): ?GenericObjectType
{
$keyType = new BenevolentUnionType([new IntegerType(), new StringType()]);
$classReflection = $valueType->getClassReflection();
if ($classReflection === null) {
return null;
}
$innerValueType = $classReflection->getActiveTemplateTypeMap()->getType('TModel');
if ($classReflection->getName() === EloquentCollection::class || $classReflection->isSubclassOf(EloquentCollection::class)) {
$keyType = new IntegerType();
}
if ($innerValueType !== null) {
return new GenericObjectType(Collection::class, [$keyType, $innerValueType]);
}
return null;
}
private function getTypeFromIterator(TypeWithClassName $valueType): ?GenericObjectType
{
$keyType = new BenevolentUnionType([new IntegerType(), new StringType()]);
$classReflection = $valueType->getClassReflection();
if ($classReflection === null) {
return null;
}
$templateTypes = array_values($classReflection->getActiveTemplateTypeMap()->getTypes());
if (count($templateTypes) === 1) {
return new GenericObjectType(Collection::class, [$keyType, $templateTypes[0]]);
}
return new GenericObjectType(Collection::class, $templateTypes);
}
}