-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
ArrayType.php
68 lines (57 loc) · 1.83 KB
/
ArrayType.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
<?php
namespace Phpactor\WorseReflection\Core\Type;
use Closure;
use Phpactor\WorseReflection\Core\Trinary;
use Phpactor\WorseReflection\Core\Type;
use Phpactor\WorseReflection\Core\TypeFactory;
use Phpactor\WorseReflection\Core\Types;
class ArrayType extends PseudoIterableType implements IterableType, HasEmptyType
{
public function __toString(): string
{
if ($this->valueType instanceof MissingType) {
return $this->toPhpString();
}
if ($this->keyType === null) {
return sprintf('%s[]', $this->valueType->__toString());
}
return sprintf('array<%s,%s>', $this->keyType->__toString(), $this->valueType->__toString());
}
public function toPhpString(): string
{
return 'array';
}
public function map(Closure $mapper): Type
{
return new self(
$this->keyType ? $this->iterableKeyType()->map($mapper) : null,
$this->valueType ? $this->iterableValueType()->map($mapper) : null,
);
}
public function emptyType(): Type
{
return new ArrayLiteral([]);
}
public function consumes(Type $type): Trinary
{
// if type is an empty array, replace with this one
if ($type instanceof ArrayLiteral && count($type->types()) === 0) {
return Trinary::true();
}
return Trinary::maybe();
}
public function allTypes(): Types
{
return (new Types([TypeFactory::array()]))->merge(parent::allTypes());
}
public function add(Type $type): self
{
if (null === $this->valueType) {
return new self($this->keyType, $type);
}
if (!$this->valueType->isDefined()) {
return new self($this->keyType, $type);
}
return new self($this->keyType, $this->valueType->addType($type));
}
}