Skip to content

Commit

Permalink
Support adding type to array
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Mar 11, 2024
1 parent e92f6ef commit 0ea4ab7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/WorseReflection/Core/DefaultResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use Phpactor\WorseReflection\Core\Inference\FunctionStub\ArrayMapStub;
use Phpactor\WorseReflection\Core\Inference\FunctionStub\ArrayMergeStub;
use Phpactor\WorseReflection\Core\Inference\FunctionStub\ArrayPopStub;
use Phpactor\WorseReflection\Core\Inference\FunctionStub\ArrayReduceStub;
use Phpactor\WorseReflection\Core\Inference\FunctionStub\ArrayShiftStub;
use Phpactor\WorseReflection\Core\Inference\FunctionStub\ArraySumStub;
use Phpactor\WorseReflection\Core\Inference\FunctionStub\AssertStub;
Expand Down Expand Up @@ -182,6 +183,7 @@ private function createStubRegistry(): FunctionStubRegistry
'reset' => new ResetStub(),
'array_shift' => new ArrayShiftStub(),
'array_pop' => new ArrayPopStub(),
'array_reduce' => new ArrayReduceStub(),
'array_merge' => new ArrayMergeStub(),
'assert' => new AssertStub(),
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\TypeFactory;
use Phpactor\WorseReflection\Core\Type\ClosureType;

class ArrayReduceStub implements FunctionStub
{
public function resolve(
Frame $frame,
NodeContext $context,
FunctionArguments $args
): NodeContext {
if (!$args->at(0)->type()->isDefined()) {
return $context;
}

$closureType = $args->at(1);
if (!$closureType instanceof ClosureType) {
return $context;
}

return $context->withType(TypeFactory::array($closureType->returnType()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private function walkSubscriptExpression(NodeContextResolver $resolver, Frame $f
foreach ($frame->locals()->byName((string)$leftOperand->postfixExpression->getName()) as $variable) {
$type = $variable->type();

if (!$type instanceof ArrayLiteral) {
if (!$type instanceof ArrayType) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ private function resolveContextFromCall(

$context = FunctionCallContext::create($name, $range, $function, FunctionArguments::fromList($resolver, $frame, $parent->argumentExpressionList));

$stub = $this->registry->get($name->short());

$byReference = $function->parameters()->passedByReference();
$arguments = null;

Expand All @@ -161,6 +159,7 @@ private function resolveContextFromCall(
}
}

$stub = $this->registry->get($name->short());
if ($stub) {
$arguments = $arguments ?: FunctionArguments::fromList(
$resolver,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

function foo(string $foo) {
$bars = [];
$bars[] = $foo;
wrAssertType('string[]', $bars);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

array_reduce(['foobar'], function (array $carry, string $foo): array {
$carry[] = $foo;
wrAssertType('array<string>', $carry);
return $carry;
}, []),

0 comments on commit 0ea4ab7

Please sign in to comment.