-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
InArrayStub.php
52 lines (45 loc) · 1.71 KB
/
InArrayStub.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
<?php
namespace Phpactor\WorseReflection\Core\Inference\FunctionStub;
use Phpactor\WorseReflection\Core\Inference\Frame;
use Phpactor\WorseReflection\Core\Inference\FunctionArguments;
use Phpactor\WorseReflection\Core\Inference\FunctionStub;
use Phpactor\WorseReflection\Core\Inference\NodeContext;
use Phpactor\WorseReflection\Core\Inference\Symbol;
use Phpactor\WorseReflection\Core\Inference\TypeAssertion;
use Phpactor\WorseReflection\Core\Inference\TypeCombinator;
use Phpactor\WorseReflection\Core\Type;
use Phpactor\WorseReflection\Core\TypeFactory;
use Phpactor\WorseReflection\Core\Type\ArrayLiteral;
class InArrayStub implements FunctionStub
{
public function resolve(
Frame $frame,
NodeContext $context,
FunctionArguments $args
): NodeContext {
$arg0 = $args->at(0);
if ($arg0->symbol()->symbolType() !== Symbol::VARIABLE) {
return $context->withType(TypeFactory::array());
}
$arrayType = $args->at(1)->type();
if (!$arrayType instanceof ArrayLiteral) {
return $context->withType(TypeFactory::array());
}
$union = TypeFactory::union(...$arrayType->iterableValueTypes());
return $context->withTypeAssertion(
TypeAssertion::forContext(
$args->at(0),
function (Type $type) use ($union) {
return $union;
},
function (Type $type) use ($union) {
$type = TypeCombinator::subtract($union, $type);
if (!$type->isDefined()) {
$type = $union->generalize();
}
return $type;
}
)
);
}
}