Skip to content

Commit

Permalink
Support array_fill_keys and array_replace
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun-itn committed Oct 19, 2018
1 parent 4fb772f commit 5be88a4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/org/klesun/deep_assoc_completion/resolvers/FuncCallRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ private DeepType array_combine(FuncCtx callCtx, FunctionReferenceImpl call)
return combine;
}

private DeepType array_fill_keys(FuncCtx callCtx, FunctionReferenceImpl call)
{
return makeAssoc(call, callCtx.getArg(0)
.fap(mt -> mt.getEl().getStringValues())
.map(keyName -> T2(keyName, PhpType.MIXED))
.arr());
}

private DeepType array_flip(FuncCtx callCtx, FunctionReferenceImpl call)
{
DeepType flip = new DeepType(call, PhpType.ARRAY);
Expand Down Expand Up @@ -382,14 +390,18 @@ private Iterable<DeepType> findBuiltInFuncCallType(FunctionReferenceImpl call)
return callCtx.getArgMt(0).types;
} else if (name.equals("array_combine")) {
return list(array_combine(callCtx, call));
} else if (name.equals("array_fill_keys")) {
return list(array_fill_keys(callCtx, call));
} else if (name.equals("array_flip")) {
return list(array_flip(callCtx, call));
} else if (name.equals("array_pop") || name.equals("array_shift")
|| name.equals("current") || name.equals("end") || name.equals("next")
|| name.equals("prev") || name.equals("reset")
) {
return callCtx.getArgMt(0).getEl().types;
} else if (name.equals("array_merge")) {
} else if (name.equals("array_merge") || name.equals("array_replace")
|| name.equals("array_replace_recursive")
) {
return Tls.range(0, params.length)
.fop(i -> callCtx.getArg(i))
.fap(mt -> mt.types).map(a -> a);
Expand Down
15 changes: 15 additions & 0 deletions tests/src/DeepTest/ExactKeysUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,21 @@ public static function provideForeachKeyLimitedResolutionCachingBug($pnrData)
];
}

public static function provideArrayFillKeys()
{
$row = array_fill_keys(['id', 'name', 'price', 'currency'], '');
$row[''];
$list[] = [$row, ['id', 'name', 'price', 'currency']];

$lreplace = array_replace(['a' => '', 'b' => ''], ['a' => 5, 'b' => 6, 'c' => 7]);
$list[] = [$lreplace, ['a', 'b', 'c']];

$rreplace = array_replace(['a' => '', 'b' => '', 'c' => ''], ['a' => 5, 'b' => 6]);
$list[] = [$rreplace, ['a', 'b', 'c']];

return $list;
}

//=============================
// following are not implemented yet
//=============================
Expand Down

0 comments on commit 5be88a4

Please sign in to comment.